Ejemplo n.º 1
0
        public void SpecificComplexSubtype()
        {
            using (var container = new RhetosTestContainer())
            {
                var repositories = container.Resolve <GenericRepositories>();

                var subtype1 = new TestPolymorphic.ComplexImplementationData {
                    ID = Guid.NewGuid(), a = "a"
                };
                repositories.Save(new[] { subtype1 }, null, repositories.Load <TestPolymorphic.ComplexImplementationData>());

                var subtype2 = new TestPolymorphic.ComplexImplementationSql {
                    ID = Guid.NewGuid(), AlternativeId = Guid.NewGuid(), s = "s"
                };
                repositories.Save(new[] { subtype2 }, null, repositories.Load <TestPolymorphic.ComplexImplementationSql>());

                var all = repositories.Query <Common.Queryable.TestPolymorphic_ComplexBase>()
                          .OrderBy(item => item.Subtype).ThenBy(item => item.Name1);

                var expectedAll = new[]
                {
                    "TestPolymorphic.ComplexImplementationQuery abc1",
                    "TestPolymorphic.ComplexImplementationQuery q2 abc2",
                    "TestPolymorphic.ComplexImplementationSql s3",
                    "TestPolymorphic.ComplexImplementationSql sql2 s4",
                };

                Assert.AreEqual(
                    TestUtility.Dump(expectedAll),
                    TestUtility.Dump(all, item => item.Subtype + " " + item.Name1));

                var references = all.Select(item => new
                {
                    q  = item.ComplexImplementationQuery.a,
                    q2 = item.ComplexImplementationQueryq2.a,
                    s  = item.ComplexImplementationSql.s,
                    s2 = item.ComplexImplementationSqlsql2.s,
                }).ToList();

                var expectedReferences = new[]
                {
                    "abc---",
                    "-abc--",
                    "--s-",
                    "---s",
                };
                Assert.AreEqual(
                    TestUtility.Dump(expectedReferences),
                    TestUtility.Dump(references, item => (item.q ?? "-") + (item.q2 ?? "-") + (item.s ?? "-") + (item.s2 ?? "-")));
            }
        }
Ejemplo n.º 2
0
        public void SpecificComplexSubtype()
        {
            using (var container = new RhetosTestContainer())
            {
                var repositories = container.Resolve<GenericRepositories>();

                var subtype1 = new TestPolymorphic.ComplexImplementationData { ID = Guid.NewGuid(), a = "a" };
                repositories.Save(new[] { subtype1 }, null, repositories.Load<TestPolymorphic.ComplexImplementationData>());

                var subtype2 = new TestPolymorphic.ComplexImplementationSql { ID = Guid.NewGuid(), AlternativeId = Guid.NewGuid(), s = "s" };
                repositories.Save(new[] { subtype2 }, null, repositories.Load<TestPolymorphic.ComplexImplementationSql>());

                var all = repositories.Query<Common.Queryable.TestPolymorphic_ComplexBase>()
                    .OrderBy(item => item.Subtype).ThenBy(item => item.Name1);

                var expectedAll = new[]
                {
                    "TestPolymorphic.ComplexImplementationQuery abc1",
                    "TestPolymorphic.ComplexImplementationQuery q2 abc2",
                    "TestPolymorphic.ComplexImplementationSql s3",
                    "TestPolymorphic.ComplexImplementationSql sql2 s4",
                };

                Assert.AreEqual(
                    TestUtility.Dump(expectedAll),
                    TestUtility.Dump(all, item => item.Subtype + " " + item.Name1));

                var references = all.Select(item => new
                {
                    q = item.ComplexImplementationQuery.a,
                    q2 = item.ComplexImplementationQueryq2.a,
                    s = item.ComplexImplementationSql.s,
                    s2 = item.ComplexImplementationSqlsql2.s,
                }).ToList();

                var expectedReferences = new[]
                {
                    "abc---",
                    "-abc--",
                    "--s-",
                    "---s",
                };
                Assert.AreEqual(
                    TestUtility.Dump(expectedReferences),
                    TestUtility.Dump(references, item => (item.q ?? "-") + (item.q2 ?? "-") + (item.s ?? "-") + (item.s2 ?? "-")));
            }
        }
