public void AddAgent() { using (CapriconContext context = new CapriconContext()) { var newAgent = new Agent() { FirstName = "Blaise", LastName = "Nyamwamba", OtherName = "", Gender = Gender.Female, MobilePhone = "0756 123 456", Email = "*****@*****.**", Town = "Nairobi", District = "Nairobi", DateOfBirth = DateTime.Now.AddYears(-40), }; var agentRep = new AgentRepository(context); agentRep.Add(newAgent); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } //retrieve saved object var context1 = new CapriconContext(); var repository = new AgentRepository(context1); var savedAgents = repository.GetAll().ToList(); Assert.AreEqual(savedAgents.Count(), 1, "returns 20 records"); var savedAgentsList = savedAgents; savedAgentsList.ForEach ( s => { Debug.WriteLine(s.AgentId + " - " + s.FirstName + " " + s.LastName); } ); }; }
public void AddAgentMessage() { using (var uow = new CapriconContext()) { //retreive an existing agent var agentRepository = new AgentRepository(uow); var existingAgent = agentRepository.GetAll().FirstOrDefault(); Assert.IsNotNull(existingAgent); //retreive an existing message var messageRepository = new MessageRepository(uow); var existingMessage = messageRepository.GetAll().LastOrDefault(); Assert.IsNotNull(existingMessage); //create new agent messsage var newAgentMessage = new AgentMessage() { Agent = existingAgent, Message = existingMessage }; //add the new agent message to the repository var agentMessageRepository = new AgentMessageRepository(uow); agentMessageRepository.Add(newAgentMessage); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } //retrieve saved object var uow1 = new CapriconContext(); var repository = new AgentMessageRepository(uow1); var savedAgentMessages = repository.GetAll().ToList(); Assert.AreEqual(savedAgentMessages[0].Agent.FirstName, existingAgent.FirstName = "Blaise"); Assert.AreEqual(savedAgentMessages[0].Message.MessageId, existingMessage.MessageId = 1); }; }
public void AgentRepositoryTest() { AgentRepository repository = new AgentRepository(); Agent expectedAgent = new Agent { name = "agensomeagent" }; repository.Add(expectedAgent); Agent realAgent = repository.GetAll().Last(); Assert.AreEqual(expectedAgent.name, realAgent.name); repository.Delete(realAgent); realAgent = repository.GetAll().Last(); Assert.AreNotEqual(expectedAgent.name, realAgent.name); }
public void AgentRepositoryTest() { AgentRepository repository = new AgentRepository(); Agent expectedAgent = new Agent(repository.GetAll().Count() + 1, "agensomeagent"); repository.Add(expectedAgent); Agent realAgent = repository.GetItem(expectedAgent.id); Assert.AreEqual(expectedAgent, realAgent); repository.Delete(expectedAgent); realAgent = repository.GetItem(expectedAgent.id); Assert.AreEqual(null, realAgent); }
// // GET: /Agent/ public ActionResult Index() { AgentRepository repository = new AgentRepository(); var agents = repository.GetAll(); var agentsToShow = new List <EditAgent>(); foreach (var agent in agents) { agentsToShow.Add ( new EditAgent { agentId = agent.id, agentName = agent.name } ); } return(View(agentsToShow)); }
// // GET: /Agent/ public ActionResult Index() { AgentRepository repository = new AgentRepository(); var agents = repository.GetAll(); var agentsToShow = new List<EditAgent>(); foreach (var agent in agents) { agentsToShow.Add ( new EditAgent { agentId = agent.id, agentName = agent.name } ); } return View(agentsToShow); }
public IEnumerable <Agent> GetAll() { return(agentRepo.GetAll()); }
public void UpdateAgent() { using (var uow = new CapriconContext()) { var agentRep = new AgentRepository(uow); var existingAgents = agentRep.GetAll().ToList(); var existingAgent = existingAgents.LastOrDefault(); Assert.IsNotNull(existingAgent); existingAgent.FirstName = "Hilda"; existingAgent.LastName = "Kunda"; existingAgent.OtherName = ""; existingAgent.Gender = Gender.Male; existingAgent.DateOfBirth = DateTime.Now.AddYears(-60); existingAgent.MobilePhone = "0777 700 700"; existingAgent.Email = "*****@*****.**"; existingAgent.Town = "Bugembe"; existingAgent.District = "Jinja"; //check for validation rules //existingAgent.FirstName = ""; //existingAgent.LastName = ""; //existingAgent.Gender = Gender.Not_Specified; //existingAgent.Email = ""; //existingAgent.Town = ""; //existingAgent.District = ""; agentRep.Attach(existingAgent); uow.Entry(existingAgent).State = EntityState.Modified; try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } }; //retrieve saved object var uow1 = new CapriconContext(); var repository = new AgentRepository(uow1); var savedAgents = repository.GetAll().ToList(); Assert.AreEqual(savedAgents[0].LastName, "Byanjeru"); }