Skip to content

Commit

Permalink
fix: tags incorrectly serialized as dictionary to string (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnc1997 authored Apr 7, 2024
1 parent eea8f1b commit 4c3f35a
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Data.Common;
using System.Text.Json;
using AzureAppConfigurationEmulator.Data.Abstractions;
Expand All @@ -14,11 +15,12 @@ public DbParameter Create<TValue>(string name, TValue? value)
bool b => new SqliteParameter(name, SqliteType.Integer) { Value = b ? 1 : 0 },
DateTime d => new SqliteParameter(name, SqliteType.Text) { Value = d.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss") },
DateTimeOffset d => new SqliteParameter(name, SqliteType.Text) { Value = d.UtcDateTime.ToString("yyyy-MM-dd HH:mm:ss") },
IDictionary<string, object?> d => new SqliteParameter(name, SqliteType.Text) { Value = JsonSerializer.Serialize(d) },
int i => new SqliteParameter(name, SqliteType.Integer) { Value = i },
null => new SqliteParameter(name, SqliteType.Text) { Value = DBNull.Value },
string s => new SqliteParameter(name, SqliteType.Text) { Value = s },
_ => new SqliteParameter(name, SqliteType.Text) { Value = value.ToString() }
IDictionary d => new SqliteParameter(name, SqliteType.Text) { Value = JsonSerializer.Serialize(d) },
IEnumerable e => new SqliteParameter(name, SqliteType.Text) { Value = JsonSerializer.Serialize(e) },
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AzureAppConfigurationEmulator.Common\AzureAppConfigurationEmulator.Common.csproj"/>
<ProjectReference Include="..\..\src\AzureAppConfigurationEmulator.Data.Sqlite\AzureAppConfigurationEmulator.Data.Sqlite.csproj"/>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,187 @@
using AzureAppConfigurationEmulator.Common.Models;
using Microsoft.Data.Sqlite;
using NUnit.Framework;

namespace AzureAppConfigurationEmulator.Data.Sqlite.Tests;

public class SqliteDbParameterFactoryTests
{
[TestCaseSource(nameof(Create_DbParameter_ConfigurationSettingContentType_TestCases))]
public void Create_DbParameter_ConfigurationSettingContentType(string? contentType, object expected)
{
// Arrange
var factory = new SqliteDbParameterFactory();
var setting = new ConfigurationSetting("TestEtag", "TestKey", DateTimeOffset.UtcNow, false, ContentType: contentType);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.ContentType);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(expected));
});
}

// ReSharper disable once InconsistentNaming
private static object[] Create_DbParameter_ConfigurationSettingContentType_TestCases =
[
new object?[] { "TestContentType", "TestContentType" },
new object?[] { null, DBNull.Value }
];

[Test]
public void Create_DbParameter_ConfigurationSettingEtag()
{
// Arrange
var factory = new SqliteDbParameterFactory();
const string etag = "TestEtag";
var setting = new ConfigurationSetting(etag, "TestKey", DateTimeOffset.UtcNow, false);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Etag);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(etag));
});
}

[Test]
public void Create_DbParameter_ConfigurationSettingKey()
{
// Arrange
var factory = new SqliteDbParameterFactory();
const string key = "TestKey";
var setting = new ConfigurationSetting("TestEtag", key, DateTimeOffset.UtcNow, false);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Key);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(key));
});
}

[TestCaseSource(nameof(Create_DbParameter_ConfigurationSettingLabel_TestCases))]
public void Create_DbParameter_ConfigurationSettingLabel(string? label, object expected)
{
// Arrange
var factory = new SqliteDbParameterFactory();
var setting = new ConfigurationSetting("TestEtag", "TestKey", DateTimeOffset.UtcNow, false, Label: label);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Label);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(expected));
});
}

// ReSharper disable once InconsistentNaming
private static object[] Create_DbParameter_ConfigurationSettingLabel_TestCases =
[
new object?[] { "TestLabel", "TestLabel" },
new object?[] { null, DBNull.Value }
];

