refactor: use snake case for cache json

recyclarr
Robert Dailey 4 years ago
parent 020e81b145
commit 77917aa08d

@ -4,9 +4,11 @@ using System.IO;
using System.IO.Abstractions;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NSubstitute;
using NUnit.Framework;
using Serilog;
using TestLibrary.NSubstitute;
using Trash.Cache;
using Trash.Config;
@ -23,6 +25,14 @@ namespace Trash.Tests.Cache
Filesystem = fs ?? Substitute.For<IFileSystem>();
StoragePath = Substitute.For<ICacheStoragePath>();
ConfigProvider = Substitute.For<IConfigurationProvider>();
JsonSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
// Set up a default for the active config's base URL. This is used to generate part of the path
ConfigProvider.ActiveConfiguration = Substitute.For<IServiceConfiguration>();
@ -31,6 +41,7 @@ namespace Trash.Tests.Cache
Cache = new ServiceCache(Filesystem, StoragePath, ConfigProvider, Substitute.For<ILogger>());
}
public JsonSerializerSettings JsonSettings { get; }
public ServiceCache Cache { get; }
public IConfigurationProvider ConfigProvider { get; }
public ICacheStoragePath StoragePath { get; }
@ -107,6 +118,17 @@ namespace Trash.Tests.Cache
.WithMessage("CacheObjectNameAttribute is missing*");
}
[Test]
public void Properties_are_saved_using_snake_case()
{
var ctx = new Context();
ctx.StoragePath.Path.Returns("testpath");
ctx.Cache.Save(new ObjectWithAttribute {TestValue = "Foo"});
ctx.Filesystem.File.Received()
.WriteAllText(Arg.Any<string>(), Verify.That<string>(json => json.Should().Contain("\"test_value\"")));
}
[Test]
public void Saving_with_attribute_parses_correctly()
{
@ -122,7 +144,7 @@ namespace Trash.Tests.Cache
dynamic expectedJson = new {TestValue = "Foo"};
var expectedPath = Path.Combine(expectedParentDirectory, $"{ValidObjectName}.json");
ctx.Filesystem.File.Received()
.WriteAllText(expectedPath, JsonConvert.SerializeObject(expectedJson, Formatting.Indented));
.WriteAllText(expectedPath, JsonConvert.SerializeObject(expectedJson, ctx.JsonSettings));
}
[Test]

@ -7,6 +7,7 @@ using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Serilog;
using Trash.Config;
@ -59,7 +60,14 @@ namespace Trash.Cache
{
var path = PathFromAttribute<T>();
_fileSystem.Directory.CreateDirectory(Path.GetDirectoryName(path));
_fileSystem.File.WriteAllText(path, JsonConvert.SerializeObject(obj, Formatting.Indented));
_fileSystem.File.WriteAllText(path, JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
}));
}
private static string GetCacheObjectNameAttribute<T>()

Loading…
Cancel
Save