示例#1
0
        public void InitializeBehavesAsExpected()
        {
            var initialized = false;

            _connection
                .SetupGet(m => m.Connected)
                .Returns(() => true);

            _connection
                .SetupGet(m => m.Initialized)
                .Returns(() => initialized);

            _connection
                .Setup(m => m.Initialize(AnyTableNameWillDo))
                .Callback(() => initialized = true);

            using (var sut = new BulkCopy())
            {
                sut.Initialize(AnyTableNameWillDo);
            }

            _connection.VerifyGet(m => m.Connected, Times.Once);
            _connection.VerifyGet(m => m.Initialized, Times.Once);
            _connection.Verify(m => m.Initialize(AnyTableNameWillDo), Times.Once);
        }
示例#2
0
        private BulkCopy GetInitializedSubjectUnderTest()
        {
            var connected = false;
            var initialized = false;

            _connection
                .SetupGet(m => m.Connected)
                .Returns(() => connected);

            _connection
                .SetupGet(m => m.Initialized)
                .Returns(() => initialized);

            _connection
                .Setup(m => m.Connect(AnyConnectionStringWillDo))
                .Callback(() => connected = true);

            _connection
                .Setup(m => m.Initialize(AnyTableNameWillDo))
                .Callback(() => initialized = true);

            var sut = new BulkCopy();

            sut.Connect(AnyConnectionStringWillDo);
            sut.Initialize(AnyTableNameWillDo);

            return sut;
        }
示例#3
0
        private static void VerifyRoundtripsAsExpected(OdbcDriver driver)
        {
            /*
            * Reasons this test might fail:
            * 1. You need to execute SqlBulkCopy.sql (in the same directory as this test) to create the test database.
            * 2. You need to update the connection string to specify the location of your instance of the test database.
            * 3. The particular driver is not installed on your PC.
            */

            const string connectionString = "Server=WORKSTATION\\SQL2012;Database=SqlBulkCopy;Trusted_Connection=yes;";
            const string tableName = "SqlBulkCopyTest";

            List<IBulkCopyBoundColumn> columns;
            var data = TestData.GetSampleData();

            BulkCopy.Driver = driver;
            BulkCopy.Trace = true;

            try
            {
                using (var sut = new BulkCopy())
                {
                    sut.LoginTimeout = 2;

                    try
                    {
                        sut.Connect(string.Concat(sut.DriverOptionString, ";", connectionString));

                    }
                    catch (Exception ex)
                    {
                        const string message = "Please refer to this test for possible solutions." +
                            "\r\n\r\nTesting: {0}." +
                            "\r\n\r\nODBC exception text: {1}.";

                        Assert.Fail(message, sut.DriverOptionString, ex.Message);
                    }

                    sut.ConnectionTimeout = 2;

                    sut.ExecuteDirect("TRUNCATE TABLE SqlBulkCopyTest;");

                    sut.Initialize(tableName);

                    var importer = new TestDataImporter(sut);

                    columns = sut.Columns
                        .Where(c => c.Index > 0)
                        .Cast<IBulkCopyBoundColumn>()
                        .ToList();

                    LoadData(importer, data);
                }
            }
            finally
            {
                BulkCopy.Trace = false;
            }

            List<object[]> rows;

            using (var cn = new SqlConnection(connectionString))
            {
                cn.Open();

                rows = FetchData(cn, columns.Count);
            }

            VerifyFetchedData(columns, data, rows);
        }