コード例 #1
0
ファイル: Attributes.cs プロジェクト: laneser/csharp-driver
        public void Attributes_PartitionKey_OnWrongField()
        {
            // Create table using Linq -- includes partition key that is not assigned by the mapping attribute
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(
                typeof(PocoWithWrongFieldLabeledPk),
                () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithWrongFieldLabeledPk)));
            var table = new Table <PocoWithWrongFieldLabeledPk>(_session, config);

            table.Create();

            string cqlSelectAll = "SELECT * from " + table.Name;

            var cqlClient = GetMapper();
            PocoWithWrongFieldLabeledPk pocoWithCustomAttributes = new PocoWithWrongFieldLabeledPk();

            cqlClient.Insert(pocoWithCustomAttributes);

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

            Assert.AreEqual(1, records.Count); // for this case, the mapper is not picky about what field is marked as "partition key"

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

            Assert.AreEqual(1, rows.Count);
        }
コード例 #2
0
        public override void OneTimeSetUp()
        {
            base.OneTimeSetUp();
            _session = Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // Create necessary tables
            MappingConfiguration config1 = new MappingConfiguration();

            config1.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(EntityWithTimeUuid),
                                                                       () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(EntityWithTimeUuid)));
            _tableEntityWithTimeUuid = new Table <EntityWithTimeUuid>(_session, config1);
            _tableEntityWithTimeUuid.Create();

            MappingConfiguration config2 = new MappingConfiguration();

            config2.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(EntityWithNullableTimeUuid),
                                                                       () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(EntityWithNullableTimeUuid)));
            _tableEntityWithNullableTimeUuid = new Table <EntityWithNullableTimeUuid>(_session, config2);
            _tableEntityWithNullableTimeUuid.Create();

            _expectedTimeUuidObjectList         = EntityWithTimeUuid.GetDefaultObjectList();
            _expectedNullableTimeUuidObjectList = EntityWithNullableTimeUuid.GetDefaultObjectList();

            _dateBefore = DateTimeOffset.Parse("2014-2-1");
            _dateAfter  = DateTimeOffset.Parse("2014-4-1");
        }
コード例 #3
0
        public override void OneTimeSetUp()
        {
            base.OneTimeSetUp();
            _session = Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // Create necessary tables
            MappingConfiguration config1 = new MappingConfiguration();

            config1.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(EntityWithTimeUuid),
                                                                       () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(EntityWithTimeUuid)));
            _tableEntityWithTimeUuid = new Table <EntityWithTimeUuid>(_session, config1);
            _tableEntityWithTimeUuid.Create();

            MappingConfiguration config2 = new MappingConfiguration();

            config2.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(EntityWithNullableTimeUuid),
                                                                       () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(EntityWithNullableTimeUuid)));
            _tableEntityWithNullableTimeUuid = new Table <EntityWithNullableTimeUuid>(_session, config2);
            _tableEntityWithNullableTimeUuid.Create();

            _expectedTimeUuidObjectList = EntityWithTimeUuid.GetDefaultObjectList();
            for (int i = 0; i < _expectedTimeUuidObjectList.Count; i++)
            {
                _expectedTimeUuidObjectList[i].StringType = i.ToString();
            }
        }
        public void AttributeBased_Without_Name_For_Clustering_Key_Test()
        {
            var definition = new LinqAttributeBasedTypeDefinition(typeof(SamplePocoWithoutClusteringKeyName), "t", "k");

            CollectionAssert.AreEqual(new [] { Tuple.Create("Id2", SortOrder.Ascending) }, definition.ClusteringKeys);
            CollectionAssert.AreEqual(new[] { "Id1" }, definition.PartitionKeys);
        }
