Exemplo n.º 1
0
        public void AllowSave()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    var simple = container.Resolve <Common.DomRepository>().TestAllowSave.Simple;
                    var s1     = new TestAllowSave.Simple {
                        Code = 1, Name = "a", CodeAS = null, NameAS = null
                    };
                    var s2 = new TestAllowSave.Simple {
                        Code = 1, Name = "aaaaa", CodeAS = 2, NameAS = "b"
                    };

                    simple.Insert(s1, s2);
                    Assert.AreEqual("1a, 1aaaaa2b", TestUtility.DumpSorted(simple.Load(new[] { s1.ID, s2.ID }), s => s.Code + s.Name + s.CodeAS + s.NameAS));
                    Assert.AreEqual(0, simple.Validate(new[] { s1.ID, s2.ID }, onSave: true).Count(), "There should be no invalid items with errors that are checked on save.");

                    // Errors that are NOT checked on save:
                    var errors = simple.Validate(new[] { s1.ID, s2.ID }, onSave: false);
                    Assert.AreEqual(
                        TestUtility.DumpSorted(new[] {
                        "The required property CodeAS is not set./CodeAS/" + s1.ID,
                        "The required property NameAS is not set./NameAS/" + s1.ID,
                        "[Test] Longer than 3.//" + s2.ID,
                        "[Test] Longer than 4./Name/" + s2.ID
                    }),
                        TestUtility.DumpSorted(errors,
                                               error => string.Format(error.Message, error.MessageParameters) + "/" + error.Property + "/" + error.ID));
                }
            }
        }
Exemplo n.º 2
0
        public void DeleteUpdateInsert_ConflictUnique()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    container.Resolve <ISqlExecuter>().ExecuteSql("DELETE FROM TestEntity.UniqueEntity");
                    var r = container.Resolve <Common.DomRepository>().TestEntity.UniqueEntity;

                    var ia = new TestEntity.UniqueEntity {
                        Name = "a", ID = Guid.NewGuid()
                    };
                    var ib = new TestEntity.UniqueEntity {
                        Name = "b", ID = Guid.NewGuid()
                    };
                    var ic1 = new TestEntity.UniqueEntity {
                        Name = "c", ID = Guid.NewGuid()
                    };

                    r.Insert(ia, ib, ic1);
                    Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data));

                    // Deleting old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name.

                    var ic2 = new TestEntity.UniqueEntity {
                        Name = "c", ID = Guid.NewGuid()
                    };

                    r.Save(new[] { ic2 }, null, new[] { ic1 });
                    Assert.AreEqual("a, b, c", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data));
                    Guid currentCID = r.Query().Where(item => item.Name == "c").Select(item => item.ID).Single();
                    Assert.AreEqual(ic2.ID, currentCID, "new inserted item 'c'");
                    Assert.AreNotEqual(ic1.ID, currentCID, "old deleted item 'c'");

                    // Deleting old 'c' and inserting new 'c' with same ID. Possible conflict on primary key.

                    var ic3 = new TestEntity.UniqueEntity {
                        Name = "c", Data = "x", ID = ic2.ID
                    };

                    r.Save(new[] { ic3 }, null, new[] { ic2 });
                    Assert.AreEqual("a, b, cx", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data));

                    // Renaming old 'c' and inserting new 'c'. Possible conflict on unique constraint for property Name.

                    ic3.Name = "oldc";
                    var ic4 = new TestEntity.UniqueEntity {
                        Name = "c", ID = Guid.NewGuid()
                    };

                    r.Save(new[] { ic4 }, new[] { ic3 }, null);
                    Assert.AreEqual("a, b, c, oldcx", TestUtility.DumpSorted(r.Load(), item => item.Name + item.Data));
                }
            }
        }
