Skip to content

Commit

Permalink
test: split file content tests to own file (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu authored Nov 4, 2024
1 parent ea2cdef commit 85e7fd3
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 216 deletions.
216 changes: 0 additions & 216 deletions JsonFlatFileDataStore.Test/DataStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,58 +77,6 @@ public void ListItems()
UTHelpers.Down(newFilePath);
}

[Fact]
public async Task WriteToFile_LowerCamelCase()
{
var newFilePath = UTHelpers.Up();

var store = new DataStore(newFilePath);

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 = File.ReadAllText(newFilePath);

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

var store2 = new DataStore(newFilePath);

var collectionUppercase = store2.GetCollection<PrivateOwner>("PrivateOwner");
Assert.Equal(1, collectionUppercase.Count);

var collectionLowercase = store2.GetCollection<PrivateOwner>("privateOwner");
Assert.Equal(1, collectionLowercase.Count);

var collectionNoCase = store2.GetCollection<PrivateOwner>();
Assert.Equal(1, collectionNoCase.Count);

UTHelpers.Down(newFilePath);
}

[Fact]
public async Task WriteToFile_UpperCamelCase()
{
var newFilePath = UTHelpers.Up();

var store = new DataStore(newFilePath, false);

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

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

var json = File.ReadAllText(newFilePath);

Assert.Contains("OwnerLongTestProperty", json);

UTHelpers.Down(newFilePath);
}

[Fact]
public void GetCollection_TypedCollection_NameParameter()
{
Expand Down Expand Up @@ -457,170 +405,6 @@ public async Task Readme_Example3()
UTHelpers.Down(pathToJson);
}