コード例 #5
0
        public void LinqAttributes_Counter_AttemptInsert()
        {
            // Create config that uses linq based attributes
            var mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes)));
            var table = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig);

            CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes()
            {
                KeyPart1 = Guid.NewGuid(),
                KeyPart2 = (decimal)123,
            };

            var expectedErrMsg = "INSERT statement(s)? are not allowed on counter tables, use UPDATE instead";

            TestCluster.PrimeFluent(
                b => b.WhenQuery(
                    "INSERT INTO \"CounterEntityWithLinqAttributes\" (\"Counter\", \"KeyPart1\", \"KeyPart2\") VALUES (?, ?, ?)",
                    when => pocoAndLinqAttributesLinqPocos.WithParams(when))
                .ThenServerError(
                    ServerError.Invalid, expectedErrMsg));

            var e = Assert.Throws <InvalidQueryException>(() => Session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos)));

            Assert.AreEqual(expectedErrMsg, e.Message);
        }
コード例 #6
0
        public void LinqAttributes_Counter()
        {
            //var mapping = new Map<PocoWithCounter>();
            var mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes)));
            var table = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig);

            table.Create();

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

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

            int    counterIncrements = 100;
            string updateStr         = String.Format("UPDATE \"{0}\" SET \"{1}\"=\"{1}\" + 1 WHERE \"{2}\"=? and \"{3}\"=?", typeof(CounterEntityWithLinqAttributes).Name, "Counter", "KeyPart1", "KeyPart2");
            var    updateSession     = _session.Prepare(updateStr);

            foreach (CounterEntityWithLinqAttributes 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 <CounterEntityWithLinqAttributes> countersQueried = table.Select(m => m).Execute().ToList();

            foreach (CounterEntityWithLinqAttributes pocoWithCounterExpected in counterPocos)
            {
                bool counterFound = false;
                foreach (CounterEntityWithLinqAttributes 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!");
            }
        }
コード例 #7
0
        public void TableCreate_ClassEmpty()
        {
            MappingConfiguration mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(PrivateEmptyClass),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PrivateEmptyClass)));
            Table <PrivateEmptyClass> table = new Table <PrivateEmptyClass>(_session, mappingConfig);

            try
            {
                table.Create();
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual("No partition key defined", e.Message);
            }
        }
コード例 #8
0
ファイル: Attributes.cs プロジェクト: laneser/csharp-driver
        public void Attributes_PartitionKey_ValueNull_LinqAndMappingAttributes()
        {
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(
                typeof(PocoWithIgnrdAttr_LinqAndMapping),
                () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithIgnrdAttr_LinqAndMapping)));
            var table = new Table <PocoWithIgnrdAttr_LinqAndMapping>(_session, config);

            table.Create();

            var cqlClient = GetMapper();
            PocoWithIgnrdAttr_LinqAndMapping pocoWithCustomAttributesLinqAndMapping = new PocoWithIgnrdAttr_LinqAndMapping
            {
                SomePartitionKey       = null,
                IgnoredStringAttribute = Guid.NewGuid().ToString(),
            };
            var err = Assert.Throws <InvalidQueryException>(() => cqlClient.Insert(pocoWithCustomAttributesLinqAndMapping));

            Assert.AreEqual("Invalid null value for partition key part somepartitionkey", err.Message);
        }
コード例 #9
0
ファイル: Attributes.cs プロジェクト: laneser/csharp-driver
        public void Attributes_Ignore_LinqAndMappingAttributes()
        {
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(
                typeof(PocoWithIgnrdAttr_LinqAndMapping),
                () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithIgnrdAttr_LinqAndMapping)));
            var table = new Table <PocoWithIgnrdAttr_LinqAndMapping>(_session, config);

            table.Create();

            var cqlClient = GetMapper();
            PocoWithIgnrdAttr_LinqAndMapping pocoToInsert = new PocoWithIgnrdAttr_LinqAndMapping
            {
                SomePartitionKey       = Guid.NewGuid().ToString(),
                IgnoredStringAttribute = Guid.NewGuid().ToString(),
            };

            cqlClient.Insert(pocoToInsert);

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

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

            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("SELECT * from " + table.Name).GetRows().ToList();

            Assert.AreEqual(1, rows.Count);
            Assert.AreEqual(pocoToInsert.SomePartitionKey, rows[0].GetValue <string>("somepartitionkey"));
            Assert.AreEqual(pocoToInsert.SomeNonIgnoredDouble, rows[0].GetValue <double>("somenonignoreddouble"));
            Assert.AreEqual(null, rows[0].GetValue <string>(IgnoredStringAttribute));
        }
