Example #1
0
        public void LongStringPropertySave()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var tests = new List <string>
                {
                    "abc",
                    "ABC",
                    "",
                    null,
                    @"čćšđžČĆŠĐŽ",
                    @"`~!@#$%^&*()_+-=[]\{}|;':"",./<>?",
                    new string('a', 256),
                    new string('a', 17000),
                };

                foreach (var test in tests)
                {
                    var entity1 = new TestStorage.AllProperties
                    {
                        ID = Guid.NewGuid(),
                        LongStringProperty = test
                    };
                    context.PersistenceStorage.Insert(entity1);
                    Assert.AreEqual(test, context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity1.ID).Single().LongStringProperty);
                }
            }
        }
        public void AutoRoundOff_ThowsIfOverflow()
        {
            // AutoRoundMoney option defaults to false to there's no need to override the options
            using (var scope = TestScope.Create())
            {
                var context = scope.Resolve <Common.ExecutionContext>();

                var tests = new[]
                {
                    0.001m,
                    1.1234m,
                    -0.001m,
                    -1.1234m,
                };

                foreach (var test in tests)
                {
                    var entity = new TestStorage.AllProperties
                    {
                        ID            = Guid.NewGuid(),
                        MoneyProperty = test
                    };

                    Action save = () => context.Repository.TestStorage.AllProperties.Save(new[] { entity }, null, null);

                    TestUtility.ShouldFail <UserException>(save, "It is not allowed to enter a money value with more than 2 decimals.");
                }
            }
        }
Example #3
0
        public void ShortStringPropertyTooLong()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var tests = new List <string>
                {
                    new string('a', 257),
                    new string('a', 17000),
                };

                foreach (var test in tests)
                {
                    var entity1 = new TestStorage.AllProperties
                    {
                        ID = Guid.NewGuid(),
                        ShortStringProperty = test
                    };
                    TestUtility.ShouldFail <SqlException>(
                        () => context.PersistenceStorage.Insert(entity1),
                        "String or binary data would be truncated"); // SQL Server 2019 includes table and column name, but older version do not.
                }
            }
        }
Example #4
0
        public void InsertAllPropertiesWithNullValueTest()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var entity = new TestStorage.AllProperties
                {
                    ID                  = Guid.NewGuid(),
                    BinaryProperty      = null,
                    BoolProperty        = null,
                    DateProperty        = null,
                    DateTimeProperty    = null,
                    DecimalProperty     = null,
                    GuidProperty        = null,
                    IntegerProperty     = null,
                    MoneyProperty       = null,
                    ShortStringProperty = null,
                    LongStringProperty  = null
                };
                context.PersistenceStorage.Insert(entity);
                var entityLoadedFromDatabase = context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity.ID).Single();
                AssertAreEqual(entity, entityLoadedFromDatabase);
            }
        }
        public void MoneyPropertySizeAndDecimals()
        {
            using (var scope = TestScope.Create(builder =>
            {
                var options = new CommonConceptsRuntimeOptions()
                {
                    AutoRoundMoney = true
                };
                builder.RegisterInstance(options);
                builder.RegisterType <GenericRepositories>().AsImplementedInterfaces();
            }))
            {
                var context = scope.Resolve <Common.ExecutionContext>();

                var tests = new List <(decimal Save, decimal Load)>
                {
                    (-922337203685477.58m, -922337203685477.58m), // T-SQL money limits.
                    (922337203685477.58m, 922337203685477.58m),   // T-SQL money limits.
                    (0m, 0m),
                };

                foreach (var test in tests)
                {
                    var entity = new TestStorage.AllProperties
                    {
                        ID            = Guid.NewGuid(),
                        MoneyProperty = test.Save
                    };
                    context.PersistenceStorage.Insert(entity);
                    Assert.AreEqual(test.Load, context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity.ID).Single().MoneyProperty,
                                    $"The money property should be cut off on the second decimal position ({test.Save}).");
                }
            }
        }
