private async Task CreateUserAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id       = CreatorId.Value;
            user.UserName = Username.Value;

            var file = FileTests.UniqueEntity(random);

            file.Id     = ProfileFileId.Value;
            file.UserId = CreatorId.Value;

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);
                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(file);
            }

            using (var databaseContext = testDatabase.CreateContext())
            {
                file = await databaseContext.Files.FirstAsync(v => v.Id == ProfileFileId.Value);

                user = await databaseContext.Users.FirstAsync(v => v.Id == CreatorId.Value);

                user.ProfileImageFile   = file;
                user.ProfileImageFileId = file.Id;
                await databaseContext.SaveChangesAsync();
            }
        }
        public File CreateTestFileWithExistingUserAsync(Guid existingUserId, Guid newFileId, DateTime uploadStartedDate)
        {
            var file = FileTests.UniqueEntity(Random);

            file.Id                = newFileId;
            file.UserId            = existingUserId;
            file.UploadStartedDate = uploadStartedDate;
            file.ChannelId         = ChannelId.Value;
            file.Purpose           = Purpose;
            return(file);
        }
        private async Task CreateHeaderFileAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var file   = FileTests.UniqueEntity(random);

            file.Id     = HeaderFileId.Value;
            file.UserId = CreatorId.Value;

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(file);
            }
        }
        private async Task <IReadOnlyList <Post> > CreatePostsAsync(
            TestDatabaseContext testDatabase,
            QueueId queueId,
            IReadOnlyList <DateTime> liveDates,
            bool scheduledByQueue)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var user = UserTests.UniqueEntity(Random);
                await databaseContext.Database.Connection.InsertAsync(user);

                var file = FileTests.UniqueEntity(Random);
                file.UserId = user.Id;
                await databaseContext.Database.Connection.InsertAsync(file);

                var blog = BlogTests.UniqueEntity(Random);
                blog.CreatorId = user.Id;
                await databaseContext.Database.Connection.InsertAsync(blog);

                var channel = ChannelTests.UniqueEntity(Random);
                channel.BlogId = blog.Id;
                await databaseContext.Database.Connection.InsertAsync(channel);

                var collection = QueueTests.UniqueEntity(Random);
                collection.Id     = queueId.Value;
                collection.BlogId = blog.Id;
                await databaseContext.Database.Connection.InsertAsync(collection);

                var postsInCollection = new List <Post>();
                foreach (var liveDate in liveDates)
                {
                    var post = PostTests.UniqueNote(Random);
                    post.ChannelId = channel.Id;
                    post.QueueId   = scheduledByQueue ? queueId.Value : (Guid?)null;
                    post.LiveDate  = liveDate;

                    // Clip dates as we will be comparing from these entities.
                    post.LiveDate     = new SqlDateTime(post.LiveDate).Value;
                    post.CreationDate = new SqlDateTime(post.CreationDate).Value;

                    postsInCollection.Add(post);
                }

                await databaseContext.Database.Connection.InsertAsync(postsInCollection);

                return(postsInCollection);
            }
        }
        private async Task <Blog> CreateBlogAsync(UserId newUserId, BlogId newBlogId, TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestBlogAsync(newUserId.Value, newBlogId.Value, Guid.NewGuid());

                var newHeaderImage = FileTests.UniqueEntity(new Random());
                newHeaderImage.Id     = HeaderImageFileId.Value;
                newHeaderImage.UserId = newUserId.Value;
                await databaseContext.Database.Connection.InsertAsync(newHeaderImage);
            }

            using (var databaseContext = testDatabase.CreateContext())
            {
                return(await databaseContext.Blogs.SingleAsync(_ => _.Id == newBlogId.Value));
            }
        }
        private async Task CreateFileAsync(UserId newUserId, FileId newFileId, TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id = newUserId.Value;

            var file = FileTests.UniqueEntity(random);

            file.Id     = newFileId.Value;
            file.UserId = newUserId.Value;

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);
                databaseContext.Files.Add(file);
                await databaseContext.SaveChangesAsync();
            }
        }
        private async Task CreateUserAsync(TestDatabaseContext testDatabase, bool emailConfirmed = true)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id             = this.userId.Value;
            user.Email          = this.email.Value;
            user.EmailConfirmed = emailConfirmed;

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);
                await databaseContext.SaveChangesAsync();
            }

            var profileImageFile = FileTests.UniqueEntity(random);

            profileImageFile.Id     = this.fileId.Value;
            profileImageFile.UserId = user.Id;

            var otherFile = FileTests.UniqueEntity(random);

            otherFile.Id     = this.newFileId.Value;
            otherFile.UserId = user.Id;

            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.Database.Connection.InsertAsync(profileImageFile);

                await databaseContext.Database.Connection.InsertAsync(otherFile);

                user.ProfileImageFile   = profileImageFile;
                user.ProfileImageFileId = profileImageFile.Id;

                await databaseContext.Database.Connection.UpdateAsync(user, FifthweekUser.Fields.ProfileImageFileId);
            }
        }
        private async Task CreateProfileImagesAsync(TestDatabaseContext testDatabase)
        {
            FifthweekUser creator1;

            using (var databaseContext = testDatabase.CreateContext())
            {
                creator1 = databaseContext.Users.First(v => v.Id == Creator1Id.Value);
            }

            var profileImageFile = FileTests.UniqueEntity(this.random);

            profileImageFile.Id     = Creator1ProfileImageFileId.Value;
            profileImageFile.UserId = creator1.Id;

            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.Database.Connection.InsertAsync(profileImageFile);

                creator1.ProfileImageFile   = profileImageFile;
                creator1.ProfileImageFileId = profileImageFile.Id;

                await databaseContext.Database.Connection.UpdateAsync(creator1, FifthweekUser.Fields.ProfileImageFileId);
            }
        }