コード例 #10
0
        public void TableCreate_Create_EntityTypeWithoutColumnNameMeta()
        {
            // Test
            MappingConfiguration mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(AllDataTypesNoColumnMeta),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(AllDataTypesNoColumnMeta)));
            Table <AllDataTypesNoColumnMeta> table = new Table <AllDataTypesNoColumnMeta>(_session, mappingConfig);

            table.Create();
            AllDataTypesNoColumnMeta expectedAllDataTypesNoColumnMetaEntity = WriteReadValidate(table);

            // Do regular CQL query, validate that correct columns names are available as RowSet keys
            string cql = string.Format("Select * from \"{0}\".\"{1}\" where \"{2}\"='{3}'", _uniqueKsName, table.Name, "StringType",
                                       expectedAllDataTypesNoColumnMetaEntity.StringType);
            List <Row> listOfAllDataTypesObjects = _session.Execute(cql).GetRows().ToList();

            Assert.AreEqual(1, listOfAllDataTypesObjects.Count);
            Row row = listOfAllDataTypesObjects[0];

            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.BooleanType, row["BooleanType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DateTimeOffsetType.ToString(), ((DateTimeOffset)row["DateTimeOffsetType"]).ToString());
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DateTimeType.ToString("US"), ((DateTimeOffset)row["DateTimeType"]).ToString("US"));
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DecimalType, row["DecimalType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DictionaryStringLongType, row["DictionaryStringLongType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DictionaryStringStringType, row["DictionaryStringStringType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.DoubleType, row["DoubleType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.FloatType, row["FloatType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.GuidType, row["GuidType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.Int64Type, row["Int64Type"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.IntType, row["IntType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.ListOfGuidsType, row["ListOfGuidsType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.ListOfStringsType, row["ListOfStringsType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.NullableIntType, row["NullableIntType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.NullableTimeUuidType, row["NullableTimeUuidType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.StringType, row["StringType"]);
            Assert.AreEqual(expectedAllDataTypesNoColumnMetaEntity.TimeUuidType.ToGuid(), (Guid)row["TimeUuidType"]);
        }
コード例 #11
0
        public void LinqAttributes_Counter_AttemptInsert()
        {
            // Create config that uses linq based attributes
            var mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes)));
            var table = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig);

            table.Create();

            CounterEntityWithLinqAttributes pocoAndLinqAttributesLinqPocos = new CounterEntityWithLinqAttributes()
            {
                KeyPart1 = Guid.NewGuid(),
                KeyPart2 = (decimal)123,
            };

            // Validate Error Message
            var    e = Assert.Throws <InvalidQueryException>(() => _session.Execute(table.Insert(pocoAndLinqAttributesLinqPocos)));
            string expectedErrMsg = "INSERT statement(s)? are not allowed on counter tables, use UPDATE instead";

            StringAssert.IsMatch(expectedErrMsg, e.Message);
        }
コード例 #12
0
        public void LinqAttributes_Counter_SelectRange()
        {
            //var mapping = new Map<PocoWithCounter>();
            var mappingConfig = new MappingConfiguration();

            mappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(CounterEntityWithLinqAttributes),
                                                                             () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(CounterEntityWithLinqAttributes)));
            var table = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig);

            table.Create();

            var expectedCounters = new List <CounterEntityWithLinqAttributes>();

            for (var i = 0; i < 10; i++)
            {
                expectedCounters.Add(
                    new CounterEntityWithLinqAttributes
                {
                    KeyPart1 = Guid.NewGuid(),
                    KeyPart2 = Guid.NewGuid().GetHashCode(),
                    Counter  = Guid.NewGuid().GetHashCode()
                });
            }

            PrimeLinqCounterRangeQuery(expectedCounters);

            var countersQueried = table.Select(m => m).Execute().ToList();

            Assert.AreEqual(10, countersQueried.Count);
            foreach (var expectedCounter in expectedCounters)
            {
                var actualCounter = countersQueried.Single(c => c.KeyPart1 == expectedCounter.KeyPart1);
                Assert.AreEqual(expectedCounter.KeyPart2, actualCounter.KeyPart2);
                Assert.AreEqual(expectedCounter.Counter, actualCounter.Counter);
            }
        }
