public async void UpdateAsync_Poco() { // Get an existing user from the DB Guid userId = TestDataHelper.Users[0].UserId; var userToUpdate = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); userToUpdate.Should().NotBeNull(); // Change some properties userToUpdate.Name = "SomeNewName"; userToUpdate.Age = 186; userToUpdate.CreatedDate = DateTimeOffset.UtcNow.AddHours(-1); userToUpdate.IsActive = !userToUpdate.IsActive; userToUpdate.LastLoginDate = userToUpdate.LastLoginDate == null ? DateTimeOffset.UtcNow : (DateTimeOffset?)null; userToUpdate.LoginHistory.Add(DateTimeOffset.UtcNow); userToUpdate.LuckyNumbers.Add(137); userToUpdate.ChildrenAges.Add("SomeOtherChild", 5); userToUpdate.FavoriteColor = Enum.GetValues(typeof(RainbowColor)).Cast <RainbowColor>().First(v => v != userToUpdate.FavoriteColor); userToUpdate.TypeOfUser = userToUpdate.TypeOfUser == null ? UserType.Administrator : (UserType?)null; userToUpdate.PreferredContact = Enum.GetValues(typeof(ContactMethod)).Cast <ContactMethod>().First(v => v != userToUpdate.PreferredContact); userToUpdate.HairColor = userToUpdate.HairColor == null ? HairColor.Black : (HairColor?)null; // Update await CqlClient.UpdateAsync(userToUpdate); // Fetch and verify var foundUser = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); foundUser.ShouldBeEquivalentTo(userToUpdate, opt => opt.AccountForTimestampAccuracy()); }
public async void InsertAsync_FluentPoco() { // Get a "new" user by using the test data from an existing user and changing the primary key TestUser user = TestDataHelper.Users.First(); var newUser = new FluentUser { Id = Guid.NewGuid(), Name = user.Name, Age = user.Age, CreatedDate = user.CreatedDate, IsActive = user.IsActive, LastLoginDate = user.LastLoginDate, LoginHistory = user.LoginHistory, LuckyNumbers = user.LuckyNumbers, ChildrenAges = user.ChildrenAges, FavoriteColor = user.FavoriteColor, TypeOfUser = user.TypeOfUser, PreferredContact = user.PreferredContactMethod, HairColor = user.HairColor }; // Insert the new user await CqlClient.InsertAsync(newUser); // Fetch and verify var foundUser = await CqlClient.SingleAsync <FluentUser>("WHERE userid = ?", newUser.Id); foundUser.ShouldBeEquivalentTo(newUser, opt => opt.AccountForTimestampAccuracy()); }
public async void DeleteAsync_Poco_NoPrimaryKey() { // Get an existing user from the DB that doesn't have a PK attribute defined on the POCO (i.e. InsertUser class) Guid userId = TestDataHelper.Users[2].UserId; var userToDelete = await CqlClient.SingleAsync <InsertUser>("WHERE userid = ?", userId); userToDelete.Should().NotBeNull(); Func <Task> deleteUser = async() => { await CqlClient.DeleteAsync(userToDelete); }; deleteUser.ShouldThrow <InvalidOperationException>("no PK was specified and the assumed PK of 'id' is not a column on the POCO"); }
public async void DeleteAsync_Poco_WithCql() { // Pick an existing user from the DB and verify they currently exist (sanity check) Guid userId = TestDataHelper.Users[4].UserId; var userToDelete = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); userToDelete.Should().NotBeNull(); // Delete using CQL string (no POCO passed here, just CQL + params) await CqlClient.DeleteAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); // Verify user is actually deleted var foundUser = await CqlClient.SingleOrDefaultAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); foundUser.Should().BeNull(); }
public async void DeleteAsync_Poco() { // Get an existing user from the DB Guid userId = TestDataHelper.Users[0].UserId; var userToDelete = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); userToDelete.Should().NotBeNull(); // Delete await CqlClient.DeleteAsync(userToDelete); // Verify user is gone var foundUser = await CqlClient.SingleOrDefaultAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); foundUser.Should().BeNull(); }
public async void GetSingleAsync_OneColumnFlattened_WithCql() { // Get the name for a user that exists var name = await CqlClient.SingleAsync <string>("SELECT name FROM users WHERE userid = ?", TestDataHelper.Users[2].UserId); name.Should().Be(TestDataHelper.Users[2].Name); // Get the type of user for a user that doesn't exist Func <Task> getUserType = async() => { var userType = await CqlClient.SingleAsync <UserType?>("SELECT typeofuser FROM users WHERE userid = ?", Guid.Empty) .ConfigureAwait(false); }; getUserType.ShouldThrow <InvalidOperationException>(); }
public async void GetSingleAsync_Poco_WithCql() { // Get a user that exists var user = await CqlClient.SingleAsync <PlainUser>("SELECT * FROM users WHERE userid = ?", TestDataHelper.Users[0].UserId); user.ShouldBeEquivalentTo(TestDataHelper.Users[0], opt => opt.AccountForTimestampAccuracy()); // Get a user that shouldn't exist (using Guid.Empty as the Id) Func <Task> getUser = async() => { var notExistingUser = await CqlClient.SingleAsync <PlainUser>("SELECT * FROM users WHERE userid = ?", Guid.Empty) .ConfigureAwait(false); }; getUser.ShouldThrow <InvalidOperationException>(); }
public async void UpdateAsync_Poco_WithCql() { // Get a user to update Guid userId = TestDataHelper.Users[4].UserId; var userToUpdate = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); userToUpdate.Should().NotBeNull(); // Modify some values on the user (just so we can assert against it) userToUpdate.Name = "SomeNameChangedWithCql"; userToUpdate.LuckyNumbers.Add(54); userToUpdate.FavoriteColor = Enum.GetValues(typeof(RainbowColor)).Cast <RainbowColor>().First(v => v != userToUpdate.FavoriteColor); // Update the user using a CQL string (we aren't passing a POCO here, just CQL + params) await CqlClient.UpdateAsync <UserWithPrimaryKeyDecoration>("SET name = ?, luckynumbers = ?, favoritecolor = ? WHERE userid = ?", userToUpdate.Name, userToUpdate.LuckyNumbers, CqlClient.ConvertCqlArgument <RainbowColor, string>(userToUpdate.FavoriteColor), userId); // Fetch and validate var foundUser = await CqlClient.SingleAsync <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId); foundUser.ShouldBeEquivalentTo(userToUpdate, opt => opt.AccountForTimestampAccuracy()); }