public async Task Should_use_ctor_of_value_types_instead_of_factory_because_EF_is_source_of_truth() { var entity = new TestEntity_with_Enum_and_ValueObjects { Id = new Guid("A53F60CD-B53E-40E3-B16F-05E9A223E238"), StringBasedReferenceValueObject = StringBasedReferenceValueObject.Create("value"), StringBasedStructValueObject = StringBasedStructValueObject.Create("other value"), TestSmartEnum_Struct_IntBased = TestSmartEnum_Struct_IntBased.Value1, TestSmartEnum_Struct_StringBased = TestSmartEnum_Struct_StringBased.Value1, Boundary = Boundary.Create(10, 20) }; _ctx.Add(entity); await _ctx.SaveChangesAsync(); await using var command = _ctx.Database.GetDbConnection().CreateCommand(); command.CommandText = @" UPDATE TestEntities_with_Enum_and_ValueObjects SET StringBasedStructValueObject = '', Boundary_Lower = 30 "; await command.ExecuteNonQueryAsync(); _ctx.ChangeTracker.Clear(); var loadedEntity = await _ctx.TestEntities_with_Enum_and_ValueObjects.SingleAsync(); loadedEntity.StringBasedStructValueObject.Property.Should().Be(String.Empty); loadedEntity.Boundary.Lower.Should().Be(30); loadedEntity.Boundary.Upper.Should().Be(20); }
public void Construction() { Assert.Throws <ArgumentNullException>(() => Boundary.Create((string)null, true)); Assert.Throws <ArgumentException>(() => Boundary.Create(double.NaN, true)); Assert.Throws <ArgumentException>(() => Boundary.Create(double.NaN, false)); Assert.Throws <ArgumentException>(() => Boundary.Create(float.NaN, true)); Assert.Throws <ArgumentException>(() => Boundary.Create(float.NaN, false)); }
public void ConstructionInvalid(double low, double high) { Assert.Throws <ArgumentException>( () => Range.Create(low, high)); if (double.IsNaN(low) || double.IsNaN(high)) { return; } Assert.Throws <ArgumentException>( () => Range.Create(Boundary.Create(low, true), Boundary.Create(high, true))); }
public void ArgumentAssertion() { var i = Boundary.Create(1, true); var nullB = (IBoundary <int>)null; Assert.Throws <ArgumentNullException>(() => i.CompareLowTo((object)null)); Assert.Throws <ArgumentNullException>(() => i.CompareLowTo(nullB)); Assert.Throws <ArgumentException>(() => i.CompareLowTo("hello")); Assert.Throws <ArgumentNullException>(() => i.CompareHighTo((object)null)); Assert.Throws <ArgumentNullException>(() => i.CompareHighTo(nullB)); Assert.Throws <ArgumentException>(() => i.CompareHighTo("hello")); var s = Boundary.Create("hi", true); Assert.Throws <ArgumentException>(() => i.CompareLowTo(s)); Assert.Throws <ArgumentException>(() => i.CompareHighTo(s)); }
public static void CheckInvalid(double low, double high) { Range.IsValid(low, high).Should().BeFalse(); if (double.IsNaN(low) || double.IsNaN(high)) { return; } var a = Boundary.Create(low, false); var b = Boundary.Create(high, false); Range.IsValid(a, b).Should().BeFalse(); a = Boundary.Create(low, true); Range.IsValid(a, b).Should().BeFalse(); b = Boundary.Create(high, true); Range.IsValid(a, b).Should().BeFalse(); Range.IsValid(new TestComparable1(), new TestComparable1()) .Should().BeFalse(); Assert.Throws <InvalidOperationException>( () => Range.IsValid(new TestComparable2(), new TestComparable2())); }
public async Task Should_write_and_read_enums_and_value_types() { var entity = new TestEntity_with_Enum_and_ValueObjects { Id = new Guid("A53F60CD-B53E-40E3-B16F-05E9A223E238"), TestEnum = TestEnum.Item1, IntBasedReferenceValueObject = IntBasedReferenceValueObject.Create(42), IntBasedStructValueObject = IntBasedStructValueObject.Create(43), StringBasedReferenceValueObject = StringBasedReferenceValueObject.Create("value 1"), StringBasedStructValueObject = StringBasedStructValueObject.Create("value 2"), TestSmartEnum_Struct_IntBased = TestSmartEnum_Struct_IntBased.Value1, TestSmartEnum_Struct_StringBased = TestSmartEnum_Struct_StringBased.Value1, Boundary = Boundary.Create(10, 20) }; _ctx.Add(entity); await _ctx.SaveChangesAsync(); _ctx.ChangeTracker.Clear(); (await _ctx.TestEntities_with_Enum_and_ValueObjects.SingleAsync()) .Should().BeEquivalentTo(entity); }
public static void CheckValidity(double low, double high) { Range.IsValid(low, high).Should().BeTrue(); var a = Boundary.Create(low, true); Range.IsValid(a, a).Should().BeTrue(); var b = Boundary.Create(high, true); Range.AssertIsValid(a, b).Should().BeTrue(); var c = Boundary.Create(low, false); Range.IsValid(c, b).Should().BeTrue(); Range.IsValid(c, a).Should().BeFalse(); Range.IsValid(c, c).Should().BeFalse(); Assert.Throws <ArgumentException>(() => Range.AssertIsValid(a, c)); var d = Boundary.Create(high, false); Range.IsValid(b, d).Should().BeFalse(); }
public static void Equality(double value, bool inclusive) { var a = Boundary.Create(value, inclusive); var b = Boundary.Create(value, inclusive); var ib = new B(value, inclusive); a.CompareLowTo(ib).Should().Be(0); a.CompareHighTo(ib).Should().Be(0); a.Equals((object)b).Should().BeTrue(); a.Equals("nope").Should().BeFalse(); a.CanRangeWith("nope").Should().BeFalse(); (a == b).Should().BeTrue(); (a >= b).Should().BeTrue(); (a <= b).Should().BeTrue(); a.CompareLowTo(a).Should().Be(0); a.CompareHighTo(a).Should().Be(0); a.GetHashCode().Should().Be(b.GetHashCode()); ib = new B(value + 1, inclusive); a.CompareLowTo(ib).Should().Be(-1); a.CompareHighTo(ib).Should().Be(-1); ib = new B(value - 1, inclusive); a.CompareLowTo(ib).Should().Be(+1); a.CompareHighTo(ib).Should().Be(+1); ib = new B(value + 1, !inclusive); a.CompareLowTo(ib).Should().Be(-1); a.CompareHighTo(ib).Should().Be(-1); ib = new B(value - 1, !inclusive); a.CompareLowTo(ib).Should().Be(+1); a.CompareHighTo(ib).Should().Be(+1); var c = Boundary.Create(value + 1, inclusive); var d = Boundary.Create(value + 1, !inclusive); a.Equals((object)c).Should().BeFalse(); (a != c).Should().BeTrue(); (a < c).Should().BeTrue(); (a <= c).Should().BeTrue(); a.CompareLowTo(c).Should().Be(-1); a.CompareHighTo(c).Should().Be(-1); c.CompareLowTo(a).Should().Be(+1); c.CompareHighTo(a).Should().Be(+1); a.CompareLowTo(d).Should().Be(-1); a.CompareHighTo(d).Should().Be(-1); d.CompareLowTo(a).Should().Be(+1); d.CompareHighTo(a).Should().Be(+1); a.GetHashCode().Should().NotBe(c.GetHashCode()); c = Boundary.Create(value - 1, inclusive); (a == c).Should().BeFalse(); (a > c).Should().BeTrue(); (a >= c).Should().BeTrue(); a.GetHashCode().Should().NotBe(c.GetHashCode()); d = Boundary.Create(value, !inclusive); d.Equals(c).Should().BeFalse(); d.CompareTo(c).Should().BePositive(); var i = inclusive ? +1 : -1; ib = new B(value, !inclusive); a.CompareLowTo(ib).Should().Be(-i); a.CompareHighTo(ib).Should().Be(+i); a.CompareLowTo(d).Should().Be(-i); a.CompareHighTo(d).Should().Be(+i); d.CompareLowTo(a).Should().Be(+i); d.CompareHighTo(a).Should().Be(-i); d.GetHashCode().Should().NotBe(c.GetHashCode()); d.GetHashCode().Should().NotBe(a.GetHashCode()); a.Equals(d).Should().BeFalse(); Assert.Throws <ArgumentException>( () => a.CompareTo(d)); }
public void Deconstruction() { var(value, inclusive) = Boundary.Create(10, true); value.Should().Be(10); inclusive.Should().Be(true); }
private static async Task DoDatabaseRequestsAsync(IServiceProvider serviceProvider, LoggingLevelSwitch loggingLevelSwitch) { await using var scope = serviceProvider.CreateAsyncScope(); var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >(); var ctx = scope.ServiceProvider.GetRequiredService <ProductsDbContext>(); await InsertProductAsync(ctx, new Product(Guid.NewGuid(), ProductName.Create("Apple"), ProductCategory.Fruits, SpecialProductType.Special, Boundary.Create(1, 2))); try { loggingLevelSwitch.MinimumLevel = LogEventLevel.Fatal; await InsertProductAsync(ctx, new Product(Guid.NewGuid(), ProductName.Create("Pear"), ProductCategory.Get("Invalid Category"), SpecialProductType.Special, Boundary.Create(1, 2))); loggingLevelSwitch.MinimumLevel = LogEventLevel.Information; } catch (DbUpdateException) { logger.LogError("Error during persistence of invalid category"); } var products = await ctx.Products.AsNoTracking().Where(p => p.Category == ProductCategory.Fruits).ToListAsync(); logger.LogInformation("Loaded products: {@Products}", products); }