コード例 #13
0
ファイル: First.cs プロジェクト: laneser/csharp-driver
        public void SetupTest()
        {
            _session = TestClusterManager.GetTestCluster(1).Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            MappingConfiguration movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            _movieTable = new Table <Movie>(_session, movieMappingConfig);
            _movieTable.Create();

            //Insert some data
            foreach (var movie in _movieList)
            {
                _movieTable.Insert(movie).Execute();
            }

            // Wait for data to be query-able
            DateTime futureDateTime = DateTime.Now.AddSeconds(2); // it should not take very long for these records to become available for querying!

            while (DateTime.Now < futureDateTime && _movieTable.Count().Execute() < _movieList.Count)
            {
                Thread.Sleep(200);
            }
            Assert.AreEqual(_movieList.Count(), _movieTable.Count().Execute(), "Setup failure: Expected number of records are not query-able");
        }
コード例 #14
0
ファイル: Attributes.cs プロジェクト: laneser/csharp-driver
        public void Attributes_MultipleClusteringKeys()
        {
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(PocoWithClusteringKeys), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithClusteringKeys)));
            var table = new Table <PocoWithClusteringKeys>(_session, config);

            table.Create();

            var cqlClient = new Mapper(_session, config);

            ;
            PocoWithClusteringKeys pocoWithCustomAttributes = new PocoWithClusteringKeys
            {
                SomePartitionKey1 = Guid.NewGuid().ToString(),
                SomePartitionKey2 = Guid.NewGuid().ToString(),
                Guid1             = Guid.NewGuid(),
                Guid2             = Guid.NewGuid(),
            };

            cqlClient.Insert(pocoWithCustomAttributes);

            // Get records using mapped object, validate that the value from Cassandra was ignored in favor of the default val
            List <PocoWithClusteringKeys> records = cqlClient.Fetch <PocoWithClusteringKeys>("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.Guid1, records[0].Guid1);
            Assert.AreEqual(pocoWithCustomAttributes.Guid2, records[0].Guid2);

            // 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.Guid1, rows[0].GetValue <Guid>("guid1"));
            Assert.AreEqual(pocoWithCustomAttributes.Guid2, rows[0].GetValue <Guid>("guid2"));
        }
コード例 #15
0
        protected override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
            _session = Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            MappingConfiguration movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            _movieTable = new Table <Movie>(_session, movieMappingConfig);
            _movieTable.Create();

            //Insert some data
            foreach (var movie in _movieList)
            {
                _movieTable.Insert(movie).Execute();
            }
        }
