예제 #1
0
        public void CqlClient_Timestamp()
        {
            var config = new MappingConfiguration()
                .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));

            //Use linq to create the table
            var table = new Table<Song>(_session, config);
            table.CreateIfNotExists();
            var mapper = new Mapper(_session, config);
            var song = new Song
            {
                Id = Guid.NewGuid(),
                Artist = "The Who",
                Title = "Substitute",
                ReleaseDate = DateTimeOffset.UtcNow
            };
            //Set timestamp to 1 day ago
            var timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(1));
            mapper.Insert(song, true, CqlQueryOptions.New().SetTimestamp(timestamp));

            //query for timestamp in a column of the record
            var cqlLowerCasePartitionKey = "SELECT WRITETIME (Artist) AS timestamp FROM " + table.Name + " WHERE Id = " + song.Id + ";";
            var rows = _session.Execute(cqlLowerCasePartitionKey).GetRows().ToList();
            Assert.AreEqual(1, rows.Count);

            var creationTimestamp = rows[0].GetValue<long>("timestamp");
            Assert.NotNull(creationTimestamp);
            //Timestamp retrieved is in macroseconds. Converting it to milliseconds
            Assert.AreEqual(TypeSerializer.SinceUnixEpoch(timestamp).Ticks / 10, rows[0].GetValue<object>("timestamp"));
        }
예제 #2
0
 public void Update_With_Single_PartitionKey()
 {
     var song = new Song()
     {
         Id = Guid.NewGuid(),
         Artist = "Nirvana",
         ReleaseDate = DateTimeOffset.Now,
         Title = "Come As You Are"
     };
     string query = null;
     object[] parameters = null;
     var sessionMock = new Mock<ISession>(MockBehavior.Strict);
     sessionMock
         .Setup(s => s.ExecuteAsync(It.IsAny<BoundStatement>()))
         .Callback<IStatement>(b =>
         {
             query = ((BoundStatement)b).PreparedStatement.Cql;
             parameters = b.QueryValues;
         })
         .Returns(TaskHelper.ToTask(new RowSet()))
         .Verifiable();
     sessionMock
         .Setup(s => s.PrepareAsync(It.IsAny<string>()))
         .Returns<string>(cql => TaskHelper.ToTask(GetPrepared(cql)))
         .Verifiable();
     var mapper = GetMappingClient(sessionMock, new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id)));
     mapper.Update(song);
     Assert.AreEqual("UPDATE Song SET Title = ?, Artist = ?, ReleaseDate = ? WHERE Id = ?", query);
     CollectionAssert.AreEqual(new object[] {song.Title, song.Artist, song.ReleaseDate, song.Id}, parameters);
     sessionMock.Verify();
 }
예제 #3
0
 public void Delete_Poco_Generates_Test()
 {
     string query = null;
     object[] parameters = null;
     var session = GetSession((q, args) => 
     { 
         query = q;
         parameters = args;
     }, new RowSet());
     var mapper = new Mapper(session, new MappingConfiguration().Define(new Map<Song>().PartitionKey(s => s.Id)));
     var song = new Song {Id = Guid.NewGuid()};
     mapper.Delete(song);
     Assert.AreEqual("DELETE FROM Song WHERE Id = ?", query);
     CollectionAssert.AreEqual(new object[] { song.Id }, parameters);
 }
예제 #4
0
 public void UpdateIf_Applied_Test()
 {
     var config = new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_update_if"));
     //Use linq to create the table
     new Table<Song>(_session, config).Create();
     var mapper = new Mapper(_session, config);
     var song = new Song { Id = Guid.NewGuid(), Artist = "Cream", Title = "Crossroad", ReleaseDate = DateTimeOffset.Parse("1970/1/1")};
     //It is the first song there, it should apply it
     mapper.Insert(song);
     const string query = "SET artist = ?, title = ? WHERE id = ? IF releasedate = ?";
     var appliedInfo = mapper.UpdateIf<Song>(Cql.New(query, song.Artist, "Crossroad2", song.Id, song.ReleaseDate));
     Assert.True(appliedInfo.Applied);
     Assert.Null(appliedInfo.Existing);
     //Following times, it should not apply the mutation as the condition is not valid
     appliedInfo = mapper.UpdateIf<Song>(Cql.New(query, song.Artist, "Crossroad3", song.Id, DateTimeOffset.Now));
     Assert.False(appliedInfo.Applied);
     Assert.NotNull(appliedInfo.Existing);
     Assert.AreEqual(song.ReleaseDate, appliedInfo.Existing.ReleaseDate);
     Assert.AreEqual("Crossroad2", mapper.First<Song>("WHERE id = ?", song.Id).Title);
 }
