Ejemplo n.º 1
0
        public void Attributes_Column_CustomLabels()
        {
            // Setup
            int expectedTotalRecords = 1;
            var definition           = new AttributeBasedTypeDefinition(typeof(SimplePocoWithColumnLabel_CustomColumnName));
            var table = new Table <SimplePocoWithColumnLabel_CustomColumnName>(_session, new MappingConfiguration().Define(definition));

            Assert.AreEqual(typeof(SimplePocoWithColumnLabel_CustomColumnName).Name, table.Name);                                                           // Assert table name is case sensitive now
            Assert.AreNotEqual(typeof(SimplePocoWithColumnLabel_CustomColumnName).Name, typeof(SimplePocoWithColumnLabel_CustomColumnName).Name.ToLower()); // Assert table name is case senstive
            table.Create();

            SimplePocoWithColumnLabel_CustomColumnName defaultInstance = new SimplePocoWithColumnLabel_CustomColumnName();
            var mapper = new Mapper(_session, new MappingConfiguration().Define(definition));

            mapper.Insert(defaultInstance);

            // Validate using mapped Fetch
            string cqlSelect = string.Format("SELECT * from \"{0}\" where {1}='{2}'", table.Name.ToLower(), "someCaseSensitivePartitionKey", defaultInstance.SomePartitionKey);
            List <SimplePocoWithColumnLabel_CustomColumnName> actualObjectsInOrder = mapper.Fetch <SimplePocoWithColumnLabel_CustomColumnName>(cqlSelect).ToList();

            Assert.AreEqual(expectedTotalRecords, actualObjectsInOrder.Count);
            Assert.AreEqual(defaultInstance.SomeColumn, actualObjectsInOrder[0].SomeColumn);

            // Validate using straight cql to verify column names
            List <Row> rows = _session.Execute(cqlSelect).GetRows().ToList();

            Assert.AreEqual(expectedTotalRecords, rows.Count);
            Assert.AreEqual(defaultInstance.SomeColumn, rows[0].GetValue <int>("some_column_label_thats_different"));
        }
Ejemplo n.º 2
0
        public void Attributes_Column_NoCustomLabel()
        {
            // Setup
            int expectedTotalRecords = 1;
            var definition           = new AttributeBasedTypeDefinition(typeof(SimplePocoWithColumnAttribute));
            var table = new Table <SimplePocoWithColumnAttribute>(_session, new MappingConfiguration().Define(definition));

            Assert.AreNotEqual(table.Name, table.Name.ToLower());
            table.Create();

            SimplePocoWithColumnAttribute defaultInstance = new SimplePocoWithColumnAttribute();
            var mapper = new Mapper(_session, new MappingConfiguration().Define(definition));

            mapper.Insert(defaultInstance);

            // Validate using mapped Fetch
            string cqlSelectAll = "select * from " + table.Name.ToLower();
            List <SimplePocoWithColumnAttribute> instancesQueried = mapper.Fetch <SimplePocoWithColumnAttribute>(cqlSelectAll).ToList();

            Assert.AreEqual(expectedTotalRecords, instancesQueried.Count);

            string cqlSelect = string.Format("SELECT * from \"{0}\" where {1}='{2}'", table.Name.ToLower(), "somepartitionkey", defaultInstance.SomePartitionKey);
            List <SimplePocoWithColumnAttribute> actualObjectsInOrder = mapper.Fetch <SimplePocoWithColumnAttribute>(cqlSelect).ToList();

            Assert.AreEqual(expectedTotalRecords, actualObjectsInOrder.Count);

            // Validate using straight cql to verify column names
            List <Row> rows = _session.Execute(cqlSelect).GetRows().ToList();

            Assert.AreEqual(expectedTotalRecords, rows.Count);
            Assert.AreEqual(defaultInstance.SomeColumn, rows[0].GetValue <int>("somecolumn"));
        }
Ejemplo n.º 3
0
 private PocoData CreatePocoData(Type pocoType)
 {
     // Try to get mapping from predefined collection, otherwise fallback to using attributes
     if (!_predefinedTypeDefinitions.TryGetItem(pocoType, out ITypeDefinition typeDefinition))
     {
         typeDefinition = new AttributeBasedTypeDefinition(pocoType);
     }
     return(CreatePocoData(pocoType, typeDefinition));
 }
        public void AttributeBased_Composite_PartitionKey_Test()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(DecoratedTimeSeries));

            Assert.True(definition.CaseSensitive);
            Assert.False(definition.CompactStorage);
            Assert.False(definition.AllowFiltering);
            Assert.False(definition.ExplicitColumns);
            CollectionAssert.AreEqual(new [] { Tuple.Create("Time", SortOrder.Unspecified) }, definition.ClusteringKeys);
            CollectionAssert.AreEqual(new[] { "name", "Slice" }, definition.PartitionKeys);
        }
        public void AttributeBased_Single_PartitionKey_Test()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(DecoratedUser));

            Assert.False(definition.CaseSensitive);
            Assert.False(definition.CompactStorage);
            Assert.False(definition.AllowFiltering);
            Assert.False(definition.ExplicitColumns);
            Assert.AreEqual(0, definition.ClusteringKeys.Length);
            CollectionAssert.AreEqual(new[] { "userid" }, definition.PartitionKeys);
        }