コード例 #16
0
        public void LinqWhere_AppendMultipleTimes()
        {
            int  userId = 1;
            int  date   = 2;
            long time   = 3;

            var config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(TestTable), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(TestTable)));
            var table = new Table <TestTable>(Session, config);

            var data = new List <TestTable>
            {
                new TestTable {
                    UserId = 1, Date = 2, TimeColumn = 1
                },
                new TestTable {
                    UserId = 1, Date = 2, TimeColumn = 2
                },
                new TestTable {
                    UserId = 1, Date = 2, TimeColumn = 3
                },
                new TestTable {
                    UserId = 1, Date = 2, TimeColumn = 4
                },
                new TestTable {
                    UserId = 1, Date = 2, TimeColumn = 5
                }
            };

            TestCluster.PrimeFluent(
                b => b.WhenQuery(
                    $"SELECT \"date\", \"time\", \"user\" FROM \"{TestTable.TableName}\" " +
                    "WHERE \"user\" = ? AND \"date\" = ? ALLOW FILTERING",
                    rows => rows.WithParams(userId, date))
                .ThenRowsSuccess(
                    new [] { "date", "time", "user" },
                    rows => rows.WithRows(data.Select(
                                              t => new object [] { t.Date, t.TimeColumn, t.UserId }).ToArray())));

            TestCluster.PrimeFluent(
                b => b.WhenQuery(
                    $"SELECT \"date\", \"time\", \"user\" FROM \"{TestTable.TableName}\" " +
                    "WHERE \"user\" = ? AND \"date\" = ? AND \"time\" >= ? ORDER BY \"time\" ALLOW FILTERING",
                    rows => rows.WithParams(userId, date, time))
                .ThenRowsSuccess(
                    new [] { "date", "time", "user" },
                    rows => rows.WithRows(data.Where(t => t.TimeColumn >= time).OrderBy(t => t.TimeColumn).Select(
                                              t => new object [] { t.Date, t.TimeColumn, t.UserId }).ToArray())));

            TestCluster.PrimeFluent(
                b => b.WhenQuery(
                    $"SELECT \"date\", \"time\", \"user\" FROM \"{TestTable.TableName}\" " +
                    "WHERE \"user\" = ? AND \"date\" = ? AND \"time\" <= ? ORDER BY \"time\" DESC ALLOW FILTERING",
                    rows => rows.WithParams(userId, date, time))
                .ThenRowsSuccess(
                    new [] { "date", "time", "user" },
                    rows => rows.WithRows(data.Where(t => t.TimeColumn <= time).OrderByDescending(t => t.TimeColumn).Select(
                                              t => new object [] { t.Date, t.TimeColumn, t.UserId }).ToArray())));

            var query1Actual = table.Where(i => i.UserId == userId && i.Date == date);

            var query2Actual = query1Actual.Where(i => i.TimeColumn >= time);

            query2Actual = query2Actual.OrderBy(i => i.TimeColumn); // ascending

            var query3Actual = query1Actual.Where(i => i.TimeColumn <= time);

            query3Actual = query3Actual.OrderByDescending(i => i.TimeColumn);

            var query1Expected = "SELECT \"date\", \"time\", \"user\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? ALLOW FILTERING";
            var query2Expected = "SELECT \"date\", \"time\", \"user\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? AND \"time\" >= ? ORDER BY \"time\" ALLOW FILTERING";
            var query3Expected = "SELECT \"date\", \"time\", \"user\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? AND \"time\" <= ? ORDER BY \"time\" DESC ALLOW FILTERING";

            Assert.AreEqual(query1Expected, query1Actual.ToString());
            Assert.AreEqual(query2Expected, query2Actual.ToString());
            Assert.AreEqual(query3Expected, query3Actual.ToString());

            var result2Actual = query2Actual.Execute().ToList();
            var result3Actual = query3Actual.Execute().ToList();

            Assert.AreEqual(3, result2Actual.First().TimeColumn);
            Assert.AreEqual(5, result2Actual.Last().TimeColumn);
            Assert.AreEqual(3, result3Actual.First().TimeColumn);
            Assert.AreEqual(1, result3Actual.Last().TimeColumn);
        }
コード例 #17
0
ファイル: Attributes.cs プロジェクト: laneser/csharp-driver
        public void Attributes_CompositeKey_AllFieldsNull()
        {
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(PocoWithCompositeKey), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithCompositeKey)));
            var table = new Table <PocoWithCompositeKey>(_session, config);

            table.Create();
            var cqlClient = GetMapper();
            PocoWithCompositeKey pocoWithCustomAttributes = new PocoWithCompositeKey
            {
                ListOfGuids       = null,
                SomePartitionKey1 = null,
                SomePartitionKey2 = null,
                IgnoredString     = null,
            };

            var err = Assert.Throws <InvalidQueryException>(() => cqlClient.Insert(pocoWithCustomAttributes));

            Assert.AreEqual("Invalid null value for partition key part somepartitionkey1", err.Message);
        }