Exemplo n.º 3
0
        public void Simple_Browse()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);

                    var repository = container.Resolve <Common.DomRepository>();

                    // Initialize data:

                    repository.TestPolymorphic.Simple1.Delete(repository.TestPolymorphic.Simple1.Query());
                    repository.TestPolymorphic.Simple2.Delete(repository.TestPolymorphic.Simple2.Query());
                    Assert.AreEqual(0, repository.TestPolymorphic.SimpleBase.Query().Count());

                    repository.TestPolymorphic.Simple1.Insert(new[] {
                        new TestPolymorphic.Simple1 {
                            Name = "a", Days = 1
                        },
                        new TestPolymorphic.Simple1 {
                            Name = "b", Days = 2
                        },
                        new TestPolymorphic.Simple1 {
                            Name = "b3", Days = 2
                        },
                        new TestPolymorphic.Simple1 {
                            Name = "b7", Days = 3
                        },
                    });
                    repository.TestPolymorphic.Simple2.Insert(new[] {
                        new TestPolymorphic.Simple2 {
                            Name1 = "aa", Name2 = 11, Finish = new DateTime(2000, 1, 1)
                        },
                        new TestPolymorphic.Simple2 {
                            Name1 = "bb", Name2 = 22, Finish = new DateTime(2000, 1, 2)
                        },
                        new TestPolymorphic.Simple2 {
                            Name1 = "cc", Name2 = 33, Finish = new DateTime(2000, 1, 3)
                        },
                    });

                    // Tests:

                    var report = repository.TestPolymorphic.SimpleBrowse.Query(item => item.Days == 2)
                                 .AsEnumerable()
                                 .Select(item => item.Name + "/" + item.Days + "(" + item.Simple1Name + "/" + item.Simple2Name1 + "/" + item.Simple2?.Name2 + ")");

                    Assert.AreEqual(
                        "b/2(b//), b3/2(b3//), bb-22/2(/bb/22)",
                        TestUtility.DumpSorted(report));
                }
            }
        }
Exemplo n.º 4
0
        public void UniqueReferenceCascadeDelete()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    var repository = container.Resolve <Common.DomRepository>();

                    var p1 = new TestBrowse.ParentBase {
                        Name = "p1"
                    };
                    var p2 = new TestBrowse.ParentBase {
                        Name = "p2"
                    };
                    repository.TestBrowse.ParentBase.Insert(p1, p2);

                    var pur1 = new TestBrowse.ParentUniqueReference {
                        ID = p1.ID, Name3 = "pur1"
                    };
                    var pur2 = new TestBrowse.ParentUniqueReference {
                        ID = p2.ID, Name3 = "pur2"
                    };
                    repository.TestBrowse.ParentUniqueReference.Insert(pur1, pur2);

                    var urc1 = new TestBrowse.UniqueReferenceChild {
                        ID = p1.ID, Name4 = "urc1"
                    };
                    var urc2 = new TestBrowse.UniqueReferenceChild {
                        ID = p2.ID, Name4 = "urc2"
                    };
                    repository.TestBrowse.UniqueReferenceChild.Insert(urc1, urc2);

                    Func <string> report = () => TestUtility.DumpSorted(
                        repository.TestBrowse.ParentBase.Query(new[] { p1.ID, p2.ID }).ToList(),
                        item => item.Name + "-" + item.Extension_ParentUniqueReference?.Name3 + "-" + item.Extension_ParentUniqueReference?.Extension_UniqueReferenceChild.Name4);

                    Assert.AreEqual("p1-pur1-urc1, p2-pur2-urc2", report());

                    repository.TestBrowse.ParentUniqueReference.Delete(pur1);
                    Assert.AreEqual("p1--, p2-pur2-urc2", report());

                    repository.TestBrowse.ParentBase.Delete(p1);
                    Assert.AreEqual("p2-pur2-urc2", report());

                    TestUtility.ShouldFail <Rhetos.UserException>(
                        () => repository.TestBrowse.ParentBase.Delete(p2),
                        "It is not allowed to delete a record that is referenced by other records.");
                }
            }
        }