Ejemplo n.º 6
0
        public void AttributeBased_Without_Name_For_Clustering_Key_Test()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(SamplePocoWithoutClusteringKeyName));

            Assert.False(definition.CaseSensitive);
            Assert.False(definition.CompactStorage);
            Assert.False(definition.AllowFiltering);
            Assert.False(definition.ExplicitColumns);
            CollectionAssert.AreEqual(new [] { Tuple.Create("Id2", SortOrder.Unspecified) }, definition.ClusteringKeys);
            CollectionAssert.AreEqual(new[] { "Id1" }, definition.PartitionKeys);
        }
Ejemplo n.º 7
0
        public void Counter_Success()
        {
            var config = new AttributeBasedTypeDefinition(typeof(PocoWithCounterAttribute));
            var table  = new Table <PocoWithCounterAttribute>(_session, new MappingConfiguration().Define(config));

            table.CreateIfNotExists();
            var cqlClient = new Mapper(_session, new MappingConfiguration().Define(config));

            List <PocoWithCounterAttribute> counterPocos = new List <PocoWithCounterAttribute>();

            for (int i = 0; i < 10; i++)
            {
                counterPocos.Add(
                    new PocoWithCounterAttribute()
                {
                    KeyPart1 = Guid.NewGuid(),
                    KeyPart2 = (decimal)123,
                });
            }

            int    counterIncrements = 100;
            string updateStr         = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", table.Name.ToLower(), "counter", "keypart1", "keypart2");
            var    updateSession     = _session.Prepare(updateStr);

            foreach (PocoWithCounterAttribute pocoWithCounter in counterPocos)
            {
                var boundStatement = updateSession.Bind(new object[] { pocoWithCounter.KeyPart1, pocoWithCounter.KeyPart2 });
                for (int j = 0; j < counterIncrements; j++)
                {
                    _session.Execute(boundStatement);
                }
                pocoWithCounter.Counter += counterIncrements;
            }

            List <PocoWithCounterAttribute> countersQueried = cqlClient.Fetch <PocoWithCounterAttribute>().ToList();

            foreach (PocoWithCounterAttribute pocoWithCounterExpected in counterPocos)
            {
                bool counterFound = false;
                foreach (PocoWithCounterAttribute pocoWithCounterActual in countersQueried)
                {
                    if (pocoWithCounterExpected.KeyPart1 == pocoWithCounterActual.KeyPart1)
                    {
                        Assert.AreEqual(pocoWithCounterExpected.KeyPart2, pocoWithCounterExpected.KeyPart2);
                        Assert.AreEqual(pocoWithCounterExpected.Counter, pocoWithCounterExpected.Counter);
                        counterFound = true;
                    }
                }
                Assert.IsTrue(counterFound, "Counter with first key part: " + pocoWithCounterExpected.KeyPart1 + " was not found!");
            }
        }
        public void AttributeBasedTypeDefinition_Defaults_Tests()
        {
            //Non decorated Poco
            var definition = new AttributeBasedTypeDefinition(typeof(AllTypesEntity));

            Assert.False(definition.CaseSensitive);
            Assert.False(definition.CompactStorage);
            Assert.False(definition.AllowFiltering);
            Assert.False(definition.ExplicitColumns);
            Assert.AreEqual(0, definition.ClusteringKeys.Length);
            Assert.AreEqual(0, definition.PartitionKeys.Length);
            Assert.Null(definition.KeyspaceName);
            Assert.AreEqual("AllTypesEntity", definition.TableName);
            Assert.AreEqual(typeof(AllTypesEntity), definition.PocoType);
        }
