示例#1
0
        public void VirtualPropertiesTest()
        {
            var table  = SessionExtensions.GetTable <InheritedEntity>(null);
            var query1 = table.Where(e => e.Id == 10);

            Assert.AreEqual("SELECT \"Id\", \"Description\", \"Name\" FROM \"InheritedEntity\" WHERE \"Id\" = ?", query1.ToString());
            var query2 = (from e in table where e.Id == 1 && e.Name == "MyName" select new { e.Id, e.Name, e.Description });

            Assert.AreEqual("SELECT \"Id\", \"Name\", \"Description\" FROM \"InheritedEntity\" WHERE \"Id\" = ? AND \"Name\" = ?", query2.ToString());
            var sessionMock = new Mock <ISession>(MockBehavior.Strict);
            var config      = new Configuration();
            var metadata    = new Metadata(config);
            var ccMock      = new Mock <IMetadataQueryProvider>(MockBehavior.Strict);

            ccMock.Setup(cc => cc.Serializer).Returns(new Serializer(4));
            metadata.ControlConnection = ccMock.Object;
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.Metadata).Returns(metadata);
            sessionMock.Setup(s => s.Cluster).Returns(clusterMock.Object);
            string createQuery = null;

            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(q => createQuery = q)
            .Verifiable();
            var table2 = SessionExtensions.GetTable <InheritedEntity>(sessionMock.Object);

            table2.CreateIfNotExists();
            Assert.AreEqual("CREATE TABLE \"InheritedEntity\" (\"Id\" int, \"Description\" text, \"Name\" text, PRIMARY KEY (\"Id\"))", createQuery);
        }
示例#2
0
        public void CreateTableCounterTest()
        {
            var actualCqlQueries = new List <string>();
            var sessionMock      = new Mock <ISession>(MockBehavior.Strict);
            var config           = new Configuration();
            var metadata         = new Metadata(config);
            var ccMock           = new Mock <IMetadataQueryProvider>(MockBehavior.Strict);

            ccMock.Setup(cc => cc.Serializer).Returns(new Serializer(4));
            metadata.ControlConnection = ccMock.Object;
            var clusterMock = new Mock <ICluster>();

            clusterMock.Setup(c => c.Metadata).Returns(metadata);
            sessionMock.Setup(s => s.Cluster).Returns(clusterMock.Object);
            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(actualCqlQueries.Add)
            .Verifiable();

            var session = sessionMock.Object;
            var table1  = SessionExtensions.GetTable <CounterTestTable1>(session);

            table1.CreateIfNotExists();

            var table2 = SessionExtensions.GetTable <CounterTestTable2>(session);

            table2.CreateIfNotExists();

            sessionMock.Verify();
            Assert.Greater(actualCqlQueries.Count, 0);
            Assert.AreEqual("CREATE TABLE \"CounterTestTable1\" (\"RowKey1\" int, \"RowKey2\" int, \"Value\" counter, PRIMARY KEY (\"RowKey1\", \"RowKey2\"))", actualCqlQueries[0]);
            Assert.AreEqual("CREATE TABLE \"CounterTestTable2\" (\"RowKey1\" int, \"RowKey2\" int, \"CKey1\" int, \"Value\" counter, PRIMARY KEY ((\"RowKey1\", \"RowKey2\"), \"CKey1\"))", actualCqlQueries[1]);
        }
示例#3
0
        public void LinqGeneratedUpdateStatementForCounterTest()
        {
            var table = SessionExtensions.GetTable <CounterTestTable1>(null);
            var query = table
                        .Where(r => r.RowKey1 == 5 && r.RowKey2 == 6)
                        .Select(r => new CounterTestTable1()
            {
                Value = 2
            })
                        .Update()
                        .ToString();

            Assert.AreEqual("UPDATE \"CounterTestTable1\" SET \"Value\" = \"Value\" + ? WHERE \"RowKey1\" = ? AND \"RowKey2\" = ?", query);
            int x = 4;

            query = table
                    .Where(r => r.RowKey1 == 5 && r.RowKey2 == 6)
                    .Select(r => new CounterTestTable1()
            {
                Value = x
            })
                    .Update()
                    .ToString();
            Assert.AreEqual("UPDATE \"CounterTestTable1\" SET \"Value\" = \"Value\" + ? WHERE \"RowKey1\" = ? AND \"RowKey2\" = ?", query);
        }
        public void Deprecated_EntryPoint_Uses_Keyspace_Provided()
        {
            MappingConfiguration.Global.Define(new Map <AllTypesEntity>().TableName("tbl1"));
            var table = SessionExtensions.GetTable <AllTypesEntity>(null, "linqTable", "linqKs");

            Assert.AreEqual(@"SELECT * FROM linqKs.linqTable", table.ToString());
        }
