From 77917aa08d1978659198a53380f8174ce37a7c67 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 15 May 2021 19:15:03 -0500 Subject: [PATCH] refactor: use snake case for cache json --- src/Trash.Tests/Cache/ServiceCacheTest.cs | 24 ++++++++++++++++++++++- src/Trash/Cache/ServiceCache.cs | 10 +++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Trash.Tests/Cache/ServiceCacheTest.cs b/src/Trash.Tests/Cache/ServiceCacheTest.cs index d264b120..a1f3e981 100644 --- a/src/Trash.Tests/Cache/ServiceCacheTest.cs +++ b/src/Trash.Tests/Cache/ServiceCacheTest.cs @@ -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(); StoragePath = Substitute.For(); ConfigProvider = Substitute.For(); + 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(); @@ -31,6 +41,7 @@ namespace Trash.Tests.Cache Cache = new ServiceCache(Filesystem, StoragePath, ConfigProvider, Substitute.For()); } + 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(), Verify.That(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] diff --git a/src/Trash/Cache/ServiceCache.cs b/src/Trash/Cache/ServiceCache.cs index 560520c1..3749f0dd 100644 --- a/src/Trash/Cache/ServiceCache.cs +++ b/src/Trash/Cache/ServiceCache.cs @@ -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(); _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()