public void Updates_the_entity(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange database.InsertRange( new[] { new Dog { Name = "Some name1", Age = 10 }, new Dog { Name = "Some name2", Age = 10 }, new Dog { Name = "Some name2", Age = 11 } }); // Act var entities = database.GetRange <Dog>($"WHERE Age = 10").ToList(); foreach (var entity in entities) { entity.Name = "Other name"; } var result = database.UpdateRange(entities); // Assert result.NumRowsAffected.Should().Be(2); var updatedEntities = database.GetRange <Dog>($"WHERE Name = 'Other name'"); updatedEntities.Count().Should().Be(2); } }
public void Updates_entities_with_composite_keys(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange database.InsertRange( new[] { new CompositeKeys { Key1 = 5, Key2 = 20, Name = "Some name1" }, new CompositeKeys { Key1 = 6, Key2 = 21, Name = "Some name2" }, new CompositeKeys { Key1 = 7, Key2 = 22, Name = "Some other name" } }); // Act var entities = database.GetRange <CompositeKeys>($"WHERE Name Like 'Some name%'").ToList(); foreach (var entity in entities) { entity.Name = "Other name"; } var result = database.UpdateRange(entities); // Assert result.NumRowsAffected.Should().Be(2); var updatedEntities = database.GetRange <CompositeKeys>($"WHERE Name = 'Other name'"); updatedEntities.Count().Should().Be(2); } }
public async void SO35470588_WrongValuePidValue() { using (var database = BlankDatabaseFactory.MakeDatabase(Dialect.SqlServer2012)) { // nuke, rebuild, and populate the table try { database.Execute("drop table TPTable"); } catch { /* don't care */ } database.Execute(@" create table TPTable (Pid int not null primary key identity(1,1), Value int not null); insert TPTable (Value) values (2), (568)"); // fetch the data using the query in the question, then force to a dictionary var rows = (await database.QueryAsync <TPTable>("select * from TPTable").ConfigureAwait(false)) .ToDictionary(x => x.Pid); // check the number of rows Assert.Equal(2, rows.Count); // check row 1 var row = rows[1]; Assert.Equal(1, row.Pid); Assert.Equal(2, row.Value); // check row 2 row = rows[2]; Assert.Equal(2, row.Pid); Assert.Equal(568, row.Value); } }
public async Task TestSchemaChangedViaFirstOrDefaultAsync() { using (var database = BlankDatabaseFactory.MakeDatabase(Dialect.SqlServer2012)) { await database.ExecuteAsync("create table #dog(Age int, Name nvarchar(max)) insert #dog values(1, \'Alf\')").ConfigureAwait(false); try { var d = await database.QueryFirstOrDefaultAsync <Dog>("select * from #dog").ConfigureAwait(false); Assert.Equal("Alf", d.Name); Assert.Equal(1, d.Age); database.Execute("alter table #dog drop column Name"); d = await database.QueryFirstOrDefaultAsync <Dog>("select * from #dog").ConfigureAwait(false); Assert.Null(d.Name); Assert.Equal(1, d.Age); } finally { await database.ExecuteAsync("drop table #dog").ConfigureAwait(false); } } }
public void Finds_entities_with_all_possible_types(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var id = database.Insert <int>( new PropertyAllPossibleTypes { Int16Property = -16, NullableInt16Property = -16, Int32Property = -32, NullableInt32Property = -32, Int64Property = -64, NullableInt64Property = -64, SingleProperty = 1, NullableSingleProperty = 1, DoubleProperty = 2, NullableDoubleProperty = 2, DecimalProperty = 10, NullableDecimalProperty = 10, BoolProperty = true, NullableBoolProperty = true, StringProperty = "Foo", CharProperty = 'F', NullableCharProperty = 'N', GuidProperty = new Guid("da8326a1-c703-4a79-9fb2-2909b0f40367"), NullableGuidProperty = new Guid("706e6bcf-4a6d-4d19-91e9-935852140c4d"), DateTimeProperty = new DateTime(2016, 12, 31), NullableDateTimeProperty = new DateTime(2016, 12, 31), DateTimeOffsetProperty = new DateTimeOffset(new DateTime(2016, 12, 31), new TimeSpan(0, 1, 0, 0)), NullableDateTimeOffsetProperty = new DateTimeOffset(new DateTime(2016, 12, 31), new TimeSpan(0, 1, 0, 0)), ByteArrayProperty = new byte[] { 1, 2, 3 } } ); // Act var entity = database.Find <PropertyAllPossibleTypes>(id); // Assert entity.Int16Property.Should().Be(-16); entity.NullableInt16Property.Should().Be(-16); entity.Int32Property.Should().Be(-32); entity.NullableInt32Property.Should().Be(-32); entity.Int64Property.Should().Be(-64); entity.NullableInt64Property.Should().Be(-64); entity.SingleProperty.Should().Be(1); entity.NullableSingleProperty.Should().Be(1); entity.DoubleProperty.Should().Be(2); entity.NullableDoubleProperty.Should().Be(2); entity.DecimalProperty.Should().Be(10); entity.NullableDecimalProperty.Should().Be(10); entity.BoolProperty.Should().Be(true); entity.NullableBoolProperty.Should().Be(true); entity.StringProperty.Should().Be("Foo"); entity.CharProperty.Should().Be('F'); entity.NullableCharProperty.Should().Be('N'); entity.GuidProperty.Should().Be(new Guid("da8326a1-c703-4a79-9fb2-2909b0f40367")); entity.NullableGuidProperty.Should().Be(new Guid("706e6bcf-4a6d-4d19-91e9-935852140c4d")); entity.DateTimeProperty.Should().Be(new DateTime(2016, 12, 31)); entity.NullableDateTimeProperty.Should().Be(new DateTime(2016, 12, 31)); entity.DateTimeOffsetProperty.Should().Be(new DateTimeOffset(new DateTime(2016, 12, 31), new TimeSpan(0, 1, 0, 0))); entity.NullableDateTimeOffsetProperty.Should().Be(new DateTimeOffset(new DateTime(2016, 12, 31), new TimeSpan(0, 1, 0, 0))); entity.ByteArrayProperty.Should().BeEquivalentTo(new byte[] { 1, 2, 3 }, o => o.WithStrictOrdering()); } }
protected DatabaseFixture(string dialectName) { this.Database = BlankDatabaseFactory.MakeDatabase(dialectName); this.DatabaseDialect = this.Database.Dialect; this.DefaultDatabase = new DefaultDatabase(this.Database.Connection, this.DatabaseDialect); }
public void Does_not_set_readonly_properties() { using (var database = BlankDatabaseFactory.MakeDatabase(Dialect.SqlServer2012)) { var obj = database.QuerySingle <HazGetOnly>("select 42 as [Id], \'def\' as [Name];"); Assert.Equal(default, obj.Id);