[Fact]
public void FileNotFound_CreateNewFile()
{
var path = UTHelpers.GetFullFilePath($"CreateNewFile_{DateTime.UtcNow.Ticks}");

var storeFileNotFound = new DataStore(path);
var collectionKeys = storeFileNotFound.GetKeys();
Assert.Equal(0, collectionKeys.Count);

var storeFileFound = new DataStore(path);
var collectionKeysFileFound = storeFileNotFound.GetKeys();
Assert.Equal(0, collectionKeysFileFound.Count);

UTHelpers.Down(path);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task FileContent_DefaultFormat(bool useLowerCamelCase)
{
var path = UTHelpers.GetFullFilePath($"FileContent_DefaultFormat_{DateTime.UtcNow.Ticks}");

var store = new DataStore(path, useLowerCamelCase: useLowerCamelCase);
var collection = store.GetCollection<Movie>("movie");
await collection.InsertOneAsync(new Movie { Name = "Test", Rating = 5 });

var content = UTHelpers.GetFileContent(path);


// NOTE: File format is different depending on used OS. Windows uses \r\n and Linux \r
// - "{\r\n \"movie\": [\r\n {\r\n \"name\": \"Test\",\r\n \"rating\": 5.0\r\n }\r\n ]\r\n}",
// - "{\r \"movie\": [\r {\r \"name\": \"Test\",\r \"rating\": 5.0\r }\r ]\r}"
// Length on Windows is 81 and on Linux 74
var allowedLengths = new[] { 81, 74 };

Assert.Contains(allowedLengths, i => i == content.Length);


UTHelpers.Down(path);
}

[Theory]
[InlineData(true, true, new[] { 40 })]
[InlineData(false, true, new[] { 40 })]
[InlineData(true, false, new[] { 81, 74 })]
[InlineData(false, false, new[] { 81, 74 })]
public async Task FileContent_MinifiedFormat(bool useLowerCamelCase, bool useMinifiedJson, int[] allowedLengths)
{
var path = UTHelpers.GetFullFilePath($"FileContent_DefaultFormat_{DateTime.UtcNow.Ticks}");

var store = new DataStore(path, useLowerCamelCase: useLowerCamelCase, minifyJson: useMinifiedJson);
var collection = store.GetCollection<Movie>("movie");
await collection.InsertOneAsync(new Movie { Name = "Test", Rating = 5 });

var content = UTHelpers.GetFileContent(path);

// NOTE: File format is different depending on used OS. Windows uses \r\n and Linux \r
// - "{\r\n \"movie\": [\r\n {\r\n \"name\": \"Test\",\r\n \"rating\": 5.0\r\n }\r\n ]\r\n}",
// - "{\r \"movie\": [\r {\r \"name\": \"Test\",\r \"rating\": 5.0\r }\r ]\r}"
// Length on Windows is 81 and on Linux 74
//
// Minified length: 40
// - "{\"movie\":\"name\":\"Test\",\"rating\":5.0}]}"

Assert.Contains(allowedLengths, i => i == content.Length);

UTHelpers.Down(path);
}

[Fact]
public void FileContent_AlwaysMinify_Encrypted()
{
var path = UTHelpers.GetFullFilePath($"{nameof(FileContent_AlwaysMinify_Encrypted)}");

var storeFileEncrypted = new DataStore(path, encryptionKey: "53cr3t");
storeFileEncrypted.InsertItem<Movie>("movie", new Movie { Name = "Matrix", Rating = 5 });
var content = UTHelpers.GetFileContent(path);

Assert.Equal(88, content.Length);

UTHelpers.Down(path);
}

[Fact]
public void FileNotFound_CreateNewFile_Encrypted()
{
var path = UTHelpers.GetFullFilePath($"CreateNewFile_Encrypted_{DateTime.UtcNow.Ticks}");

var storeFileNotFound = new DataStore(path, encryptionKey: "53cr3t");
var collectionKeys = storeFileNotFound.GetKeys();
Assert.Equal(0, collectionKeys.Count);

var storeFileFound = new DataStore(path, encryptionKey: "53cr3t");
var collectionKeysFileFound = storeFileNotFound.GetKeys();
Assert.Equal(0, collectionKeysFileFound.Count);

UTHelpers.Down(path);
}

[Fact]
public void File_Has_Correct_PropertyNames_DynamicCollection()
{
var path = UTHelpers.GetFullFilePath($"CreateNewFile_{DateTime.UtcNow.Ticks}");

var store = new DataStore(path);

var collection = store.GetCollection("User");
collection.InsertOne(new { id = 1, name = "Test" });
var collection2 = store.GetCollection("User");
collection2.InsertOne(new { id = 2, name = "Test2" });
var collection3 = store.GetCollection("user");
collection3.InsertOne(new { id = 3, name = "Test3" });

var content = UTHelpers.GetFileContent(path);
var propCountLower = Regex.Matches(content, "user").Count;
Assert.Equal(1, propCountLower);
var propCountUpper = Regex.Matches(content, "User").Count;
Assert.Equal(0, propCountUpper);

var assertCollection = store.GetCollection("user");
Assert.Equal(3, assertCollection.Count);
}

[Fact]
public void File_Has_Correct_PropertyNames_TypedCollection()
{
var path = UTHelpers.GetFullFilePath($"CreateNewFile_{DateTime.UtcNow.Ticks}");

var store = new DataStore(path);

var collection = store.GetCollection<Employee>();
collection.InsertOne(new Employee { Id = 1, Name = "first" });
var collection2 = store.GetCollection("Employee");
collection2.InsertOne(new Employee { Id = 2, Name = "second" });
var collection3 = store.GetCollection("employee");
collection3.InsertOne(new Employee { Id = 3, Name = "third" });

var content = UTHelpers.GetFileContent(path);
var propCountLower = Regex.Matches(content, "employee").Count;
Assert.Equal(1, propCountLower);
var propCountUpper = Regex.Matches(content, "Employee").Count;
Assert.Equal(0, propCountUpper);

var assertCollection = store.GetCollection<Employee>();
Assert.Equal(3, assertCollection.Count);
}

[Fact]
public void File_Has_Correct_PropertyNames_Single_Item()
{
var path = UTHelpers.GetFullFilePath($"CreateNewFile_{DateTime.UtcNow.Ticks}");

var store = new DataStore(path);

store.ReplaceItem("TestOkIsThis1", 1, true);
store.ReplaceItem("TestOkIsThis2", 2, true);
store.ReplaceItem("TestOkIsThis2", 3, true);

var content = UTHelpers.GetFileContent(path);
var propCount = Regex.Matches(content, "testOkIsThis2").Count;
Assert.Equal(1, propCount);
}

public class Employee
{
public int Id { get; set; }
Expand Down
Loading

0 comments on commit 85e7fd3

Please sign in to comment.