Пример #9
0
        private async Task CreateProfileImagesAsync(TestDatabaseContext testDatabase, FileId fileId, UserId userId)
        {
            FifthweekUser user;

            using (var databaseContext = testDatabase.CreateContext())
            {
                user = databaseContext.Users.First(v => v.Id == userId.Value);
            }

            var profileImageFile = FileTests.UniqueEntity(this.random);

            profileImageFile.Id     = fileId.Value;
            profileImageFile.UserId = user.Id;

            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.Database.Connection.InsertAsync(profileImageFile);

                user.ProfileImageFile   = profileImageFile;
                user.ProfileImageFileId = profileImageFile.Id;

                await databaseContext.Database.Connection.UpdateAsync(user, FifthweekUser.Fields.ProfileImageFileId);
            }
        }
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool createLivePosts, bool createFuturePosts)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await this.CreateUserAsync(databaseContext, UnsubscribedUserId);

                await this.CreateUserAsync(databaseContext, SubscribedUserId);

                await this.CreateUserAsync(databaseContext, GuestListUserId);

                var channelSubscriptions      = new List <ChannelSubscription>();
                var calculatedAccountBalances = new List <CalculatedAccountBalance>();
                var freeAccessUsers           = new List <FreeAccessUser>();

                var channels        = new List <ChannelId>();
                var queues          = new List <QueueId>();
                var files           = new List <FileId>();
                var images          = new List <FileId>();
                var channelEntities = new List <Channel>();
                var queueEntities   = new List <Queue>();
                var postEntities    = new List <Post>();
                var origins         = new List <UserPaymentOrigin>();
                var likes           = new List <Like>();
                var comments        = new List <Persistence.Comment>();

                if (createLivePosts || createFuturePosts)
                {
                    channelSubscriptions.Add(new ChannelSubscription(ChannelIds[0].Value, null, SubscribedUserId.Value, null, ChannelPrice, Now, Now));
                    channelSubscriptions.Add(new ChannelSubscription(ChannelIds[1].Value, null, SubscribedUserId.Value, null, ChannelPrice, Now, Now));
                    calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserId.Value, LedgerAccountType.FifthweekCredit, Now, 10));
                    calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserId.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(-1), 0));

                    channelSubscriptions.Add(new ChannelSubscription(ChannelIds[0].Value, null, GuestListUserId.Value, null, 0, Now, Now));
                    channelSubscriptions.Add(new ChannelSubscription(ChannelIds[1].Value, null, GuestListUserId.Value, null, 0, Now, Now));

                    freeAccessUsers.Add(new FreeAccessUser(BlogId.Value, GuestListUserId.Value + "@test.com"));

                    foreach (var newsfeedPost in SortedNewsfeedPosts)
                    {
                        if (createLivePosts && !createFuturePosts && newsfeedPost.LiveDate > Now)
                        {
                            continue;
                        }

                        if (!createLivePosts && createFuturePosts && newsfeedPost.LiveDate <= Now)
                        {
                            continue;
                        }

                        if (newsfeedPost.ImageId != null)
                        {
                            images.Add(newsfeedPost.ImageId);
                        }

                        if (!channels.Contains(newsfeedPost.ChannelId))
                        {
                            channels.Add(newsfeedPost.ChannelId);
                        }

                        postEntities.Add(new Post(
                                             newsfeedPost.PostId.Value,
                                             newsfeedPost.ChannelId.Value,
                                             null,
                                             (Guid?)null,
                                             null,
                                             newsfeedPost.ImageId == null ? (Guid?)null : newsfeedPost.ImageId.Value,
                                             null,
                                             newsfeedPost.PreviewText == null ? null : newsfeedPost.PreviewText.Value,
                                             "content",
                                             newsfeedPost.PreviewWordCount,
                                             newsfeedPost.WordCount,
                                             newsfeedPost.ImageCount,
                                             newsfeedPost.FileCount,
                                             newsfeedPost.VideoCount,
                                             newsfeedPost.LiveDate,
                                             newsfeedPost.CreationDate));

                        for (int i = 0; i < newsfeedPost.LikesCount; i++)
                        {
                            likes.Add(
                                new Like(
                                    newsfeedPost.PostId.Value,
                                    null,
                                    UserIds[i].Value,
                                    null,
                                    DateTime.UtcNow));
                        }

                        for (int i = 0; i < newsfeedPost.CommentsCount; i++)
                        {
                            comments.Add(
                                new Persistence.Comment(
                                    Guid.NewGuid(),
                                    newsfeedPost.PostId.Value,
                                    null,
                                    UserIds[i].Value,
                                    null,
                                    "Comment " + this.random.Next(),
                                    DateTime.UtcNow));
                        }
                    }
                }

                foreach (var channelId in channels)
                {
                    var channel = ChannelTests.UniqueEntity(Random);
                    channel.Id     = channelId.Value;
                    channel.BlogId = BlogId.Value;
                    channel.Price  = ChannelPrice;

                    channelEntities.Add(channel);
                }

                foreach (var queueId in queues)
                {
                    var queue = QueueTests.UniqueEntity(Random);
                    queue.Id     = queueId.Value;
                    queue.BlogId = BlogId.Value;

                    queueEntities.Add(queue);
                }

                var fileEntities = files.Select(fileId =>
                {
                    var file    = FileTests.UniqueEntity(Random);
                    file.Id     = fileId.Value;
                    file.UserId = CreatorId.Value;
                    file.FileNameWithoutExtension = FileName;
                    file.FileExtension            = FileExtension;
                    file.BlobSizeBytes            = FileSize;
                    return(file);
                });

                var imageEntities = images.Select(fileId =>
                {
                    var file    = FileTests.UniqueEntity(Random);
                    file.Id     = fileId.Value;
                    file.UserId = CreatorId.Value;
                    file.FileNameWithoutExtension = FileName;
                    file.FileExtension            = FileExtension;
                    file.BlobSizeBytes            = FileSize;
                    file.RenderWidth  = FileWidth;
                    file.RenderHeight = FileHeight;
                    return(file);
                });

                await databaseContext.CreateTestBlogAsync(CreatorId.Value, BlogId.Value, null, Random);

                await databaseContext.Database.Connection.InsertAsync(channelEntities);

                await databaseContext.Database.Connection.InsertAsync(queueEntities);

                await databaseContext.Database.Connection.InsertAsync(fileEntities);

                await databaseContext.Database.Connection.InsertAsync(imageEntities);

                await databaseContext.Database.Connection.InsertAsync(postEntities);

                await databaseContext.Database.Connection.InsertAsync(channelSubscriptions);

                await databaseContext.Database.Connection.InsertAsync(calculatedAccountBalances);

                await databaseContext.Database.Connection.InsertAsync(freeAccessUsers);

                await databaseContext.Database.Connection.InsertAsync(origins);

                await databaseContext.Database.Connection.InsertAsync(likes);

                await databaseContext.Database.Connection.InsertAsync(comments);
            }
        }