Exemplo n.º 5
0
        public void OptimizeContainsNullableWithMixedNullValueTest()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    var repository = container.Resolve <Common.DomRepository>();

                    var id1 = Guid.NewGuid();
                    var id2 = Guid.NewGuid();
                    container.Resolve <ISqlExecuter>().ExecuteSql(new[]
                    {
                        $@"INSERT INTO Test12.Entity1 (ID) SELECT '{id1}';",
                        $@"INSERT INTO Test12.Entity1 (ID) SELECT '{id2}';",
                    });
                    Console.WriteLine($"ID1: {id1}");
                    Console.WriteLine($"ID2: {id2}");

                    var listWithNullValue = new List <Guid?> {
                        null, id1
                    };

                    Expression <Func <Common.Queryable.Test12_Entity1, bool> > containsId = x => listWithNullValue.Contains(x.ID);
                    CompareQueries(
                        basicQuery: repository.Test12.Entity1.Query().Where(containsId),
                        optimizedQuery: repository.Test12.Entity1.Query().Where(EFExpression.OptimizeContains(containsId)),
                        testDescription: $"on ID dbNull={useDatabaseNullSemantics}");

                    Expression <Func <Common.Queryable.Test12_Entity1, bool> > containsGuidProperty = x => listWithNullValue.Contains(x.GuidProperty);
                    CompareQueries(
                        basicQuery: repository.Test12.Entity1.Query().Where(containsGuidProperty),
                        optimizedQuery: repository.Test12.Entity1.Query().Where(EFExpression.OptimizeContains(containsGuidProperty)),
                        testDescription: $"on GuidProperty dbNull={useDatabaseNullSemantics}");

                    Expression <Func <Common.Queryable.Test12_Entity1, bool> > notContainsId = x => !listWithNullValue.Contains(x.ID);
                    CompareQueries(
                        basicQuery: repository.Test12.Entity1.Query().Where(notContainsId),
                        optimizedQuery: repository.Test12.Entity1.Query().Where(EFExpression.OptimizeContains(notContainsId)),
                        testDescription: $"notin on ID dbNull={useDatabaseNullSemantics}");

                    Expression <Func <Common.Queryable.Test12_Entity1, bool> > notContainsGuidProperty = x => !listWithNullValue.Contains(x.GuidProperty);
                    CompareQueries(
                        basicQuery: repository.Test12.Entity1.Query().Where(notContainsGuidProperty),
                        optimizedQuery: repository.Test12.Entity1.Query().Where(EFExpression.OptimizeContains(notContainsGuidProperty)),
                        testDescription: $"notin on GuidProperty dbNull={useDatabaseNullSemantics}");
                }
            }
        }
Exemplo n.º 6
0
        public void Disjunctive()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    var repository = container.Resolve <Common.DomRepository>();

                    repository.TestPolymorphic.Disjunctive1.Delete(repository.TestPolymorphic.Disjunctive1.Load());
                    repository.TestPolymorphic.Disjunctive2.Delete(repository.TestPolymorphic.Disjunctive2.Load());
                    Assert.AreEqual(0, repository.TestPolymorphic.Disjunctive.Load().Count());

                    var d1 = new TestPolymorphic.Disjunctive1 {
                        ID = Guid.NewGuid(), Name = "abc"
                    };
                    repository.TestPolymorphic.Disjunctive1.Insert(new[] { d1 });

                    var d2 = new TestPolymorphic.Disjunctive2 {
                        ID = Guid.NewGuid(), Days = 123
                    };
                    repository.TestPolymorphic.Disjunctive2.Insert(new[] { d2 });

                    var all = repository.TestPolymorphic.Disjunctive.Query();
                    Assert.AreEqual(
                        TestUtility.DumpSorted(new[] { d1.ID, d2.ID }),
                        TestUtility.DumpSorted(all, item => item.ID));

                    var browseReport = repository.TestPolymorphic.DisjunctiveBrowse.Query()
                                       .AsEnumerable()
                                       .Select(item => item.Subtype + "-" + item.Disjunctive1?.Name + "-" + item.Disjunctive2Days)
                                       .ToList();
                    Assert.AreEqual(
                        "TestPolymorphic.Disjunctive1-abc-, TestPolymorphic.Disjunctive2--123",
                        TestUtility.DumpSorted(browseReport));
                }
            }
        }
