Skip to content

Commit

Permalink
add system.io.abstractions for an option to have a mockable filesyste…
Browse files Browse the repository at this point in the history
…m in unit testing
  • Loading branch information
youri committed Jun 1, 2023
1 parent 779a307 commit 83d4bc8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NSubstitute" Version="4.4.0" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="19.2.29" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
40 changes: 40 additions & 0 deletions JsonFlatFileDataStore.Test/MockedFIleSystemTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace JsonFlatFileDataStore.Test
{
public class MockedFileSystemTest
{
[Fact]
public async Task WriteToFileTest()
{
var filepath = @"c:\data.json";

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ filepath, new MockFileData("{}") },
});

var store = new DataStore(fileSystem, "c:\\data.json");

var collection = store.GetCollection<PrivateOwner>("PrivateOwner");
Assert.Equal(0, collection.Count);

await collection.InsertOneAsync(new PrivateOwner {
FirstName = "Jimmy",
OwnerLongTestProperty = "UT" });
Assert.Equal(1, collection.Count);

var json = fileSystem.File.ReadAllText(filepath);

Assert.Contains("privateOwner", json);
Assert.Contains("ownerLongTestProperty", json);

}
}
}
19 changes: 16 additions & 3 deletions JsonFlatFileDataStore/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -34,8 +35,15 @@ public class DataStore : IDataStore
private bool _executingJsonUpdate;

public DataStore(string path, bool useLowerCamelCase = true, string keyProperty = null, bool reloadBeforeGetCollection = false,
string encryptionKey = null, bool minifyJson = false) :
this(new FileSystem(), path, useLowerCamelCase, keyProperty, reloadBeforeGetCollection, encryptionKey, minifyJson)
{
}

public DataStore(IFileSystem fs, string path, bool useLowerCamelCase = true, string keyProperty = null, bool reloadBeforeGetCollection = false,
string encryptionKey = null, bool minifyJson = false)
{
this.FS = fs;
_filePath = path;

var useEncryption = !string.IsNullOrWhiteSpace(encryptionKey);
Expand Down Expand Up @@ -86,12 +94,17 @@ public class DataStore : IDataStore
_jsonData = JObject.Parse(jsonText);
}
return FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonText);
return FileAccess.WriteJsonToFile(this.FS, _filePath, _encryptJson, jsonText);
},
GetJsonTextFromFile);
});
}

/// <summary>
/// Gets the filesystem.
/// </summary>
private IFileSystem FS { get; }

public void Dispose()
{
while (IsUpdating)
Expand All @@ -114,7 +127,7 @@ public void UpdateAll(string jsonData)
_jsonData = JObject.Parse(jsonData);
}

FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonData);
FileAccess.WriteJsonToFile(this.FS,_filePath, _encryptJson, jsonData);
}

public void Reload()
Expand Down Expand Up @@ -418,7 +431,7 @@ private dynamic SingleDynamicItemReadConverter(JToken e)
}
}

private string GetJsonTextFromFile() => FileAccess.ReadJsonFromFile(_filePath, _encryptJson, _decryptJson);
private string GetJsonTextFromFile() => FileAccess.ReadJsonFromFile(this.FS, _filePath, _encryptJson, _decryptJson);

private JObject GetJsonObjectFromFile() => JObject.Parse(GetJsonTextFromFile());

Expand Down
11 changes: 6 additions & 5 deletions JsonFlatFileDataStore/FileAccess.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;

namespace JsonFlatFileDataStore
{
internal static class FileAccess
{
internal static string ReadJsonFromFile(string path, Func<string, string> encryptJson, Func<string, string> decryptJson)
internal static string ReadJsonFromFile(IFileSystem fs, string path, Func<string, string> encryptJson, Func<string, string> decryptJson)
{
Stopwatch sw = null;
var json = "{}";
Expand All @@ -15,13 +16,13 @@ internal static string ReadJsonFromFile(string path, Func<string, string> encryp
{
try
{
json = File.ReadAllText(path);
json = fs.File.ReadAllText(path);
break;
}
catch (FileNotFoundException)
{
json = encryptJson(json);
File.WriteAllText(path, json);
fs.File.WriteAllText(path, json);
break;
}
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
Expand All @@ -36,15 +37,15 @@ internal static string ReadJsonFromFile(string path, Func<string, string> encryp
return decryptJson(json);
}

internal static bool WriteJsonToFile(string path, Func<string, string> encryptJson, string content)
internal static bool WriteJsonToFile(IFileSystem fs, string path, Func<string, string> encryptJson, string content)
{
Stopwatch sw = null;

while (true)
{
try
{
File.WriteAllText(path, encryptJson(content));
fs.File.WriteAllText(path, encryptJson(content));
return true;
}
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
Expand Down
1 change: 1 addition & 0 deletions JsonFlatFileDataStore/JsonFlatFileDataStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.IO.Abstractions" Version="19.2.29" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 83d4bc8

Please sign in to comment.