示例#5
0
        public void Select_One_Columns()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(null);
            var query = table.Select(t => t.f1);

            Assert.AreEqual(@"SELECT ""x_f1"" FROM ""x_t"" ALLOW FILTERING", query.ToString());
        }
        public void TestCqlNullValuesLinqSupport()
        {
            var table = SessionExtensions.GetTable <TestTable>(null);

            Assert.AreEqual(
                (table.Insert(new TestTable()
            {
                ck1 = null, ck2 = 2, f1 = 3, pk = "x"
            })).ToString(),
                @"INSERT INTO ""x_t""(""x_pk"", ""x_ck2"", ""x_f1"") VALUES ('x', 2, 3)");

            Assert.AreEqual((from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new { f1 = 1223 }).Update().ToString(),
                            @"UPDATE ""x_t"" SET ""x_f1"" = 1223 WHERE ""x_ck1"" IN (10, 30, 40)");

            Assert.AreEqual((from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new TestTable()
            {
                f1 = 1223, ck1 = null
            }).Update().ToString(),
                            @"UPDATE ""x_t"" SET ""x_f1"" = 1223, ""x_ck1"" = NULL WHERE ""x_ck1"" IN (10, 30, 40)");

            Assert.AreEqual((from ent in table where ent.ck1 == 1 select new TestTable()
            {
                f1 = 1223, ck1 = null
            }).Update().ToString(),
                            @"UPDATE ""x_t"" SET ""x_f1"" = 1223, ""x_ck1"" = NULL WHERE ""x_ck1"" = 1");

            Assert.AreEqual((from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new { f1 = 1223, ck1 = (int?)null }).UpdateIf((a) => a.f1 == 123).ToString(),
                            @"UPDATE ""x_t"" SET ""x_f1"" = 1223, ""x_ck1"" = NULL WHERE ""x_ck1"" IN (10, 30, 40) IF ""x_f1"" = 123");
        }
示例#7
0
        public void LinqGeneratedUpdateStatementTest()
        {
            var    table = SessionExtensions.GetTable <AllTypesEntity>(null);
            string query;
            string expectedQuery;

            query = table
                    .Where(r => r.StringValue == "key")
                    .Select(r => new AllTypesEntity()
            {
                IntValue = 1
            })
                    .Update()
                    .ToString();
            expectedQuery = "UPDATE \"AllTypesEntity\" SET \"IntValue\" = ? WHERE \"StringValue\" = ?";
            Assert.AreEqual(expectedQuery, query);

            Assert.Throws <CqlArgumentException>(() =>
            {
                //Update without a set statement
                //Must include SELECT to project to a new form
                query = table
                        .Where(r => r.StringValue == "key")
                        .Update()
                        .ToString();
            });
        }
        public void Deprecated_EntryPoint_Honors_Mapping_Defined()
        {
            MappingConfiguration.Global.Define(new Map <AllTypesEntity>().TableName("tbl1"));
            var table = SessionExtensions.GetTable <AllTypesEntity>(null);

            Assert.AreEqual(@"SELECT * FROM tbl1", table.ToString());
        }
示例#9
0
        public void Select_OrderBy_Columns()
        {
            var table = SessionExtensions.GetTable <AllTypesEntity>(null);
            var query = table.OrderBy(t => t.UuidValue).OrderByDescending(t => t.DateTimeValue);

            Assert.AreEqual(@"SELECT ""BooleanValue"", ""DateTimeValue"", ""DecimalValue"", ""DoubleValue"", ""Int64Value"", ""IntValue"", ""StringValue"", ""UuidValue"" FROM ""AllTypesEntity"" ORDER BY ""UuidValue"", ""DateTimeValue"" DESC", query.ToString());
        }