Exemplo n.º 7
0
        public void MultipleImplementations()
        {
            foreach (bool useDatabaseNullSemantics in new[] { false, true })
            {
                using (var container = new RhetosTestContainer())
                {
                    container.SetUseDatabaseNullSemantics(useDatabaseNullSemantics);
                    var repository = container.Resolve <Common.DomRepository>();

                    // Initialize data:

                    repository.TestPolymorphic.MultipleImplementations.Delete(repository.TestPolymorphic.MultipleImplementations.Query());

                    var mi1 = new TestPolymorphic.MultipleImplementations {
                        Name1 = "abc", Name2 = "123"
                    };
                    var mi2 = new TestPolymorphic.MultipleImplementations {
                        Name1 = "def", Name2 = "456"
                    };

                    repository.TestPolymorphic.MultipleImplementations.Insert(new[] { mi1, mi2 });

                    // Testing unions:

                    var base1 = repository.TestPolymorphic.Base1.Load();
                    Assert.AreEqual("abc, cba, def, fed", TestUtility.DumpSorted(base1, item => item.Name1));

                    var base2 = repository.TestPolymorphic.Base2.Load();
                    Assert.AreEqual("123, 321, 456, 654", TestUtility.DumpSorted(base2, item => item.Name2));

                    var base3 = repository.TestPolymorphic.Base3.Load();
                    Assert.AreEqual("abc-3, def-3", TestUtility.DumpSorted(base3, item => item.Name1));

                    // Testing specific implementation ID uniqueness:

                    var base1IDs = base1.Select(item => item.ID).ToList();
                    Assert.AreEqual(base1IDs.Count, base1IDs.Distinct().Count());

                    // Testing specific implementation ID stability:

                    var secondRead = repository.TestPolymorphic.Base1.Query();
                    Assert.AreEqual(
                        TestUtility.DumpSorted(base1IDs),
                        TestUtility.DumpSorted(secondRead, item => item.ID));

                    // Testing querying by specific implementation subtype:

                    Assert.AreEqual(
                        "abc-, cba-abc, def-, fed-def",
                        TestUtility.DumpSorted(repository.TestPolymorphic.Base1.Query()
                                               .ToList()
                                               .Select(item => item.Name1 + "-" + item.MultipleImplementationsReverse?.Name1)));

                    // Testing C# implementation:

                    int implementationHash = DomUtility.GetSubtypeImplementationHash("Reverse");

                    var expected = new[] {
                        new TestPolymorphic.Base1 {
                            ID = DomUtility.GetSubtypeImplementationId(mi1.ID, implementationHash),
                            MultipleImplementationsReverseID = mi1.ID
                        },
                        new TestPolymorphic.Base1 {
                            ID = DomUtility.GetSubtypeImplementationId(mi2.ID, implementationHash),
                            MultipleImplementationsReverseID = mi2.ID
                        },
                    };
                    var actual = base1.Where(item => item.MultipleImplementationsReverseID != null);
                    Assert.AreEqual(
                        TestUtility.DumpSorted(expected, item => item.MultipleImplementationsReverseID.ToString() + "/" + item.ID.ToString()),
                        TestUtility.DumpSorted(actual, item => item.MultipleImplementationsReverseID.ToString() + "/" + item.ID.ToString()));

                    // Testing persisted IDs for specific implementation subtype:

                    Assert.AreEqual(
                        TestUtility.DumpSorted(base1IDs),
                        TestUtility.DumpSorted(repository.TestPolymorphic.Base1_Materialized.Query().Select(item => item.ID)));
                }
            }
        }