예제 #1
0
        public void TestDecimalOutOfRangeException()
        {
            var schema = (LogicalSchema)Schema.Parse("{\"type\": \"bytes\", \"logicalType\": \"decimal\", \"precision\": 4, \"scale\": 2 }");

            var avroDecimal = new Avro.Util.Decimal();
            var decimalVal  = (AvroDecimal)1234.567M; // scale of 3 should throw ArgumentOutOfRangeException

            Assert.Throws <ArgumentOutOfRangeException>(() => avroDecimal.ConvertToBaseValue(decimalVal, schema));
        }
예제 #2
0
        public void TestDecimal(string s)
        {
            var schema = (LogicalSchema)Schema.Parse("{\"type\": \"bytes\", \"logicalType\": \"decimal\", \"precision\": 4, \"scale\": 2 }");

            var avroDecimal = new Avro.Util.Decimal();
            var decimalVal  = (AvroDecimal)decimal.Parse(s);

            var convertedDecimalVal = (AvroDecimal)avroDecimal.ConvertToLogicalValue(avroDecimal.ConvertToBaseValue(decimalVal, schema), schema);

            Assert.AreEqual(decimalVal, convertedDecimalVal);
        }
예제 #3
0
        public void TestDecimal(string s)
        {
            var schema = (LogicalSchema)Schema.Parse("{\"type\": \"bytes\", \"logicalType\": \"decimal\", \"precision\": 4, \"scale\": 2 }");

            var avroDecimal = new Avro.Util.Decimal();
            // CultureInfo.InvariantCulture ensures that "." is always accepted as the decimal point
            var decimalVal = (AvroDecimal)decimal.Parse(s, CultureInfo.InvariantCulture);

            var convertedDecimalVal = (AvroDecimal)avroDecimal.ConvertToLogicalValue(avroDecimal.ConvertToBaseValue(decimalVal, schema), schema);

            Assert.AreEqual(decimalVal, convertedDecimalVal);
        }
예제 #4
0
        public void TestDecimalMinMax()
        {
            var schema = (LogicalSchema)Schema.Parse("{\"type\": \"bytes\", \"logicalType\": \"decimal\", \"precision\": 4, \"scale\": 0 }");

            var avroDecimal = new Avro.Util.Decimal();

            foreach (var decimalVal in new AvroDecimal[] { decimal.MinValue, decimal.MaxValue })
            {
                var convertedDecimalVal = (AvroDecimal)avroDecimal.ConvertToLogicalValue(avroDecimal.ConvertToBaseValue(decimalVal, schema), schema);

                Assert.AreEqual(decimalVal, convertedDecimalVal);
            }
        }
예제 #5
0
        // This tests ensures that changes to Decimal.ConvertToBaseValue and ConvertToLogicalValue can be validated (bytes)
        public void TestDecimalConvert(string s, int scale, byte[] converted)
        {
            var schema = (LogicalSchema)Schema.Parse(@$ "{{" "type" ": " "bytes" ", " "logicalType" ": " "decimal" ", " "precision" ": 4, " "scale" ": {scale}}}");

            var avroDecimal = new Avro.Util.Decimal();
            // CultureInfo.InvariantCulture ensures that "." is always accepted as the decimal point
            var decimalVal = (AvroDecimal)decimal.Parse(s, CultureInfo.InvariantCulture);

            // TestDecimal tests ConvertToLogicalValue(ConvertToBaseValue(...)) which might hide symmetrical breaking changes in both functions
            // The following 2 tests are checking the conversions seperately

            // Validate Decimal.ConvertToBaseValue
            Assert.AreEqual(converted, avroDecimal.ConvertToBaseValue(decimalVal, schema));

            // Validate Decimal.ConvertToLogicalValue
            Assert.AreEqual(decimalVal, (AvroDecimal)avroDecimal.ConvertToLogicalValue(converted, schema));
        }