public void RecreatingTheDatabaseDoesRecreateIt() { var rnd = new Random().Next(); var person = new Person() { FirstName = $"TEST PERSON {rnd}", }; // initialize the database, round 1 PeopleSearchContext.Initialize(); // now, add a unique person to the database, so we can be sure it DID NOT SURVIVE using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { ctx.People.Add(person); ctx.SaveChanges(); } // make sure we got a valid ID assigned to the test person Assert.True(person.PersonId > 0); // now, initialize the database, round 2, this time, asking for it to be recreated PeopleSearchContext.Initialize(true, true); // finally, we should NOT be able to find the person we added using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { var person2 = ctx.People.SingleOrDefault(p => p.PersonId == person.PersonId); Assert.Null(person2); person2 = ctx.People.SingleOrDefault(p => p.FirstName == person.FirstName); Assert.Null(person2); } }
public void UpdatingAPersonWorks() { // first, clear out the database so we don't have any collisions PeopleSearchContext.Initialize(true, true); using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { // add a person that we can use to update ctx.Add(new Person() { FirstName = "test", LastName = "person" }); ctx.SaveChanges(); // grab the person from the db var person = ctx.People.Single(p => p.FirstName == "test"); // mutate the person person.LastName = "other"; var ctrlr = new PersonController(m_loggerFactory.CreateLogger <PersonController>(), ctx); // use the controller to update the person ctrlr.Index(person.PersonId, person); // re-retrieve the person from the db person = ctx.People.Single(p => p.PersonId == person.PersonId); // ensure the last name was updated Assert.Equal("other", person.LastName); } }
public void InitializingTheDatabaseDoesNotRecreateIt() { var rnd = new Random().Next(); var person = new Person() { FirstName = $"TEST PERSON {rnd}", }; // initialize the database, round 1 PeopleSearchContext.Initialize(); // now, add a unique person to the database, so we can be sure it survived using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { ctx.People.Add(person); ctx.SaveChanges(); } // make sure we got a valid ID assigned to the test person Assert.True(person.PersonId > 0); // now, initialize the database, round 2 PeopleSearchContext.Initialize(); // finally, we should be able to find the person we added using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { var person2 = ctx.People.Single(p => p.PersonId == person.PersonId); Assert.Equal(person.PersonId, person2.PersonId); Assert.Equal(person.FirstName, person2.FirstName); } }
public void SearchWithoutCriteriaReturnsNothing() { // first, clear out the database and add some data real quick PeopleSearchContext.Initialize(true, true); using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { ctx.AddRange(Enumerable.Range(0, 100).Select(i => new Person() { FirstName = $"John {i}", LastName = "Smith" })); ctx.SaveChanges(); // then, make sure none of it is returned var ctrlr = new PersonController(m_loggerFactory.CreateLogger <PersonController>(), ctx); Assert.Empty(ctrlr.Index()); } }
public void SearchWithCriteriaReturnsPeople() { // first, clear out the database and add some data real quick PeopleSearchContext.Initialize(true, true); using (var ctx = new PeopleSearchTestContext(m_loggerFactory)) { ctx.AddRange(Enumerable.Range(0, 100).Select(i => new Person() { FirstName = $"John {i}", LastName = "Smith" })); ctx.AddRange(Enumerable.Range(0, 5).Select(i => new Person() { FirstName = $"John {i}", LastName = "Jones" })); ctx.SaveChanges(); // ensure that the correct number of results are returned var ctrlr = new PersonController(m_loggerFactory.CreateLogger <PersonController>(), ctx); Assert.Equal(5, ctrlr.Index("Jones", 0, 10).Count()); Assert.Equal(5, ctrlr.Index("Jones", 0, 20).Count()); } }