public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

/* This insert should trigger 1 row affected */
INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );
";
            var          databaseCommand = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                           .SetCommandText(sql);

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

            // 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.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                  .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.SqlClient.SqlException>(action);
            Assert.IsTrue(wasUnhandledExceptionEventHandlerCalled);
        }
Exemplo n.º 4
0
        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.ExecuteToDynamicObjectAsync()
            .Wait();     // Block until the task completes.

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Exemplo n.º 5
0
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

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

            // Act
            var superHeroes = databaseCommand.ExecuteToList <SuperHero>();

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Exemplo n.º 6
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 superHero = Sequelocity.GetDatabaseCommandForSQLite(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString)
                            .SetCommandText(sql)
                            .ExecuteToDynamicObject();

            // Assert
            Assert.NotNull(superHero);
            Assert.That(superHero.SuperHeroId == 1);
            Assert.That(superHero.SuperHeroName == "Superman");
        }
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null primary key,
    SuperHeroName	VARCHAR(120)    NOT NULL
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

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

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

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

            // Cleanup
            databaseCommand.Dispose();
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public void Should_Be_Able_To_Specify_The_Table_Name()
        {
            // Arrange
            const string createSchemaSql = @"
DROP TABLE IF EXISTS Person;

CREATE TABLE IF NOT EXISTS Person
(
    CustomerId      INT             NOT NULL    AUTO_INCREMENT,
    FirstName       NVARCHAR(120)   NOT NULL,
    LastName        NVARCHAR(120)   NOT NULL,
    DateOfBirth     DATETIME        NOT NULL,
    PRIMARY KEY ( CustomerId )
);
";

            Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
            .SetCommandText(createSchemaSql)
            .ExecuteNonQuery();

            var customer = new Customer {
                FirstName = "Clark", LastName = "Kent", DateOfBirth = DateTime.Parse("06/18/1938")
            };

            // Act
            var customerId = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
                             .GenerateInsertForMySql(customer, "Person") // Specifying a table name of Person
                             .ExecuteScalar <int>();

            // Assert
            Assert.That(customerId == 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");
        }
Exemplo n.º 11
0
        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 #SuperHero
(
	SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
	SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
		SuperHeroName
FROM    #SuperHero;
";

            // Act
            var superHeroesTask = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                  .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");
        }
Exemplo n.º 12
0
        public void Should_Return_A_Type_Of_T()
        {
            // Arrange
            const string sql = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

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

            // Assert
            Assert.NotNull(superHero);
            Assert.That(superHero.SuperHeroId == 1);
            Assert.That(superHero.SuperHeroName == "Superman");
        }
        public void Should_Throw_An_Exception_When_Passing_An_Anonymous_Object_And_Not_Specifying_A_TableName()
        {
            // Arrange
            const string sql          = @"
CREATE TABLE IF NOT EXISTS Person
(
    CustomerId      INTEGER         NOT NULL    PRIMARY KEY     AUTOINCREMENT,
    FirstName       NVARCHAR(120)   NOT NULL,
    LastName        NVARCHAR(120)   NOT NULL,
    DateOfBirth     DATETIME        NOT NULL
);";
            var          dbConnection = Sequelocity.CreateDbConnection(ConnectionStringsNames.SqliteInMemoryDatabaseConnectionString);

            new DatabaseCommand(dbConnection)
            .SetCommandText(sql)
            .ExecuteNonQuery(true);

            var customer1 = new { FirstName = "Clark", LastName = "Kent", DateOfBirth = DateTime.Parse("06/18/1938") };
            var customer2 = new { FirstName = "Bruce", LastName = "Wayne", DateOfBirth = DateTime.Parse("05/27/1939") };
            var customer3 = new { FirstName = "Peter", LastName = "Parker", DateOfBirth = DateTime.Parse("08/18/1962") };
            var list      = new List <object> {
                customer1, customer2, customer3
            };

            // Act
            TestDelegate action = () => new DatabaseCommand(dbConnection)
                                  .GenerateInsertsForSQLite(list)
                                  .ExecuteScalar(true)
                                  .ToInt();

            // Assert
            var exception = Assert.Catch <ArgumentNullException>(action);

            Assert.That(exception.Message.Contains("The 'tableName' parameter must be provided when the object supplied is an anonymous type."));
        }
Exemplo n.º 14
0
        public void Should_Return_A_DataSet()
        {
            // Arrange
            const string sql = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";

            // Act
            var dataSet = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                          .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");
        }
Exemplo n.º 15
0
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                           .SetCommandText(sql);

            // Act
            var superHeroes = databaseCommand.ExecuteToDynamicList(true);

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

            // Cleanup
            databaseCommand.Dispose();
        }
Exemplo n.º 16
0
        public void Should_Throw_An_Exception_When_Passing_An_Anonymous_Object_And_Not_Specifying_A_TableName()
        {
            // Arrange
            const string createSchemaSql = @"
DROP TABLE IF EXISTS Customer;

CREATE TABLE IF NOT EXISTS Customer
(
    CustomerId      INT             NOT NULL    AUTO_INCREMENT,
    FirstName       NVARCHAR(120)   NOT NULL,
    LastName        NVARCHAR(120)   NOT NULL,
    DateOfBirth     DATETIME        NOT NULL,
    PRIMARY KEY ( CustomerId )
);
";

            Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
            .SetCommandText(createSchemaSql)
            .ExecuteNonQuery();

            var customer = new { FirstName = "Clark", LastName = "Kent", DateOfBirth = DateTime.Parse("06/18/1938") };

            // Act
            TestDelegate action = () => Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
                                  .GenerateInsertForMySql(customer)
                                  .ExecuteScalar <int>();

            // Assert
            var exception = Assert.Catch <ArgumentNullException>(action);

            Assert.That(exception.Message.Contains("The 'tableName' parameter must be provided when the object supplied is an anonymous type."));
        }
