public void Query_CanQueryWithSingleTagId_NoNoteAssignedToGivenTagId() { 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 } }; context.Notes.AddRange(notes); context.Tags.AddRange(tags); context.NoteTags.AddRange(noteTags); context.SaveChanges(); } using (var context = new NexusContext(options)) { int[] tagIds = { 3 }; NotesByTagsQuery query = CreateQuery(context, tagIds); List <Note> resultList = query.Query().ToList(); Assert.Empty(resultList); } }
public void CanUpdateRangeOfBooks() { string databaseName = Guid.NewGuid().ToString(); using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { context.Books.AddRange(GetBooks()); context.SaveChanges(); } var updateRepository = CreateNewGenericRepositoryWithNewContext <Book>(databaseName); var savedBooks = updateRepository.GetAll().ToList(); foreach (var savedBook in savedBooks) { savedBook.Title = "MODIFIED"; } updateRepository.Update(savedBooks); updateRepository.UnitOfWork.SaveChanges(); using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { foreach (var book in context.Books.ToList()) { Assert.Equal("MODIFIED", book.Title); } } }
public void CanDeleteBookById() { ReInitMapper(); string databaseName = Guid.NewGuid().ToString(); int id = 0; using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var entity = new Book { Author = "adam smith" }; context.Books.Add(entity); context.SaveChanges(); id = entity.Id; } using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var service = new Service <Book, BookDto>(new Repository <Book>(context)); var dto = service.Get(id); service.Delete(dto); } using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var service = new Service <Book, BookDto>(new Repository <Book>(context)); var dto = service.Get(id); Assert.Null(dto); } }
public void Add_DtosShouldHaveSameIdsWithCorrespondingEntitiesAfterInsertionToDb_DtosIdsShouldntBeDefaultValue() { ReInitMapper(); var dtos = new List <TagDto> { new TagDto { Id = 0, Title = "XXX" }, new TagDto { Id = 0, Title = "YYY" } }; using (var context = new NexusContext(InMemoryHelpers.CreateOptions("luminescent"))) { var service = new Service <Tag, TagDto>(new Repository <Tag>(context)); service.Add(dtos); var idx = context.Tags.Single(t => t.Title == "XXX").Id; var idy = context.Tags.Single(t => t.Title == "YYY").Id; Assert.Equal(idx, dtos.First().Id); Assert.Equal(idy, dtos.Last().Id); } }
public void UpdateNoteTags_NoteHasNoTags_AddingBrandNewTagsToAnExistingNote() { string databaseName = Guid.NewGuid().ToString(); var options = InMemoryHelpers.CreateOptions(databaseName); using (var context = new NexusContext(options)) { var note = DataProvider.CreateNote(); context.Notes.Add(note); context.SaveChanges(); } using (var context = new NexusContext(options)) { var repo = new NoteRepository(context); var tagsToBeAdded = DataProvider.GetAlphabeticalTags(); var note = repo.GetAll().First(); repo.UpdateNoteTags(note.Id, tagsToBeAdded.Select(t => t.Title)); repo.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"); } }
public void Add_DtoShouldHaveSameIdWithTheEntityAfterInsertionToDb_IdShouldNotBeDefaultValue() { ReInitMapper(); using (var context = new NexusContext(InMemoryHelpers.CreateOptions("nascence"))) { var service = new Service <Tag, TagDto>(new Repository <Tag>(context)); var dto = new TagDto { Id = 0, Title = "mir" }; service.Add(dto); Assert.True(dto.Id != default(int)); } }
public void CanRetrieveBooksAsync() { using (var context = new NexusContext(InMemoryHelpers.CreateOptions("dbx"))) { context.Books.AddRange(GetBooks()); // two books context.SaveChanges(); } Repository <Book> repository = CreateNewGenericRepositoryWithNewContext <Book>("dbx"); var books = repository.GetAllAsync().Result; Assert.True(books.Count() == 2); }
public void GetFiltered_CanQueryWithSinglePredicate() { NexusContext context = new NexusContext(InMemoryHelpers.CreateOptions(Guid.NewGuid().ToString())); context.Books.Add(new Book() { Author = "auth 1" }); context.SaveChanges(); Repository <Book> bookGenericRepository = new Repository <Book>(context); var books = bookGenericRepository.GetFiltered(b => b.Author.Contains("auth")).ToList(); Assert.True(books.Any()); }
public void CanInsertBook() { var bookGenericRepository = CreateNewGenericRepositoryWithNewContext <Book>("a.db"); Book book = GetBooks(initIdField: false).First(); bookGenericRepository.Add(book); bookGenericRepository.UnitOfWork.SaveChanges(); using (var c = new NexusContext(InMemoryHelpers.CreateOptions("a.db"))) { var recentlySavedBook = c.Books.FirstOrDefault(); Assert.True(recentlySavedBook != null); } }
public void ShouldntWriteAnythingWhenEmptyTagListIsGiven() { var options = InMemoryHelpers.CreateOptions("ShouldntWriteAnythingWhenEmptyTagListIsGiven"); using (var context = new NexusContext(options)) { var tags = Enumerable.Empty <string>(); AddTagsCommand command = new AddTagsCommand(context, tags); command.Execute(); } using (var context = new NexusContext(options)) { var tags = context.Tags.ToList(); Assert.False(tags.Any()); } }
public void CanWriteMoreThanOneTag() { var options = InMemoryHelpers.CreateOptions("CanWriteMoreThanOneTag"); using (var context = new NexusContext(options)) { var tags = GetTags(3); AddTagsCommand command = new AddTagsCommand(context, tags); command.Execute(); } using (var context = new NexusContext(options)) { var tags = context.Tags.ToList(); Assert.True(tags.Count == 3); } }
public void Get_CanRetrieveExistingBook() { string databaseName = Guid.NewGuid().ToString(); int bookId; using (var context = new NexusContext(InMemoryHelpers.CreateOptions(databaseName))) { var book = new Book(); context.Books.Add(book); context.SaveChanges(); bookId = book.Id; } var genericRepository = CreateNewGenericRepositoryWithNewContext <Book>(databaseName); var recentlySavedBook = genericRepository.Get(bookId); Assert.NotNull(recentlySavedBook); }
public void ExtendQuery_ExtendingQueryWithEmptyTagsList_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.ExtendQuery(context.Notes.AsQueryable()).ToList(); Assert.Empty(resultList); } }
public void Query_CanQueryWithMultipleTagIds_() { 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 { TagId = 2, NoteId = 2 }, new NoteTag { 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 = { 2, 3 }; NotesByTagsQuery query = CreateQuery(context, tagIds); List <Note> resultList = query.Query().ToList(); Assert.Equal(2, resultList.Count); Assert.Contains(resultList, note => note.Id == 2 && note.Title == "Note 2"); Assert.Contains(resultList, note => note.Id == 3 && note.Title == "Note 3"); Assert.DoesNotContain(resultList, note => note.Id == 1 && note.Title == "Note 1"); } }
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 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 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); }
private Repository <T> CreateNewGenericRepositoryWithNewContext <T>(string databaseName) where T : class, IEntity { return(new Repository <T>(new NexusContext(InMemoryHelpers.CreateOptions(databaseName)))); }