public void SQLiteUnit_SeveralCommandsInUnit_ReturnTrue()
        {
            // Arrange
            var unit = new SQLiteUnit(this.PathToDataBase);

            for (int i = 0; i < this.Names.Length; i++)
            {
                string command =
                    $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                    + $"VALUES ({i}, '{this.Names[i]}', '{this.LastNames[i]}')";
                unit.AddSqliteCommand(command, string.Empty);
            }

            // Act
            unit.Commit();

            // Assert
            var personsInDatabase = this.GetInfOfDataBase();

            for (int i = 0; i < this.Names.Length; i++)
            {
                var firstNameInDb = personsInDatabase[i][0];
                var lastNameInDb  = personsInDatabase[i][1];
                Assert.AreEqual(this.Names[i], firstNameInDb);
                Assert.AreEqual(this.LastNames[i], lastNameInDb);
            }
        }
        public void SQLiteUnit_RunAndRollbackUnitWithOneCommandBadCommand_ReturnTrue()
        {
            // Arrange
            var unit = new SQLiteUnit(this.PathToDataBase);

            for (int i = 0; i < this.Names.Length; i++)
            {
                int    id      = i + 1;
                string command =
                    $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                    + $"VALUES ({id}, '{this.Names[i]}', '{this.LastNames[i]}')";
                string rollbackComand = $"DELETE FROM {this.DbTableName} WHERE {DbFieldId} = {id}";

                if (i == 2)
                {
                    string badField = "String instead of integer value";
                    command =
                        $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                        + $"VALUES ({badField}, '{this.FirstName1}', '{this.LastName1}')";
                }

                unit.AddSqliteCommand(command, rollbackComand);
            }

            // Assert
            Assert.Throws <CommitException>(() => unit.Commit());
            var personsInDatabase = this.GetInfOfDataBase();

            Assert.IsTrue(!personsInDatabase.Any());
        }
Esempio n. 3
0
        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 SQLiteUnit_BadCommandInUnitAndThrowException_ReturnTrue()
        {
            // Arrange
            var    unit    = new SQLiteUnit(this.PathToDataBase);
            string command =
                $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}, FAKE_COLUMN) "
                + $"VALUES (1, '{this.FirstName1}', '{this.LastName1}', 'FAKE FIELD')";

            unit.AddSqliteCommand(command, string.Empty);

            // Assert
            Assert.Throws <CommitException>(() => unit.Commit());
        }
Esempio n. 5
0
        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 SQLiteUnit_BadFieldInCommandAndThrowException_ReturnTrue()
        {
            // Arrange
            var    unit     = new SQLiteUnit(this.PathToDataBase);
            string badField = "String instead of integer value";
            string command  =
                $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                + $"VALUES ({badField}, '{this.FirstName1}', '{this.LastName1}')";

            unit.AddSqliteCommand(command, string.Empty);

            // Assert
            Assert.Throws <CommitException>(() => unit.Commit());
        }
        public void SQLiteUnit_RollbackUnitWithOneCommand_ReturnTrue()
        {
            // Arrange
            var    unit    = new SQLiteUnit(this.PathToDataBase);
            string command =
                $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                + $"VALUES (1, '{this.FirstName1}', '{this.LastName1}')";
            string rollbackCommand = $"DELETE FROM {this.DbTableName} WHERE {DbFieldId} = 1";

            unit.AddSqliteCommand(command, rollbackCommand);

            // Act
            unit.Commit();
            unit.Rollback();

            // Assert
            var personsInDatabase = this.GetInfOfDataBase();

            Assert.IsTrue(!personsInDatabase.Any());
        }
        public void SQLiteUnit_OneCommandInUnit_ReturnTrue()
        {
            // Arrange
            var    unit    = new SQLiteUnit(this.PathToDataBase);
            string command =
                $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                + $"VALUES (1, '{this.FirstName1}', '{this.LastName1}')";

            unit.AddSqliteCommand(command, string.Empty);

            // Act
            unit.Commit();

            // Assert
            var personsInDatabase = this.GetInfOfDataBase();
            var firstNameInDb     = personsInDatabase[0][0];
            var lastNameInDb      = personsInDatabase[0][1];

            Assert.AreEqual(this.FirstName1, firstNameInDb);
            Assert.AreEqual(this.LastName1, lastNameInDb);
        }
        public void SQLiteUnit_RollbackUnitWithSeveralCommands_ReturnTrue()
        {
            // Arrange
            var unit = new SQLiteUnit(this.PathToDataBase);

            for (int i = 0; i < this.Names.Length; i++)
            {
                int    id      = i + 1;
                string command =
                    $"INSERT INTO {this.DbTableName}({this.DbFieldId}, {this.DbFieldFirstName}, {this.DbFieldLastName}) "
                    + $"VALUES ({id}, '{Names[i]}', '{this.LastNames[i]}')";
                string rollbackComand = $"DELETE FROM {this.DbTableName} WHERE {DbFieldId} = {id}";
                unit.AddSqliteCommand(command, rollbackComand);
            }

            // Act
            unit.Commit();
            unit.Rollback();

            // Assert
            var personsInDatabase = this.GetInfOfDataBase();

            Assert.IsTrue(!personsInDatabase.Any());
        }
Esempio n. 10
0
        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();
        }