Exemplo n.º 1
0
        public void Attributes_NonPartitionKeyFieldOmittedFromPocoClass()
        {
            string tableName      = typeof(PocoWithPartitionKeyIncluded).Name.ToLower();
            string selectAllCql   = "SELECT * from " + tableName;
            string createTableCql = "Create table " + tableName + "(somestring text PRIMARY KEY, somelist list<text>, somelist2 list<text>, somedouble double)";

            _session.Execute(createTableCql);
            var cqlClient = GetMapper();

            // insert new record
            PocoWithPartitionKeyIncluded pocoWithCustomAttributesKeyIncluded = new PocoWithPartitionKeyIncluded();

            cqlClient.Insert(pocoWithCustomAttributesKeyIncluded);

            // Get records using mapped object, validate that the value from Cassandra was ignored in favor of the default val
            List <PocoWithPartitionKeyIncluded> records = cqlClient.Fetch <PocoWithPartitionKeyIncluded>(selectAllCql).ToList();

            Assert.AreEqual(1, records.Count);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeString, records[0].SomeString);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeDouble, records[0].SomeDouble);

            // Query for the column that the Linq table create created, verify no value was uploaded to it
            List <Row> rows = _session.Execute(selectAllCql).GetRows().ToList();

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeString, rows[0].GetValue <string>("somestring"));
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeDouble, rows[0].GetValue <double>("somedouble"));
        }
Exemplo n.º 2
0
        public void Attributes_InsertFailsWhenPartitionKeyAttributeOmitted_FixedWithMapping()
        {
            // Setup
            string        tableName    = typeof(PocoWithPartitionKeyOmitted).Name.ToLower();
            string        selectAllCql = "SELECT * from " + tableName;
            List <string> stringList   = new List <string>()
            {
                "string1", "string2"
            };
            string createTableCql = "Create table " + tableName + "(somestring text PRIMARY KEY, somelist list<varchar>, somedouble double)";

            _session.Execute(createTableCql);

            // Instantiate CqlClient with mapping rule that resolves the missing key issue
            var cqlClientWithMappping = new Mapper(_session, new MappingConfiguration().Define(new PocoWithPartitionKeyIncludedMapping()));
            // insert new record
            PocoWithPartitionKeyIncluded pocoWithCustomAttributesKeyIncluded = new PocoWithPartitionKeyIncluded();

            pocoWithCustomAttributesKeyIncluded.SomeList = stringList; // make it not empty
            cqlClientWithMappping.Insert(pocoWithCustomAttributesKeyIncluded);

            // Get records using mapped object, validate that the value from Cassandra was ignored in favor of the default val
            List <PocoWithPartitionKeyIncluded> records_1 = cqlClientWithMappping.Fetch <PocoWithPartitionKeyIncluded>(selectAllCql).ToList();

            Assert.AreEqual(1, records_1.Count);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeString, records_1[0].SomeString);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeList, records_1[0].SomeList);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeDouble, records_1[0].SomeDouble);
            records_1.Clear();

            // Query for the column that the Linq table create created, verify no value was uploaded to it
            List <Row> rows = _session.Execute(selectAllCql).GetRows().ToList();

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeString, rows[0].GetValue <string>("somestring"));
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeList, rows[0].GetValue <List <string> >("somelist"));
            Assert.AreEqual(pocoWithCustomAttributesKeyIncluded.SomeDouble, rows[0].GetValue <double>("somedouble"));

            // try to Select new record using poco that does not contain partition key, validate that the mapping mechanism matches what it can
            var cqlClientNomapping = GetMapper();
            List <PocoWithPartitionKeyOmitted> records_2 = cqlClientNomapping.Fetch <PocoWithPartitionKeyOmitted>(selectAllCql).ToList();

            Assert.AreEqual(1, records_2.Count);
            records_2.Clear();

            // try again with the old CqlClient instance
            records_1 = cqlClientWithMappping.Fetch <PocoWithPartitionKeyIncluded>(selectAllCql).ToList();
            Assert.AreEqual(1, records_1.Count);

            // Clear out the table, verify
            string truncateCql = "TRUNCATE " + tableName;

            _session.Execute(truncateCql);
            records_1 = cqlClientWithMappping.Fetch <PocoWithPartitionKeyIncluded>(selectAllCql).ToList();
            Assert.AreEqual(0, records_1.Count); // should have gone from 1 to 0 records
        }