[Test]
public void Create_DbParameter_ConfigurationSettingLastModified()
{
// Arrange
var factory = new SqliteDbParameterFactory();
var lastModified = DateTimeOffset.UtcNow;
var setting = new ConfigurationSetting("TestEtag", "TestKey", lastModified, false);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.LastModified);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(lastModified.UtcDateTime.ToString("yyyy-MM-dd HH:mm:ss")));
});
}

[TestCase(false, 0)]
[TestCase(true, 1)]
public void Create_DbParameter_ConfigurationSettingLocked(bool locked, int expected)
{
// Arrange
var factory = new SqliteDbParameterFactory();
var setting = new ConfigurationSetting("TestEtag", "TestKey", DateTimeOffset.UtcNow, locked);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Locked);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Integer));
Assert.That(parameter.Value, Is.EqualTo(expected));
});
}

[TestCaseSource(nameof(Create_DbParameter_ConfigurationSettingTags_TestCases))]
public void Create_DbParameter_ConfigurationSettingTags(IReadOnlyDictionary<string, string>? tags, object expected)
{
// Arrange
var factory = new SqliteDbParameterFactory();
var setting = new ConfigurationSetting("TestEtag", "TestKey", DateTimeOffset.UtcNow, false, Tags: tags);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Tags);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(expected));
});
}

// ReSharper disable once InconsistentNaming
private static object[] Create_DbParameter_ConfigurationSettingTags_TestCases =
[
new object?[] { new Dictionary<string, string> { { "TestKey", "TestValue" } }, "{\"TestKey\":\"TestValue\"}" },
new object?[] { null, DBNull.Value }
];

[TestCaseSource(nameof(Create_DbParameter_ConfigurationSettingValue_TestCases))]
public void Create_DbParameter_ConfigurationSettingValue(string? value, object expected)
{
// Arrange
var factory = new SqliteDbParameterFactory();
var setting = new ConfigurationSetting("TestEtag", "TestKey", DateTimeOffset.UtcNow, false, Value: value);

// Act
var parameter = (SqliteParameter)factory.Create("TestName", setting.Value);

// Assert
Assert.Multiple(() =>
{
Assert.That(parameter.SqliteType, Is.EqualTo(SqliteType.Text));
Assert.That(parameter.Value, Is.EqualTo(expected));
});
}

// ReSharper disable once InconsistentNaming
private static object[] Create_DbParameter_ConfigurationSettingValue_TestCases =
[
new object?[] { "TestValue", "TestValue" },
new object?[] { null, DBNull.Value }
];

[Test]
public void Create_DbParameterName_NameAndValue()
{
Expand Down Expand Up @@ -44,8 +221,7 @@ public void Create_DbParameterType_NameAndValue(object? value, SqliteType expect
new object?[] { new Dictionary<string, object?> { { "TestKey", null } }, SqliteType.Text },
new object?[] { 0, SqliteType.Integer },
new object?[] { null, SqliteType.Text },
new object?[] { "Hello World", SqliteType.Text },
new object?[] { 0.0, SqliteType.Text }
new object?[] { "Hello World", SqliteType.Text }
];

[TestCaseSource(nameof(Create_DbParameterValue_NameAndValue_TestCases))]
Expand Down Expand Up @@ -74,7 +250,16 @@ public void Create_DbParameterValue_NameAndValue(object? value, object expected)
new object?[] { new Dictionary<string, object?> { { "TestKey", null } }, "{\"TestKey\":null}" },
new object?[] { 0, 0 },
new object?[] { null, DBNull.Value },
new object?[] { "Hello World", "Hello World" },
new object?[] { 0.0, "0" }
new object?[] { "Hello World", "Hello World" }
];

[Test]
public void Create_ArgumentOutOfRangeException_NonSupportedType()
{
// Arrange
var factory = new SqliteDbParameterFactory();

// Assert
Assert.Throws<ArgumentOutOfRangeException>(() => factory.Create("TestName", 0.0));
}
}

0 comments on commit 4c3f35a

Please sign in to comment.