public void AllRowsInserted(TableDataInsertAllResponse response)
        {
            BigQueryInsertResults results = new BigQueryInsertResults(
                _client,
                new InsertOptions(),
                new List <BigQueryInsertRow> {
                new BigQueryInsertRow()
            },
                response);

            Assert.Equal(BigQueryInsertStatus.AllRowsInserted, results.Status);
            Assert.Equal(0, results.OriginalRowsWithErrors);
            Assert.Equal(1, results.InsertAttemptRowCount);
            Assert.Empty(results.Errors);
            Assert.Same(results, results.ThrowOnNoneInserted());
            Assert.Same(results, results.ThrowOnNotAllInserted());
            Assert.Same(results, results.ThrowOnAnyError());
        }
        public void SomeRowsInserted()
        {
            ErrorProto row1Error1 = new ErrorProto {
                Location = "field_1", Reason = "reason_1", Message = "message_1"
            };
            ErrorProto row1Error2 = new ErrorProto {
                Location = "field_2", Message = "message_2"
            };
            InsertErrorsData row1List1 = new InsertErrorsData {
                Index = 1, Errors = new List <ErrorProto> {
                    row1Error1, row1Error2
                }
            };

            ErrorProto row5Error1 = new ErrorProto {
                Location = "field_3", Reason = "reason_3"
            };
            InsertErrorsData row5List1 = new InsertErrorsData {
                Index = 5, Errors = new List <ErrorProto> {
                    row5Error1
                }
            };

            InsertErrorsData row6List1 = new InsertErrorsData {
                Index = 6
            };

            ErrorProto row1Error3 = new ErrorProto {
                Location = "field_4", Reason = "reason_4", Message = "message_4"
            };
            InsertErrorsData row1List2 = new InsertErrorsData {
                Index = 1, Errors = new List <ErrorProto> {
                    row1Error3
                }
            };

            TableDataInsertAllResponse response = new TableDataInsertAllResponse
            {
                InsertErrors = new List <InsertErrorsData>
                {
                    row1List1,
                    row5List1,
                    row6List1,
                    row1List2
                }
            };

            IReadOnlyList <BigQueryInsertRow> rows = Enumerable.Range(0, 8).Select(_ => new BigQueryInsertRow()).ToList().AsReadOnly();

            List <BigQueryInsertRowErrors> expectedInsertRowErrors = new List <BigQueryInsertRowErrors>
            {
                new BigQueryInsertRowErrors(rows[1], new List <InsertErrorsData> {
                    row1List1, row1List2
                }),
                new BigQueryInsertRowErrors(rows[5], new List <InsertErrorsData> {
                    row5List1
                }),
                new BigQueryInsertRowErrors(rows[6], new List <InsertErrorsData> {
                    row6List1
                }),
            };
            List <SingleError> expectedSingleErrors = expectedInsertRowErrors.SelectMany(rowError => rowError).ToList();

            BigQueryInsertResults results = new BigQueryInsertResults(
                _client,
                new InsertOptions {
                SkipInvalidRows = true
            },
                rows,
                response);

            Assert.Equal(BigQueryInsertStatus.SomeRowsInserted, results.Status);
            Assert.Equal(3, results.OriginalRowsWithErrors);
            Assert.Equal(8, results.InsertAttemptRowCount);
            Assert.Equal(expectedInsertRowErrors, results.Errors.ToList(), new BigQueryInsertRowErrorEqualityComparer());
            Assert.Same(results, results.ThrowOnNoneInserted());
            AssertException(results.ThrowOnNotAllInserted, expectedSingleErrors, BigQueryInsertStatus.SomeRowsInserted);
            AssertException(results.ThrowOnAnyError, expectedSingleErrors, BigQueryInsertStatus.SomeRowsInserted);
        }
        public void NoRowsInserted_NoSkip()
        {
            ErrorProto row1Error1 = new ErrorProto {
                Location = "field_1", Reason = "reason_1", Message = "message_1"
            };
            ErrorProto row1Error2 = new ErrorProto {
                Location = "field_2", Message = "message_2"
            };
            InsertErrorsData row1List1 = new InsertErrorsData {
                Index = 1, Errors = new List <ErrorProto> {
                    row1Error1, row1Error2
                }
            };

            ErrorProto row5Error1 = new ErrorProto {
                Location = "field_3", Reason = "reason_3"
            };
            InsertErrorsData row5List1 = new InsertErrorsData {
                Index = 5, Errors = new List <ErrorProto> {
                    row5Error1
                }
            };

            InsertErrorsData row6List1 = new InsertErrorsData {
                Index = 6
            };

            ErrorProto row1Error3 = new ErrorProto {
                Location = "field_4", Reason = "reason_4", Message = "message_4"
            };
            InsertErrorsData row1List2 = new InsertErrorsData {
                Index = 1, Errors = new List <ErrorProto> {
                    row1Error3
                }
            };

            TableDataInsertAllResponse response = new TableDataInsertAllResponse
            {
                InsertErrors = new List <InsertErrorsData>
                {
                    row1List1,
                    row5List1,
                    row6List1,
                    row1List2
                }
            };

            IReadOnlyList <BigQueryInsertRow> rows = Enumerable.Range(0, 8).Select(_ => new BigQueryInsertRow()).ToList().AsReadOnly();

            // We don't need to have errors for all rows to know that no rows were inserted.
            // If SkipInvalidRows is not set to true, then even if only one row has errors, no rows are inserted.
            List <BigQueryInsertRowErrors> expectedInsertRowErrors = new List <BigQueryInsertRowErrors>
            {
                new BigQueryInsertRowErrors(rows[1], new List <InsertErrorsData> {
                    row1List1, row1List2
                }),
                new BigQueryInsertRowErrors(rows[5], new List <InsertErrorsData> {
                    row5List1
                }),
                new BigQueryInsertRowErrors(rows[6], new List <InsertErrorsData> {
                    row6List1
                }),
            };
            List <SingleError> expectedSingleErrors = expectedInsertRowErrors.SelectMany(rowError => rowError).ToList();

            BigQueryInsertResults results = new BigQueryInsertResults(
                _client,
                new InsertOptions(),
                rows,
                response);

            Assert.Equal(BigQueryInsertStatus.NoRowsInserted, results.Status);
            Assert.Equal(3, results.OriginalRowsWithErrors);
            Assert.Equal(8, results.InsertAttemptRowCount);
            Assert.Equal(expectedInsertRowErrors, results.Errors.ToList(), new BigQueryInsertRowErrorEqualityComparer());
            AssertException(results.ThrowOnNoneInserted, expectedSingleErrors, BigQueryInsertStatus.NoRowsInserted);
            AssertException(results.ThrowOnNotAllInserted, expectedSingleErrors, BigQueryInsertStatus.NoRowsInserted);
            AssertException(results.ThrowOnAnyError, expectedSingleErrors, BigQueryInsertStatus.NoRowsInserted);
        }
Ejemplo n.º 4
0
 private void AssertAllRowsInserted(BigQueryInsertResults insertResult)
 {
     Assert.Equal(BigQueryInsertStatus.AllRowsInserted, insertResult.Status);
     Assert.Empty(insertResult.Errors);
 }