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 async Task TestGetByIdAsyncIdInvalid()
        {
            // Arrange
            var options = TestUtilities.BuildTestDbOptions();

            PersonalDetails result;

            await using (var context = new NexusContext(options))
            {
                context.Database.EnsureCreated();

                TestUtilities.SeedTestDb(context);

                var repositoryWrapper = new RepositoryWrapper(context);

                var service = new PersonalDetailsService(repositoryWrapper);

                // Act
                result = await service.GetByIdAsync(2);

                context.Database.EnsureDeleted();
            }

            // Assert
            Assert.Null(result);
        }
        public async Task TestGetByIdAsyncIdValid()
        {
            // Arrange
            var options = TestUtilities.BuildTestDbOptions();

            PersonalDetails result;

            await using (var context = new NexusContext(options))
            {
                context.Database.EnsureCreated();

                TestUtilities.SeedTestDb(context);

                var repositoryWrapper = new RepositoryWrapper(context);

                var service = new PersonalDetailsService(repositoryWrapper);

                // Act
                result = await service.GetByIdAsync(1);

                context.Database.EnsureDeleted();
            }

            // Assert
            Assert.NotNull(result);
            Assert.IsAssignableFrom <PersonalDetails>(result);
            Assert.Equal("Jon", result.FirstName);
        }
Beispiel #4
0
        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());
            }
        }
Beispiel #5
0
        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_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 async Task TestGetIdValid()
        {
            // Arrange
            var options = TestUtilities.BuildTestDbOptions();

            IActionResult result;

            await using (var context = new NexusContext(options))
            {
                context.Database.EnsureCreated();

                TestUtilities.SeedTestDb(context);

                var repositoryWrapper = new RepositoryWrapper(context);

                var service = new PersonalDetailsService(repositoryWrapper);

                var controller = new PersonalDetailsController(service);

                // Act
                result = await controller.Get(1);

                context.Database.EnsureDeleted();
            }

            // Assert
            var objectResult = Assert.IsAssignableFrom <OkObjectResult>(result);
            var entity       = Assert.IsAssignableFrom <PersonalDetails>(objectResult.Value);

            Assert.Equal("Jon", entity.FirstName);
        }
Beispiel #8
0
        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 async Task TestGetIdNull()
        {
            // Arrange
            var options = TestUtilities.BuildTestDbOptions();

            IActionResult result;

            await using (var context = new NexusContext(options))
            {
                var repositoryWrapper = new RepositoryWrapper(context);

                var service = new PersonalDetailsService(repositoryWrapper);

                var controller = new PersonalDetailsController(service);

                // Act
                result = await controller.Get(null);
            }

            // Assert
            var objectResult = Assert.IsAssignableFrom <BadRequestObjectResult>(result);

            Assert.Equal("The 'id' parameter cannot be null. Please try again with a valid parameter.",
                         objectResult.Value);
        }
        public async Task TestGetIdInvalid()
        {
            // Arrange
            var options = TestUtilities.BuildTestDbOptions();

            IActionResult result;

            await using (var context = new NexusContext(options))
            {
                context.Database.EnsureCreated();

                TestUtilities.SeedTestDb(context);

                var repositoryWrapper = new RepositoryWrapper(context);

                var service = new PersonalDetailsService(repositoryWrapper);

                var controller = new PersonalDetailsController(service);

                // Act
                result = await controller.Get(2);

                context.Database.EnsureDeleted();
            }

            // Assert
            Assert.IsAssignableFrom <NotFoundResult>(result);
        }
Beispiel #11
0
        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);
            }
        }
Beispiel #12
0
        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);
            }
        }
Beispiel #13
0
        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_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 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 NoteRepositoryIntegrationTests()
        {
            _options = PersistenceHelper.BuildOptions("TestDbConnectionString");
            var context = new NexusContext(PersistenceHelper.BuildOptions("TestDbConnectionString"));

            context.Database.EnsureDeleted();
            context.Database.Migrate();
            context.Dispose();
        }
Beispiel #17
0
 public UnitOfWork(
     NexusContext context,
     IBaseRepository <Gallery> galleryRepository,
     IBaseRepository <Order> orderRepository,
     IBaseRepository <Product> productRepository)
 {
     _nexusContext     = context;
     GalleryRepository = galleryRepository;
     OrderRepository   = orderRepository;
     ProductRepository = productRepository;
 }
Beispiel #18
0
        public static RecordingLevel GetRecordingLevel(GetInteractionArguments args)
        {
            using (NexusContext ctx = new NexusContext())
            {
                Circuit circuit = ctx.Circuits.FirstOrDefault(c => c.Ani == args.Ani && c.SiteId == args.SiteId);
                if (circuit == null)
                {
                    throw new ArgumentException("Invalid ANI or Site ID");
                }

                return(GetRecordingLevel(args.Ani, args.SiteId, args.InmateId, args.ExternalIdentifier, circuit.RecordingLevel));
            }
        }