Пример #11
0
        private async Task <IEnumerable <IIdentityEquatable> > AddUser(DbConnection connection, Random random, UserId userId, DateTime registrationDate, bool isTestUser, UserId subscriberId = null, ChannelId subscribedToId = null)
        {
            var users = new List <FifthweekUser>();
            var user  = UserTests.UniqueEntity(random);

            user.Id = userId.Value;
            user.RegistrationDate = registrationDate;
            users.Add(user);
            await connection.InsertAsync(users, false);

            var userRoles = new List <FifthweekUserRole>();

            userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, user.Id));
            if (isTestUser)
            {
                userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, user.Id));
            }
            await connection.InsertAsync(userRoles, false);

            var blogs = new List <Blog>();
            var blog  = BlogTests.UniqueEntity(random);

            blog.CreatorId = userId.Value;
            blogs.Add(blog);
            await connection.InsertAsync(blogs);

            var channels = new List <Channel>();
            var channel  = ChannelTests.UniqueEntity(random);

            channel.BlogId = blog.Id;
            channels.Add(channel);
            await connection.InsertAsync(channels);

            var queues = new List <Queue>();
            var queue  = QueueTests.UniqueEntity(random);

            queue.BlogId = blog.Id;
            queues.Add(queue);
            await connection.InsertAsync(queues);

            var files = new List <File>();
            var file  = FileTests.UniqueEntity(random);

            file.ChannelId = channel.Id;
            file.UserId    = userId.Value;
            files.Add(file);
            await connection.InsertAsync(files);

            var posts = new List <Post>();
            var post  = PostTests.UniqueFileOrImage(random);

            post.ChannelId      = channel.Id;
            post.PreviewImageId = file.Id;
            post.ChannelId      = channel.Id;
            posts.Add(post);
            await connection.InsertAsync(posts);

            var postFiles = new List <PostFile>();

            postFiles.Add(new PostFile(post.Id, file.Id));
            await connection.InsertAsync(postFiles);

            var channelSubscriptions = new List <ChannelSubscription>();

            if (subscriberId != null)
            {
                channelSubscriptions.Add(new ChannelSubscription(channel.Id, null, subscriberId.Value, null, 100, Now, Now));
                await connection.InsertAsync(channelSubscriptions);
            }
            if (subscribedToId != null)
            {
                channelSubscriptions.Add(new ChannelSubscription(subscribedToId.Value, null, userId.Value, null, 100, Now, Now));
                await connection.InsertAsync(channelSubscriptions);
            }

            var calculatedAccountBalances = new List <CalculatedAccountBalance>
            {
                new CalculatedAccountBalance(user.Id, LedgerAccountType.FifthweekCredit, Now),
            };
            await connection.InsertAsync(calculatedAccountBalances);

            var subscriberSnapshots = new List <SubscriberSnapshot>
            {
                new SubscriberSnapshot(Now, user.Id, "email"),
            };
            await connection.InsertAsync(subscriberSnapshots);

            var subscriberChannelSnapshots = new List <SubscriberChannelsSnapshot>
            {
                new SubscriberChannelsSnapshot(Guid.NewGuid(), Now, user.Id),
            };
            await connection.InsertAsync(subscriberChannelSnapshots);

            var subscriberChannelSnapshotItems = new List <SubscriberChannelsSnapshotItem>
            {
                new SubscriberChannelsSnapshotItem(subscriberChannelSnapshots[0].Id, null, channel.Id, user.Id, 100, Now),
            };
            await connection.InsertAsync(subscriberChannelSnapshotItems);

            var creatorChannelSnapshots = new List <CreatorChannelsSnapshot>
            {
                new CreatorChannelsSnapshot(Guid.NewGuid(), Now, user.Id),
            };
            await connection.InsertAsync(creatorChannelSnapshots);

            var creatorChannelSnapshotItems = new List <CreatorChannelsSnapshotItem>
            {
                new CreatorChannelsSnapshotItem(creatorChannelSnapshots[0].Id, null, channel.Id, 100),
            };
            await connection.InsertAsync(creatorChannelSnapshotItems);

            var creatorFreeAccessUsersSnapshots = new List <CreatorFreeAccessUsersSnapshot>
            {
                new CreatorFreeAccessUsersSnapshot(Guid.NewGuid(), Now, user.Id),
            };
            await connection.InsertAsync(creatorFreeAccessUsersSnapshots);

            var creatorFreeAccessUsersSnapshotItems = new List <CreatorFreeAccessUsersSnapshotItem>
            {
                new CreatorFreeAccessUsersSnapshotItem(creatorFreeAccessUsersSnapshots[0].Id, null, "email"),
            };
            await connection.InsertAsync(creatorFreeAccessUsersSnapshotItems);

            var userPaymentOrigins = new List <UserPaymentOrigin>
            {
                new UserPaymentOrigin(userId.Value, null, "paymentOriginKey", PaymentOriginKeyType.Stripe, null, null, null, null, PaymentStatus.None),
            };
            await connection.InsertAsync(userPaymentOrigins);

            return(users.Cast <IIdentityEquatable>()
                   .Concat(userRoles)
                   .Concat(blogs)
                   .Concat(channels)
                   .Concat(queues)
                   .Concat(files)
                   .Concat(posts)
                   .Concat(channelSubscriptions)
                   .Concat(calculatedAccountBalances)
                   .Concat(subscriberSnapshots)
                   .Concat(subscriberChannelSnapshots)
                   .Concat(subscriberChannelSnapshotItems)
                   .Concat(creatorChannelSnapshots)
                   .Concat(creatorChannelSnapshotItems)
                   .Concat(creatorFreeAccessUsersSnapshots)
                   .Concat(creatorFreeAccessUsersSnapshotItems)
                   .Concat(userPaymentOrigins)
                   .Concat(postFiles));
        }
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool likePost = false, bool isFreePost = false)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestBlogAsync(CreatorId.Value, BlogId.Value, null, Random, CreatorUsername, BlogId.ToString());

                await databaseContext.Database.Connection.UpdateAsync(
                    new Blog { Id = BlogId.Value, Introduction = Introduction.Value },
                    Blog.Fields.Introduction);

                var channel = ChannelTests.UniqueEntity(Random);
                channel.BlogId = BlogId.Value;
                channel.Name   = ChannelId.ToString();
                channel.Id     = ChannelId.Value;
                await databaseContext.Database.Connection.InsertAsync(channel);

                var file1 = FileTests.UniqueEntity(Random);
                file1.BlobSizeBytes            = FileSize1;
                file1.ChannelId                = ChannelId.Value;
                file1.FileExtension            = FileExtension1;
                file1.FileNameWithoutExtension = FileName1;
                file1.Id           = FileId1.Value;
                file1.Purpose      = FilePurpose1;
                file1.RenderHeight = FileHeight1;
                file1.RenderWidth  = FileWidth1;
                file1.UserId       = CreatorId.Value;
                await databaseContext.Database.Connection.InsertAsync(file1);

                var file2 = FileTests.UniqueEntity(Random);
                file2.BlobSizeBytes            = FileSize2;
                file2.ChannelId                = ChannelId.Value;
                file2.FileExtension            = FileExtension2;
                file2.FileNameWithoutExtension = FileName2;
                file2.Id           = FileId2.Value;
                file2.Purpose      = FilePurpose2;
                file2.RenderHeight = FileHeight2;
                file2.RenderWidth  = FileWidth2;
                file2.UserId       = CreatorId.Value;
                await databaseContext.Database.Connection.InsertAsync(file2);

                var post = PostTests.UniqueFileOrImage(Random);
                post.ChannelId        = ChannelId.Value;
                post.PreviewText      = PreviewText.Value;
                post.Content          = Content.Value;
                post.CreationDate     = CreationDate;
                post.FileCount        = FileCount;
                post.Id               = PostId.Value;
                post.ImageCount       = ImageCount;
                post.VideoCount       = VideoCount;
                post.LiveDate         = LiveDate;
                post.PreviewImageId   = FileId1.Value;
                post.PreviewWordCount = PreviewWordCount;
                post.WordCount        = WordCount;

                await databaseContext.Database.Connection.InsertAsync(post);

                await databaseContext.Database.Connection.InsertAsync(new PostFile(post.Id, file1.Id));

                await databaseContext.Database.Connection.InsertAsync(new PostFile(post.Id, file2.Id));

                await databaseContext.CreateTestUserAsync(UserId.Value, Random);

                var user2 = await databaseContext.CreateTestUserAsync(Guid.NewGuid(), Random);

                await databaseContext.Database.Connection.InsertAsync(new Like(post.Id, null, user2.Id, null, CreationDate));

                await databaseContext.Database.Connection.InsertAsync(new Persistence.Comment(Guid.NewGuid(), post.Id, null, user2.Id, null, "coment", CreationDate));

                if (likePost)
                {
                    await databaseContext.Database.Connection.InsertAsync(new Like(post.Id, null, UserId.Value, null, CreationDate));
                }

                if (isFreePost)
                {
                    await databaseContext.Database.Connection.InsertAsync(new FreePost(UserId.Value, post.Id, null, Now));
                }
            }
        }
        private async Task <IReadOnlyList <Post> > CreatePostsAsync(
            TestDatabaseContext testDatabase,
            UserId userId,
            QueueId queueId,
            bool liveDateInFuture,
            bool scheduledByQueue)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var user = UserTests.UniqueEntity(Random);
                user.Id = userId.Value;
                await databaseContext.Database.Connection.InsertAsync(user);

                var file = FileTests.UniqueEntity(Random);
                file.Id     = FileId.Value;
                file.UserId = user.Id;
                await databaseContext.Database.Connection.InsertAsync(file);

                var blog = BlogTests.UniqueEntity(Random);
                blog.CreatorId = user.Id;
                await databaseContext.Database.Connection.InsertAsync(blog);

                var channel = ChannelTests.UniqueEntity(Random);
                channel.BlogId = blog.Id;
                await databaseContext.Database.Connection.InsertAsync(channel);

                var queue = QueueTests.UniqueEntity(Random);
                queue.Id     = queueId.Value;
                queue.BlogId = blog.Id;
                await databaseContext.Database.Connection.InsertAsync(queue);

                var notes = new List <Post>();
                for (var i = 0; i < CollectionTotal; i++)
                {
                    // Notes are not covered by this feature as they do not belong in a collection, but we add them to create a more realistic test state.
                    var post = PostTests.UniqueNote(Random);
                    post.ChannelId = channel.Id;

                    notes.Add(post);
                }

                var postsInCollection = new List <Post>();
                for (var i = 0; i < CollectionTotal; i++)
                {
                    var post = PostTests.UniqueFileOrImage(Random);
                    post.ChannelId      = channel.Id;
                    post.QueueId        = scheduledByQueue ? queueId.Value : (Guid?)null;
                    post.PreviewImageId = file.Id;
                    post.LiveDate       = Now.AddDays((1 + Random.Next(100)) * (liveDateInFuture ? 1 : -1));

                    // Clip dates as we will be comparing from these entities.
                    post.LiveDate     = new SqlDateTime(post.LiveDate).Value;
                    post.CreationDate = new SqlDateTime(post.CreationDate).Value;

                    postsInCollection.Add(post);
                }

                await databaseContext.Database.Connection.InsertAsync(notes.Concat(postsInCollection));

                return(postsInCollection);
            }
        }
        private async Task CreateDataAsync(
            TestDatabaseContext testDatabase,
            UserId userId,
            Username username,
            Email email,
            FileId fileId,
            int accountBalanceCount,
            bool createOrigin,
            bool populateCreditCard,
            bool populateCreatorPercentageOverride)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id       = userId.Value;
            user.Email    = email.Value;
            user.UserName = username.Value;

            var postId1 = Guid.NewGuid();
            var postId2 = Guid.NewGuid();
            var postId3 = Guid.NewGuid();

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);

                await databaseContext.CreateTestNoteAsync(Guid.NewGuid(), postId1, random);

                await databaseContext.CreateTestNoteAsync(Guid.NewGuid(), postId2, random);

                await databaseContext.CreateTestNoteAsync(Guid.NewGuid(), postId3, random);

                await databaseContext.SaveChangesAsync();
            }

            if (fileId != null)
            {
                var profileImageFile = FileTests.UniqueEntity(random);
                profileImageFile.Id     = fileId.Value;
                profileImageFile.UserId = user.Id;

                using (var connection = testDatabase.CreateConnection())
                {
                    await connection.InsertAsync(profileImageFile);

                    user.ProfileImageFile   = profileImageFile;
                    user.ProfileImageFileId = profileImageFile.Id;

                    await connection.UpdateAsync(user, FifthweekUser.Fields.ProfileImageFileId);
                }
            }

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(
                    new CalculatedAccountBalance(
                        user.Id,
                        LedgerAccountType.Stripe,
                        Now,
                        -120));

                await connection.InsertAsync(
                    new CalculatedAccountBalance(
                        user.Id,
                        LedgerAccountType.SalesTax,
                        Now,
                        20));

                await connection.InsertAsync(
                    new CalculatedAccountBalance(
                        user.Id,
                        LedgerAccountType.FifthweekRevenue,
                        Now,
                        200));

                for (int i = 0; i < accountBalanceCount; i++)
                {
                    await connection.InsertAsync(
                        new CalculatedAccountBalance(
                            user.Id,
                            LedgerAccountType.FifthweekCredit,
                            Now.AddHours(-1 - i),
                            100 + i));
                }

                if (createOrigin)
                {
                    if (populateCreditCard)
                    {
                        await connection.InsertAsync(
                            new UserPaymentOrigin(
                                user.Id,
                                null,
                                "blah",
                                PaymentOriginKeyType.Stripe,
                                null,
                                null,
                                null,
                                null,
                                PaymentStatus.Retry2));
                    }
                    else
                    {
                        var populateKey = random.NextDouble() > 0.5;
                        await connection.InsertAsync(
                            new UserPaymentOrigin(
                                user.Id,
                                null,
                                populateKey ? "blah" : null,
                                populateKey ? PaymentOriginKeyType.None : PaymentOriginKeyType.Stripe,
                                null,
                                null,
                                null,
                                null,
                                PaymentStatus.Retry2));
                    }
                }

                if (populateCreatorPercentageOverride)
                {
                    await connection.InsertAsync(new CreatorPercentageOverride(user.Id, PercentageOverride, Expiry));
                }

                await connection.InsertAsync(new FreePost(UserId.Value, postId1, null, FreePostsTimestamp1));

                await connection.InsertAsync(new FreePost(UserId.Value, postId2, null, FreePostsTimestamp2));

                await connection.InsertAsync(new FreePost(UserId.Value, postId3, null, FreePostsTimestamp2));
            }
        }
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool createLivePosts, bool createFuturePosts)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var channels        = new List <ChannelId>();
                var queues          = new List <QueueId>();
                var files           = new List <FileId>();
                var images          = new List <FileId>();
                var channelEntities = new List <Channel>();
                var queueEntities   = new List <Queue>();
                var postEntities    = new List <Post>();

                if (createLivePosts)
                {
                    var channelId = new ChannelId(Guid.NewGuid());
                    var queueId   = new QueueId(Guid.NewGuid());
                    channels.Add(channelId);
                    queues.Add(queueId);
                    for (var i = 1; i <= 10; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(Random);
                        post.ChannelId = channelId.Value;
                        post.QueueId   = queueId.Value;
                        post.LiveDate  = DateTime.UtcNow.AddDays(i * -1);
                        postEntities.Add(post);
                    }
                }

                if (createFuturePosts)
                {
                    foreach (var backlogPost in SortedBacklogPosts)
                    {
                        if (backlogPost.ImageId != null)
                        {
                            images.Add(backlogPost.ImageId);
                        }

                        if (!channels.Contains(backlogPost.ChannelId))
                        {
                            channels.Add(backlogPost.ChannelId);
                        }

                        if (backlogPost.QueueId != null)
                        {
                            queues.Add(backlogPost.QueueId);
                        }

                        postEntities.Add(new Post(
                                             backlogPost.PostId.Value,
                                             backlogPost.ChannelId.Value,
                                             null,
                                             backlogPost.QueueId == null ? (Guid?)null : backlogPost.QueueId.Value,
                                             null,
                                             backlogPost.ImageId == null ? (Guid?)null : backlogPost.ImageId.Value,
                                             null,
                                             backlogPost.PreviewText == null ? null : backlogPost.PreviewText.Value,
                                             "content",
                                             backlogPost.PreviewWordCount,
                                             backlogPost.WordCount,
                                             backlogPost.ImageCount,
                                             backlogPost.FileCount,
                                             backlogPost.VideoCount,
                                             backlogPost.LiveDate,
                                             backlogPost.CreationDate));
                    }
                }

                foreach (var channelId in channels)
                {
                    var channel = ChannelTests.UniqueEntity(Random);
                    channel.Id     = channelId.Value;
                    channel.BlogId = BlogId.Value;

                    channelEntities.Add(channel);
                }

                foreach (var queueId in queues)
                {
                    var queue = QueueTests.UniqueEntity(Random);
                    queue.Id     = queueId.Value;
                    queue.BlogId = BlogId.Value;

                    queueEntities.Add(queue);
                }

                var fileEntities = files.Select(fileId =>
                {
                    var file    = FileTests.UniqueEntity(Random);
                    file.Id     = fileId.Value;
                    file.UserId = UserId.Value;
                    file.FileNameWithoutExtension = FileName;
                    file.FileExtension            = FileExtension;
                    file.BlobSizeBytes            = FileSize;
                    return(file);
                });

                var imageEntities = images.Select(fileId =>
                {
                    var file    = FileTests.UniqueEntity(Random);
                    file.Id     = fileId.Value;
                    file.UserId = UserId.Value;
                    file.FileNameWithoutExtension = FileName;
                    file.FileExtension            = FileExtension;
                    file.BlobSizeBytes            = FileSize;
                    file.RenderWidth  = FileWidth;
                    file.RenderHeight = FileHeight;
                    return(file);
                });
                await databaseContext.CreateTestBlogAsync(UserId.Value, BlogId.Value);

                await databaseContext.Database.Connection.InsertAsync(channelEntities);

                await databaseContext.Database.Connection.InsertAsync(queueEntities);

                await databaseContext.Database.Connection.InsertAsync(fileEntities);

                await databaseContext.Database.Connection.InsertAsync(imageEntities);

                await databaseContext.Database.Connection.InsertAsync(postEntities);
            }
        }