Skip to content

Commit

Permalink
Add immutable convention tests for nullable (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci authored Oct 23, 2020
1 parent fdac807 commit 878ba12
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/Context.Tests/ImmutableConventionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,106 @@ public interface IA
}
}

public class NullableReferenceTypeCase : IClassFixture<MongoResource>
{
private readonly MongoDbContextData _context;

public NullableReferenceTypeCase(MongoResource mongoResource)
{
_context = CreateContext(mongoResource);
}

[Fact]
public async Task ApplyConvention_WithoutValue_SerializeSuccessful()
{
// Arrange
IMongoCollection<A> collection = _context.CreateCollection<A>();

// Act
await collection.InsertOneAsync(new A("a"));

// Assert
A result = await collection.FindSync(FilterDefinition<A>.Empty).FirstAsync();
result.MatchSnapshot();
}

[Fact]
public async Task ApplyConvention_WithValue_SerializeSuccessful()
{
// Arrange
IMongoCollection<A> collection = _context.CreateCollection<A>();

// Act
await collection.InsertOneAsync(new A("a", "b"));

// Assert
A result = await collection.FindSync(FilterDefinition<A>.Empty).FirstAsync();
result.MatchSnapshot();
}

public class A
{
public A(string _a, string? _b = default)
{
_A = _a;
_B = _b;
}

public string _A { get; }
public string? _B { get; }
}
}

public class NullableValueTypeCase : IClassFixture<MongoResource>
{
private readonly MongoDbContextData _context;

public NullableValueTypeCase(MongoResource mongoResource)
{
_context = CreateContext(mongoResource);
}

[Fact]
public async Task ApplyConvention_WithoutValue_SerializeSuccessful()
{
// Arrange
IMongoCollection<A> collection = _context.CreateCollection<A>();

// Act
await collection.InsertOneAsync(new A("a"));

// Assert
A result = await collection.FindSync(FilterDefinition<A>.Empty).FirstAsync();
result.MatchSnapshot();
}

[Fact]
public async Task ApplyConvention_WithValue_SerializeSuccessful()
{
// Arrange
IMongoCollection<A> collection = _context.CreateCollection<A>();

// Act
await collection.InsertOneAsync(new A("a", 9));

// Assert
A result = await collection.FindSync(FilterDefinition<A>.Empty).FirstAsync();
result.MatchSnapshot();
}

public class A
{
public A(string _a, int? _b = default)
{
_A = _a;
_B = _b;
}

public string _A { get; }
public int? _B { get; }
}
}

public class AbstractImmutableWithBasePropertyCase : IClassFixture<MongoResource>
{
private readonly MongoDbContextData _context;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_A": "a",
"_B": "b"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_A": "a",
"_B": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_A": "a",
"_B": 9
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_A": "a",
"_B": null
}

0 comments on commit 878ba12

Please sign in to comment.