예제 #5
0
 public void Insert_SetTimestamp_Test()
 {
     BoundStatement statement = null;
     var sessionMock = new Mock<ISession>(MockBehavior.Strict);
     sessionMock.Setup(s => s.Cluster).Returns((ICluster)null);
     sessionMock
         .Setup(s => s.ExecuteAsync(It.IsAny<BoundStatement>()))
         .Returns(() => TestHelper.DelayedTask(RowSet.Empty()))
         .Callback<BoundStatement>(stmt => statement = stmt)
         .Verifiable();
     sessionMock
         .Setup(s => s.PrepareAsync(It.IsAny<string>()))
         .Returns<string>(query => TaskHelper.ToTask(GetPrepared(query)))
         .Verifiable();
     var mapper = GetMappingClient(sessionMock);
     var song = new Song { Id = Guid.NewGuid(), Title = "t2", ReleaseDate = DateTimeOffset.Now };
     var timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(1));
     mapper.Insert(song);
     Assert.Null(statement.Timestamp);
     mapper.Insert(song, CqlQueryOptions.New().SetTimestamp(timestamp));
     Assert.AreEqual(timestamp, statement.Timestamp);
     timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromHours(10));
     mapper.InsertIfNotExists(song, CqlQueryOptions.New().SetTimestamp(timestamp));
     Assert.AreEqual(timestamp, statement.Timestamp);
 }
예제 #6
0
 public void InsertIfNotExists_With_Ttl_Test()
 {
     string query = null;
     object[] parameters = null;
     var mapper = GetMappingClient(() => TaskHelper.ToTask(RowSet.Empty()), (q, p) =>
     {
         query = q;
         parameters = p;
     });
     var song = new Song { Id = Guid.NewGuid(), Title = "t2", ReleaseDate = DateTimeOffset.Now };
     const int ttl = 600;
     mapper.InsertIfNotExists(song, false, ttl);
     Assert.AreEqual("INSERT INTO Song (Id, Title, ReleaseDate) VALUES (?, ?, ?) IF NOT EXISTS USING TTL ?", query);
     Assert.AreEqual(song.Id, parameters[0]);
     Assert.AreEqual(song.Title, parameters[1]);
     Assert.AreEqual(song.ReleaseDate, parameters[2]);
     Assert.AreEqual(ttl, parameters[3]);
 }
예제 #7
0
 public void Insert_With_Ttl_Test()
 {
     string query = null;
     object[] parameters = null;
     var mapper = GetMappingClient(() => TaskHelper.ToTask(RowSet.Empty()), (q, p) =>
     {
         query = q;
         parameters = p;
     });
     var song = new Song { Id = Guid.NewGuid() };
     const int ttl = 600;
     mapper.Insert(song, true, ttl);
     Assert.AreEqual("INSERT INTO Song (Id, Title, Artist, ReleaseDate) VALUES (?, ?, ?, ?) USING TTL ?", query);
     Assert.AreEqual(song.Id, parameters[0]);
     Assert.AreEqual(ttl, parameters.Last());
 }
예제 #8
0
 public void Insert_With_TTL()
 {
     var config = new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
     //Use linq to create the table
     new Table<Song>(_session, config).CreateIfNotExists();
     var mapper = new Mapper(_session, config);
     var song = new Song
     {
         Id = Guid.NewGuid(),
         Artist = "The Who",
         Title = "Substitute",
         ReleaseDate = DateTimeOffset.UtcNow
     };
     mapper.Insert(song, true, 5);
     var notExpiredSong = mapper.First<Song>("WHERE id = ?", song.Id);
     Assert.NotNull(notExpiredSong);
     Assert.AreEqual(song.Id, notExpiredSong.Id);
     Assert.AreEqual(song.Artist, notExpiredSong.Artist);
     Assert.AreEqual(song.Title, notExpiredSong.Title);
     Thread.Sleep(6000);
     var expiredSong = mapper.FirstOrDefault<Song>("WHERE id = ?", song.Id);
     Assert.Null(expiredSong);
 }
예제 #9
0
 public void Insert_Without_Nulls_Test()
 {
     var config = new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
     //Use linq to create the table
     new Table<Song>(_session, config).CreateIfNotExists();
     var mapper = new Mapper(_session, config);
     var song = new Song 
     { 
         Id = Guid.NewGuid(), 
         Artist = "The Who", 
         Title = "Substitute", 
         ReleaseDate = DateTimeOffset.UtcNow
     };
     mapper.Insert(song);
     var storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
     Assert.AreEqual(song.Artist, storedSong.Artist);
     //do NOT insert nulls
     mapper.Insert(new Song { Id = song.Id, Artist = null, Title = "Substitute 2", ReleaseDate = DateTimeOffset.UtcNow}, false);
     //it should have the new title but the artist should still be the same (not null)
     storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
     Assert.NotNull(storedSong.Artist);
     Assert.AreEqual(song.Artist, storedSong.Artist);
     Assert.AreEqual("Substitute 2", storedSong.Title);
     //Now insert nulls
     mapper.Insert(new Song { Id = song.Id, Artist = null, Title = "Substitute 3", ReleaseDate = DateTimeOffset.UtcNow }, true);
     //it should have the new title and the artist should be null
     storedSong = mapper.First<Song>("WHERE id = ?", song.Id);
     Assert.Null(storedSong.Artist);
     Assert.AreEqual("Substitute 3", storedSong.Title);
 }