コード例 #18
0
ファイル: CqlClientTest.cs プロジェクト: weexp/csharp-driver
        public void CqlClient_TwoInstancesBasedOnSameSession()
        {
            // Setup
            MappingConfiguration config1 = new MappingConfiguration();

            config1.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Poco1), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Poco1)));
            var    table1        = new Table <Poco1>(Session, config1);
            string cqlSelectAll1 = "SELECT * from " + table1.Name;

            MappingConfiguration config2 = new MappingConfiguration();

            config2.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Poco2), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Poco2)));
            var    table2        = new Table <Poco2>(Session, config2);
            string cqlSelectAll2 = "SELECT * from " + table2.Name;

            // Now re-instantiate the cqlClient, but with mapping rule that resolves the missing key issue
            var cqlClient1 = new Mapper(Session, new MappingConfiguration().Define(new Poco1Mapping()));
            var cqlClient2 = new Mapper(Session, new MappingConfiguration().Define(new Poco2Mapping()));

            // insert new record into two separate tables
            Poco1 poco1 = new Poco1();

            poco1.SomeString1 += "1";
            cqlClient1.Insert(poco1);

            VerifyBoundStatement(
                "INSERT INTO poco1 (SomeDouble1, somestring1) VALUES (?, ?)",
                1,
                poco1.SomeDouble1, poco1.SomeString1);

            Poco2 poco2 = new Poco2();

            poco2.SomeString2 += "1";
            cqlClient2.Insert(poco2);

            VerifyBoundStatement(
                "INSERT INTO poco2 (SomeDouble2, somestring2) VALUES (?, ?)",
                1,
                poco2.SomeDouble2, poco2.SomeString2);

            // Select Values from each table

            TestCluster.PrimeFluent(
                b => b.WhenQuery("SELECT * from poco1")
                .ThenRowsSuccess(new[] { "SomeDouble1", "somestring1" }, r => r.WithRow(poco1.SomeDouble1, poco1.SomeString1)));

            List <Poco1> poco1s = cqlClient1.Fetch <Poco1>(cqlSelectAll1).ToList();

            Assert.AreEqual(1, poco1s.Count);
            Assert.AreEqual(poco1.SomeString1, poco1s[0].SomeString1);
            Assert.AreEqual(poco1.SomeDouble1, poco1s[0].SomeDouble1);

            TestCluster.PrimeFluent(
                b => b.WhenQuery("SELECT * from poco2")
                .ThenRowsSuccess(new[] { "SomeDouble2", "somestring2" }, r => r.WithRow(poco2.SomeDouble2, poco2.SomeString2)));

            List <Poco2> poco2s = cqlClient2.Fetch <Poco2>(cqlSelectAll2).ToList();

            Assert.AreEqual(1, poco2s.Count);
            Assert.AreEqual(poco2.SomeString2, poco2s[0].SomeString2);
            Assert.AreEqual(poco2.SomeDouble2, poco2s[0].SomeDouble2);

            // Try that again
            poco1s.Clear();
            Assert.AreEqual(0, poco1s.Count);
            poco1s = cqlClient1.Fetch <Poco1>(cqlSelectAll1).ToList();
            Assert.AreEqual(1, poco1s.Count);
            Assert.AreEqual(poco1.SomeString1, poco1s[0].SomeString1);
            Assert.AreEqual(poco1.SomeDouble1, poco1s[0].SomeDouble1);

            poco2s.Clear();
            Assert.AreEqual(0, poco2s.Count);
            poco2s = cqlClient1.Fetch <Poco2>(cqlSelectAll2).ToList();
            Assert.AreEqual(1, poco2s.Count);
            Assert.AreEqual(poco2.SomeString2, poco2s[0].SomeString2);
            Assert.AreEqual(poco2.SomeDouble2, poco2s[0].SomeDouble2);
        }
コード例 #19
0
        public override void SetUp()
        {
            base.SetUp();
            Session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            var movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(ManyDataTypesEntity),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(ManyDataTypesEntity)));
            _movieTable = new Table <Movie>(Session, movieMappingConfig);

            _manyDataTypesEntitiesTable = new Table <ManyDataTypesEntity>(Session, movieMappingConfig);
        }
