public void CRUD_Test() { IRepositoryInitializer repoInitr = Kernel.Get<IRepositoryInitializer>(); repoInitr.ConnectionStringName = "UserRepository_CRUD_Test"; repoInitr.Refresh(); using (IUserRepository repo = Kernel.Get<IUserRepository>()) { string testsUserName = "******"; User u1 = new User(); u1.UserName = testsUserName; u1.Password = "******"; u1.Claims.Add("http://yahoo"); u1.Claims.Add("http://google"); u1 = repo.Create(u1); u1.Id.ShouldBeGreaterThan(0); IEnumerable<User> userList = repo.Read(); userList.ShouldNotBeNull(); User createdUser = userList.FirstOrDefault(delegate (User u) { return string.CompareOrdinal(u.UserName, testsUserName) == 0; }); createdUser.Id.ShouldBeGreaterThan(0); createdUser.Claims.Count.ShouldEqual(2); u1.Password = "******"; u1.Claims.Remove("http://yahoo"); u1.Claims.Add("http://top.st"); u1.Claims.Add("http://webcrawler.com"); repo.Update(u1); User updatedUser = repo.Read().FirstOrDefault(delegate (User u) { return string.CompareOrdinal(u.UserName, testsUserName) == 0; }); updatedUser.Id.ShouldEqual(createdUser.Id); updatedUser.Password.ShouldEqual("Cat"); updatedUser.Claims.Count.ShouldEqual(3); updatedUser.Claims[0].ShouldEqual("http://google"); updatedUser.Claims[1].ShouldEqual("http://top.st"); updatedUser.Claims[2].ShouldEqual("http://webcrawler.com"); repo.Delete(u1).ShouldBeTrue(); userList = null; userList = repo.Read(); User shouldBeNullUser = userList.Select( delegate (User u) { return u.UserName == testsUserName ? u : null; }).FirstOrDefault(); Assert.Null(shouldBeNullUser); } }
public UserTests() { // Example service URLs // http://localhost/dog/{id} // https://localhost/dog/{id}/schedule // http://localhost:9000/cat?name={name} testUser = new User("dog", "bone", @"^GET:http(s)?:\/\/.*\/dog\/\d.$", @"^GET:http(s)?:\/\/.*\/dog\/\d.\/schedule(\/\d.)?$", @"^POST:http(s)?:\/\/.*\/cat\?name=.+$"); }
public User Update(User entity) { // start transaction Context.UseTransaction(true); try { // update user int updatedCnt = Context .Sql(SQL.Update_User) .Parameter("UserName", entity.UserName) .Parameter("Password", entity.Password) .Parameter("Id", entity.Id) .Execute(); // delete all claims int delCnt = Context .Sql(SQL.Delete_All_User_Claims) .Parameter("UserName", entity.UserName) .Execute(); // add current set of claims int addedCnt = 0; foreach (string claim in entity.Claims) { addedCnt += Context.Sql(SQL.Create_Claim) .Parameter("UserName", entity.UserName) .Parameter("Claim", claim) .Execute(); } // compoleted transaction Context.Commit(); // return updated user return entity; } catch { Context.Rollback(); throw; } finally { Context.UseTransaction(false); } }
public User Create(User entity) { Context.UseTransaction(true); try { entity.Id = Context.Sql(SQL.Create_User) .Parameter("UserName", entity.UserName) .Parameter("Password", entity.Password) .ExecuteReturnLastId<long>("id"); foreach (string Claim in entity.Claims) { int rows = Context.Sql(SQL.Create_Claim) .Parameter("UserName", entity.UserName) .Parameter("Claim", Claim) .Execute(); if (rows < 1) { throw new DataException("Insert into User Claims failed"); } } Context.Commit(); return entity; } catch { Context.Rollback(); throw; } finally { Context.UseTransaction(false); } }
public bool Delete(User entity) { Context.UseTransaction(true); try { int cnt = Context.Sql(SQL.Delete_Claim) .Parameter("UserName", entity.UserName) .Execute(); cnt += Context.Sql(SQL.Delete_User) .Parameter("UserName", entity.UserName) .Execute(); Context.Commit(); return cnt > 0; } catch { Context.Rollback(); throw; } finally { Context.UseTransaction(false); } }
public void IsNewTest() { User user = new User(); user.IsNew().ShouldBeTrue(); }
public void GetByUserNameTest() { IRepositoryInitializer repoInitr = Kernel.Get<IRepositoryInitializer>(); repoInitr.ConnectionStringName = "UserRepository_GetByUserNameTest"; repoInitr.Refresh(); using (IUserRepository repo = Kernel.Get<IUserRepository>()) { string testsUserName = "******"; User u1 = new User(); u1.UserName = testsUserName; u1.Claims.Add("gold"); u1.Claims.Add("frankincense"); u1.Claims.Add("myrrh"); u1.Password = "******"; repo.Create(u1); User fetchedUser = repo.GetByUserName(u1.UserName); fetchedUser.Id.ShouldEqual(u1.Id); fetchedUser.Password.ShouldEqual(u1.Password); fetchedUser.Claims.Count.ShouldEqual(u1.Claims.Count); fetchedUser.Claims[2].ShouldEqual(u1.Claims[2]); repo.Delete(u1); } }