Пример #1
0
 public void LinqUdt_Insert()
 {
     var table = GetAlbumTable();
     var id = Guid.NewGuid();
     var album = new Album
     {
         Id = id,
         Name = "Mothership",
         PublishingDate = DateTimeOffset.Parse("2010-01-01"),
         Songs = new List<Song>
         {
             new Song
             {
                 Id = Guid.NewGuid(),
                 Artist = "Led Zeppelin",
                 Title = "Good Times Bad Times"
             },
             new Song
             {
                 Id = Guid.NewGuid(),
                 Artist = "Led Zeppelin",
                 Title = "Communication Breakdown"
             }
         }
     };
     table.Insert(album).Execute();
     //Check that the values exists using core driver
     var row = _session.Execute(new SimpleStatement("SELECT * FROM albums WHERE id = ?", id)).First();
     Assert.AreEqual("Mothership", row.GetValue<object>("name"));
     var songs = row.GetValue<List<Song>>("songs");
     Assert.NotNull(songs);
     Assert.AreEqual(2, songs.Count);
     Assert.NotNull(songs.FirstOrDefault(s => s.Title == "Good Times Bad Times"));
     Assert.NotNull(songs.FirstOrDefault(s => s.Title == "Communication Breakdown"));
 }
Пример #2
0
 public void Insert_Udt()
 {
     var album = new Album
     {
         Id = Guid.NewGuid(),
         Name = "Images and Words",
         PublishingDate = DateTimeOffset.Now,
         Songs = new List<Song>
         {
             new Song {Artist = "Dream Theater", Title = "Pull me under"},
             new Song {Artist = "Dream Theater", Title = "Under a glass moon"}
         }
     };
     var sessionMock = new Mock<ISession>(MockBehavior.Strict);
     sessionMock
         .Setup(s => s.ExecuteAsync(It.IsAny<BoundStatement>()))
         .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);
     mapper.Insert(album);
     sessionMock.Verify(s => s.ExecuteAsync(It.Is<BoundStatement>(stmt =>
         stmt.QueryValues.Length > 0 &&
         stmt.PreparedStatement.Cql == "INSERT INTO Album (Id, Name, PublishingDate, Songs) VALUES (?, ?, ?, ?)"
         )), Times.Exactly(1));
     sessionMock.Verify();
 }
Пример #3
0
 public void Insert_Without_Nulls()
 {
     var album = new Album
     {
         Id = Guid.NewGuid(),
         Name = null,
         PublishingDate = DateTimeOffset.Now,
         Songs = null
     };
     var sessionMock = new Mock<ISession>(MockBehavior.Strict);
     string query = null;
     object[] parameters = null;
     sessionMock
         .Setup(s => s.ExecuteAsync(It.IsAny<BoundStatement>()))
         .Returns(TaskHelper.ToTask(new RowSet()))
         .Callback<BoundStatement>(stmt =>
         {
             query = stmt.PreparedStatement.Cql;
             parameters = stmt.QueryValues;
         })
         .Verifiable();
     sessionMock
         .Setup(s => s.PrepareAsync(It.IsAny<string>()))
         .Returns<string>(cql => TaskHelper.ToTask(GetPrepared(cql)))
         .Verifiable();
     var mapper = GetMappingClient(sessionMock);
     //with nulls by default
     mapper.Insert(album);
     Assert.AreEqual("INSERT INTO Album (Id, Name, PublishingDate, Songs) VALUES (?, ?, ?, ?)", query);
     CollectionAssert.AreEqual(new object[] { album.Id, null, album.PublishingDate, null}, parameters);
     //Without nulls
     mapper.Insert(album, false);
     Assert.AreEqual("INSERT INTO Album (Id, PublishingDate) VALUES (?, ?)", query);
     CollectionAssert.AreEqual(new object[] { album.Id, album.PublishingDate }, parameters);
     sessionMock.Verify();
 }