Exemplo n.º 17
0
        public void Should_Null_The_DbCommand_If_Iteration_Ends_Before_Full_Enumeration()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                           .SetCommandText(sql);

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

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Exemplo n.º 18
0
        public void Should_Return_The_Last_Inserted_Id()
        {
            // Arrange
            const string createSchemaSql = @"
DROP TABLE IF EXISTS Customer;

CREATE TABLE IF NOT EXISTS Customer
(
    CustomerId      INT             NOT NULL    AUTO_INCREMENT,
    FirstName       NVARCHAR(120)   NOT NULL,
    LastName        NVARCHAR(120)   NOT NULL,
    DateOfBirth     DATETIME        NOT NULL,
    PRIMARY KEY ( CustomerId )
);
";

            Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
            .SetCommandText(createSchemaSql)
            .ExecuteNonQuery();

            var customer = new Customer {
                FirstName = "Clark", LastName = "Kent", DateOfBirth = DateTime.Parse("06/18/1938")
            };

            // Act
            var customerId = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
                             .GenerateInsertForMySql(customer)
                             .ExecuteScalar()
                             .ToInt();

            // Assert
            Assert.That(customerId == 1);
        }
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null primary key,
    SuperHeroName	VARCHAR(120)    NOT NULL
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

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

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

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Exemplo n.º 20
0
        public void Should_Return_A_Task_Resulting_In_The_Number_Of_Affected_Rows()
        {
            // Arrange
            const string sql = @"
DROP TEMPORARY TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     INT             NOT NULL    AUTO_INCREMENT,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName ) VALUES ( 'Superman' ); /* This insert should trigger 1 row affected */
";

            // Act
            var rowsAffectedTask = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.MySqlConnectionString)
                                   .SetCommandText(sql)
                                   .ExecuteNonQueryAsync();

            // Assert
            Assert.IsInstanceOf <Task <int> >(rowsAffectedTask);
            Assert.That(rowsAffectedTask.Result == 1);
        }
Exemplo n.º 21
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");
        }
Exemplo n.º 22
0
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
DROP TEMPORARY TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     INT             NOT NULL    AUTO_INCREMENT,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName ) VALUES ( 'Superman' ); /* This insert should trigger 1 row affected */
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForMySql(ConnectionStringsNames.MySqlConnectionString)
                                           .SetCommandText(sql);

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

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
Exemplo n.º 23
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' );

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

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

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

            // Cleanup
            databaseCommand.Dispose();
        }
Exemplo n.º 24
0
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
DROP TEMPORARY TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     INT             NOT NULL    AUTO_INCREMENT,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName ) VALUES ( 'Superman' ); /* This insert should trigger 1 row affected */
";
            var          databaseCommand = Sequelocity.GetDatabaseCommandForMySql(ConnectionStringsNames.MySqlConnectionString)
                                           .SetCommandText(sql);

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

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

            // Cleanup
            databaseCommand.Dispose();
        }
Exemplo n.º 25
0
        public void Should_Keep_The_Database_Connection_Open_If_keepConnectionOpen_Parameter_Was_True()
        {
            // Arrange
            const string sql             = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null,
    SuperHeroName	VARCHAR(120)    NOT NULL,
    PRIMARY KEY ( SuperHeroId )
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

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

            // Act
            var superHeroes = databaseCommand.ExecuteToList <SuperHero>(true);

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

            // Cleanup
            databaseCommand.Dispose();
        }
Exemplo n.º 26
0
        public void Should_Map_The_Results_Back_To_A_List_Of_Dynamic()
        {
            // Arrange
            const string sql = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";

            // Act
            var superHeroes = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                              .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_Call_The_DatabaseCommandPostExecuteEventHandler()
        {
            // Arrange
            bool wasPostExecuteEventHandlerCalled = false;

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

            // Act
            Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
            .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);
        }
Exemplo n.º 28
0
        public void Should_Null_The_DbCommand_By_Default()
        {
            // Arrange
            const string sql             = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";
            var          databaseCommand = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                           .SetCommandText(sql);

            // Act
            var superHeroes = databaseCommand.ExecuteToDynamicList();

            // Assert
            Assert.IsNull(databaseCommand.DbCommand);
        }
        public void Should_Return_A_Task_Resulting_In_A_Map_Of_The_First_Result_To_A_Dynamic_Object()
        {
            // Arrange
            const string sql = @"
DROP TABLE IF EXISTS SuperHero;

CREATE TEMPORARY TABLE SuperHero
(
    SuperHeroId     serial not null primary key,
    SuperHeroName	VARCHAR(120)    NOT NULL
);

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    SuperHero;
";

            // Act
            var superHeroTask = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.PostgreSQLConnectionString)
                                .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");
        }
        public void Should_Return_A_Task_Resulting_In_A_Map_Of_The_First_Result_To_A_Dynamic_Object()
        {
            // Arrange
            const string sql = @"
CREATE TABLE #SuperHero
(
    SuperHeroId     INT             NOT NULL    IDENTITY(1,1)   PRIMARY KEY,
    SuperHeroName	NVARCHAR(120)   NOT NULL
);

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Superman' );

INSERT INTO #SuperHero ( SuperHeroName )
VALUES ( 'Batman' );

SELECT  SuperHeroId,
        SuperHeroName
FROM    #SuperHero;
";

            // Act
            var superHeroTask = Sequelocity.GetDatabaseCommand(ConnectionStringsNames.SqlServerConnectionString)
                                .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");
        }