public async Task Should_return_the_videos_with_specified_kind() { //Arrange Kind expectedKind = TestDataFactory.CreateKind(); List <Video> videos = new List <Video>() { TestDataFactory.CreateVideo(expectedKind), TestDataFactory.CreateVideo(expectedKind), }; GenData.RepeatCount = 1; GenData.AddManyTo(videos, TestDataFactory.CreateVideo); using (VODContext context = ContextFactory.CreateContext()) { context.Videos.AddRange(videos); context.SaveChanges(); } List <Video> expectedVideos = videos.Take(2).ToList(); //Act IEnumerable <Video> result = await RepoUnderTest.GetVideosByKindIdAsync(expectedKind.Id); //Assert result.Should().BeEquivalentTo(expectedVideos, o => o.Excluding(x => x.Kind) .Excluding(x => x.Genre)); }
public RoleClaimsRepositoryTest() { var factory = new ContextFactory(); context = factory.CreateContext(); unitOfWork = new UnitOfWork(context); }
public void DeletePerson(int personId, DateTime?lastModified) { using (var context = ContextFactory.CreateContext()) { var repository = RepositoryFactory.CreatePersonRepository(context); repository.Delete(new Model.Person { PersonId = personId, LastModified = lastModified }); try { context.SaveChanges(); } catch (OptimisticConcurrencyException ex) { var person = repository.Get(personId); if (person != null) { throw new DataException("Concurrency", ex); } } } }
public Person UpdatePerson(int personId, string firstName, string lastName, DateTime birthday, string email, string phone, DateTime?lastModified) { using (var context = ContextFactory.CreateContext()) { var repository = RepositoryFactory.CreatePersonRepository(context); var updatedPerson = new Model.Person { PersonId = personId, FirstName = firstName, LastName = lastName, Birthday = birthday, Email = email, Phone = phone, LastModified = lastModified }; repository.Update(updatedPerson); try { context.SaveChanges(); return(updatedPerson); } catch (OptimisticConcurrencyException ex) { throw new DataException("Concurrency", ex); } } }
public async Task Should_return_the_videos_with_specified_genre() { //Arrange Genre expectedGenre = TestDataFactory.CreateGenre(); Genre genre = TestDataFactory.CreateGenre(); List <Video> videos = new List <Video>() { TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), expectedGenre), TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), expectedGenre), TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), genre) }; using (VODContext context = ContextFactory.CreateContext()) { context.Videos.AddRange(videos); context.SaveChanges(); } List <Video> expectedVideos = videos.Take(2).ToList(); //Act IEnumerable <Video> result = await RepoUnderTest.GetVideosByGenreIdAsync(expectedGenre.Id); //Assert result.Should().BeEquivalentTo(expectedVideos, o => o.Excluding(x => x.Kind) .Excluding(x => x.Genre)); }
public async Task Should_not_return_inactive_videos() { //Arrange Genre expectedGenre = TestDataFactory.CreateGenre(); Genre genre = TestDataFactory.CreateGenre(); List <Video> videos = new List <Video>() { TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), expectedGenre), TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), expectedGenre), TestDataFactory.CreateVideo(TestDataFactory.CreateKind(), genre) }; videos.First().IsInactive = true; using (VODContext context = ContextFactory.CreateContext()) { context.Videos.AddRange(videos); context.SaveChanges(); } //Act IEnumerable <Video> result = await RepoUnderTest.GetVideosByGenreIdAsync(expectedGenre.Id); //Assert result.Any(x => x.IsInactive).Should().BeFalse(); }
public void NewPasswordShouldBeAddedToExistingUser() { using (var factory = new ContextFactory()) { using (var unitofwork = new UnitOfWork(factory.CreateContext())) { // Arrange var newUser = new User { Email = "*****@*****.**" }; unitofwork.Users.Add(newUser); unitofwork.Complete(); // Act var pwHash = "sda876h65"; var foundUser = unitofwork.Users.GetByUserName(newUser.Email); unitofwork.Users.AddPassword(foundUser, pwHash); unitofwork.Complete(); // Assert var passwordEntity = unitofwork.Passwords.Get(foundUser.PasswordId.Value); Assert.NotNull(passwordEntity); Assert.Equal(pwHash, passwordEntity.Hash); } } }
public async Task Should_return_empty_collection() { //Arrange string title = "Expected Title"; string titleToSearch = "not existing"; Video expectedVideo0 = TestDataFactory.CreateVideo(); expectedVideo0.Title = title; Video expectedVideo1 = TestDataFactory.CreateVideo(); expectedVideo1.Title = title; List <Video> videos = new List <Video>(2) { expectedVideo0, expectedVideo1 }; using (VODContext context = ContextFactory.CreateContext()) { context.Videos.AddRange(videos); context.SaveChanges(); } //Act IEnumerable <Video> result = await RepoUnderTest.GetVideosContainTitleAsync(titleToSearch); //Assert Assert.Empty(result); }
protected void ASPxButton1_Click(object sender, EventArgs e) { string userName = Request.Form["txtUserName"]; string password = Request.Form["txtPassword"]; if (string.IsNullOrEmpty(userName)) { Response.Write("<script>alert('必须输入用户名!');</script>"); return; } if (string.IsNullOrEmpty(password)) { Response.Write("<script>alert('必须输入密码!');</script>"); return; } // 密码MD5加密 password = FrmUtil.CalculateMD5Hash(password); using (var context = ContextFactory.CreateContext <User>()) { var user = context.Set <User>() .FirstOrDefault(u => u.UserName == userName && u.Password == password); if (user != null) { Session["MyUserName"] = userName; Response.Redirect("~/Default.aspx"); } else { Response.Write("<script>alert('用户名或密码错误!');</script>"); } } }
public void Adds_Password() { using (var factory = new ContextFactory()) { using (var unitofwork = new UnitOfWork(factory.CreateContext())) { // Arrange var expectedHash = "b7tk4uia"; Password expectedPassword = new Password { PasswordId = new Guid(), Hash = expectedHash }; // Act unitofwork.Passwords.Add(expectedPassword); unitofwork.Complete(); // SaveChanges() // Assert Password actualPassword = unitofwork.Passwords.Single(password => password.Hash == expectedHash); string actualHash = actualPassword.Hash; Assert.Equal(expectedHash, actualHash); Assert.Equal(expectedPassword, actualPassword); } } }
public override Entity.OA_Attachment Add(Entity.OA_Attachment entity) { using (DataContext cxt = ContextFactory.CreateContext()) { try { Table <FineOffice.Entity.OA_FlowRunProcess> runProcess = cxt.GetTable <FineOffice.Entity.OA_FlowRunProcess>(); Table <FineOffice.Entity.OA_Attachment> attachment = cxt.GetTable <FineOffice.Entity.OA_Attachment>(); attachment.InsertOnSubmit(entity); if (entity.RunProcessID != null) { FineOffice.Entity.OA_FlowRunProcess temp = runProcess.Where(t => t.ID == entity.RunProcessID).FirstOrDefault(); if (temp.State != 0) { throw new Exception("该流程已办理,不能操作!"); } } cxt.SubmitChanges(); return(entity); } catch (Exception ex) { throw new Exception(ex.Message); } } }
/// <summary> /// 重写Update方法 /// </summary> /// <param name="entity"></param> /// <returns></returns> public override Entity.SYS_MenuList Update(Entity.SYS_MenuList entity) { using (DataContext cxt = ContextFactory.CreateContext()) { try { Table <FineOffice.Entity.SYS_MenuList> authority = cxt.GetTable <FineOffice.Entity.SYS_MenuList>(); authority.Attach(entity, true); if (entity.ParentID != 0) { Entity.SYS_MenuList temp = authority.Where(d => d.ID == entity.ParentID).FirstOrDefault(); if (temp == null) { throw new Exception("父级菜单不存在!"); } } cxt.SubmitChanges(ConflictMode.ContinueOnConflict); } catch (ChangeConflictException) { cxt.Refresh(RefreshMode.KeepChanges, entity); cxt.SubmitChanges(ConflictMode.FailOnFirstConflict); } catch (Exception ex) { throw new Exception(ex.Message); } return(entity); } }
public void TransferToComAttachment(List <int> ids) { using (DataContext cxt = ContextFactory.CreateContext()) { try { Table <FineOffice.Entity.OA_Attachment> attachment = cxt.GetTable <FineOffice.Entity.OA_Attachment>(); Table <FineOffice.Entity.HD_Attachment> comAtt = cxt.GetTable <FineOffice.Entity.HD_Attachment>(); List <FineOffice.Entity.HD_Attachment> list = new List <Entity.HD_Attachment>(); foreach (int id in ids) { FineOffice.Entity.OA_Attachment entity = attachment.Where(d => d.ID == id).FirstOrDefault(); FineOffice.Entity.HD_Attachment att = new Entity.HD_Attachment { AttachmentData = entity.AttachmentData, CreateTime = entity.CreateTime, FileName = entity.FileName, PersonnelID = entity.HR_Personnel.ID, Remark = entity.Remark, XType = entity.XType, XTypeName = entity.XTypeName, Size = entity.Size }; list.Add(att); } comAtt.InsertAllOnSubmit(list); cxt.SubmitChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } } }
private void DoWork() { while (m_canceled == false) { if (m_paused) { Sleep(); continue; } try { using (var db = ContextFactory.CreateContext()) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.AutoDetectChangesEnabled = false; var schedules = GetActiveSchedules(db); ProcessSchedules(schedules, db); } } catch (Exception e) { m_logger.Error(e); } Sleep(); } }
/// <summary> /// 返回已解决的步骤 /// </summary> /// <param name="flowRun"></param> /// <returns></returns> public List <FineOffice.Modules.OA_FlowRunProcess> FlowRunProcessList(Modules.OA_FlowRun flowRun) { using (DataContext cxt = ContextFactory.CreateContext()) { Table <FineOffice.Entity.OA_FlowRunProcess> runProcess = cxt.GetTable <FineOffice.Entity.OA_FlowRunProcess>(); Table <FineOffice.Entity.OA_FlowRunTransmit> transmit = cxt.GetTable <FineOffice.Entity.OA_FlowRunTransmit>(); FineOffice.Entity.OA_FlowRunProcess tempProcess = runProcess.Where(p => p.RunID == flowRun.ID && p.IsEntrance == true).FirstOrDefault(); List <FineOffice.Entity.OA_FlowRunTransmit> transmitList = transmit.Where(p => p.RunProcessID == tempProcess.ID && p.Pattern == 1).ToList(); List <FineOffice.Entity.OA_FlowRunProcess> list = new List <Entity.OA_FlowRunProcess>(); list.Add(tempProcess); foreach (FineOffice.Entity.OA_FlowRunTransmit temp in transmitList) { FineOffice.Entity.OA_FlowRunProcess runp = runProcess.Where(p => p.ID == temp.TransmitTo).FirstOrDefault(); list.Add(runp); this.AddRunProcess(cxt, runp, list); } List <FineOffice.Modules.OA_FlowRunProcess> result = (from p in list select new FineOffice.Modules.OA_FlowRunProcess { ID = p.ID, AcceptTime = p.AcceptTime, HandleTime = p.HandleTime, IsEnd = p.OA_FlowProcess.IsEnd, IsStart = p.OA_FlowProcess.IsStart, ProcessName = p.OA_FlowProcess.ProcessName, State = p.State, }).ToList(); return(result); } }
public void Should_update_and_return_the_specified_video() { // Arrange Kind newKind = TestDataFactory.CreateKind(); Genre newGenre = TestDataFactory.CreateGenre(); Video oldVideo = TestDataFactory.CreateVideo(); using (VODContext context = ContextFactory.CreateContext()) { context.Videos.Add(oldVideo); context.Genres.Add(newGenre); context.Kinds.Add(newKind); context.SaveChanges(); } Video newVideo = TestDataFactory.CreateVideo(newKind, newGenre); newVideo.Id = oldVideo.Id; //Act Video result = RepoUnderTest.Update(newVideo); //Assert result.Should().BeEquivalentTo(newVideo); }
public async Task Should_not_return_inactive_videos() { //Arrange Kind expectedKind = TestDataFactory.CreateKind(); List <Video> videos = new List <Video>() { TestDataFactory.CreateVideo(expectedKind), TestDataFactory.CreateVideo(expectedKind), }; GenData.RepeatCount = 1; GenData.AddManyTo(videos, TestDataFactory.CreateVideo); videos.First().IsInactive = true; using (VODContext context = ContextFactory.CreateContext()) { context.Videos.AddRange(videos); context.SaveChanges(); } //Act IEnumerable <Video> result = await RepoUnderTest.GetVideosByKindIdAsync(expectedKind.Id); //Assert result.Any(x => x.IsInactive).Should().BeFalse(); }
public void CanUpdateUser() { using (var factory = new ContextFactory()) { using (var unitofwork = new UnitOfWork(factory.CreateContext())) { // Arrange var user = new User { Email = "*****@*****.**", PhoneNumber = "981278921" }; unitofwork.Users.Add(user); unitofwork.Complete(); //var foundUser = unitofwork.Users.Single(usr => usr.Email == user.Email); var oldEmail = user.Email; var newEmail = "*****@*****.**"; //foundUser.Email = newEmail; user.Email = newEmail; // Act unitofwork.Users.Update(user); unitofwork.Complete(); // Assert Assert.Null(unitofwork.Users.Single(usr => usr.Email == oldEmail)); Assert.NotNull(unitofwork.Users.Single(usr => usr.Email == newEmail)); } } }
public void Explore() { if (!_assembly.ReferencedAssembliesNames.Any(x => String.Equals( #if RESHARPER_6 x.Name, #else x.AssemblyName.Name, #endif typeof(It).Assembly.GetName().Name, StringComparison.InvariantCultureIgnoreCase))) { return; } _assembly.GetTypes().Where(type => type.IsContext()).ForEach(type => { var contextElement = _contextFactory.CreateContext(type); _consumer(contextElement); type .GetSpecifications() .ForEach(x => _consumer(_contextSpecificationFactory.CreateContextSpecification(contextElement, x))); type.GetBehaviors().ForEach(x => { var behaviorElement = _behaviorFactory.CreateBehavior(contextElement, x); _consumer(behaviorElement); _behaviorSpecificationFactory .CreateBehaviorSpecificationsFromBehavior(behaviorElement, x) .ForEach(y => _consumer(y)); }); }); }
public UserManagerTest() { var factory = new ContextFactory(); context = factory.CreateContext(); var unitOfWork = new UnitOfWork(context); }
/// <summary> /// 重写add方法 /// </summary> public override Entity.CRM_Area Add(Entity.CRM_Area entity) { using (DataContext cxt = ContextFactory.CreateContext()) { Table <FineOffice.Entity.CRM_Area> area = cxt.GetTable <FineOffice.Entity.CRM_Area>(); try { area.InsertOnSubmit(entity); if (entity.ParentID != 0) { Entity.CRM_Area temp = area.Where(d => d.ID == entity.ParentID).FirstOrDefault(); if (temp == null) { throw new Exception("所属地区信息不存在!"); } } cxt.SubmitChanges(); return(entity); } catch (Exception ex) { throw new Exception(ex.Message); } } }
/// <summary> /// GirdView的编辑按钮事件 /// </summary> protected void grdView_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e) { if (e.ButtonID.Equals("btnDelete")) { // 表格“删除” string UserName, msg = ""; UserName = grdView.GetRowValues(e.VisibleIndex, "UserName").ToString(); using (var context = ContextFactory.CreateContext <User>()) { var user = context.Set <User>().FirstOrDefault(u => u.UserName == UserName); context.Set <User>().Remove(user); int rtn = context.SaveChanges(); if (rtn == 1) { msg = "操作成功!"; loadUserInfo(); } else { msg = "操作失败!"; } grdView.JSProperties["cpMsg"] = msg; } } }
/// <summary> /// 重写update方法 /// </summary> public override Entity.CRM_Area Update(Entity.CRM_Area entity) { using (DataContext cxt = ContextFactory.CreateContext()) { Table <FineOffice.Entity.CRM_Area> area = cxt.GetTable <FineOffice.Entity.CRM_Area>(); try { area.Attach(entity, true); if (entity.ParentID != 0) { Entity.CRM_Area temp = area.Where(d => d.ID == entity.ParentID).FirstOrDefault(); if (temp == null) { throw new Exception("所属地区信息不存在!"); } } List <Entity.CRM_Area> tempList = this.GetSubList(entity); if (tempList.Where(a => a.ID == entity.ParentID).Count() > 0) { throw new Exception("所属地区信息不能是自己或者是其子地区信息!"); } cxt.SubmitChanges(); return(entity); } catch (Exception ex) { throw new Exception(ex.Message); } } }
public Model.Person FindPerson(int personId) { using (var context = ContextFactory.CreateContext()) { var repository = RepositoryFactory.CreatePersonRepository(context); return(repository.Get(personId)); } }
public IEnumerable <Account> GetAll() { using (var context = ContextFactory.CreateContext()) { return(context.Accounts .ToList()); } }
public virtual IContext GetContext() { IContext context = ContextFactory.CreateContext(); context.AutoTransactions = false; return(context); }
public async Task <IEnumerable <UserProject> > GetProjects(int managerId) { using (IWorkFlowDbContext context = ContextFactory.CreateContext()) { return(await context.UserProject .Where(p => p.CreatorId == managerId) .ToListAsync()); } }
public async Task <IEnumerable <UserReport> > GetReports(int userId) { using (IWorkFlowDbContext context = ContextFactory.CreateContext()) { return(await context.UserReport .Where(r => r.UserId == userId) .ToListAsync()); } }
/// <summary> /// 更改流程明细列表 /// </summary> public FineOffice.Entity.OA_Flow UpdateProcess(FineOffice.Entity.OA_Flow entity) { using (DataContext cxt = ContextFactory.CreateContext()) { Table <FineOffice.Entity.OA_Flow> flow = cxt.GetTable <FineOffice.Entity.OA_Flow>(); Table <FineOffice.Entity.OA_FlowProcess> flowProcess = cxt.GetTable <FineOffice.Entity.OA_FlowProcess>(); try { flow.Attach(entity, true); flowProcess.AttachAll(entity.OA_FlowProcess, true); //以当前实体更新数据 flowProcess.DeleteAllOnSubmit(flowProcess.Where(f => f.FlowID == entity.ID && !(from e in entity.OA_FlowProcess select e.ID).Contains(f.ID))); //删除数据 cxt.SubmitChanges(ConflictMode.ContinueOnConflict); //乐观并发 } catch (ChangeConflictException) { foreach (ObjectChangeConflict occ in cxt.ChangeConflicts) { FineOffice.Entity.OA_FlowProcess newDetail = occ.Object as FineOffice.Entity.OA_FlowProcess; if (occ.IsDeleted) { if (newDetail != null) { flowProcess.InsertOnSubmit(Changes(newDetail)); } } else { foreach (MemberChangeConflict mc in occ.MemberConflicts) { MemberInfo mi = mc.Member; string memberName = mi.Name; if (memberName == "ProcessName" || memberName == "Remark" || memberName == "Next") { mc.Resolve(RefreshMode.KeepCurrentValues); } else { mc.Resolve(RefreshMode.OverwriteCurrentValues); } } } occ.Resolve(); } cxt.SubmitChanges(ConflictMode.FailOnFirstConflict); } catch (Exception ex) { throw new Exception(ex.Message); } return(flow.Where(f => f.ID == entity.ID).FirstOrDefault()); } }
/// <summary> /// 加载用户清单到表格 /// </summary> private void loadUserInfo() { using (var context = ContextFactory.CreateContext <User>()) { context.InputParams["UserNameLike"] = txtQryUserName.Text; context.InputParams["NameLike"] = txtQryName.Text; var users = context.SelectBySP <User>(); grdView.DataSource = users; grdView.DataBind(); } }