public void GlosssaryServicesShouldReturnTheListOfItems() { var options = new DbContextOptionsBuilder <GlossaryDbContext>() .UseInMemoryDatabase(databaseName: "List_database") .Options; using (var context = new GlossaryDbContext(options)) { var service1 = new GlossaryService(context); var t1 = Task.Run(async() => await service1.Create(Fake1)).Result; var t2 = Task.Run(async() => await service1.Create(Fake2)).Result; var t3 = Task.Run(async() => await service1.Create(Fake3)).Result; } var items = new List <GlossaryItem>(); // Use a separate instance of the context to verify correct data was saved to database using (var context1 = new GlossaryDbContext(options)) { var service2 = new GlossaryService(context1); items = Task.Run(async() => await service2.Get("")).Result; Assert.AreEqual(items.Count, 3); } }
public void GlossaryServiceShouldAddNewItemAndSearch() { using (var context = new GlossaryDbContext(Options)) { var service = new GlossaryService(context); var t = Task.Run(async() => await service.Create(Fake1)).Result; } GlossaryItem Inserted; // Use a separate instance of the context to verify correct data was saved to database using (var context = new GlossaryDbContext(Options)) { var service = new GlossaryService(context); Inserted = Task.Run(async() => await service.Get(Fake1.Id)).Result; Assert.AreEqual(Fake1.Id, Inserted.Id); } }
public void GlosssaryServicesShouldUpdateAndDeleteanItem() { using (var context = new GlossaryDbContext(Options)) { var service = new GlossaryService(context); var t = Task.Run(async() => await service.Create(Fake1)).Result; } GlossaryItem Updated; // Use a separate instance of the context to verify correct data was saved to database using (var context = new GlossaryDbContext(Options)) { var service = new GlossaryService(context); var i = Task.Run(async() => await service.Update(new GlossaryItem { Id = Fake1.Id, Term = "Updated", Definition = "Updated" })).Result; Updated = Task.Run(async() => await service.Get(Fake1.Id)).Result; Assert.AreEqual(Updated.Id, Fake1.Id); Assert.AreEqual(Updated.Term, "Updated"); Assert.AreEqual(Updated.Definition, "Updated"); } }
public int UpdateTerm(string term, string definition) { if (String.IsNullOrEmpty(term)) { return(-1); } try { Glossary glossary = _glossaryService.Find(term); //Glossary does not exist, so create it if (glossary == null) { glossary = new Glossary(); glossary.Term = term; glossary.Definition = definition; _glossaryService.Create(glossary); } else { // Glossary exists, but empty definition, so delete if (String.IsNullOrEmpty(definition)) { return(DeleteTerm(glossary)); } // Gossary exists, and valid definition, so update else { glossary.Definition = definition; _glossaryService.Update(glossary.Id, glossary); } } return(0); } catch (Exception) { // @todo Add more descriptive error message handling / logging return(-1); } }