public void Should_Call_The_DatabaseCommandPostExecuteEventHandler()
        {
            // Arrange
            bool wasPostExecuteEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandPostExecuteEventHandlers.Add(command => wasPostExecuteEventHandlerCalled = true);

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText("SELECT 1 as SuperHeroId, 'Superman' as SuperHeroName")
            .ExecuteToMapAsync(record =>
            {
                var obj = new SuperHero
                {
                    SuperHeroId   = record.GetValue(0).ToLong(),
                    SuperHeroName = record.GetValue(1).ToString()
                };

                return(obj);
            })
            .Wait();     // Block until the task completes.

            // Assert
            Assert.IsTrue(wasPostExecuteEventHandlerCalled);
        }
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand.ExecuteToObject <SuperHero>(true);

            // Assert
            Assert.That(databaseCommand.DbCommand.Connection.State == ConnectionState.Open);

            // Cleanup
            databaseCommand.Dispose();
        }
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand.ExecuteToObject <SuperHero>();

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
        public void Should_Return_A_DataSet()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var dataSet = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                          .SetCommandText(sql)
                          .ExecuteToDataSet();

            // Assert
            Assert.That(dataSet.Tables[0].Rows.Count == 2);
            Assert.That(dataSet.Tables[0].Rows[0][0].ToString() == "1");
            Assert.That(dataSet.Tables[0].Rows[0][1].ToString() == "Superman");
            Assert.That(dataSet.Tables[0].Rows[1][0].ToString() == "2");
            Assert.That(dataSet.Tables[0].Rows[1][1].ToString() == "Batman");
        }
Beispiel #5
0
        public void Should_Call_The_DataRecordCall_Func_For_Each_Record_In_The_Result_Set()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            List <object> list;

            // Act
            list = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                   .SetCommandText(sql)
                   .ExecuteReader <object>(record => new
            {
                SuperHeroId   = record.GetValue(0),
                SuperHeroName = record.GetValue(1)
            })
                   .ToList();


            // Assert
            Assert.That(list.Count == 2);
        }
Beispiel #6
0
        public void Should_Return_A_Task_Resulting_In_A_Map_Of_The_First_Result_To_A_Dynamic_Object()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroTask = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                .SetCommandText(sql)
                                .ExecuteToDynamicObjectAsync();

            // Assert
            Assert.IsInstanceOf <Task <dynamic> >(superHeroTask);
            Assert.NotNull(superHeroTask.Result);
            Assert.That(superHeroTask.Result.SuperHeroId == 1);
            Assert.That(superHeroTask.Result.SuperHeroName == "Superman");
        }
Beispiel #7
0
        public void Should_Null_The_DbCommand_If_Iteration_Ends_Before_Full_Enumeration()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand
            .ExecuteReader(record => new
            {
                SuperHeroId   = record.GetValue(0),
                SuperHeroName = record.GetValue(1)
            })
            .First();

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
        public void Should_Return_A_Type_Of_T()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHero = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                            .SetCommandText(sql)
                            .ExecuteToObject <SuperHero>();

            // Assert
            Assert.NotNull(superHero);
            Assert.That(superHero.SuperHeroId == 1);
            Assert.That(superHero.SuperHeroName == "Superman");
        }
