Exemplo n.º 1
0
        public void Test([IncludeDataSources(ProviderName.SQLite, ProviderName.SQLiteClassic, ProviderName.SQLiteMS)] string context)
        {
            var typeId = Guid.NewGuid();

            var resources = new[] { new WmsResourceTypeDTO {
                                        Depth = 256, Height = 110, Id = typeId, Name = "Resource Name", ShortName = "RN", Width = 333
                                    } };

            var carriersA = new[] { new WmsLoadCarrierDTO {
                                        Id = Guid.NewGuid(), TypeID = typeId
                                    } };
            var carriersB = new[] { new WmsLoadCarrierDTO {
                                        Id = Guid.NewGuid(), TypeID = typeId
                                    } };

            using (var db = GetDataContext(context))
                using (db.CreateLocalTable <WmsResourceTypeDTO>(resources))
                    using (db.CreateLocalTable <WmsLoadCarrierDTO>(carriersA))
                        using (db.CreateLocalTable <WmsLoadCarrierDTO>("WMS_ResourceA", carriersB))
                        {
                            var qryUnion = from res in db.GetTable <WmsLoadCarrierDTO>().Union(db.GetTable <WmsLoadCarrierDTO>().TableName("WMS_ResourceA"))
                                           join type in db.GetTable <WmsResourceTypeDTO>() on res.TypeID equals type.Id into tpList
                                           from tp in tpList.DefaultIfEmpty()
                                           select new WmsResourceCombinedDTO {
                                LoadCarrier = res, ResourceType = tp
                            };

                            var staticResult = from res in carriersA.Union(carriersB)
                                               join type in resources on res.TypeID equals type.Id into tpList
                                               from tp in tpList.DefaultIfEmpty()
                                               select new WmsResourceCombinedDTO {
                                LoadCarrier = res, ResourceType = tp
                            };

                            var actual   = qryUnion.ToArray();
                            var expected = staticResult.ToArray();

                            AreEqual(expected, actual, ComparerBuilder <WmsResourceCombinedDTO> .GetEqualityComparer());
                        }
        }
Exemplo n.º 2
0
        public void TestConcatInheritance([IncludeDataSources(ProviderName.SQLiteClassic)] string context)
        {
            var testData = new BaseEntity[]
            {
                new BaseEntity {
                    Discr = 0, EntityId = 1, Value = "VBase1"
                },
                new BaseEntity {
                    Discr = 0, EntityId = 2, Value = "VBase2"
                },
                new BaseEntity {
                    Discr = 0, EntityId = 3, Value = "VBase3"
                },

                new DerivedEntity {
                    Discr = 1, EntityId = 10, Value = "Derived1"
                },
                new DerivedEntity {
                    Discr = 1, EntityId = 20, Value = "Derived2"
                },
                new DerivedEntity {
                    Discr = 1, EntityId = 30, Value = "Derived3"
                }
            };

            using (var db = GetDataContext(context))
                using (db.CreateLocalTable(testData))
                {
                    var result = db.GetTable <BaseEntity>().OfType <BaseEntity>()
                                 .Concat(db.GetTable <BaseEntity>().OfType <DerivedEntity>())
                                 .ToArray();

                    var expected = testData.Where(t => t.GetType() == typeof(BaseEntity))
                                   .Concat(testData.OfType <DerivedEntity>())
                                   .ToArray();

                    AreEqual(expected, result, ComparerBuilder <BaseEntity> .GetEqualityComparer());
                }
        }
Exemplo n.º 3
0
        public void DeleteWithOutputTest([IncludeDataSources(true, TestProvName.AllSqlServer2008Plus)] string context, [Values(100, 200)] int param)
        {
            var sourceData = GetSourceData();

            using (var db = GetDataContext(context))
                using (var source = db.CreateLocalTable(sourceData))
                {
                    var expected = source
                                   .Where(s => s.Id > 3)
                                   .ToArray();

                    var output = source
                                 .Where(s => s.Id > 3)
                                 .DeleteWithOutput()
                                 .ToArray();

                    AreEqual(
                        expected,
                        output,
                        ComparerBuilder.GetEqualityComparer <TableWithData>());
                }
        }
Exemplo n.º 4
0
        public void InvocationTestByInvoke([SQLiteDataSources] string context, [Values(1, 2)] int param)
        {
            var sampleData = GenerateData();

            Expression <Func <int, int, int> > func = (p1, p2) => p1 * 10 + p2 * 2;

            using (var db = GetDataContext(context))
                using (var table = db.CreateLocalTable(sampleData))
                {
                    var query = table.AsQueryable();

                    query.Set(q => q.Value, q => func.Compile()(param, q.Value)).Update();

                    var compiled = func.Compile();
                    foreach (var sd in sampleData)
                    {
                        sd.Value = compiled(param, sd.Value);
                    }

                    AreEqual(sampleData, query, ComparerBuilder <SampleClass> .GetEqualityComparer());
                }
        }
