Beispiel #1
0
        public async Task TestEntity_ConvertValuesWithDecimalOverflow_Fails()
        {
            var sql = $"SELECT `t`.`Id`, `t`.`ByteCol`, `t`.`DecimalCol`, `t`.`FloatCol`" +
                      $"{Environment.NewLine}FROM `TestEntities` AS `t`{Environment.NewLine}" +
                      $"WHERE `t`.`Id` = @__p_0{Environment.NewLine}LIMIT 1";

            _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateResultSet(
                                                                new List <Tuple <V1.Type, string> >
            {
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "Id"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "ByteCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Numeric
                }, "DecimalCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Float64
                }, "FloatCol"),
            },
                                                                new List <object[]>
            {
                new object[] { 1L, 1L, $"99999999999999999999999999999", 1d },
            }
                                                                ));

            using var db = new TypeConversionDbContext(ConnectionString);
            await Assert.ThrowsAsync <OverflowException>(() => db.TestEntities.FindAsync(1L).AsTask());
        }
Beispiel #2
0
        public async Task TestEntity_ConvertValuesWithFloatOverflow_Succeeds()
        {
            var sql = $"SELECT `t`.`Id`, `t`.`ByteCol`, `t`.`DecimalCol`, `t`.`FloatCol`" +
                      $"{Environment.NewLine}FROM `TestEntities` AS `t`{Environment.NewLine}" +
                      $"WHERE `t`.`Id` = @__p_0{Environment.NewLine}LIMIT 1";

            _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateResultSet(
                                                                new List <Tuple <V1.Type, string> >
            {
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "Id"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "ByteCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Numeric
                }, "DecimalCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Float64
                }, "FloatCol"),
            },
                                                                new List <object[]>
            {
                new object[] { 1L, 1L, "3.14", float.MaxValue * 2d },
            }
                                                                ));

            using var db = new TypeConversionDbContext(ConnectionString);
            var row = await db.TestEntities.FindAsync(1L);

            Assert.Equal(float.PositiveInfinity, row.FloatCol);
        }
        private string AddSelectSingerFullNameResult(string fullName, int paramIndex)
        {
            var selectFullNameSql = $"{Environment.NewLine}SELECT `FullName`{Environment.NewLine}FROM `Singers`" +
                                    $"{Environment.NewLine}WHERE  TRUE  AND `SingerId` = @p{paramIndex}";

            _fixture.SpannerMock.AddOrUpdateStatementResult(selectFullNameSql, StatementResult.CreateResultSet(
                                                                new List <Tuple <V1.TypeCode, string> >
            {
                Tuple.Create(V1.TypeCode.Int64, "SingerId"),
                Tuple.Create(V1.TypeCode.String, "FullName"),
            },
                                                                new List <object[]>
            {
                new object[] { 1L, fullName },
            }
                                                                ));
            return(selectFullNameSql);
        }
 private string AddFindSingerResult(string sql)
 {
     _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateResultSet(
                                                         new List <Tuple <V1.TypeCode, string> >
     {
         Tuple.Create(V1.TypeCode.Int64, "SingerId"),
         Tuple.Create(V1.TypeCode.Date, "BirthDate"),
         Tuple.Create(V1.TypeCode.String, "FirstName"),
         Tuple.Create(V1.TypeCode.String, "FullName"),
         Tuple.Create(V1.TypeCode.String, "LastName"),
         Tuple.Create(V1.TypeCode.Bytes, "Picture"),
     },
                                                         new List <object[]>
     {
         new object[] { 1L, null, "Alice", "Alice Morrison", "Morrison", null },
     }
                                                         ));
     return(sql);
 }
        public async Task ReadWriteTransaction_QueryFullyConsumed_WithModifiedResultsAfterLastRow_FailsRetry()
        {
            string sql = $"SELECT Id FROM Foo WHERE Id IN ({_fixture.RandomLong()}, {_fixture.RandomLong()})";

            _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateSingleColumnResultSet(new V1.Type {
                Code = V1.TypeCode.Int64
            }, "Id", 1));
            using var connection = CreateConnection();
            await connection.OpenAsync();

            using var transaction = await connection.BeginTransactionAsync();

            var cmd = connection.CreateSelectCommand(sql);

            cmd.Transaction = transaction;
            using (var reader = await cmd.ExecuteReaderAsync())
            {
                while (await reader.ReadAsync())
                {
                    Assert.Equal(1, reader.GetInt64(reader.GetOrdinal("Id")));
                }
            }
            // Add a row to the result of the query on the server and abort the transaction. Even though the
            // original query did not see the additional row, it did see a 'false' being returned after consuming
            // the first row in the query, meaning that it knew that there were no more results.
            // The retry should now fail.
            _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateResultSet(
                                                                new List <Tuple <V1.TypeCode, string> >
            {
                Tuple.Create(V1.TypeCode.Int64, "Id"),
            },
                                                                new List <object[]>
            {
                new object[] { 1L },
                new object[] { 2L },
            }
                                                                ));
            _fixture.SpannerMock.AbortTransaction(transaction.TransactionId);
            var e = await Assert.ThrowsAsync <SpannerAbortedDueToConcurrentModificationException>(() => transaction.CommitAsync());

            Assert.Equal(1, transaction.RetryCount);
        }
Beispiel #6
0
        public async Task TestEntity_ConvertValuesWithoutPrecisionLossOrOverflow_Succeeds()
        {
            var sql = $"SELECT `t`.`Id`, `t`.`ByteCol`, `t`.`DecimalCol`, `t`.`FloatCol`" +
                      $"{Environment.NewLine}FROM `TestEntities` AS `t`{Environment.NewLine}" +
                      $"WHERE `t`.`Id` = @__p_0{Environment.NewLine}LIMIT 1";

            _fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateResultSet(
                                                                new List <Tuple <V1.Type, string> >
            {
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "Id"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Int64
                }, "ByteCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Numeric
                }, "DecimalCol"),
                new Tuple <V1.Type, string>(new V1.Type {
                    Code = V1.TypeCode.Float64
                }, "FloatCol"),
            },
                                                                new List <object[]>
            {
                new object[] { 1L, 1L, "3.14", 1.0d },
            }
                                                                ));

            using var db = new TypeConversionDbContext(ConnectionString);
            var row = await db.TestEntities.FindAsync(1L);

            Assert.Equal(1L, row.Id);
            Assert.Equal((byte)1, row.ByteCol);
            Assert.Equal(SpannerNumeric.Parse("3.14"), SpannerNumeric.FromDecimal(row.DecimalCol, LossOfPrecisionHandling.Truncate));
            Assert.Equal(1.0d, row.FloatCol);
        }