Beispiel #9
0
        public void Should_Map_The_Results_Back_To_A_List_Of_Dynamic()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroes = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                              .SetCommandText(sql)
                              .ExecuteToDynamicList();

            // Assert
            Assert.That(superHeroes.Count == 2);
            Assert.That(superHeroes[0].SuperHeroId == 1);
            Assert.That(superHeroes[0].SuperHeroName == "Superman");
            Assert.That(superHeroes[1].SuperHeroId == 2);
            Assert.That(superHeroes[1].SuperHeroName == "Batman");
        }
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand.ExecuteToListAsync <SuperHero>()
            .Wait();     // Block until the task completes.

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Beispiel #11
0
        public void Should_Return_The_First_Column_Of_The_First_Row_In_The_Result_Set()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroId = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                              .SetCommandText(sql)
                              .ExecuteScalar()
                              .ToLong();

            // Assert
            Assert.That(superHeroId == 1);
        }
        public void Should_Return_A_Task_Resulting_In_A_Map_Of_The_Results_To_A_List_Of_Type_T()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroesTask = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                  .SetCommandText(sql)
                                  .ExecuteToListAsync <SuperHero>();

            // Assert
            Assert.IsInstanceOf <Task <List <SuperHero> > >(superHeroesTask);
            Assert.That(superHeroesTask.Result.Count == 2);
            Assert.That(superHeroesTask.Result[0].SuperHeroId == 1);
            Assert.That(superHeroesTask.Result[0].SuperHeroName == "Superman");
            Assert.That(superHeroesTask.Result[1].SuperHeroId == 2);
            Assert.That(superHeroesTask.Result[1].SuperHeroName == "Batman");
        }
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand.ExecuteToDynamicListAsync(true)
            .Wait();     // Block until the task completes.

            // Assert
            Assert.That(databaseCommand.DbCommand.Connection.State == ConnectionState.Open);

            // Cleanup
            databaseCommand.Dispose();
        }
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' ); /* This insert should trigger 1 row affected */
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            var rowsAffected = databaseCommand.ExecuteNonQuery(true);

            // Assert
            Assert.That(databaseCommand.DbCommand.Connection.State == ConnectionState.Open);

            // Cleanup
            databaseCommand.Dispose();
        }
        public void Should_Call_The_DatabaseCommandUnhandledExceptionEventHandler()
        {
            // Arrange
            bool wasUnhandledExceptionEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandUnhandledExceptionEventHandlers.Add((exception, command) =>
            {
                wasUnhandledExceptionEventHandlerCalled = true;
            });

            // Act
            TestDelegate action = async() => await Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                  .SetCommandText("asdf;lkj")
                                  .ExecuteToMapAsync(record =>
            {
                var obj = new SuperHero
                {
                    SuperHeroId   = record.GetValue(0).ToLong(),
                    SuperHeroName = record.GetValue(1).ToString()
                };

                return(obj);
            });

            // Assert
            Assert.Throws <System.Data.SQLite.SQLiteException>(action);
            Assert.IsTrue(wasUnhandledExceptionEventHandlerCalled);
        }
Beispiel #16
0
        public void Should_Null_The_DbCommand_On_Dispose()
        {
            // Arrange
            DatabaseCommand databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString);

            // Act
            databaseCommand.Dispose();

            // Assert
            Assert.Null(databaseCommand.DbCommand);
        }
Beispiel #17
0
        public void Should_Null_The_DbCommand_On_Dispose_In_A_Using_Statement()
        {
            // Arrange
            DatabaseCommand databaseCommand;

            // Act
            using (databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString))
            {
            }

            // Assert
            Assert.Null(databaseCommand.DbCommand);
        }
        public void Should_Call_The_DatabaseCommandPostExecuteEventHandler()
        {
            // Arrange
            bool wasPostExecuteEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandPostExecuteEventHandlers.Add(command => wasPostExecuteEventHandlerCalled = true);

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText("SELECT 1 as SuperHeroId, 'Superman' as SuperHeroName")
            .ExecuteToObject <SuperHero>();

            // Assert
            Assert.IsTrue(wasPostExecuteEventHandlerCalled);
        }
        public void Should_Call_The_DatabaseCommandPreExecuteEventHandler()
        {
            // Arrange
            bool wasPreExecuteEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandPreExecuteEventHandlers.Add(command => wasPreExecuteEventHandlerCalled = true);

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText("SELECT 1")
            .ExecuteNonQuery();

            // Assert
            Assert.IsTrue(wasPreExecuteEventHandlerCalled);
        }
Beispiel #20
0
        public void Should_Call_The_DatabaseCommandPostExecuteEventHandler()
        {
            // Arrange
            bool wasPostExecuteEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandPostExecuteEventHandlers.Add(command => wasPostExecuteEventHandlerCalled = true);

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText("SELECT 1")
            .ExecuteReaderAsync(record => new Object())
            .Wait();     // Block until the task completes.

            // Assert
            Assert.IsTrue(wasPostExecuteEventHandlerCalled);
        }
        public void Should_Call_The_DatabaseCommandPreExecuteEventHandler()
        {
            // Arrange
            bool wasPreExecuteEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandPreExecuteEventHandlers.Add(command => wasPreExecuteEventHandlerCalled = true);

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText("SELECT 1 as SuperHeroId, 'Superman' as SuperHeroName")
            .ExecuteToDynamicListAsync()
            .Wait();     // Block until the task completes.

            // Assert
            Assert.IsTrue(wasPreExecuteEventHandlerCalled);
        }