Ejemplo n.º 3
0
        public void FilterSubtype()
        {
            using (var container = new RhetosTestContainer())
            {
                var repository = container.Resolve<Common.DomRepository>();
                repository.TestPolymorphic.ComplexImplementationData.Delete(repository.TestPolymorphic.ComplexImplementationData.Load());
                repository.TestPolymorphic.ComplexImplementationSql.Delete(repository.TestPolymorphic.ComplexImplementationSql.Load());

                Func<Guid, string, Guid> hash = (id, implementation) =>
                    DomUtility.GetSubtypeImplementationId(id,
                        DomUtility.GetSubtypeImplementationHash(implementation));

                var d = new TestPolymorphic.ComplexImplementationData { a = "d", ID = Guid.NewGuid() };
                var sx = new TestPolymorphic.ComplexImplementationSql { s = "sx", ID = Guid.NewGuid() };
                var sy = new TestPolymorphic.ComplexImplementationSql { s = "sy", ID = Guid.NewGuid() };
                // Automatic update of _Materialized entity will not work if AlternativeId does not match ID hashed with implementation name.
                sx.AlternativeId = hash(sx.ID, "sql2");
                sy.AlternativeId = hash(sy.ID, "sql2");

                repository.TestPolymorphic.ComplexImplementationData.Insert(new[] { d });
                repository.TestPolymorphic.ComplexImplementationSql.Insert(new[] { sx, sy });

                Assert.AreEqual("abc1, abc2, sx3, sx4, sy3, sy4",
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));

                Action<string, IEnumerable<Guid>, string, string> test =
                    (expected, ids, subtype, implementationName) => Assert.AreEqual(expected,
                        TestUtility.DumpSorted(
                            repository.TestPolymorphic.ComplexBase.Filter(new FilterSubtype { Ids = ids, Subtype = subtype, ImplementationName = implementationName }),
                            item => item.Name1));

                // Testing filter:

                test("abc1", new[] { d.ID }, "TestPolymorphic.ComplexImplementationQuery", null);
                test("abc1", new[] { d.ID }, "TestPolymorphic.ComplexImplementationQuery", "");
                test("abc2", new[] { hash(d.ID, "q2") }, "TestPolymorphic.ComplexImplementationQuery", "q2");
                test("", new[] { d.ID }, "TestPolymorphic.ComplexImplementationSql", null);
                test("sx3", new[] { sx.ID }, "TestPolymorphic.ComplexImplementationSql", null);
                test("sx4", new[] { sx.AlternativeId.Value }, "TestPolymorphic.ComplexImplementationSql", "sql2");

                TestUtility.ShouldFail<Rhetos.ClientException>(() => repository.TestPolymorphic.ComplexBase.Filter(new FilterSubtype { Ids = new Guid[] {},
                    Subtype = "nonexisting", ImplementationName = "" }), "nonexisting");

                // Testing update of materialized data (it uses the Subtype filter):

                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                sx.s = "sxx";
                repository.TestPolymorphic.ComplexImplementationSql.Update(new[] { sx });
                Assert.AreEqual("abc1, abc2, sxx3, sxx4, sy3, sy4",
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationSql.Delete(new[] { sx });
                Assert.AreEqual("abc1, abc2, sy3, sy4",
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationData.Delete(new[] { d });
                Assert.AreEqual("sy3, sy4",
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationSql.Delete(new[] { sy });
                Assert.AreEqual("",
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));
            }
        }
Ejemplo n.º 4
0
        public void FilterSubtype()
        {
            using (var container = new RhetosTestContainer())
            {
                var repository = container.Resolve <Common.DomRepository>();
                repository.TestPolymorphic.ComplexImplementationData.Delete(repository.TestPolymorphic.ComplexImplementationData.Load());
                repository.TestPolymorphic.ComplexImplementationSql.Delete(repository.TestPolymorphic.ComplexImplementationSql.Load());

                Func <Guid, string, Guid> hash = (id, implementation) =>
                                                 DomUtility.GetSubtypeImplementationId(id,
                                                                                       DomUtility.GetSubtypeImplementationHash(implementation));

                var d = new TestPolymorphic.ComplexImplementationData {
                    a = "d", ID = Guid.NewGuid()
                };
                var sx = new TestPolymorphic.ComplexImplementationSql {
                    s = "sx", ID = Guid.NewGuid()
                };
                var sy = new TestPolymorphic.ComplexImplementationSql {
                    s = "sy", ID = Guid.NewGuid()
                };
                // Automatic update of _Materialized entity will not work if AlternativeId does not match ID hashed with implementation name.
                sx.AlternativeId = hash(sx.ID, "sql2");
                sy.AlternativeId = hash(sy.ID, "sql2");

                repository.TestPolymorphic.ComplexImplementationData.Insert(new[] { d });
                repository.TestPolymorphic.ComplexImplementationSql.Insert(new[] { sx, sy });

                Assert.AreEqual("abc1, abc2, sx3, sx4, sy3, sy4",
                                TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));

                Action <string, IEnumerable <Guid>, string, string> test =
                    (expected, ids, subtype, implementationName) => Assert.AreEqual(expected,
                                                                                    TestUtility.DumpSorted(
                                                                                        repository.TestPolymorphic.ComplexBase.Filter(new FilterSubtype {
                    Ids = ids, Subtype = subtype, ImplementationName = implementationName
                }),
                                                                                        item => item.Name1));

                // Testing filter:

                test("abc1", new[] { d.ID }, "TestPolymorphic.ComplexImplementationQuery", null);
                test("abc1", new[] { d.ID }, "TestPolymorphic.ComplexImplementationQuery", "");
                test("abc2", new[] { hash(d.ID, "q2") }, "TestPolymorphic.ComplexImplementationQuery", "q2");
                test("", new[] { d.ID }, "TestPolymorphic.ComplexImplementationSql", null);
                test("sx3", new[] { sx.ID }, "TestPolymorphic.ComplexImplementationSql", null);
                test("sx4", new[] { sx.AlternativeId.Value }, "TestPolymorphic.ComplexImplementationSql", "sql2");

                TestUtility.ShouldFail <Rhetos.ClientException>(() => repository.TestPolymorphic.ComplexBase.Filter(new FilterSubtype {
                    Ids     = new Guid[] {},
                    Subtype = "nonexisting", ImplementationName = ""
                }), "nonexisting");

                // Testing update of materialized data (it uses the Subtype filter):

                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                sx.s = "sxx";
                repository.TestPolymorphic.ComplexImplementationSql.Update(new[] { sx });
                Assert.AreEqual("abc1, abc2, sxx3, sxx4, sy3, sy4",
                                TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationSql.Delete(new[] { sx });
                Assert.AreEqual("abc1, abc2, sy3, sy4",
                                TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationData.Delete(new[] { d });
                Assert.AreEqual("sy3, sy4",
                                TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));

                repository.TestPolymorphic.ComplexImplementationSql.Delete(new[] { sy });
                Assert.AreEqual("",
                                TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.Name1));
                Assert.AreEqual(
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase.Query(), item => item.ID),
                    TestUtility.DumpSorted(repository.TestPolymorphic.ComplexBase_Materialized.Query(), item => item.ID));
            }
        }