Esempio n. 1
0
        public async Task TestSchemaLoaderCacheWithExistingConnectionAndImmediateLoadingAsync()
        {
            ISqlBulkHelpersConnectionProvider sqlConnectionProvider = SqlConnectionHelper.GetConnectionProvider();

            List <ISqlBulkHelpersDBSchemaLoader> schemaLoadersList = new List <ISqlBulkHelpersDBSchemaLoader>();

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
            {
                schemaLoadersList.Add(SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(conn));
            }

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
            {
                schemaLoadersList.Add(SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(conn));
            }

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
            {
                schemaLoadersList.Add(SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(conn));
            }

            Assert.IsNotNull(schemaLoadersList[0]);
            Assert.IsNotNull(schemaLoadersList[1]);
            Assert.IsNotNull(schemaLoadersList[2]);

            Assert.AreEqual(schemaLoadersList[0], schemaLoadersList[1]);
            Assert.AreEqual(schemaLoadersList[1], schemaLoadersList[2]);
            Assert.AreEqual(schemaLoadersList[0], schemaLoadersList[2]);

            //ALL should already be initialized since used existing connections to construct them!
            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[0]).IsInitialized);
            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[1]).IsInitialized);
            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[2]).IsInitialized);
        }
Esempio n. 2
0
        public void TestSchemaLoaderCacheWithLazyLoading()
        {
            ISqlBulkHelpersConnectionProvider sqlConnectionProvider = SqlConnectionHelper.GetConnectionProvider();

            List <ISqlBulkHelpersDBSchemaLoader> schemaLoadersList = new List <ISqlBulkHelpersDBSchemaLoader>
            {
                SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(sqlConnectionProvider),
                SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(sqlConnectionProvider),
                SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(sqlConnectionProvider)
            };

            Assert.IsNotNull(schemaLoadersList[0]);
            Assert.IsNotNull(schemaLoadersList[1]);
            Assert.IsNotNull(schemaLoadersList[2]);

            Assert.AreEqual(schemaLoadersList[0], schemaLoadersList[1]);
            Assert.AreEqual(schemaLoadersList[1], schemaLoadersList[2]);
            Assert.AreEqual(schemaLoadersList[0], schemaLoadersList[2]);

            //NOTE: We can't actually test this since it's Cached and the TestFramework may have already
            //  initialized the Schema Definitions for the connection from other tests!
            //Assert.IsFalse(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[0]).IsInitialized);
            schemaLoadersList[1].InitializeSchemaDefinitions();

            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[0]).IsInitialized);
            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[1]).IsInitialized);
            Assert.IsTrue(((SqlBulkHelpersDBSchemaStaticLoader)schemaLoadersList[2]).IsInitialized);
        }
        public async Task TestBulkInsertConstructorWithDBSchemaLoaderInstanceDeferred()
        {
            ISqlBulkHelpersConnectionProvider sqlConnectionProvider = SqlConnectionHelper.GetConnectionProvider();
            var sqlBulkDbSchemaLoader = SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(sqlConnectionProvider);

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
                using (SqlTransaction transaction = conn.BeginTransaction())
                {
                    ISqlBulkHelper <TestElement> sqlBulkIdentityHelper = new SqlBulkIdentityHelper <TestElement>(sqlBulkDbSchemaLoader);

                    await DoInsertOrUpdateTestAsync(sqlBulkIdentityHelper, transaction);
                }
        }
        public async Task TestBulkInsertConstructorWithDBSchemaLoaderInstanceFromExistingConnectionAndTransaction()
        {
            ISqlBulkHelpersConnectionProvider sqlConnectionProvider = SqlConnectionHelper.GetConnectionProvider();

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
                using (SqlTransaction transaction = conn.BeginTransaction())
                {
                    //TEST the code flow where the DB Schema Loader is initialized from existing
                    //Connection + Transaction and immediately initialized
                    var sqlBulkDbSchemaLoader = SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(conn, transaction, true);

                    ISqlBulkHelper <TestElement> sqlBulkIdentityHelper = new SqlBulkIdentityHelper <TestElement>(sqlBulkDbSchemaLoader);

                    await DoInsertOrUpdateTestAsync(sqlBulkIdentityHelper, transaction);
                }
        }
Esempio n. 5
0
        public async Task TestBulkInsertResultSortOrderWithIdentitySetterSupportAsync()
        {
            var testData = TestHelpers.CreateTestDataWithIdentitySetter(10);

            var sqlConnectionString = SqlConnectionHelper.GetSqlConnectionString();
            ISqlBulkHelpersConnectionProvider sqlConnectionProvider = new SqlBulkHelpersConnectionProvider(sqlConnectionString);

            var sqlBulkHelpersSchemaLoader = SqlBulkHelpersSchemaLoaderCache.GetSchemaLoader(sqlConnectionProvider);

            using (var conn = await sqlConnectionProvider.NewConnectionAsync())
                using (SqlTransaction transaction = conn.BeginTransaction())
                {
                    ISqlBulkHelper <TestElement> sqlBulkIdentityHelper = new SqlBulkIdentityHelper <TestElement>(sqlBulkHelpersSchemaLoader);

                    var results = await sqlBulkIdentityHelper.BulkInsertOrUpdateAsync(
                        testData,
                        TestHelpers.TestTableName,
                        transaction
                        );

                    transaction.Commit();

                    //ASSERT Results are Valid...
                    Assert.IsNotNull(results);

                    //We Sort the Results by Identity Id to ensure that the inserts occurred in the correct
                    //  order with incrementing ID values as specified in the original Data!
                    //This validates that data is inserted as expected for Identity columns so that it can
                    //  be correctly sorted by Incrementing Identity value when Queried (e.g. ORDER BY Id)
                    var resultsSorted = results.OrderBy(r => r.Id).ToList();
                    Assert.AreEqual(resultsSorted.Count(), testData.Count);

                    var i = 0;
                    foreach (var result in resultsSorted)
                    {
                        Assert.IsNotNull(result);
                        Assert.IsTrue(result.Id > 0);
                        Assert.AreEqual(result.Key, testData[i].Key);
                        Assert.AreEqual(result.Value, testData[i].Value);
                        i++;
                    }
                }
        }