Ejemplo n.º 9
0
        public void Attributes_Ignore_TableCreatedWithMappingAttributes()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(PocoWithIgnoredAttributes));
            var table      = new Table <PocoWithIgnoredAttributes>(_session, new MappingConfiguration().Define(definition));

            Assert.AreNotEqual(table.Name, table.Name.ToLower());
            table.Create();

            //var mapper = new Mapper(_session, new MappingConfiguration().Define(definition));
            var mapper = new Mapper(_session, new MappingConfiguration());
            PocoWithIgnoredAttributes pocoToUpload = new PocoWithIgnoredAttributes
            {
                SomePartitionKey       = Guid.NewGuid().ToString(),
                IgnoredStringAttribute = Guid.NewGuid().ToString(),
            };

            mapper.Insert(pocoToUpload);
            string cqlSelect = string.Format("SELECT * from \"{0}\" where \"{1}\"='{2}'", table.Name.ToLower(), "somepartitionkey", pocoToUpload.SomePartitionKey);

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

            Assert.AreEqual(1, records.Count);
            Assert.AreEqual(pocoToUpload.SomePartitionKey, records[0].SomePartitionKey);
            PocoWithIgnoredAttributes defaultPoco = new PocoWithIgnoredAttributes();

            Assert.AreNotEqual(defaultPoco.IgnoredStringAttribute, pocoToUpload.IgnoredStringAttribute);
            Assert.AreEqual(defaultPoco.IgnoredStringAttribute, records[0].IgnoredStringAttribute);
            Assert.AreEqual(defaultPoco.SomeNonIgnoredDouble, records[0].SomeNonIgnoredDouble);

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

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoToUpload.SomePartitionKey, rows[0].GetValue <string>("somepartitionkey"));
            Assert.AreEqual(pocoToUpload.SomeNonIgnoredDouble, rows[0].GetValue <double>("somenonignoreddouble"));

            // Verify there was no column created for the ignored column
            var    e = Assert.Throws <ArgumentException>(() => rows[0].GetValue <string>(IgnoredStringAttribute));
            string expectedErrMsg = "Column " + IgnoredStringAttribute + " not found";

            Assert.AreEqual(expectedErrMsg, e.Message);
        }
Ejemplo n.º 10
0
        public void Attributes_CompositeKey()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(PocoWithCompositeKey));
            var table      = new Table <PocoWithCompositeKey>(_session, new MappingConfiguration().Define(definition));

            table.Create();

            List <Guid> listOfGuids = new List <Guid>()
            {
                new Guid(), new Guid()
            };

            var mapper = new Mapper(_session, new MappingConfiguration().Define(definition));
            PocoWithCompositeKey pocoWithCustomAttributes = new PocoWithCompositeKey
            {
                ListOfGuids       = listOfGuids,
                SomePartitionKey1 = Guid.NewGuid().ToString(),
                SomePartitionKey2 = Guid.NewGuid().ToString(),
                IgnoredString     = Guid.NewGuid().ToString(),
            };

            mapper.Insert(pocoWithCustomAttributes);

            // Get records using mapped object, validate that the value from Cassandra was ignored in favor of the default val
            List <PocoWithCompositeKey> records = mapper.Fetch <PocoWithCompositeKey>("SELECT * from " + table.Name).ToList();

            Assert.AreEqual(1, records.Count);
            Assert.AreEqual(pocoWithCustomAttributes.SomePartitionKey1, records[0].SomePartitionKey1);
            Assert.AreEqual(pocoWithCustomAttributes.SomePartitionKey2, records[0].SomePartitionKey2);
            Assert.AreEqual(pocoWithCustomAttributes.ListOfGuids, records[0].ListOfGuids);
            Assert.AreEqual(new PocoWithCompositeKey().IgnoredString, records[0].IgnoredString);

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

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoWithCustomAttributes.SomePartitionKey1, rows[0].GetValue <string>("somepartitionkey1"));
            Assert.AreEqual(pocoWithCustomAttributes.SomePartitionKey2, rows[0].GetValue <string>("somepartitionkey2"));
            Assert.AreEqual(pocoWithCustomAttributes.ListOfGuids, rows[0].GetValue <List <Guid> >("listofguids"));
            var ex = Assert.Throws <ArgumentException>(() => rows[0].GetValue <string>("ignoredstring"));

            Assert.AreEqual("Column ignoredstring not found", ex.Message);
        }
Ejemplo n.º 11
0
        public void Attributes_ClusteringKey_NoName()
        {
            var table = GetTable <EmptyClusteringColumnName>();

            table.Create();
            var definition   = new AttributeBasedTypeDefinition(typeof(EmptyClusteringColumnName));
            var mapper       = new Mapper(_session, new MappingConfiguration().Define(definition));
            var pocoToUpload = new EmptyClusteringColumnName
            {
                Id      = 1,
                cluster = "c2",
                value   = "v2"
            };

            mapper.Insert(pocoToUpload);
            var cqlSelect        = $"SELECT * from {table.Name} where id={pocoToUpload.Id}";
            var instancesQueried = mapper.Fetch <EmptyClusteringColumnName>(cqlSelect).ToList();

            Assert.AreEqual(1, instancesQueried.Count);
            Assert.AreEqual(pocoToUpload.Id, instancesQueried[0].Id);
            Assert.AreEqual(pocoToUpload.cluster, instancesQueried[0].cluster);
            Assert.AreEqual(pocoToUpload.value, instancesQueried[0].value);
        }