Exemplo n.º 5
0
        public void DeleteWithOutputTestSingleRecord([IncludeDataSources(true, FeatureDeleteOutputSingle)] string context, [Values(100, 200)] int param)
        {
            var sourceData = GetSourceData();

            using (var db = GetDataContext(context))
                using (var source = db.CreateLocalTable(sourceData))
                {
                    var expected = source
                                   .Where(s => s.Id == 3)
                                   .ToArray();

                    var output = source
                                 .Where(s => s.Id == 3)
                                 .DeleteWithOutput()
                                 .ToArray();

                    AreEqual(
                        expected,
                        output,
                        ComparerBuilder.GetEqualityComparer <TableWithData>());
                }
        }
Exemplo n.º 6
0
        public void After_clean_custom_comparer_for_value_type_dynamic_comparer_should_be_created()
        {
            var x = _fixture.Create <ComparableObject <ComparableStruct <string> > >();
            var y = _fixture.Create <ComparableObject <ComparableStruct <string> > >();

            var reference          = new ComparableObjectEqualityComparer <ComparableStruct <string> >(new ComparableStructEqualityComparer <string>());
            var expectedEquals     = reference.Equals(x, y);
            var expectedHash       = reference.GetHashCode(x);
            var expectedCustomHash = HashCodeCombiner.Combine(0, 0);

            var builder         = new ComparerBuilder(c => c.SetCustomEqualityComparer <SampleStructCustomEqualityComparer>());
            var comparerCustom  = builder.GetEqualityComparer <ComparableObject <ComparableStruct <string> > >();
            var comparerDefault =
                builder.Configure(c => c.SetCustomEqualityComparer <ComparableStruct <string> >(null))
                .GetEqualityComparer <ComparableObject <ComparableStruct <string> > >();

            using (new AssertionScope()) {
                comparerCustom.Equals(x, y).Should().BeTrue();
                comparerCustom.GetHashCode(x).Should().Be(expectedCustomHash);
                comparerDefault.Equals(x, y).Should().Be(expectedEquals);
                comparerDefault.GetHashCode(x).Should().Be(expectedHash);
            }
        }
Exemplo n.º 7
0
        public void TestUnnestFunction(
            // `with ordinality` added to pgsql 9.4
            [IncludeDataSources(true, TestProvName.AllPostgreSQL95Plus)]
            string context)
        {
            using (var db = GetDataContext(context))
            {
                var query =
                    from c in db.SelectQuery(() => Sql.Expr <int[]>("ARRAY[10,20]::int[]"))
                    from s in UnnestWithOrdinality(db, c)
                    select s;
                var result   = query.ToArray();
                var expected = new[]
                {
                    new UnnestEnvelope <int> {
                        Value = 10, Index = 1
                    }, new UnnestEnvelope <int> {
                        Value = 20, Index = 2
                    }
                };

                AreEqual(expected, result, ComparerBuilder.GetEqualityComparer <UnnestEnvelope <int> >());
            }
        }
Exemplo n.º 8
0
        public void TestIntersectAll([DataSources] string context)
        {
            var testData = GenerateTestData();

            using (var db = GetDataContext(context))
                using (var table = db.CreateLocalTable(testData))
                {
                    var rs1 = table.Where(t => t.Id % 2 == 0);
                    rs1 = rs1.Concat(rs1);
                    var rs2   = table.Where(t => t.Id % 4 == 0);
                    var query = rs1.IntersectAll(rs2);

                    var e1 = testData.Where(t => t.Id % 2 == 0);
                    e1 = e1.Concat(e1);
                    var e2       = testData.Where(t => t.Id % 4 == 0);
                    var expected = e1.Where(e => e2.Contains(e, ComparerBuilder.GetEqualityComparer <SampleData>())).ToArray();
                    var actual   = query.ToArray();

                    if (!context.Contains(ProviderName.PostgreSQL))             // postgres has a bug?
                    {
                        AreEqual(expected, actual, ComparerBuilder.GetEqualityComparer <SampleData>());
                    }
                }
        }
Exemplo n.º 9
0
 protected void AreEqualWithComparer <T>(IEnumerable <T> expected, IEnumerable <T> result, Func <MemberAccessor, bool> memberPredicate)
 {
     AreEqual(t => t, expected, result, ComparerBuilder.GetEqualityComparer <T>(memberPredicate));
 }
Exemplo n.º 10
0
 protected void AreEqualWithComparer <T>(IEnumerable <T> expected, IEnumerable <T> result)
 {
     AreEqual(t => t, expected, result, ComparerBuilder.GetEqualityComparer <T>());
 }
