Exemplo n.º 1
0
        public IEnumerable <SeriesDto> Build(int count)
        {
            var seriesList = new List <SeriesDto>();
            var fixture    = new Fixture();

            for (int i = 0; i < count; i++)
            {
                var author = fixture.Build <AuthorDto>()
                             .With(a => a.LibraryId, _libraryId)
                             .Without(a => a.ImageId)
                             .Create();

                _connection.AddAuthor(author);
                _authors.Add(author);

                FileDto seriesImage = null;
                if (_withImage)
                {
                    seriesImage = fixture.Build <FileDto>()
                                  .With(a => a.FilePath, Helpers.RandomData.BlobUrl)
                                  .With(a => a.IsPublic, true)
                                  .Create();
                    _connection.AddFile(seriesImage);

                    _files.Add(seriesImage);
                    _fileStorage.SetupFileContents(seriesImage.FilePath, Helpers.RandomData.Bytes);
                    _connection.AddFile(seriesImage);
                }

                var series = fixture.Build <SeriesDto>()
                             .With(a => a.Name, () => fixture.Create(_namePattern))
                             .With(s => s.LibraryId, _libraryId)
                             .With(a => a.ImageId, seriesImage?.Id)
                             .Create();

                _connection.AddSeries(series);
                seriesList.Add(series);

                var books = fixture.Build <BookDto>()
                            .With(b => b.LibraryId, _libraryId)
                            .With(b => b.Language, RandomData.Locale)
                            .Without(b => b.ImageId)
                            .With(b => b.SeriesId, series.Id)
                            .CreateMany(_bookCount);
                _books.AddRange(books);
                _connection.AddBooks(books);
                _connection.AddBooksAuthor(books.Select(b => b.Id), author.Id);
            }

            _series.AddRange(seriesList);
            return(seriesList);
        }
Exemplo n.º 2
0
        public IEnumerable <AuthorDto> Build(int count)
        {
            var fixture = new Fixture();

            var authors = new List <AuthorDto>();

            for (int i = 0; i < count; i++)
            {
                FileDto authorImage = null;
                if (_withImage)
                {
                    authorImage = fixture.Build <FileDto>()
                                  .With(a => a.FilePath, Helpers.RandomData.BlobUrl)
                                  .With(a => a.IsPublic, true)
                                  .Create();
                    _connection.AddFile(authorImage);

                    _files.Add(authorImage);
                    _fileStorage.SetupFileContents(authorImage.FilePath, Helpers.RandomData.Bytes);
                    _connection.AddFile(authorImage);
                }

                var author = fixture.Build <AuthorDto>()
                             .With(a => a.Name, () => fixture.Create(_namePattern))
                             //.With(a => a.Name, Random.Name)
                             .With(a => a.LibraryId, _libraryId)
                             .With(a => a.ImageId, authorImage?.Id)
                             .Create();

                _connection.AddAuthor(author);
                _authors.Add(author);

                var books = fixture.Build <BookDto>()
                            .With(b => b.LibraryId, _libraryId)
                            .With(b => b.Language, RandomData.Locale)
                            .With(b => b.Status, Domain.Models.BookStatuses.Published)
                            .Without(b => b.ImageId)
                            .Without(b => b.SeriesId)
                            .CreateMany(_bookCount);
                _connection.AddBooks(books);

                _connection.AddBooksAuthor(books.Select(b => b.Id), author.Id);

                _books.AddRange(books);

                authors.Add(author);
            }

            return(authors);
        }
