public void SeedBooks() { var books = GetBooks(); _context.Books.AddRange(books); _context.SaveChanges(); }
//public PagedDtoList<TagNoteCountDto> GetTagsWithNoteCount(int page, int pageSize) //{ // IQueryable<NoteTag> noteTags = _context.NoteTags.AsNoTracking().AsQueryable(); // IQueryable<Tag> tags = _context.Tags.AsNoTracking().AsQueryable(); // var query = // from ntag in noteTags // join tag in tags on ntag.TagId equals tag.Id // group ntag by new { tag.Id, tag.Title } into grup // select new TagNoteCountDto // { // Id = grup.Key.Id, // Title = grup.Key.Title, // NoteCount = grup.Count() // }; // //return new PagedList<TagNoteCountDto>(query, page, pageSize); // return null; //} public void Update(TagDto tagDto) { if (tagDto == null) { throw new ArgumentNullException(nameof(tagDto)); } Tag tag = _context.Tags.Find(tagDto.Id); tag = _mapper.Map(tagDto, tag); _context.SaveChanges(); }
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 UpdateNoteTags_RemovingExistingNoteTags() { var options = SQLiteHelpers.CreateOptions(); // SEEDING using (var context = new NexusContext(options)) { context.Database.EnsureCreated(); // Creates the in-memory SQLite database var note = DataProvider.CreateNote().AssignNoteTags(DataProvider.GetAlphabeticalTags()); context.Notes.Add(note); context.SaveChanges(); } using (var context = new NexusContext(options)) { var repo = new NoteRepository(context); repo.UpdateNoteTags(1, new List <string>()); repo.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(options)) { var note = context.Notes.Include(n => n.NoteTags).First(); Assert.False(note.NoteTags.Any()); } }
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 UpdateNoteTags_ModifyingExistingNoteTagsByAddingAndRemovingTags() { // SEEDING using (var context = new NexusContext(_options)) { var note = DataProvider.CreateNote().AssignNoteTags(DataProvider.GetAlphabeticalTags()); context.Notes.Add(note); context.SaveChanges(); } using (var context = new NexusContext(_options)) { var repo = new NoteRepository(context); // Adding D, removing C var note = context.Notes.First(); repo.UpdateNoteTags(note.Id, new[] { "A", "B", "D" }); repo.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(_options)) { var note = context.Notes.Include(n => n.NoteTags).ThenInclude(nt => nt.Tag).First(); var tagTitles = note.NoteTags.Select(nt => nt.Tag.Title).ToList(); Assert.Contains("A", tagTitles); Assert.Contains("B", tagTitles); Assert.Contains("D", tagTitles); Assert.DoesNotContain("C", tagTitles); } }
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 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 UpdateNoteTags_AddingAllTheExistingTagsToAnExistingNoteThatHasNoTags() { 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"); } }
public void UpdateNoteTags_AddingNewTagBeforeAnExistingTag() { using (var context = new NexusContext(_options)) { context.Notes.Add(DataProvider.CreateNote()); context.Tags.AddRange(DataProvider.GetAlphabeticalTags()); context.SaveChanges(); } using (var context = new NexusContext(_options)) { var repo = new NoteRepository(context); var note = repo.GetAll().First(); repo.UpdateNoteTags(note.Id, new List <string>() { "X", "A" }); repo.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(_options)) { var note = context.Notes.Include(n => n.NoteTags).ThenInclude(nt => nt.Tag).First(); Assert.True(note.NoteTags.Count == 2); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "A"); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "X"); } }
public void UpdateNoteTags_GivenTheSameNoteTagsToUpdateNotesTags() { using (var context = new NexusContext(_options)) { context.Database.EnsureCreated(); var note = DataProvider.CreateNote(); var tags = DataProvider.GetAlphabeticalTags(); note.AssignNoteTags(tags); context.Notes.Add(note); 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"); } }
public static void SeedTestDb(NexusContext context) { context.PersonalDetails.Add( new PersonalDetails { Id = 1, FirstName = "Jon", LastName = "Karlsen", Biography = "Once upon a time", JobTitle = "Back-End Web Dev" }); context.SaveChanges(); }
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 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 UpdateNoteTags_NoteHasTags_AddingNewANDExistingTag() { var options = SQLiteHelpers.CreateOptions(); using (var context = new NexusContext(options)) { context.Database.EnsureCreated(); context.Notes.Add(DataProvider.CreateNote().AssignNoteTags(DataProvider.GetAlphabeticalTags())); context.Tags.Add(new Tag() { Title = "D" }); context.SaveChanges(); } using (var context = new NexusContext(options)) { var repo = new NoteRepository(context); var note = repo.GetAll().First(); repo.UpdateNoteTags(note.Id, new List <string>() { "A", "B", "X", "D" }); repo.UnitOfWork.SaveChanges(); } using (var context = new NexusContext(options)) { var note = context.Notes.Include(n => n.NoteTags).ThenInclude(nt => nt.Tag).First(); Assert.True(note.NoteTags.Count == 4); 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 == "X"); Assert.Contains(note.NoteTags, nt => nt.Tag.Title == "D"); int totalTagCount = context.Tags.Count(); Assert.True(totalTagCount == 5); } }
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 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 int SaveChanges() { return(_nexusContext.SaveChanges()); }