コード例 #20
0
        public void LinqWhere_AppendMultipleTimes()
        {
            int  userId = 1;
            int  date   = 2;
            long time   = 3;

            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(TestTable), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(TestTable)));
            Table <TestTable> table = new Table <TestTable>(_session, config);

            table.CreateIfNotExists();

            table.Insert(new TestTable {
                UserId = 1, Date = 2, TimeColumn = 1
            }).Execute();
            table.Insert(new TestTable {
                UserId = 1, Date = 2, TimeColumn = 2
            }).Execute();
            table.Insert(new TestTable {
                UserId = 1, Date = 2, TimeColumn = 3
            }).Execute();
            table.Insert(new TestTable {
                UserId = 1, Date = 2, TimeColumn = 4
            }).Execute();
            table.Insert(new TestTable {
                UserId = 1, Date = 2, TimeColumn = 5
            }).Execute();

            CqlQuery <TestTable> query1Actual = table.Where(i => i.UserId == userId && i.Date == date);

            CqlQuery <TestTable> query2Actual = query1Actual.Where(i => i.TimeColumn >= time);

            query2Actual = query2Actual.OrderBy(i => i.TimeColumn); // ascending

            CqlQuery <TestTable> query3Actual = query1Actual.Where(i => i.TimeColumn <= time);

            query3Actual = query3Actual.OrderByDescending(i => i.TimeColumn);

            string query1Expected = "SELECT \"user\", \"date\", \"time\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? ALLOW FILTERING";
            string query2Expected = "SELECT \"user\", \"date\", \"time\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? AND \"time\" >= ? ORDER BY \"time\" ALLOW FILTERING";
            string query3Expected = "SELECT \"user\", \"date\", \"time\" FROM \"test1\" WHERE \"user\" = ? AND \"date\" = ? AND \"time\" <= ? ORDER BY \"time\" DESC ALLOW FILTERING";

            Assert.AreEqual(query1Expected, query1Actual.ToString());
            Assert.AreEqual(query2Expected, query2Actual.ToString());
            Assert.AreEqual(query3Expected, query3Actual.ToString());

            List <TestTable> result2Actual = query2Actual.Execute().ToList();
            List <TestTable> result3Actual = query3Actual.Execute().ToList();

            Assert.AreEqual(3, result2Actual.First().TimeColumn);
            Assert.AreEqual(5, result2Actual.Last().TimeColumn);
            Assert.AreEqual(3, result3Actual.First().TimeColumn);
            Assert.AreEqual(1, result3Actual.Last().TimeColumn);
        }
コード例 #21
0
ファイル: Where.cs プロジェクト: panuoksala/csharp-driver
        public override void OneTimeSetUp()
        {
            base.OneTimeSetUp();
            _session = Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            var movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(ManyDataTypesEntity),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(ManyDataTypesEntity)));
            _movieTable = new Table <Movie>(_session, movieMappingConfig);
            _movieTable.Create();

            _manyDataTypesEntitiesTable = new Table <ManyDataTypesEntity>(_session, movieMappingConfig);
            _manyDataTypesEntitiesTable.Create();

            //Insert some data
            foreach (var movie in _movieList)
            {
                _movieTable.Insert(movie).Execute();
            }

            foreach (var manyData in _manyDataTypesEntitiesList)
            {
                _manyDataTypesEntitiesTable.Insert(manyData).Execute();
            }
        }
コード例 #22
0
ファイル: OrderBy.cs プロジェクト: oguimbal/csharp-driver
        public void SetupTest()
        {
            _session = TestClusterManager.GetTestCluster(1).Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            MappingConfiguration movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            _movieTable = new Table <Movie>(_session, movieMappingConfig);
            _movieTable.Create();

            //Insert some data
            foreach (var movie in _movieList)
            {
                _movieTable.Insert(movie).Execute();
            }
        }