示例#10
0
        public void TestCqlNullValuesLinqSupport()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(null);

            Assert.AreEqual(
                @"INSERT INTO ""x_t"" (""x_pk"", ""x_ck1"", ""x_ck2"", ""x_f1"") VALUES (?, ?, ?, ?)",
                (table.Insert(new LinqDecoratedEntity()
            {
                ck1 = null, ck2 = 2, f1 = 3, pk = "x"
            })).ToString());

            Assert.AreEqual(
                @"UPDATE ""x_t"" SET ""x_f1"" = ? WHERE ""x_ck1"" IN (?, ?, ?)",
                (from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new { f1 = 1223 }).Update().ToString());

            Assert.AreEqual(
                @"UPDATE ""x_t"" SET ""x_f1"" = ?, ""x_ck1"" = ? WHERE ""x_ck1"" IN (?, ?, ?)",
                (from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new LinqDecoratedEntity()
            {
                f1 = 1223, ck1 = null
            }).Update().ToString());

            Assert.AreEqual(
                @"UPDATE ""x_t"" SET ""x_f1"" = ?, ""x_ck1"" = ? WHERE ""x_ck1"" = ?",
                (from ent in table where ent.ck1 == 1 select new LinqDecoratedEntity()
            {
                f1 = 1223, ck1 = null
            }).Update().ToString());

            Assert.AreEqual(
                @"UPDATE ""x_t"" SET ""x_f1"" = ?, ""x_ck1"" = ? WHERE ""x_ck1"" IN (?, ?, ?) IF ""x_f1"" = ?",
                (from ent in table where new int?[] { 10, 30, 40 }.Contains(ent.ck1) select new { f1 = 1223, ck1 = (int?)null }).UpdateIf((a) => a.f1 == 123).ToString());
        }
示例#11
0
        public void CreateTableCounterTest()
        {
            var actualCqlQueries = new List <string>();
            var sessionMock      = new Mock <ISession>();

            sessionMock
            .Setup(s => s.Execute(It.IsAny <string>()))
            .Returns(() => new RowSet())
            .Callback <string>(actualCqlQueries.Add)
            .Verifiable();

            var session = sessionMock.Object;
            var table1  = SessionExtensions.GetTable <CounterTestTable1>(session);

            table1.CreateIfNotExists();

            var table2 = SessionExtensions.GetTable <CounterTestTable2>(session);

            table2.CreateIfNotExists();

            sessionMock.Verify();
            Assert.Greater(actualCqlQueries.Count, 0);
            Assert.AreEqual("CREATE TABLE \"CounterTestTable1\" (\"RowKey1\" int, \"RowKey2\" int, \"Value\" counter, PRIMARY KEY (\"RowKey1\", \"RowKey2\"))", actualCqlQueries[0]);
            Assert.AreEqual("CREATE TABLE \"CounterTestTable2\" (\"RowKey1\" int, \"RowKey2\" int, \"CKey1\" int, \"Value\" counter, PRIMARY KEY ((\"RowKey1\", \"RowKey2\"), \"CKey1\"))", actualCqlQueries[1]);
        }
        public void Deprecated_EntryPoint_Defaults_To_LinqBasedAttributes()
        {
            var table = SessionExtensions.GetTable <AllTypesEntity>(null);

            Assert.AreEqual(
                @"SELECT ""BooleanValue"", ""DateTimeValue"", ""DecimalValue"", ""DoubleValue"", ""Int64Value"", ""IntValue"", ""StringValue"", ""UuidValue"" FROM ""AllTypesEntity""",
                table.ToString());
        }
示例#13
0
        public void Select_Where_Contains()
        {
            var table = SessionExtensions.GetTable <AllTypesDecorated>(null);
            var ids   = new [] { 1, 2, 3 };
            var query = table.Where(t => ids.Contains(t.IntValue) && t.Int64Value == 10);

            Assert.AreEqual(@"SELECT * FROM ""atd"" WHERE ""int_VALUE"" IN (?, ?, ?) AND ""int64_VALUE"" = ?", query.ToString());
        }
