Example #1
0
        public async Task KeepIdentity_SkipOnInsertTrue(
            [DataSources(false)] string context,
            [Values(null, true, false)] bool?keepIdentity,
            [Values] BulkCopyType copyType,
            [Values(0, 1, 2)] int asyncMode)             // 0 == sync, 1 == async, 2 == async with IAsyncEnumerable
        {
            if ((context == ProviderName.Sybase) && copyType == BulkCopyType.ProviderSpecific && keepIdentity != true)
            {
                Assert.Inconclusive("Sybase native bulk copy doesn't support identity insert (despite documentation)");
            }

            ResetAllTypesIdentity(context);

            if ((context == ProviderName.OracleNative || context == TestProvName.Oracle11Native) && copyType == BulkCopyType.ProviderSpecific)
            {
                Assert.Inconclusive("Oracle BulkCopy doesn't support identity triggers");
            }

            // don't use transactions as some providers will fallback to non-provider-specific implementation then
            using (var db = new TestDataConnection(context))
            {
                var lastId = db.InsertWithInt32Identity(new TestTable2());
                try
                {
                    var options = new BulkCopyOptions()
                    {
                        KeepIdentity = keepIdentity,
                        BulkCopyType = copyType
                    };

                    if (!await ExecuteAsync(db, context, perform, keepIdentity, copyType))
                    {
                        return;
                    }

                    var data = db.GetTable <TestTable2>().Where(_ => _.ID > lastId).OrderBy(_ => _.ID).ToArray();

                    Assert.AreEqual(2, data.Length);

                    // oracle supports identity insert only starting from version 12c, which is not used yet for tests
                    var useGenerated = keepIdentity != true ||
                                       context.Contains("Oracle");

                    Assert.AreEqual(lastId + (!useGenerated ? 10 : 1), data[0].ID);
                    Assert.AreEqual(200, data[0].Value);
                    Assert.AreEqual(lastId + (!useGenerated ? 20 : 2), data[1].ID);
                    Assert.AreEqual(300, data[1].Value);

                    async Task perform()
                    {
                        var values = new[]
                        {
                            new TestTable2()
                            {
                                ID    = lastId + 10,
                                Value = 200
                            },
                            new TestTable2()
                            {
                                ID    = lastId + 20,
                                Value = 300
                            }
                        };

                        if (asyncMode == 0)                         // synchronous
                        {
                            db.BulkCopy(
                                options,
                                values);
                        }
                        else if (asyncMode == 1)                         // asynchronous
                        {
                            await db.BulkCopyAsync(
                                options,
                                values);
                        }
                        else                         // asynchronous with IAsyncEnumerable
                        {
                            await db.BulkCopyAsync(
                                options,
                                AsAsyncEnumerable(values));
                        }
                    }
                }
                finally
                {
                    // cleanup
                    db.GetTable <TestTable2>().Delete(_ => _.ID >= lastId);
                }
            }
        }