public void Query_QueryingWithEmptyTagsList_ShouldReturnAnEmptyListOfNotes() { var options = InMemoryHelpers.CreateOptions(); using (var context = new NexusContext(options)) { List <Note> notes = DataProvider.CreateNotes(initIdField: true, count: 3); List <Tag> tags = DataProvider.CreateTags(initField: true, count: 3); List <NoteTag> noteTags = new List <NoteTag>() { new NoteTag() { Id = 1, TagId = 1, NoteId = 1 }, new NoteTag() { Id = 2, TagId = 2, NoteId = 2 }, new NoteTag() { Id = 3, TagId = 3, NoteId = 3 } }; context.Notes.AddRange(notes); context.Tags.AddRange(tags); context.NoteTags.AddRange(noteTags); context.SaveChanges(); } using (var context = new NexusContext(options)) { int[] tagIds = { }; NotesByTagsQuery query = CreateQuery(context, tagIds); List <Note> resultList = query.Query().ToList(); Assert.Empty(resultList); } }
public void CanUpdateBook() { string databaseName = Guid.NewGuid().ToString(); using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { context.Books.AddRange(GetBooks()); context.SaveChanges(); } var repository = CreateNewGenericRepositoryWithNewContext <Book>(databaseName); ; Book book = repository.Get(1); book.Title = "UPDATED"; repository.Update(book); repository.UnitOfWork.SaveChanges(); Assert.Equal("UPDATED", repository.Get(book.Id).Title); }
public void CanDeleteBook() { int bookId; string databaseName = Guid.NewGuid().ToString(); string presudoTitle = Guid.NewGuid().ToString(); using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var entity = new Book { Title = presudoTitle }; context.Books.Add(entity); context.SaveChanges(); bookId = entity.Id; } using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var repository = new Repository <Book>(context); var book = new Book() { Id = bookId }; repository.Delete(book); repository.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var repository = new Repository <Book>(context); Book b = repository.GetFiltered(book => book.Id == bookId && book.Title == presudoTitle) .FirstOrDefault(); Assert.Null(b); } }
public void GetFiltered_CanQueryWithMultiplePredicate() { NexusContext context = new NexusContext(InMemoryHelpers.CreateOptions(Guid.NewGuid().ToString())); var b1 = new Book() { Author = "auth", Title = "book 1" }; var b2 = new Book() { Author = "auth", Title = "book 2" }; context.Books.Add(b1); context.Books.Add(b2); context.SaveChanges(); Repository <Book> bookGenericRepository = new Repository <Book>(context); var books = bookGenericRepository.GetFiltered(b => b.Author.Contains("auth") && b.Title == "book 1") .ToList(); Assert.True(books.Count == 1); }
public void UpdateNoteTags_AddingAllTheExistingTagsToAnExistingNoteThatHasNoTags() { string databaseName = Guid.NewGuid().ToString(); var options = InMemoryHelpers.CreateOptions(databaseName); using (var context = new NexusContext(options)) { var note = DataProvider.CreateNote(); var tags = DataProvider.GetAlphabeticalTags(); context.Notes.Add(note); context.Tags.AddRange(tags); context.SaveChanges(); } using (var context = new NexusContext(options)) { var noteRepo = new NoteRepository(context); var note = noteRepo.GetAll().Single(); noteRepo.UpdateNoteTags(note.Id, DataProvider.GetAlphabeticalTags().Select(t => t.Title)); noteRepo.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(options)) { var note = context.Notes.Include(n => n.NoteTags).ThenInclude(nt => nt.Tag).First(); Assert.Equal(3, note.NoteTags.Count); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "A"); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "B"); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "C"); } }
private Repository <T> CreateNewGenericRepositoryWithNewContext <T>(string databaseName) where T : class, IEntity { return(new Repository <T>(new NexusContext(InMemoryHelpers.CreateOptions(databaseName)))); }