示例#14
0
        public void EmptyListTest()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(GetSession((_, __) => {}));
            var keys  = new string[0];
            var query = table.Where(item => keys.Contains(item.pk));

            Assert.True(query.ToString().Contains("\"x_pk\" IN ?"), "The query must contain an empty IN statement");
        }
        public void EmptyListTest()
        {
            var table = SessionExtensions.GetTable <TestTable>(null);
            var keys  = new string[0];
            var query = table.Where(item => keys.Contains(item.pk));

            Assert.True(query.ToString().Contains("\"x_pk\" IN ()"), "The query must contain an empty IN statement");
        }
示例#16
0
        public void Select_Count()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(GetSession((_, __) => {}));

            Assert.AreEqual(
                (from ent in table select ent).Count().ToString(),
                @"SELECT count(*) FROM ""x_t"" ALLOW FILTERING");
        }
示例#17
0
        public void Select_Count()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(null);

            Assert.AreEqual(
                (from ent in table select ent).Count().ToString(),
                @"SELECT count(*) FROM ""x_t""");
        }
        public void Deprecated_EntryPoint_Honors_Mapping_Defined()
        {
            MappingConfiguration.Global.Define(new Map <AllTypesEntity>().TableName("tbl1"));
            var table = SessionExtensions.GetTable <AllTypesEntity>(null);

            Assert.AreEqual(
                @"SELECT BooleanValue, DateTimeValue, DecimalValue, DoubleValue, Int64Value, IntValue, StringValue, UuidValue FROM tbl1",
                table.ToString());
        }
示例#19
0
        public void Select_Specific_Columns()
        {
            var table = SessionExtensions.GetTable <LinqDecoratedEntity>(GetSession((_, __) => {}));
            var query = table.Select(t => new LinqDecoratedEntity {
                f1 = t.f1, pk = t.pk
            });

            Assert.AreEqual(@"SELECT ""x_f1"", ""x_pk"" FROM ""x_t"" ALLOW FILTERING", query.ToString());
        }
        public void Deprecated_EntryPoint_Uses_Keyspace_Provided()
        {
            MappingConfiguration.Global.Define(new Map <AllTypesEntity>().TableName("tbl1"));
            var table = SessionExtensions.GetTable <AllTypesEntity>(null, "linqTable", "linqKs");

            Assert.AreEqual(
                @"SELECT BooleanValue, DateTimeValue, DecimalValue, DoubleValue, Int64Value, IntValue, StringValue, UuidValue FROM linqKs.linqTable",
                table.ToString());
        }
        public void SelectGenericTypeTest()
        {
            var table = SessionExtensions.GetTable <TestTable>(null);
            var query = table
                        .Where(row => row.pk == "value")
                        .Select(row => new TestTableResult2(row.ck1, row.f1));

            var cql = query.ToString();

            Assert.True(cql.StartsWith("SELECT \"x_ck1\", \"x_f1\" FROM"));
        }
        public void SelectAnonymousTypeTest()
        {
            var table = SessionExtensions.GetTable <TestTable>(null);
            var query = table
                        .Where(row => row.pk == "value")
                        .Select(row => new { row.f1, TheCk1 = row.ck1 });

            var cql = query.ToString();

            Assert.True(cql.StartsWith("SELECT \"x_f1\", \"x_ck1\" FROM"));
        }
示例#23
0
        public void DeleteIf_With_Where_Clause()
        {
            var table = SessionExtensions.GetTable <AllTypesDecorated>(GetSession((_, __) => {}));
            var query = table
                        .Where(t => t.Int64Value == 1)
                        .DeleteIf(t => t.StringValue == "conditional!");

            Assert.AreEqual(
                @"DELETE FROM ""atd"" WHERE ""int64_VALUE"" = ? IF ""string_VALUE"" = ?",
                query.ToString());
            Trace.TraceInformation(query.ToString());
        }
