/// <summary> /// Creates an SqlQuery to perform an update based upon the values in the object delta. /// </summary> /// <param name="objectDelta">The object delta to create the query for.</param> /// <returns> /// The created <see cref="SqlQuery" />. /// </returns> public SqlQuery BuildUpdateSqlQuery(ObjectDelta objectDelta) { if (objectDelta == null) { throw new ArgumentNullException("objectDelta"); } if (log.IsDebug) { log.Debug(LogMessages.SqlDialect_CreatingSqlQuery, "UPDATE"); } var objectInfo = ObjectInfo.For(objectDelta.ForType); var builder = new UpdateSqlBuilder(this.SqlCharacters) .Table(objectInfo); foreach (var change in objectDelta.Changes) { builder.SetColumnValue(change.Key, change.Value); } var sqlQuery = builder .Where(objectInfo.TableInfo.IdentifierColumn.ColumnName).IsEqualTo(objectDelta.Identifier) .ToSqlQuery(); return(sqlQuery); }
public void AbleToAddObjectListItemThenApplyViaPatch() { var testObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(testObj); var updatedTestObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(updatedTestObj); updatedTestObj.ListOfObjectProperty.Add(new TestClass { StringProperty = "added" }); var diff = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var updatePatch = JsonConvert.SerializeObject(diff.NewValues); var objToUpdate = GetSimpleTestObject(); PopulateObjectListOnTestClass(objToUpdate); var updatedObj = ObjectDelta.PatchObject(objToUpdate, updatePatch); updatedTestObj.ListOfObjectProperty.Count.ShouldEqual(updatedObj.ListOfObjectProperty.Count); var addedListItem = updatedObj.ListOfObjectProperty.SingleOrDefault(obj => obj != null && obj.StringProperty == "added"); addedListItem.ShouldNotBeNull(); }
public void BuildUpdateSqlQueryForObjectDelta() { ObjectInfo.MappingConvention = new ConventionMappingConvention( UnitTest.GetConventionMappingSettings(IdentifierStrategy.DbGenerated)); var mockSqlDialect = new Mock <SqlDialect>(SqlCharacters.Empty); mockSqlDialect.CallBase = true; var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var sqlQuery = mockSqlDialect.Object.BuildUpdateSqlQuery(objectDelta); Assert.Equal("UPDATE Sales.Customers SET Name = ? WHERE (Id = ?)", sqlQuery.CommandText); Assert.Equal(2, sqlQuery.Arguments.Count); Assert.Equal(DbType.String, sqlQuery.Arguments[0].DbType); Assert.Equal("Fred Flintstone", sqlQuery.Arguments[0].Value); Assert.Equal(DbType.Int32, sqlQuery.Arguments[1].DbType); Assert.Equal(1234, sqlQuery.Arguments[1].Value); }
public void Equals_Samecollection_ReturnsTrue() { //arrange var snd = new ObjectDelta <ExampleComplexClass>(new ExampleComplexClass() { CoverId = 1 }, new List <PropertyDelta>() { new PropertyDelta() { DataType = typeof(int), Name = "test", Value = 1, } }); //act var result = new ObjectDelta <ExampleComplexClass>(new ExampleComplexClass() { CoverId = 1 }, new List <PropertyDelta>() { new PropertyDelta() { DataType = typeof(int), Name = "test", Value = 1, } }).Equals(snd); //assert Assert.IsTrue(result); }
public void UpdateObjectDeltaReturnsTrueIfRecordUpdated() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockSqlDialect = new Mock <ISqlDialect>(); mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery("")); var mockCommand = new Mock <IDbCommand>(); mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1); var mockConnection = new Mock <IDbConnection>(); mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object); var mockDbDriver = new Mock <IDbDriver>(); mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object); var session = new Session( ConnectionScope.PerTransaction, mockSqlDialect.Object, mockDbDriver.Object, new SessionListeners()); Assert.True(session.Update(objectDelta)); mockDbDriver.VerifyAll(); mockCommand.VerifyAll(); }
public void UpdateObjectDeltaReturnsFalseIfNoRecordUpdated() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockSqlDialect = new Mock <ISqlDialect>(); mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery("")); var mockCommand = new Mock <IDbCommand>(); mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(0); var mockConnection = new Mock <IDbConnection>(); mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object); var mockDbDriver = new Mock <IDbDriver>(); mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object)); var session = new AsyncSession( ConnectionScope.PerTransaction, mockSqlDialect.Object, mockDbDriver.Object, new IDeleteListener[0], new IInsertListener[0], new IUpdateListener[0]); Assert.False(session.UpdateAsync(objectDelta).Result); mockDbDriver.VerifyAll(); mockCommand.VerifyAll(); }
public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); var session = new Session( ConnectionScope.PerTransaction, new Mock <ISqlDialect>().Object, new Mock <IDbDriver>().Object, new SessionListeners()); var exception = Assert.Throws <MicroLiteException>(() => session.Update(objectDelta)); Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.Message); }
public void BuildUpdateSqlQueryForObjectDelta() { ObjectInfo.MappingConvention = new ConventionMappingConvention( UnitTest.GetConventionMappingSettings(IdentifierStrategy.DbGenerated)); var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockSqlDialect = new Mock<SqlDialect>(SqlCharacters.Empty); mockSqlDialect.CallBase = true; var sqlQuery = mockSqlDialect.Object.BuildUpdateSqlQuery(objectDelta); Assert.Equal("UPDATE Sales.Customers SET Name = ? WHERE Id = ?", sqlQuery.CommandText); Assert.Equal(2, sqlQuery.Arguments.Count); Assert.Equal("Fred Flintstone", sqlQuery.Arguments[0]); Assert.Equal(1234, sqlQuery.Arguments[1]); }
public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); var session = new AsyncSession( ConnectionScope.PerTransaction, new Mock <ISqlDialect>().Object, new Mock <IDbDriver>().Object, new IDeleteListener[0], new IInsertListener[0], new IUpdateListener[0]); var exception = Assert.Throws <AggregateException>( () => session.UpdateAsync(objectDelta).Result); Assert.IsType <MicroLiteException>(exception.InnerException); Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.InnerException.Message); }
public void Comparer_DiffrentObjectsWithNoComplex_ReturnsDelta() { //arrange var fstObject = new ExampleComplexClass { CoverId = 1, IsDismounted = false, LightingId = 1 }; var sndObject = new ExampleComplexClass { CoverId = 2, IsDismounted = true, LightingId = 1 }; var expected = new ObjectDelta <ExampleComplexClass>(sndObject, new List <PropertyDelta> { new PropertyDelta { Value = false, DataType = typeof(bool), Name = nameof(ExampleComplexClass.IsDismounted) }, new PropertyDelta { Value = 1, DataType = typeof(int?), Name = nameof(ExampleComplexClass.CoverId) } }); //assert var result = new ObjectComparer().Compare(fstObject, sndObject); //act Assert.IsNotNull(result); Assert.IsNotNull(result.Deltas); Assert.That(result.Deltas, Has.Count.EqualTo(2)); Assert.AreEqual(result.NewValue, expected.NewValue); CollectionAssert.AreEqual(result.Deltas, expected.Deltas); }
public void AbleToDeltaAndPatchSimpleObject() { var testObj = GetSimpleTestObject(); var updatedTestObj = GetSimpleTestObject(); updatedTestObj.StringProperty = "this is an updated string"; updatedTestObj.IntProperty = 5678; updatedTestObj.DoubleProperty = 123.456; var delta = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var revertPatch = JsonConvert.SerializeObject(delta.OldValues); var revertedObj = ObjectDelta.PatchObject(updatedTestObj, revertPatch); testObj.StringProperty.ShouldEqual(revertedObj.StringProperty); testObj.IntProperty.ShouldEqual(revertedObj.IntProperty); testObj.DoubleProperty.ShouldEqual(revertedObj.DoubleProperty); }
public async Task <bool> UpdateAsync(ObjectDelta objectDelta, CancellationToken cancellationToken) { this.ThrowIfDisposed(); if (objectDelta == null) { throw new ArgumentNullException("objectDelta"); } if (objectDelta.ChangeCount == 0) { throw new MicroLiteException(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange); } var sqlQuery = this.SqlDialect.BuildUpdateSqlQuery(objectDelta); var rowsAffected = await this.ExecuteQueryAsync(sqlQuery, cancellationToken).ConfigureAwait(false); return(rowsAffected == 1); }
public bool Update(ObjectDelta objectDelta) { this.ThrowIfDisposed(); if (objectDelta == null) { throw new ArgumentNullException("objectDelta"); } if (objectDelta.ChangeCount == 0) { throw new MicroLiteException(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange); } var sqlQuery = this.SqlDialect.BuildUpdateSqlQuery(objectDelta); var rowsAffected = this.ExecuteQuery(sqlQuery); return(rowsAffected == 1); }
public void AbleToDeleteObjectListItemThenRevertViaPatch() { var testObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(testObj); var updatedTestObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(updatedTestObj); updatedTestObj.ListOfObjectProperty.RemoveAt(1); testObj.ListOfObjectProperty.Count.ShouldNotEqual(updatedTestObj.ListOfObjectProperty.Count); var deltaResult = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var revertedObj = ObjectDelta.PatchObject(updatedTestObj, deltaResult.OldValues); testObj.ListOfObjectProperty.Count.ShouldEqual(revertedObj.ListOfObjectProperty.Count); }
public void AbleToEditObjectInListThenRevertViaPatch() { var testObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(testObj); var updatedTestObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(updatedTestObj); updatedTestObj.ListOfObjectProperty[2].IntProperty = 30; updatedTestObj.ListOfObjectProperty[2].StringProperty = "this is an update to the last object in the list"; updatedTestObj.ListOfObjectProperty[2].DoubleProperty = 33.333; var deltaResult = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var revertedObj = ObjectDelta.PatchObject(updatedTestObj, deltaResult.OldValues); testObj.ListOfObjectProperty[2].IntProperty.ShouldEqual(revertedObj.ListOfObjectProperty[2].IntProperty); testObj.ListOfObjectProperty[2].StringProperty.ShouldEqual(revertedObj.ListOfObjectProperty[2].StringProperty); testObj.ListOfObjectProperty[2].DoubleProperty.ShouldEqual(revertedObj.ListOfObjectProperty[2].DoubleProperty); }
public void AbleToDeleteStringListItemThenRevertViaPatch() { var testObj = GetSimpleTestObject(); PopulateStringListOnTestClass(testObj); var updatedTestObj = GetSimpleTestObject(); PopulateStringListOnTestClass(updatedTestObj); updatedTestObj.ListOfStringProperty.Remove("list"); testObj.ListOfStringProperty.SequenceEqual(updatedTestObj.ListOfStringProperty).ShouldBeFalse(); var deltaResult = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var revertPatch = JsonConvert.SerializeObject(deltaResult.OldValues); var revertedObj = ObjectDelta.PatchObject(updatedTestObj, revertPatch); testObj.ListOfStringProperty.SequenceEqual(revertedObj.ListOfStringProperty).ShouldBeTrue(); }
public void Calculate_DeltaWithProperties_ReturnsTransformedInput() { //arrange var input = new ExampleComplexClass() { TypeId = 1, CoverId = 4, SerialNumber = "!231" }; var delta = new ObjectDelta <ExampleComplexClass>(input, new List <PropertyDelta>() { new PropertyDelta() { DataType = typeof(int?), Name = nameof(ExampleComplexClass.CoverId), Value = null, }, new PropertyDelta() { DataType = typeof(int), Name = nameof(ExampleComplexClass.TypeId), Value = 3, } }); var expected = new ExampleComplexClass() { TypeId = 3, CoverId = null, SerialNumber = "!231" }; //act var result = new ObjectComparer().Calculate(delta); //assert Assert.IsNotNull(result); Assert.AreEqual(expected, result); }
public void AbleToAddAndDeleteObjectListItemThenApplyViaPatch() { var testObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(testObj); PopulateStringListOnTestClass(testObj); var updatedTestObj = GetSimpleTestObject(); PopulateObjectListOnTestClass(updatedTestObj); PopulateStringListOnTestClass(updatedTestObj); updatedTestObj.ListOfStringProperty.RemoveAt(1); updatedTestObj.ListOfStringProperty.Remove("list"); updatedTestObj.ListOfObjectProperty.RemoveAt(1); updatedTestObj.ListOfObjectProperty.Add(new TestClass { StringProperty = "added" }); var delta = ObjectDelta.GenerateDelta(testObj, updatedTestObj); var objToUpdate = GetSimpleTestObject(); PopulateObjectListOnTestClass(objToUpdate); PopulateStringListOnTestClass(objToUpdate); var updatedObj = ObjectDelta.PatchObject(objToUpdate, delta); updatedTestObj.ListOfObjectProperty.Count.ShouldEqual(updatedObj.ListOfObjectProperty.Count); updatedTestObj.ListOfStringProperty.Count.ShouldEqual(updatedObj.ListOfStringProperty.Count); updatedObj.ListOfStringProperty.Skip(1).First().ShouldEqual("a"); updatedObj.ListOfStringProperty.ShouldNotContain("list"); var addedListItem = updatedObj.ListOfObjectProperty.SingleOrDefault(obj => obj != null && obj.StringProperty == "added"); addedListItem.ShouldNotBeNull(); }
public void UpdateObjectDeltaReturnsTrueIfRecordUpdated() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockSqlDialect = new Mock<ISqlDialect>(); mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery("")); var mockCommand = new Mock<IDbCommand>(); mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1); var mockConnection = new Mock<IDbConnection>(); mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object); var mockDbDriver = new Mock<IDbDriver>(); mockDbDriver.Setup(x => x.CreateConnection()).Returns(new MockDbConnectionWrapper(mockConnection.Object)); var session = new AsyncSession( ConnectionScope.PerTransaction, mockSqlDialect.Object, mockDbDriver.Object, new IDeleteListener[0], new IInsertListener[0], new IUpdateListener[0]); Assert.True(session.UpdateAsync(objectDelta).Result); mockDbDriver.VerifyAll(); mockCommand.VerifyAll(); }
public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); var session = new Session( ConnectionScope.PerTransaction, new Mock<ISqlDialect>().Object, new Mock<IDbDriver>().Object, new SessionListeners()); var exception = Assert.Throws<MicroLiteException>(() => session.Update(objectDelta)); Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.Message); }
/// <summary> /// Creates an SqlQuery to perform an update based upon the values in the object delta. /// </summary> /// <param name="objectDelta">The object delta to create the query for.</param> /// <returns> /// The created <see cref="SqlQuery" />. /// </returns> public SqlQuery BuildUpdateSqlQuery(ObjectDelta objectDelta) { if (objectDelta == null) { throw new ArgumentNullException("objectDelta"); } if (log.IsDebug) { log.Debug(LogMessages.SqlDialect_CreatingSqlQuery, "UPDATE"); } var objectInfo = ObjectInfo.For(objectDelta.ForType); var builder = new UpdateSqlBuilder(this.SqlCharacters) .Table(objectInfo); foreach (var change in objectDelta.Changes) { builder.SetColumnValue(change.Key, change.Value); } var sqlQuery = builder .Where(objectInfo.TableInfo.IdentifierColumn.ColumnName).IsEqualTo(objectDelta.Identifier) .ToSqlQuery(); return sqlQuery; }
public WhenConstructed() { this.objectDelta = new ObjectDelta(typeof(Customer), 1332); }
public Task <bool> UpdateAsync(ObjectDelta objectDelta) { return(this.UpdateAsync(objectDelta, CancellationToken.None)); }
public void UpdateObjectDeltaThrowsMicroLiteExceptionIfItContainsNoChanges() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); var session = new AsyncSession( ConnectionScope.PerTransaction, new Mock<ISqlDialect>().Object, new Mock<IDbDriver>().Object, new IDeleteListener[0], new IInsertListener[0], new IUpdateListener[0]); var exception = Assert.Throws<AggregateException>( () => session.UpdateAsync(objectDelta).Result); Assert.IsType<MicroLiteException>(exception.InnerException); Assert.Equal(ExceptionMessages.ObjectDelta_MustContainAtLeastOneChange, exception.InnerException.Message); }
public void UpdateObjectDeltaReturnsFalseIfNoRecordUpdated() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockSqlDialect = new Mock<ISqlDialect>(); mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery("")); var mockCommand = new Mock<IDbCommand>(); mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(0); var mockConnection = new Mock<IDbConnection>(); mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object); var mockDbDriver = new Mock<IDbDriver>(); mockDbDriver.Setup(x => x.CreateConnection()).Returns(mockConnection.Object); var session = new Session( ConnectionScope.PerTransaction, mockSqlDialect.Object, mockDbDriver.Object, new SessionListeners()); Assert.False(session.Update(objectDelta)); mockDbDriver.VerifyAll(); mockCommand.VerifyAll(); }
/// <summary> /// Creates an SqlQuery to perform an update based upon the values in the object delta. /// </summary> /// <param name="objectDelta">The object delta to create the query for.</param> /// <returns> /// The created <see cref="SqlQuery" />. /// </returns> public SqlQuery BuildUpdateSqlQuery(ObjectDelta objectDelta) { if (objectDelta == null) { throw new ArgumentNullException("objectDelta"); } var objectInfo = ObjectInfo.For(objectDelta.ForType); var builder = new UpdateSqlBuilder(this.SqlCharacters) .Table(objectInfo); foreach (var change in objectDelta.Changes) { builder.SetColumnValue(change.Key, change.Value); } var sqlQuery = builder .WhereEquals(objectInfo.TableInfo.IdentifierColumn.ColumnName, objectDelta.Identifier) .ToSqlQuery(); return sqlQuery; }
public void UpdateObjectDeltaReturnsTrueIfRecordUpdated() { var objectDelta = new ObjectDelta(typeof(Customer), 1234); objectDelta.AddChange("Name", "Fred Flintstone"); var mockCommand = new Mock<IDbCommand>(); mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1); mockCommand.As<IDisposable>().Setup(x => x.Dispose()); var mockSqlDialect = new Mock<ISqlDialect>(); mockSqlDialect.Setup(x => x.BuildUpdateSqlQuery(objectDelta)).Returns(new SqlQuery("")); var mockDbDriver = new Mock<IDbDriver>(); mockDbDriver.Setup(x => x.GetConnection(ConnectionScope.PerTransaction)).Returns(new Mock<IDbConnection>().Object); mockDbDriver.Setup(x => x.BuildCommand(It.IsNotNull<SqlQuery>())).Returns(mockCommand.Object); var session = new Session( ConnectionScope.PerTransaction, mockSqlDialect.Object, mockDbDriver.Object, new IListener[0]); Assert.True(session.Update(objectDelta)); mockDbDriver.VerifyAll(); mockCommand.VerifyAll(); }