public void FileUnit_BadOperationBeforeGoodOperationsAndRollback_ReturnTrue() { // Arrange var unit = new FileUnit(); // Плохая операция unit.Copy(this.DeleteFilePath, this.CopybleFilePath, true); unit.CreateFile(this.CreateFilePath); unit.Copy(this.CopyFilePath, this.CopybleFilePath, true); unit.AppendAllText(this.AppendFilePath, this.AddedContent); unit.WriteAllText(this.WriteFilePath, this.AddedContent); unit.Delete(this.DeleteFilePath); unit.Move(this.MoveFilePath, this.MovebleFilePath); unit.Copy(this.DeleteFilePath, this.CopybleFilePath, true); // Assert Assert.Throws <CommitException>(() => unit.Commit()); Assert.IsFalse(File.Exists(this.CreateFilePath)); Assert.IsTrue(!File.Exists(this.CopybleFilePath) && File.Exists(this.CopyFilePath)); string textInAppendFile = File.ReadAllText(this.AppendFilePath); Assert.AreEqual(textInAppendFile, this.Content); string textInWriteFile = File.ReadAllText(this.WriteFilePath); Assert.AreEqual(textInWriteFile, this.Content); Assert.IsTrue(File.Exists(this.DeleteFilePath)); Assert.IsTrue(!File.Exists(this.MovebleFilePath) && File.Exists(this.MoveFilePath)); }
public void BadUnitBetweenGoodUnits_ReturnTrue() { // Arrange var sqliteTransactionFirst = new SQLiteUnit(this.PathToDataBase); string firstSqlCommand = $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) " + $"VALUES ({this.DbRowId}, '{this.FirstName}', '{this.LastName}')"; string firstSqlRollback = $"DELETE FROM {this.DbTableName} WHERE {this.DbFieldId} = {this.DbRowId}"; sqliteTransactionFirst.AddSqliteCommand( firstSqlCommand, firstSqlRollback); var fileTransaction = new FileUnit(); fileTransaction.CreateFile(this.CreateFilePath); fileTransaction.Copy(this.CopyFilePath, this.CopybleFilePath, true); fileTransaction.AppendAllText(this.AppendFilePath, this.AddedContent); fileTransaction.WriteAllText(this.WriteFilePath, this.AddedContent); fileTransaction.Delete(this.DeleteFilePath); fileTransaction.Move(this.MoveFilePath, this.MovebleFilePath); var badFileUnit = new FileUnit(); badFileUnit.Copy(this.CopybleFilePath, this.CopybleFilePath, true); // Act using (var bussinesTransaction = this.unit.BeginTransaction()) { bussinesTransaction.ExecuteUnit(sqliteTransactionFirst); bussinesTransaction.ExecuteUnit(badFileUnit); bussinesTransaction.ExecuteUnit(fileTransaction); bussinesTransaction.Commit(); } string firstNameInDb = string.Empty; string lastNameInDb = string.Empty; this.GetInfOfDataBase(out firstNameInDb, out lastNameInDb); // Assert Assert.IsFalse(File.Exists(this.CreateFilePath)); Assert.IsTrue(!File.Exists(this.CopybleFilePath) && File.Exists(this.CopyFilePath)); string textInAppendFile = File.ReadAllText(this.AppendFilePath); Assert.AreEqual(textInAppendFile, this.Content); string textInWriteFile = File.ReadAllText(this.WriteFilePath); Assert.AreEqual(textInWriteFile, this.Content); Assert.IsTrue(File.Exists(this.DeleteFilePath)); Assert.IsTrue(!File.Exists(this.MovebleFilePath) && File.Exists(this.MoveFilePath)); Assert.AreEqual(string.Empty, firstNameInDb); Assert.AreEqual(string.Empty, lastNameInDb); }
public void FileUnit_Delete_ReturnFalse() { // Arrange var unit = new FileUnit(); unit.Delete(this.DeleteFilePath); // Act unit.Commit(); // Assert Assert.IsFalse(File.Exists(this.DeleteFilePath)); }
public void RollbackAfterCrush_ReturnTrue() { // Arrange var sqliteTransactionFirst = new SQLiteUnit(this.PathToDataBase); string firstSqlCommand = $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) " + $"VALUES ({this.DbRowId}, '{this.FirstName}', '{this.LastName}')"; string firstSqlRollback = $"DELETE FROM {this.DbTableName} WHERE {this.DbFieldId} = {this.DbRowId}"; sqliteTransactionFirst.AddSqliteCommand( firstSqlCommand, firstSqlRollback); var fileTransaction = new FileUnit(); fileTransaction.CreateFile(this.CreateFilePath); fileTransaction.Copy(this.CopyFilePath, this.CopybleFilePath, true); fileTransaction.AppendAllText(this.AppendFilePath, this.AddedContent); fileTransaction.WriteAllText(this.WriteFilePath, this.AddedContent); fileTransaction.Delete(this.DeleteFilePath); fileTransaction.Move(this.MoveFilePath, this.MovebleFilePath); // Act var bussinesTransaction = this.unit.BeginTransaction(); bussinesTransaction.ExecuteUnit(sqliteTransactionFirst); bussinesTransaction.ExecuteUnit(fileTransaction); // Эмитируем падение программы и повторное создание объекта UnitOfWork // Так как журнал остался, будет выполнен роллбек. var newUnit = new UnitOfWork(new UnitJsonJournalManager()); string firstNameInDb = string.Empty; string lastNameInDb = string.Empty; this.GetInfOfDataBase(out firstNameInDb, out lastNameInDb); // Assert Assert.IsFalse(File.Exists(this.CreateFilePath)); Assert.IsTrue(!File.Exists(this.CopybleFilePath) && File.Exists(this.CopyFilePath)); string textInAppendFile = File.ReadAllText(this.AppendFilePath); Assert.AreEqual(textInAppendFile, this.Content); string textInWriteFile = File.ReadAllText(this.WriteFilePath); Assert.AreEqual(textInWriteFile, this.Content); Assert.IsTrue(File.Exists(this.DeleteFilePath)); Assert.IsTrue(!File.Exists(this.MovebleFilePath) && File.Exists(this.MoveFilePath)); Assert.AreEqual(string.Empty, firstNameInDb); Assert.AreEqual(string.Empty, lastNameInDb); }
public void AllFunctionallityPositiveTestWithUserConfig_ReturnTrue() { // Arrange unit.UseMyFolderForJournals(this.UserFolderForJournals); var sqliteTransactionFirst = new SQLiteUnit(this.PathToDataBase); string firstSqlCommand = $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) " + $"VALUES ({this.DbRowId}, '{this.FirstName}', '{this.LastName}')"; string firstSqlRollback = $"DELETE FROM {this.DbTableName} WHERE {this.DbFieldId} = {this.DbRowId}"; sqliteTransactionFirst.AddSqliteCommand( firstSqlCommand, firstSqlRollback); var fileTransaction = new FileUnit(); fileTransaction.CreateFile(this.CreateFilePath); fileTransaction.Copy(this.CopyFilePath, this.CopybleFilePath, true); fileTransaction.AppendAllText(this.AppendFilePath, this.AddedContent); fileTransaction.WriteAllText(this.WriteFilePath, this.AddedContent); fileTransaction.Delete(this.DeleteFilePath); fileTransaction.Move(this.MoveFilePath, this.MovebleFilePath); var sqliteTransactionSecond = new SQLiteUnit(this.PathToDataBase); string secondSqlCommand = $"UPDATE {this.DbTableName} set {this.DbFieldFirstName} = " + $"'{this.NewFirstName}' WHERE {this.DbFieldId} = {this.DbRowId}"; string secondSqlRollback = $"UPDATE {this.DbTableName} set {this.DbFieldFirstName} = " + $"'{this.LastName}' WHERE {this.DbFieldId} = {this.DbRowId}"; sqliteTransactionSecond.AddSqliteCommand( secondSqlCommand, secondSqlRollback); // Act var bussinesTransaction = this.unit.BeginTransaction(); bussinesTransaction.ExecuteUnit(sqliteTransactionFirst); bussinesTransaction.ExecuteUnit(sqliteTransactionFirst); bussinesTransaction.ExecuteUnit(fileTransaction); bussinesTransaction.ExecuteUnit(sqliteTransactionSecond); string firstNameInDb = string.Empty; string lastNameInDb = string.Empty; this.GetInfOfDataBase(out firstNameInDb, out lastNameInDb); // Assert Assert.IsTrue(File.Exists(this.CreateFilePath)); Assert.IsTrue(File.Exists(this.CopybleFilePath) && File.Exists(this.CopyFilePath)); string textInAppendFile = File.ReadAllText(this.AppendFilePath); Assert.AreEqual(textInAppendFile, this.Content + this.AddedContent); string textInWriteFile = File.ReadAllText(this.WriteFilePath); Assert.AreEqual(textInWriteFile, this.AddedContent); Assert.IsFalse(File.Exists(this.DeleteFilePath)); Assert.IsTrue(File.Exists(this.MovebleFilePath) && !File.Exists(this.MoveFilePath)); Assert.AreEqual(this.NewFirstName, firstNameInDb); Assert.AreEqual(this.LastName, lastNameInDb); Assert.IsTrue(Directory.EnumerateFiles(this.UserFolderForJournals).Any()); bussinesTransaction.Commit(); bussinesTransaction.Dispose(); unit.UseDefaultFolderForJournals(); }