示例#24
0
        public void Select_Where_Contains()
        {
            var table = SessionExtensions.GetTable <AllTypesDecorated>(GetSession((_, __) => {}));
            var ids   = new [] { 1, 2, 3 };
            var query = table.Where(t => ids.Contains(t.IntValue) && t.Int64Value == 10);

            Assert.AreEqual(
                "SELECT \"boolean_VALUE\", \"datetime_VALUE\", \"decimal_VALUE\", \"double_VALUE\", \"int_VALUE\"," +
                " \"int64_VALUE\", \"string_VALUE\", \"timeuuid_VALUE\", \"uuid_VALUE\" FROM \"atd\" WHERE" +
                " \"int_VALUE\" IN ? AND \"int64_VALUE\" = ?",
                query.ToString());
        }
        public void AllowFilteringTest()
        {
            var table = SessionExtensions.GetTable <AllowFilteringTestTable>(null);

            var cqlQuery = table
                           .Where(item => item.ClusteringKey == "x" && item.Value == 1M)
                           .AllowFiltering();

            Assert.That(cqlQuery, Is.Not.Null);
            Assert.That(cqlQuery.ToString(), Is.StringEnding("ALLOW FILTERING"));
            Console.WriteLine(cqlQuery.ToString());
        }
示例#26
0
        public void AllowFilteringTest()
        {
            var table    = SessionExtensions.GetTable <AllowFilteringTestTable>(GetSession((_, __) => {}));
            var key1     = "x";
            var cqlQuery = table
                           .Where(item => item.ClusteringKey == key1 && item.Value == 1M)
                           .AllowFiltering();

            Assert.That(cqlQuery, Is.Not.Null);
            StringAssert.EndsWith("ALLOW FILTERING", cqlQuery.ToString());
            Trace.WriteLine(cqlQuery.ToString());
        }
        public void InsertNullTest()
        {
            var table = SessionExtensions.GetTable <InsertNullTable>(null);
            var row   = new InsertNullTable()
            {
                Key = 1, Value = null
            };

            var cqlInsert = table.Insert(row);
            var cql       = cqlInsert.ToString();

            Assert.That(cql, Is.EqualTo("INSERT INTO \"InsertNullTable\"(\"Key\", \"Value\") VALUES (1, null)"));
        }
示例#28
0
        public void Insert_IfNotExists_Test()
        {
            var table = SessionExtensions.GetTable <AllTypesDecorated>(GetSession((_, __) => {}));
            var uuid  = Guid.NewGuid();
            var row   = new AllTypesDecorated {
                Int64Value = 202, UuidValue = uuid
            };

            var cqlInsert = table.Insert(row).IfNotExists();
            var cql       = cqlInsert.GetCql(out object[] values);

            StringAssert.EndsWith("IF NOT EXISTS", cql);
        }
        public void LinqGeneratedUpdateStatementForCounterTest()
        {
            var    table = SessionExtensions.GetTable <CounterTestTable1>(null);
            string query;
            string expectedQuery;

            query = table
                    .Where(r => r.RowKey1 == 5 && r.RowKey2 == 6)
                    .Select(r => new CounterTestTable1()
            {
                Value = 1
            })
                    .Update()
                    .ToString();
            expectedQuery = "UPDATE \"CounterTestTable1\" SET \"Value\" = \"Value\" + 1 WHERE \"RowKey1\" = 5 AND \"RowKey2\" = 6";
            Assert.AreEqual(expectedQuery, query);
        }
示例#30
0
        public void Linq_Counter_Batch_Test()
        {
            //TODO: Write actual counter statements
            var table = SessionExtensions.GetTable <CounterTestTable1>(GetSession((_, __) => {}));
            var batch = SessionExtensions.CreateBatch(GetSession((_, __) => {}), BatchType.Counter);

            batch.Append(table.Where(ent => ent.RowKey1 == 1 && ent.RowKey2 == 2).Select(_ => new CounterTestTable1 {
                Value = 1
            }).Update());
            batch.Append(table.Where(ent => ent.RowKey1 == 3 && ent.RowKey2 == 4).Select(_ => new CounterTestTable1 {
                Value = 1
            }).Update());
            Assert.AreEqual(batch.ToString().Replace("\r", ""),
                            @"BEGIN COUNTER BATCH
UPDATE ""CounterTestTable1"" SET ""Value"" = ""Value"" + ? WHERE ""RowKey1"" = ? AND ""RowKey2"" = ?;
UPDATE ""CounterTestTable1"" SET ""Value"" = ""Value"" + ? WHERE ""RowKey1"" = ? AND ""RowKey2"" = ?;
APPLY BATCH".Replace("\r", ""));
        }