Beispiel #22
0
        public void Can_Get_A_DatabaseCommand_For_Sqlite()
        {
            // Arrange
            TestHelpers.ClearDefaultConfigurationSettings();

            string connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString].ConnectionString;

            // Act
            var databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(connectionString);

            // Assert
            Assert.NotNull(databaseCommand);
            Assert.That(databaseCommand.DbCommand.Connection.ToString() == "System.Data.SQLite.SQLiteConnection");

            // Reset
            TestHelpers.ClearDefaultConfigurationSettings();
        }
        public void Should_Call_The_DatabaseCommandUnhandledExceptionEventHandler()
        {
            // Arrange
            bool wasUnhandledExceptionEventHandlerCalled = false;

            Sequelocity.ConfigurationSettings.EventHandlers.DatabaseCommandUnhandledExceptionEventHandlers.Add((exception, command) =>
            {
                wasUnhandledExceptionEventHandlerCalled = true;
            });

            // Act
            TestDelegate action = () => Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                  .SetCommandText("asdf;lkj")
                                  .ExecuteToObject <SuperHero>();

            // Assert
            Assert.Throws <System.Data.SQLite.SQLiteException>(action);
            Assert.IsTrue(wasUnhandledExceptionEventHandlerCalled);
        }
Beispiel #24
0
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            var list = new List <object>();

            // Act
            databaseCommand.ExecuteReaderAsync(record =>
            {
                var obj = new
                {
                    SuperHeroId   = record.GetValue(0),
                    SuperHeroName = record.GetValue(1)
                };

                list.Add(obj);
            }, true)
            .Wait(); // Block until the task completes.

            // Assert
            Assert.That(databaseCommand.DbCommand.Connection.State == ConnectionState.Open);

            // Cleanup
            databaseCommand.Dispose();
        }
Beispiel #25
0
        public void Should_Null_The_DbCommand_If_Exception_Occurs_During_Iteration()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            var iter = databaseCommand.ExecuteReader(record => new
            {
                SuperHeroId   = record.GetValue(0),
                SuperHeroName = record.GetValue(1)
            });

            // Act
            try
            {
                foreach (var item in iter)
                {
                    throw new Exception("Exception occured during iteration.");
                }
            }
            catch { }

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Beispiel #26
0
        public void Should_Call_The_DataRecordCall_Action_For_Each_Record_In_The_Result_Set()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId, /* This should be the only value returned from ExecuteScalar */
        SuperHeroName
FROM    SuperHero;
";

            var list = new List <object>();

            // Act
            Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
            .SetCommandText(sql)
            .ExecuteReaderAsync(record =>
            {
                var obj = new
                {
                    SuperHeroId   = record.GetValue(0),
                    SuperHeroName = record.GetValue(1)
                };

                list.Add(obj);
            })
            .Wait();     // Block until the task completes.

            // Assert
            Assert.That(list.Count == 2);
        }
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' ); /* This insert should trigger 1 row affected */
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            databaseCommand.ExecuteNonQuery();

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                           .SetCommandText(sql);

            // Act
            var superHeroes = databaseCommand.ExecuteToMap(record =>
            {
                var obj = new SuperHero
                {
                    SuperHeroId   = record.GetValue(0).ToLong(),
                    SuperHeroName = record.GetValue(1).ToString()
                };

                return(obj);
            }, true);

            // Assert
            Assert.That(databaseCommand.DbCommand.Connection.State == ConnectionState.Open);

            // Cleanup
            databaseCommand.Dispose();
        }
        public void Should_Return_The_Number_Of_Affected_Rows()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' ); /* This insert should trigger 1 row affected */
";

            // Act
            var rowsAffected = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                               .SetCommandText(sql)
                               .ExecuteNonQuery();

            // Assert
            Assert.That(rowsAffected == 1);
        }
        public void Should_Call_The_DataRecordCall_Action_For_Each_Record_In_The_Result_Set()
        {
            // Arrange
            const string sql = @"
CREATE TABLE IF NOT EXISTS SuperHero
(
    SuperHeroId     INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    SuperHeroName	NVARCHAR(120)   NOT NULL,
    UNIQUE(SuperHeroName)
);

INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Superman' );
INSERT OR IGNORE INTO SuperHero VALUES ( NULL, 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroesTask = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                                  .SetCommandText(sql)
                                  .ExecuteToMapAsync(record =>
            {
                var obj = new SuperHero
                {
                    SuperHeroId   = record.GetValue(0).ToLong(),
                    SuperHeroName = record.GetValue(1).ToString()
                };

                return(obj);
            });

            // Assert
            Assert.IsInstanceOf <Task <List <SuperHero> > >(superHeroesTask);
            Assert.That(superHeroesTask.Result.Count == 2);
        }