Example #6
0
        public void DateTimeSave()
        {
            using (var container = new RhetosTestContainer())
            {
                var  context        = container.Resolve <Common.ExecutionContext>();
                bool usingDateTime2 = !container.Resolve <CommonConceptsDatabaseSettings>().UseLegacyMsSqlDateTime;

                var tests = new List <DateTime>
                {
                    new DateTime(1900, 1, 1),
                    new DateTime(1900, 1, 1, 1, 1, 1, 300),
                    new DateTime(3000, 12, 31),
                    new DateTime(3000, 12, 31, 23, 59, 59, 300),
                    new DateTime(2001, 2, 3, 4, 5, 6, 3),
                    new DateTime(2001, 2, 3, 4, 5, 6, 7),
                    new DateTime(2001, 2, 3, 4, 5, 6, 10),
                    new DateTime(2001, 2, 3, 4, 5, 6, 13),
                };

                if (usingDateTime2)
                {
                    tests.AddRange(new[]
                    {
                        new DateTime(2001, 2, 3, 4, 5, 6, 14),
                        new DateTime(2001, 2, 3, 4, 5, 6, 15),
                    });
                }

                foreach (var test in tests)
                {
                    var entity1 = new TestStorage.AllProperties
                    {
                        ID = Guid.NewGuid(),
                        DateTimeProperty = test
                    };
                    context.PersistenceStorage.Insert(entity1);
                    DateTime?loaded = context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity1.ID).Single().DateTimeProperty;
                    string   info   = $"Saved value: '{test:o}'. Loaded value: '{loaded:o}'."; // Includes milliseconds, Assert.AreEqual does not show them.
                    if (usingDateTime2)
                    {
                        Assert.AreEqual(test, loaded, info);
                    }
                    else
                    {
                        // Old DateTime column type has 3 ms precision.
                        Console.WriteLine(info);
                        Assert.IsTrue(Math.Abs(test.Subtract(loaded.Value).TotalMilliseconds) <= 3, info);
                    }
                }
            }
        }
Example #7
0
        public void ShortStringPropertyTruncationErrorTest()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var entity1 = new TestStorage.AllProperties
                {
                    ID = Guid.NewGuid(),
                    ShortStringProperty = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt."
                };
                TestUtility.ShouldFail <SqlException>(() => context.PersistenceStorage.Insert(entity1),
                                                      "data would be truncated");
            }
        }
Example #8
0
 private void AssertAreEqual(TestStorage.AllProperties expected, TestStorage.AllProperties actual)
 {
     if (expected.BinaryProperty == null)
     {
         Assert.IsNull(actual.BinaryProperty);
     }
     else
     {
         Assert.IsTrue(expected.BinaryProperty.SequenceEqual(actual.BinaryProperty));
     }
     Assert.AreEqual(expected.BoolProperty, actual.BoolProperty);
     Assert.AreEqual(expected.DateProperty, actual.DateProperty);
     Assert.AreEqual(expected.DateTimeProperty, actual.DateTimeProperty);
     Assert.AreEqual(expected.DecimalProperty, actual.DecimalProperty);
     Assert.AreEqual(expected.GuidProperty, actual.GuidProperty);
     Assert.AreEqual(expected.IntegerProperty, actual.IntegerProperty);
     Assert.AreEqual(expected.MoneyProperty, actual.MoneyProperty);
     Assert.AreEqual(expected.ShortStringProperty, actual.ShortStringProperty);
     Assert.AreEqual(expected.LongStringProperty, actual.LongStringProperty);
 }