Exemplo n.º 3
0
        public IEnumerable <BookDto> Build(int numberOfBooks)
        {
            var fixture = new Fixture();

            if (Author == null && !_authors.Any())
            {
                _authors = _authorBuilder.WithLibrary(_libraryId).Build(_numberOfAuthors > 0 ? _numberOfAuthors : numberOfBooks).ToList();
            }

            Func <bool> isPublic = () => _isPublic ?? RandomData.Bool;

            _books = fixture.Build <BookDto>()
                     .With(b => b.LibraryId, _libraryId)
                     .With(b => b.ImageId, _hasImage ? RandomData.Number : 0)
                     .With(b => b.IsPublic, isPublic)
                     .With(b => b.Language, RandomData.Locale)
                     .With(b => b.SeriesIndex, _hasSeries ? RandomData.Number : (int?)null)
                     .With(b => b.DateAdded, RandomData.Date)
                     .With(b => b.DateUpdated, RandomData.Date)
                     .With(b => b.Status, Domain.Models.BookStatuses.Published)
                     .CreateMany(numberOfBooks)
                     .ToList();

            IEnumerable <CategoryDto> categories;

            if (_categoriesCount > 0 && !_categories.Any())
            {
                categories = _categoriesBuilder.WithLibrary(_libraryId).Build(_categoriesCount);
            }
            else
            {
                categories = _categories;
            }

            foreach (var book in _books)
            {
                if (_hasSeries && _series == null)
                {
                    var series = _seriesBuilder.WithLibrary(_libraryId).Build();
                    book.SeriesId = series.Id;
                }
                else
                {
                    book.SeriesId = _series?.Id;
                }

                FileDto bookImage = null;
                if (_hasImage)
                {
                    bookImage = fixture.Build <FileDto>()
                                .With(a => a.FilePath, RandomData.BlobUrl)
                                .With(a => a.IsPublic, true)
                                .Create();
                    _connection.AddFile(bookImage);

                    _files.Add(bookImage);
                    _fileStorage.SetupFileContents(bookImage.FilePath, RandomData.Bytes);
                    _connection.AddFile(bookImage);

                    book.ImageId = bookImage.Id;
                }
                else
                {
                    book.ImageId = null;
                }

                List <FileDto> files = null;
                if (_contentCount > 0)
                {
                    var mimeType = _contentMimeType ?? RandomData.MimeType;
                    files = fixture.Build <FileDto>()
                            .With(f => f.FilePath, RandomData.BlobUrl)
                            .With(f => f.IsPublic, false)
                            .With(f => f.MimeType, mimeType)
                            .With(f => f.FilePath, RandomData.BlobUrl)
                            .CreateMany(_contentCount)
                            .ToList();
                    _files.AddRange(files);
                    files.ForEach(f => _fileStorage.SetupFileContents(f.FilePath, RandomData.Bytes));
                    _connection.AddFiles(files);
                }

                _connection.AddBook(book);

                if (Author != null)
                {
                    _connection.AddBookAuthor(book.Id, Author.Id);
                }
                else
                {
                    foreach (var author in _authors)
                    {
                        _connection.AddBookAuthor(book.Id, author.Id);
                    }
                }

                if (categories != null && categories.Any())
                {
                    _connection.AddBookToCategories(book.Id, categories);
                }

                if (files != null)
                {
                    var contents = files.Select(f => new BookContentDto
                    {
                        BookId   = book.Id,
                        Language = _language ?? RandomData.NextLocale(),
                        FileId   = f.Id,
                        MimeType = f.MimeType,
                        FilePath = f.FilePath
                    }).ToList();
                    _connection.AddBookFiles(book.Id, contents);
                    _contents.AddRange(contents);
                }

                if (_pageCount > 0)
                {
                    var pages = new List <BookPageDto>();

                    for (int i = 0; i < _pageCount; i++)
                    {
                        FileDto pageImage = null;
                        if (_addPageImage)
                        {
                            pageImage = fixture.Build <FileDto>()
                                        .With(a => a.FilePath, RandomData.BlobUrl)
                                        .With(a => a.IsPublic, true)
                                        .Create();
                            _connection.AddFile(pageImage);

                            _files.Add(pageImage);
                            _fileStorage.SetupFileContents(pageImage.FilePath, RandomData.Bytes);
                            _connection.AddFile(pageImage);
                        }

                        pages.Add(fixture.Build <BookPageDto>()
                                  .With(p => p.BookId, book.Id)
                                  .With(p => p.SequenceNumber, i + 1)
                                  .With(p => p.Text, RandomData.Text)
                                  .With(p => p.ImageId, pageImage?.Id)
                                  .With(p => p.AccountId, (int?)null)
                                  .With(p => p.Status, EditingStatus.All)
                                  .Create());
                    }

                    if (_assignments.Any())
                    {
                        foreach (var assignment in _assignments)
                        {
                            var pagesToAssign = RandomData.PickRandom(pages.Where(p => p.AccountId == null), assignment.Value);
                            foreach (var pageToAssign in pagesToAssign)
                            {
                                pageToAssign.AccountId = assignment.Key;
                            }
                        }
                    }

                    if (_pageStatuses.Any())
                    {
                        foreach (var pageStatus in _pageStatuses)
                        {
                            var pagesToSetStatus = RandomData.PickRandom(pages.Where(p => p.Status == EditingStatus.All), pageStatus.Value);
                            foreach (var pageToSetStatus in pagesToSetStatus)
                            {
                                pageToSetStatus.Status = pageStatus.Key;
                            }
                        }
                    }

                    _connection.AddBookPages(pages);
                    _pages.AddRange(pages);
                }
            }

            if (_favoriteBooks.Any())
            {
                foreach (var f in _favoriteBooks)
                {
                    var booksToAddToFavorite = f.Count.HasValue ? _books.PickRandom(f.Count.Value) : _books;
                    _connection.AddBooksToFavorites(_libraryId, booksToAddToFavorite.Select(b => b.Id), f.AccountId);
                }
            }

            if (_readBooks.Any())
            {
                foreach (var r in _readBooks)
                {
                    var booksToAddToRecent = r.Count.HasValue ? _books.PickRandom(r.Count.Value) : _books;
                    foreach (var recentBook in booksToAddToRecent)
                    {
                        RecentBookDto recent = new RecentBookDto {
                            LibraryId = _libraryId, BookId = recentBook.Id, AccountId = r.AccountId, DateRead = RandomData.Date
                        };
                        _connection.AddBookToRecentReads(recent);
                        _recentBooks.Add(recent);
                    }
                }
            }

            return(_books);
        }