Exemplo n.º 11
0
 public IEqualityComparer <T> GetEqualityComparer() => _subject.GetEqualityComparer <T>();
Exemplo n.º 12
0
        public void GuidMappingTest([IncludeDataSources(TestProvName.AllSQLite)] string context)
        {
            using var db = GetDataConnection(context);
            var interceptor = new SaveCommandInterceptor();

            db.AddInterceptor(interceptor);

            using var table = db.CreateLocalTable <GuidMapping>();

            var data = new GuidMapping[]
            {
                new ()
                {
                    BlobGuid1 = TestData.Guid1,
                    BlobGuid2 = TestData.Guid1,
                    BlobGuid3 = TestData.Guid1,
                    BlobGuid4 = TestData.Guid1,
                    TextGuid1 = TestData.Guid1,
                    TextGuid2 = TestData.Guid1,
                }
            };

            var comparer = ComparerBuilder.GetEqualityComparer <GuidMapping>();

            // test literal passed with proper type
            db.InlineParameters = true;
            table.Insert(() => new() { BlobGuid1 = TestData.Guid1 });
            TestWrite("BlobGuid1", "blob", true);
            table.Insert(() => new() { BlobGuid2 = TestData.Guid1 });
            TestWrite("BlobGuid2", "blob", true);
            table.Insert(() => new() { BlobGuid3 = TestData.Guid1 });
            TestWrite("BlobGuid3", "blob", true);
            table.Insert(() => new() { BlobGuid4 = TestData.Guid1 });
            TestWrite("BlobGuid4", "blob", true);
            table.Insert(() => new() { TextGuid1 = TestData.Guid1 });
            TestWrite("TextGuid1", "text", true);
            table.Insert(() => new() { TextGuid2 = TestData.Guid1 });
            TestWrite("TextGuid2", "text", true);

            // test parameter passed with proper type
            db.InlineParameters = false;
            table.Insert(() => new() { BlobGuid1 = TestData.Guid1 });
            TestWrite("BlobGuid1", "blob", false);
            table.Insert(() => new() { BlobGuid2 = TestData.Guid1 });
            TestWrite("BlobGuid2", "blob", false);
            table.Insert(() => new() { BlobGuid3 = TestData.Guid1 });
            TestWrite("BlobGuid3", "blob", false);
            table.Insert(() => new() { BlobGuid4 = TestData.Guid1 });
            TestWrite("BlobGuid4", "blob", false);
            table.Insert(() => new() { TextGuid1 = TestData.Guid1 });
            TestWrite("TextGuid1", "text", false);
            table.Insert(() => new() { TextGuid2 = TestData.Guid1 });
            TestWrite("TextGuid2", "text", false);

            // test bulk copy roundtrip
            table.BulkCopy(data);
            var result = table.ToArray();

            AreEqual(data, result, comparer);
            table.Delete();

            // test insert literals roundtrip
            db.InlineParameters = true;
            db.Insert(data[0]);
            Assert.AreEqual(0, interceptor.Parameters.Length);
            result = table.ToArray();
            AreEqual(data, result, comparer);
            table.Delete();

            // test insert parameters roundtrip
            db.InlineParameters = false;
            db.Insert(data[0]);
            Assert.AreEqual(6, interceptor.Parameters.Length);
            result = table.ToArray();
            AreEqual(data, result, comparer);
            table.Delete();

            // test mixed values read
            var value = $"'{TestData.Guid1.ToString().ToUpperInvariant()}'";

            db.Execute($"INSERT INTO GuidMapping(BlobGuid1, BlobGuid2, BlobGuid3, BlobGuid4, TextGuid1, TextGuid2) VALUES({value}, {value}, {value}, {value}, {value}, {value})");
            value = $"x'{string.Join(string.Empty, TestData.Guid1.ToByteArray().Select(x => $"{x:X2}"))}'";
            db.Execute($"INSERT INTO GuidMapping(BlobGuid1, BlobGuid2, BlobGuid3, BlobGuid4, TextGuid1, TextGuid2) VALUES({value}, {value}, {value}, {value}, {value}, {value})");

            result = table.ToArray();
            AreEqual(data, new[] { result[0] }, comparer);
            AreEqual(data, new[] { result[1] }, comparer);

            void TestWrite(string FieldName, string expectedType, bool inline)
            {
                Assert.AreEqual(inline ? 0 : 1, interceptor.Parameters.Length);
                var type = db.Execute <string>($"SELECT typeof({FieldName}) FROM GuidMapping");

                Assert.AreEqual(expectedType, type);

                // assert literal is uppercased (M.D.SQLite format)
                if (expectedType == "text")
                {
                    Assert.AreEqual(TestData.Guid1.ToString().ToUpperInvariant(), db.Execute <string>($"SELECT {FieldName} FROM GuidMapping"));
                }

                table.Delete();
            }
        }
    }