Example #9
0
        public void DecimalPropertySizeAndDecimals()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var tests = new List <(decimal Save, decimal Load)>
                {
                    (12.34000000001m, 12.34m),
                    (12.34000000009m, 12.34m),
                    (-12.34000000001m, -12.34m),
                    (-12.34000000009m, -12.34m),
                    (12.34000000011m, 12.3400000001m),
                    (12.34000000019m, 12.3400000001m),
                    (-12.34000000011m, -12.3400000001m),
                    (-12.34000000019m, -12.3400000001m),
                    (923456789012345678.0123456789m, 923456789012345678.0123456789m),   // decimal(28,10) should allow 18 digits on left and 10 digits on right side of the decimal point.
                    (-923456789012345678.0123456789m, -923456789012345678.0123456789m), // decimal(28,10) should allow 18 digits on left and 10 digits on right side of the decimal point.
                    (0m, 0m),
                    (0.00000000001m, 0m),
                    (0.00000000009m, 0m),
                    (0.00000000019m, 0.0000000001m),
                    (-0.00000000001m, 0m),
                    (-0.00000000009m, 0m),
                    (-0.00000000019m, -0.0000000001m),
                };

                foreach (var test in tests)
                {
                    var entity1 = new TestStorage.AllProperties
                    {
                        ID = Guid.NewGuid(),
                        DecimalProperty = test.Save
                    };
                    context.PersistenceStorage.Insert(entity1);
                    Assert.AreEqual(test.Load, context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity1.ID).Single().DecimalProperty,
                                    $"The money property should be cut off on the 10th decimal position ({test.Save}).");
                }
            }
        }
Example #10
0
        public void MoneyPropertySizeAndDecimals()
        {
            using (var container = new RhetosTestContainer())
            {
                var context = container.Resolve <Common.ExecutionContext>();

                var tests = new List <(decimal Save, decimal Load)>
                {
                    (12.34100m, 12.34m),
                    (12.34900m, 12.34m),
                    (-12.3410m, -12.34m),
                    (-12.3490m, -12.34m),
                    (-922337203685477.58m, -922337203685477.58m), // T-SQL money limits.
                    (922337203685477.58m, 922337203685477.58m),   // T-SQL money limits.
                    (0m, 0m),
                    // Current behavior is rounding money values, but it should be changed in future,
                    // see https://github.com/Rhetos/Rhetos/issues/389: Money type should throw an exception, instead of implicit rounding, if saving more decimals then allowed.
                    (0.001m, 0m),
                    (0.009m, 0m),
                    (0.019m, 0.01m),
                    (-0.001m, 0m),
                    (-0.009m, 0m),
                    (-0.019m, -0.01m),
                };

                foreach (var test in tests)
                {
                    var entity = new TestStorage.AllProperties
                    {
                        ID            = Guid.NewGuid(),
                        MoneyProperty = test.Save
                    };
                    context.PersistenceStorage.Insert(entity);
                    Assert.AreEqual(test.Load, context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity.ID).Single().MoneyProperty,
                                    $"The money property should be cut off on the second decimal position ({test.Save}).");
                }
            }
        }
        public void AutoRoundOn()
        {
            using (var scope = TestScope.Create(builder =>
            {
                var options = new CommonConceptsRuntimeOptions()
                {
                    AutoRoundMoney = true
                };
                builder.RegisterInstance(options);
            }))
            {
                var context = scope.Resolve <Common.ExecutionContext>();
                var tests   = new List <(decimal ValueToWrite, decimal ExpectedPersistedValue)>()
                {
                    (0.001m, 0m),
                    (0.009m, 0m),
                    (0.019m, 0.01m),
                    (-0.001m, 0m),
                    (-0.009m, 0m),
                    (-0.019m, -0.01m),
                };

                foreach (var test in tests)
                {
                    var entity = new TestStorage.AllProperties
                    {
                        ID            = Guid.NewGuid(),
                        MoneyProperty = test.ValueToWrite
                    };
                    context.Repository.TestStorage.AllProperties.Save(new[] { entity }, null, null);
                    var actualPersistedValue = context.Repository.TestStorage.AllProperties.Load(x => x.ID == entity.ID).Single().MoneyProperty;

                    Assert.AreEqual(test.ExpectedPersistedValue, actualPersistedValue);
                }
            }
        }