public async Task Can_open_the_underlying_connection(bool async)
        {
            var dbConnection = new FakeDbConnection("A=B");
            var context      = RelationalTestHelpers.Instance.CreateContext();

            ((FakeRelationalConnection)context.GetService <IRelationalConnection>()).UseConnection(dbConnection);

            if (async)
            {
                await context.Database.OpenConnectionAsync();

                Assert.Equal(1, dbConnection.OpenAsyncCount);
            }
            else
            {
                context.Database.OpenConnection();
                Assert.Equal(1, dbConnection.OpenCount);
            }
        }
Example #2
0
        public void Existing_connection_can_start_in_opened_state()
        {
            var dbConnection = new FakeDbConnection(
                "Database=FrodoLives",
                state: ConnectionState.Open);

            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection))))
            {
                Assert.Equal(0, connection.DbConnections.Count);

                connection.Open();

                Assert.Equal(0, connection.DbConnections.Count);

                Assert.Equal(0, dbConnection.OpenCount);

                connection.Open();
                connection.Open();

                Assert.Equal(0, dbConnection.OpenCount);

                connection.Close();
                connection.Close();

                Assert.Equal(0, dbConnection.OpenCount);
                Assert.Equal(0, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(0, dbConnection.OpenCount);
                Assert.Equal(0, dbConnection.CloseCount);

                connection.Open();

                Assert.Equal(0, dbConnection.OpenCount);

                connection.Close();

                Assert.Equal(0, dbConnection.OpenCount);
                Assert.Equal(0, dbConnection.CloseCount);
            }
        }
Example #3
0
        public void Can_use_existing_transaction()
        {
            var dbConnection = new FakeDbConnection("Database=FrodoLives");

            var dbTransaction = dbConnection.BeginTransaction(IsolationLevel.Unspecified);

            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection))))
            {
                Assert.Null(connection.CurrentTransaction);

                using (connection.UseTransaction(dbTransaction))
                {
                    Assert.Equal(dbTransaction, connection.CurrentTransaction.GetDbTransaction());
                }

                Assert.Null(connection.CurrentTransaction);
            }
        }
        public void Existing_connection_is_opened_and_closed_when_necessary()
        {
            var dbConnection = new FakeDbConnection("Database=FrodoLives");

            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection))))
            {
                Assert.Equal(0, connection.DbConnections.Count);

                connection.Open();

                Assert.Equal(0, connection.DbConnections.Count);

                Assert.Equal(1, dbConnection.OpenCount);

                connection.Open();
                connection.Open();

                Assert.Equal(1, dbConnection.OpenCount);

                connection.Close();
                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(0, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                connection.Open();

                Assert.Equal(2, dbConnection.OpenCount);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);
            }
        }
Example #5
0
        public void Existing_connection_can_be_changed_and_reset()
        {
            var dbConnection = new FakeDbConnection("Database=FrodoLives");

            using var connection = new FakeRelationalConnection(
                      CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection)));

            Assert.Equal(0, connection.DbConnections.Count);

            connection.DbConnection = null;
            Assert.Null(connection.ConnectionString);

            dbConnection            = new FakeDbConnection("Database=SamLives");
            connection.DbConnection = dbConnection;

            Assert.Equal("Database=SamLives", connection.ConnectionString);

            Assert.Equal(0, connection.DbConnections.Count);
            Assert.Same(dbConnection, connection.DbConnection);
            Assert.Equal(0, connection.DbConnections.Count);
            Assert.Equal("Database=SamLives", connection.ConnectionString);

            connection.DbConnection = null;

            Assert.Equal(0, connection.DbConnections.Count);
            Assert.Null(connection.ConnectionString);

            connection.ConnectionString = "Database=MerryLives";

            dbConnection            = new FakeDbConnection("Database=MerryLives");
            connection.DbConnection = dbConnection;

            Assert.Equal(0, connection.DbConnections.Count);
            Assert.Same(dbConnection, connection.DbConnection);
            Assert.Equal(0, connection.DbConnections.Count);
            Assert.Equal("Database=MerryLives", connection.ConnectionString);
        }
        public void Existing_connection_can_be_opened_and_closed_externally()
        {
            var dbConnection = new FakeDbConnection(
                "Database=FrodoLives",
                state: ConnectionState.Closed);

            using (var connection = new FakeRelationalConnection(
                       CreateOptions(new FakeRelationalOptionsExtension().WithConnection(dbConnection))))
            {
                Assert.Equal(0, connection.DbConnections.Count);

                connection.Open();

                Assert.Equal(0, connection.DbConnections.Count);

                Assert.Equal(1, dbConnection.OpenCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Open);

                connection.Open();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(1, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Open();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(1, dbConnection.CloseCount);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Open);

                connection.Open();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Close();

                Assert.Equal(2, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                connection.Open();
                connection.Open();

                Assert.Equal(3, dbConnection.OpenCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Open();

                Assert.Equal(4, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);

                dbConnection.SetState(ConnectionState.Closed);

                connection.Close();

                Assert.Equal(4, dbConnection.OpenCount);
                Assert.Equal(2, dbConnection.CloseCount);
            }
        }