예제 #1
0
        public void Update_Poco()
        {
            // Get an existing user from the DB
            Guid userId       = TestDataHelper.Users[1].UserId;
            var  userToUpdate = CqlClient.Single <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
            CqlClient.Update(userToUpdate);

            // Fetch and verify
            var foundUser = CqlClient.Single <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            foundUser.ShouldBeEquivalentTo(userToUpdate, opt => opt.AccountForTimestampAccuracy());
        }
예제 #2
0
        public void Insert_Poco()
        {
            // 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 InsertUser
            {
                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
            CqlClient.Insert(newUser);

            // Fetch and verify
            var foundUser = CqlClient.Single <InsertUser>("WHERE userid = ?", newUser.Id);

            foundUser.ShouldBeEquivalentTo(newUser, opt => opt.AccountForTimestampAccuracy());
        }
예제 #3
0
        public void Delete_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[3].UserId;
            var  userToDelete = CqlClient.Single <InsertUser>("WHERE userid = ?", userId);

            userToDelete.Should().NotBeNull();

            Action deleteUser = () => CqlClient.Delete(userToDelete);

            deleteUser.ShouldThrow <InvalidOperationException>("no PK was specified and the assumed PK of 'id' is not a column on the POCO");
        }
예제 #4
0
        public void GetSingle_OneColumnFlattened_WithCql()
        {
            // Get the name for a user that exists
            var name = CqlClient.Single <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
            Action getUserType = () =>
            {
                var userType = CqlClient.Single <UserType?>("SELECT typeofuser FROM users WHERE userid = ?", Guid.Empty);
            };

            getUserType.ShouldThrow <InvalidOperationException>();
        }
예제 #5
0
        public void GetSingle_Poco_WithCql()
        {
            // Get a user that exists
            var user = CqlClient.Single <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)
            Action getUser = () =>
            {
                var notExistingUser = CqlClient.Single <PlainUser>("SELECT * FROM users WHERE userid = ?", Guid.Empty);
            };

            getUser.ShouldThrow <InvalidOperationException>();
        }
예제 #6
0
        public void Delete_Poco_WithCql()
        {
            // Pick an existing user from the DB and verify they currently exist (sanity check)
            Guid userId       = TestDataHelper.Users[5].UserId;
            var  userToDelete = CqlClient.Single <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            userToDelete.Should().NotBeNull();

            // Delete using CQL string (no POCO passed here, just CQL + params)
            CqlClient.Delete <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            // Verify user is actually deleted
            var foundUser = CqlClient.SingleOrDefault <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            foundUser.Should().BeNull();
        }
예제 #7
0
        public void Delete_Poco()
        {
            // Get an existing user from the DB
            Guid userId       = TestDataHelper.Users[1].UserId;
            var  userToDelete = CqlClient.Single <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            userToDelete.Should().NotBeNull();

            // Delete
            CqlClient.Delete(userToDelete);

            // Verify user is gone
            var foundUser = CqlClient.SingleOrDefault <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            foundUser.Should().BeNull();
        }
예제 #8
0
        public void Update_Poco_WithCql()
        {
            // Get a user to update
            Guid userId       = TestDataHelper.Users[5].UserId;
            var  userToUpdate = CqlClient.Single <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)
            CqlClient.Update <UserWithPrimaryKeyDecoration>("SET name = ?, luckynumbers = ?, favoritecolor = ? WHERE userid = ?",
                                                            userToUpdate.Name, userToUpdate.LuckyNumbers,
                                                            CqlClient.ConvertCqlArgument <RainbowColor, string>(userToUpdate.FavoriteColor), userId);

            // Fetch and validate
            var foundUser = CqlClient.Single <UserWithPrimaryKeyDecoration>("WHERE userid = ?", userId);

            foundUser.ShouldBeEquivalentTo(userToUpdate, opt => opt.AccountForTimestampAccuracy());
        }