Ejemplo n.º 12
0
        public void Counter_Success()
        {
            var config = new AttributeBasedTypeDefinition(typeof(PocoWithCounterAttribute));
            var table  = new Table <PocoWithCounterAttribute>(Session, new MappingConfiguration().Define(config));

            table.CreateIfNotExists();

            VerifyQuery(
                "CREATE TABLE PocoWithCounterAttribute (Counter counter, KeyPart1 uuid, KeyPart2 decimal, " +
                "PRIMARY KEY ((KeyPart1, KeyPart2)))",
                1);

            var cqlClient = new Mapper(Session, new MappingConfiguration().Define(config));

            var counterPocos = new List <PocoWithCounterAttribute>();

            for (var i = 0; i < 10; i++)
            {
                counterPocos.Add(
                    new PocoWithCounterAttribute()
                {
                    KeyPart1 = Guid.NewGuid(),
                    KeyPart2 = (decimal)123,
                });
            }

            var counterIncrements = 100;

            foreach (var pocoWithCounter in counterPocos)
            {
                pocoWithCounter.Counter += counterIncrements;
            }

            TestCluster.PrimeFluent(
                b => b.WhenQuery("SELECT Counter, KeyPart1, KeyPart2 FROM PocoWithCounterAttribute")
                .ThenRowsSuccess(new[]
Ejemplo n.º 13
0
        public void Attributes_Ignore_TableCreatedWithMappingAttributes()
        {
            var definition = new AttributeBasedTypeDefinition(typeof(PocoWithIgnoredAttributes));
            var table      = new Linq::Table <PocoWithIgnoredAttributes>(Session, new MappingConfiguration().Define(definition));

            Assert.AreNotEqual(table.Name, table.Name.ToLower());
            table.Create();

            VerifyQuery(
                "CREATE TABLE PocoWithIgnoredAttributes " +
                "(SomeNonIgnoredDouble double, SomePartitionKey text, " +
                "PRIMARY KEY (SomePartitionKey))",
                1);

            //var mapper = new Mapper(Session, new MappingConfiguration().Define(definition));
            var mapper       = new Mapper(Session, new MappingConfiguration());
            var pocoToUpload = new PocoWithIgnoredAttributes
            {
                SomePartitionKey       = Guid.NewGuid().ToString(),
                IgnoredStringAttribute = Guid.NewGuid().ToString(),
            };

            mapper.Insert(pocoToUpload);

            VerifyBoundStatement(
                "INSERT INTO PocoWithIgnoredAttributes (SomeNonIgnoredDouble, SomePartitionKey) " +
                "VALUES (?, ?)",
                1,
                pocoToUpload.SomeNonIgnoredDouble, pocoToUpload.SomePartitionKey);

            var cqlSelect = $"SELECT * from \"{table.Name.ToLower()}\" where \"somepartitionkey\"='{pocoToUpload.SomePartitionKey}'";

            // Get records using mapped object, validate that the value from Cassandra was ignored in favor of the default val

            TestCluster.PrimeFluent(
                b => b.WhenQuery(cqlSelect)
                .ThenRowsSuccess(
                    new[] { "somenonignoreddouble", "somepartitionkey" },
                    r => r.WithRow(pocoToUpload.SomeNonIgnoredDouble, pocoToUpload.SomePartitionKey)));

            var records = mapper.Fetch <PocoWithIgnoredAttributes>(cqlSelect).ToList();

            Assert.AreEqual(1, records.Count);
            Assert.AreEqual(pocoToUpload.SomePartitionKey, records[0].SomePartitionKey);
            var defaultPoco = new PocoWithIgnoredAttributes();

            Assert.AreNotEqual(defaultPoco.IgnoredStringAttribute, pocoToUpload.IgnoredStringAttribute);
            Assert.AreEqual(defaultPoco.IgnoredStringAttribute, records[0].IgnoredStringAttribute);
            Assert.AreEqual(defaultPoco.SomeNonIgnoredDouble, records[0].SomeNonIgnoredDouble);

            // Query for the column that the Linq table create created, verify no value was uploaded to it
            var rows = Session.Execute(cqlSelect).GetRows().ToList();

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoToUpload.SomePartitionKey, rows[0].GetValue <string>("somepartitionkey"));
            Assert.AreEqual(pocoToUpload.SomeNonIgnoredDouble, rows[0].GetValue <double>("somenonignoreddouble"));

            // Verify there was no column created for the ignored column
            var e = Assert.Throws <ArgumentException>(() => rows[0].GetValue <string>(IgnoredStringAttribute));
            var expectedErrMsg = "Column " + IgnoredStringAttribute + " not found";

            Assert.AreEqual(expectedErrMsg, e.Message);
        }