コード例 #23
0
        public void CqlClient_TwoInstancesBasedOnSameSession()
        {
            // Setup
            MappingConfiguration config1 = new MappingConfiguration();

            config1.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Poco1), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Poco1)));
            var table1 = new Table <Poco1>(_session, config1);

            table1.Create();
            string cqlSelectAll1 = "SELECT * from " + table1.Name;

            MappingConfiguration config2 = new MappingConfiguration();

            config2.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Poco2), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Poco2)));
            var table2 = new Table <Poco2>(_session, config2);

            table2.Create();
            string cqlSelectAll2 = "SELECT * from " + table2.Name;

            // Now re-instantiate the cqlClient, but with mapping rule that resolves the missing key issue
            var cqlClient1 = new Mapper(_session, new MappingConfiguration().Define(new Poco1Mapping()));
            var cqlClient2 = new Mapper(_session, new MappingConfiguration().Define(new Poco2Mapping()));

            // insert new record into two separate tables
            Poco1 poco1 = new Poco1();

            poco1.SomeString1 += "1";
            cqlClient1.Insert(poco1);

            Poco2 poco2 = new Poco2();

            poco2.SomeString2 += "1";
            cqlClient2.Insert(poco2);

            // Select Values from each table
            List <Poco1> poco1s = cqlClient1.Fetch <Poco1>(cqlSelectAll1).ToList();

            Assert.AreEqual(1, poco1s.Count);
            Assert.AreEqual(poco1.SomeString1, poco1s[0].SomeString1);
            Assert.AreEqual(poco1.SomeDouble1, poco1s[0].SomeDouble1);

            List <Poco2> poco2s = cqlClient2.Fetch <Poco2>(cqlSelectAll2).ToList();

            Assert.AreEqual(1, poco2s.Count);
            Assert.AreEqual(poco2.SomeString2, poco2s[0].SomeString2);
            Assert.AreEqual(poco2.SomeDouble2, poco2s[0].SomeDouble2);

            // Try that again
            poco1s.Clear();
            Assert.AreEqual(0, poco1s.Count);
            poco1s = cqlClient1.Fetch <Poco1>(cqlSelectAll1).ToList();
            Assert.AreEqual(1, poco1s.Count);
            Assert.AreEqual(poco1.SomeString1, poco1s[0].SomeString1);
            Assert.AreEqual(poco1.SomeDouble1, poco1s[0].SomeDouble1);

            poco2s.Clear();
            Assert.AreEqual(0, poco2s.Count);
            poco2s = cqlClient1.Fetch <Poco2>(cqlSelectAll2).ToList();
            Assert.AreEqual(1, poco2s.Count);
            Assert.AreEqual(poco2.SomeString2, poco2s[0].SomeString2);
            Assert.AreEqual(poco2.SomeDouble2, poco2s[0].SomeDouble2);
        }
コード例 #24
0
ファイル: FirstOrDefault.cs プロジェクト: weexp/csharp-driver
        public override void SetUp()
        {
            base.SetUp();

            MappingConfiguration movieMappingConfig = new MappingConfiguration();

            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            _movieTable = new Table <Movie>(Session, movieMappingConfig);
        }
コード例 #25
0
ファイル: Attributes.cs プロジェクト: oguimbal/csharp-driver
        public void Attributes_CompositeKey_FirstPartOfKeyNull()
        {
            MappingConfiguration config = new MappingConfiguration();

            config.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(PocoWithCompositeKey), () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(PocoWithCompositeKey)));
            var table = new Table <PocoWithCompositeKey>(_session, config);

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

            var cqlClient = GetMapper();
            PocoWithCompositeKey pocoWithCustomAttributes = new PocoWithCompositeKey
            {
                ListOfGuids       = listOfGuids,
                SomePartitionKey1 = Guid.NewGuid().ToString(),
                SomePartitionKey2 = null,
                IgnoredString     = Guid.NewGuid().ToString(),
            };
            var    err            = Assert.Throws <InvalidQueryException>(() => cqlClient.Insert(pocoWithCustomAttributes));
            string expectedErrMsg = "Invalid null value (for partition key part|in condition for column) somepartitionkey2";

            StringAssert.IsMatch(expectedErrMsg, err.Message);
        }
コード例 #26
0
        public override void OneTimeSetUp()
        {
            base.OneTimeSetUp();
            _session = TestClusterManager.GetTestCluster(1).Session;
            _session.CreateKeyspace(_uniqueKsName);
            _session.ChangeKeyspace(_uniqueKsName);

            // drop table if exists, re-create
            MappingConfiguration movieMappingConfig = new MappingConfiguration();

            //Using attributes is deprecated
            #pragma warning disable 612
            movieMappingConfig.MapperFactory.PocoDataFactory.AddDefinitionDefault(typeof(Movie),
                                                                                  () => LinqAttributeBasedTypeDefinition.DetermineAttributes(typeof(Movie)));
            _movieTable = new Table <Movie>(_session, movieMappingConfig);
            #pragma warning restore 612
            _movieTable.Create();


            //Insert some data
            foreach (var movie in _movieList)
            {
                _movieTable.Insert(movie).Execute();
            }
        }