예제 #10
0
 public void InsertIfNotExists_Applied_Test()
 {
     var config = new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_insert"));
     //Use linq to create the table
     new Table<Song>(_session, config).Create();
     var mapper = new Mapper(_session, config);
     var song = new Song {Id = Guid.NewGuid(), Artist = "Led Zeppelin", Title = "Good Times Bad Times"};
     //It is the first song there, it should apply it
     var appliedInfo = mapper.InsertIfNotExists(song);
     Assert.True(appliedInfo.Applied);
     Assert.Null(appliedInfo.Existing);
     //Following times, it should not apply the mutation as the partition key is the same
     var nextSong = new Song { Id = song.Id, Title = "Communication Breakdown" };
     appliedInfo = mapper.InsertIfNotExists(nextSong);
     Assert.False(appliedInfo.Applied);
     Assert.NotNull(appliedInfo.Existing);
     Assert.AreEqual(song.Title, appliedInfo.Existing.Title);
 }
예제 #11
0
 public void DeleteIf_Applied_Test()
 {
     var config = new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Id).TableName("song_delete_if"));
     //Use linq to create the table
     new Table<Song>(_session, config).Create();
     var mapper = new Mapper(_session, config);
     var song = new Song { Id = Guid.NewGuid(), Artist = "Cream", Title = "Crossroad", ReleaseDate = DateTimeOffset.Parse("1970/1/1") };
     mapper.Insert(song);
     //It should not apply it as the condition will NOT be satisfied
     var appliedInfo = mapper.DeleteIf<Song>(Cql.New("WHERE id = ? IF title = ?", song.Id, "Crossroad2"));
     Assert.False(appliedInfo.Applied);
     Assert.NotNull(appliedInfo.Existing);
     Assert.AreEqual("Crossroad", appliedInfo.Existing.Title);
     //It should apply it as the condition will be satisfied
     appliedInfo = mapper.DeleteIf<Song>(Cql.New("WHERE id = ? IF title = ?", song.Id, song.Title));
     Assert.True(appliedInfo.Applied);
     Assert.Null(appliedInfo.Existing);
 }
 public void Update_Set_From_Other_Instances_With_Where_Clause()
 {
     string query = null;
     object[] parameters = null;
     var session = GetSession((q, v) =>
     {
         query = q;
         parameters = v;
     });
     var table = new Table<Song>(session);
     var other = new Song()
     {
         Id = Guid.NewGuid(),
         Artist = "The Rolling Stones",
         Title = "Paint It Black"
     };
     table
         .Where(t => t.Id == Guid.Empty)
         .Select(t => new Song
         {
             Title = other.Artist,
             Artist = other.Artist,
             ReleaseDate = DateTimeOffset.MinValue
         })
         .Update()
         .Execute();
     Assert.AreEqual(
         @"UPDATE Song SET Title = ?, Artist = ?, ReleaseDate = ? WHERE Id = ?",
         query);
     CollectionAssert.AreEqual(new object[] { other.Artist, other.Artist, DateTimeOffset.MinValue, Guid.Empty }, parameters);
 }
예제 #13
0
 public void Update_Sets_Consistency()
 {
     var song = new Song()
     {
         Id = Guid.NewGuid(),
         Artist = "Dream Theater",
         ReleaseDate = DateTimeOffset.Now,
         Title = "Lines in the Sand"
     };
     ConsistencyLevel? consistency = null;
     ConsistencyLevel? serialConsistency = null;
     var sessionMock = new Mock<ISession>(MockBehavior.Strict);
     sessionMock
         .Setup(s => s.ExecuteAsync(It.IsAny<BoundStatement>()))
         .Callback<IStatement>(b =>
         {
             consistency = b.ConsistencyLevel;
             serialConsistency = b.SerialConsistencyLevel;
         })
         .Returns(TestHelper.DelayedTask(new RowSet()))
         .Verifiable();
     sessionMock
         .Setup(s => s.PrepareAsync(It.IsAny<string>()))
         .Returns<string>(cql => TaskHelper.ToTask(GetPrepared(cql)))
         .Verifiable();
     var mapper = GetMappingClient(sessionMock, new MappingConfiguration()
         .Define(new Map<Song>().PartitionKey(s => s.Title)));
     mapper.Update(song, new CqlQueryOptions().SetConsistencyLevel(ConsistencyLevel.LocalQuorum));
     Assert.AreEqual(ConsistencyLevel.LocalQuorum, consistency);
     Assert.AreEqual(ConsistencyLevel.Any, serialConsistency);
     mapper.Update(song, new CqlQueryOptions().SetConsistencyLevel(ConsistencyLevel.Two).SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial));
     Assert.AreEqual(ConsistencyLevel.Two, consistency);
     Assert.AreEqual(ConsistencyLevel.LocalSerial, serialConsistency);
     sessionMock.Verify();
 }