Beispiel #19
0
 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));
     }
 }
Beispiel #20
0
        private static List <GlobalNumber> GetGlobalNumbers(String siteId, Boolean restrictedOnly = false)
        {
            NexusContext        nexusContext            = new NexusContext();
            List <GlobalNumber> globals                 = nexusContext.GlobalNumbers.Where(x => (x.SiteId == null || x.SiteId.ToUpper() == "GLOBAL" || x.SiteId == siteId) && x.Active && (!restrictedOnly || x.Restricted)).ToList();
            IQueryable <WildcardGlobalNumber> wildcards = nexusContext.WildcardGlobalNumbers.Where(x => (x.SiteId == null || x.SiteId.ToUpper() == "GLOBAL" || x.SiteId == siteId) && x.Active && (!restrictedOnly || x.Restricted));

            foreach (WildcardGlobalNumber wildcardGlobalNumber in wildcards)
            {
                Char[] wildcardArray = wildcardGlobalNumber.Phone.ToCharArray();
                Char[] newPhone      = wildcardGlobalNumber.Phone.ToCharArray();

                for (Int32 i = 0; i < wildcardArray.Length; i++)
                {
                    if (wildcardArray[i] == '*')
                    {
                        for (Char j = '0'; j <= '9'; j++)
                        {
                            newPhone[i] = j;
                            String resultString = new String(newPhone);
                            if (resultString.Contains('*'))
                            {
                                break;
                            }
                            else
                            {
                                GlobalNumber wildcardResult = new GlobalNumber
                                {
                                    Active           = wildcardGlobalNumber.Active,
                                    Alert            = wildcardGlobalNumber.Alert,
                                    CallsNotTimed    = wildcardGlobalNumber.CallsNotTimed,
                                    Description      = wildcardGlobalNumber.Description,
                                    DoNotValidate    = wildcardGlobalNumber.DoNotValidate,
                                    EntryDate        = wildcardGlobalNumber.EntryDate,
                                    NoPinRequired    = false,
                                    Phone            = resultString,
                                    RecordingLevelId = wildcardGlobalNumber.RecordingLevelId,
                                    Restricted       = wildcardGlobalNumber.Restricted,
                                    SiteId           = wildcardGlobalNumber.SiteId,
                                    SpeedDialCode    = wildcardGlobalNumber.SpeedDialCode,
                                    TimingClassId    = String.Empty
                                };
                                globals.Add(wildcardResult);
                            }
                        }
                    }
                }
            }

            return(globals);
        }
Beispiel #21
0
        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();
        }
Beispiel #22
0
        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);
        }
Beispiel #23
0
        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());
        }
Beispiel #24
0
        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);
            }
        }
Beispiel #25
0
        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());
            }
        }
Beispiel #26
0
        public async Task <bool> Add(NexusContext _context, User request)
        {
            if (request == null)
            {
                return(false);
            }
            try
            {
                _context.User.Add(request);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #27
0
        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);
            }
        }
Beispiel #28
0
        public void Dispose(bool disposing)
        {
            {
                if (_disposed)
                {
                    return;
                }

                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }

                _nexusContext?.Dispose();
                _nexusContext = null;
                _disposed     = true;
            }
        }
Beispiel #29
0
        public static List <Domain.Circuit> GetCircuits(Domain.GetCircuitArguments args)
        {
            List <Domain.Circuit> result = new List <Domain.Circuit>();
            List <Circuit>        circuits;

            if (args == null)
            {
                throw new ArgumentException("Invalid arguments");
            }

            using (NexusContext ctx = new NexusContext())
            {
                if (args.SiteList?.Count > 0)
                {
                    circuits = ctx.Circuits.Where(c => args.SiteList.Contains(c.SiteId)).ToList();
                }
                else if (args.AniList?.Count > 0)
                {
                    circuits = ctx.Circuits.Where(c => args.AniList.Contains(c.Ani)).ToList();
                }
                else
                {
                    throw new ArgumentException("Either a site ID or ANI is required");
                }
            }

            if (circuits.Count > 0)
            {
                foreach (Circuit circuit in circuits.OrderBy(c => c.SiteId).ThenBy(c => c.Ani))
                {
                    Domain.Circuit resultCircuit = new Domain.Circuit
                    {
                        Ani            = circuit.Ani,
                        Description    = circuit.Description,
                        ScheduleId     = circuit.ScheduleId,
                        RecordingLevel = circuit.RecordingLevel,
                        SiteId         = circuit.SiteId
                    };
                    result.Add(resultCircuit);
                }
            }

            return(result);
        }
Beispiel #30
0
        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);
            }
        }