private async Task CreateQueuedPostsForAnotherCollectionAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var userId    = Guid.NewGuid();
                var channelId = Guid.NewGuid();
                var entities  = await databaseContext.CreateTestEntitiesAsync(userId, channelId, QueueId.Value);

                var otherQueue = QueueTests.UniqueEntity(Random);
                otherQueue.BlogId = entities.Blog.Id;
                await databaseContext.Database.Connection.InsertAsync(otherQueue);

                var posts = new List <Post>();

                for (var i = 0; i < PostCount; i++)
                {
                    var post = PostTests.UniqueFileOrImage(Random);
                    post.ChannelId = channelId;
                    post.QueueId   = otherQueue.Id;
                    post.LiveDate  = Now.AddDays(1);
                    posts.Add(post);
                }

                await databaseContext.Database.Connection.InsertAsync(posts);
            }
        }
Exemple #2
0
        private async Task <Post> CreateCollectionAsync(TestDatabaseContext testDatabase, bool createPost)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestEntitiesAsync(UserId.Value, ChannelId.Value, QueueId.Value);

                if (createPost)
                {
                    var existingFileId = Guid.NewGuid();
                    await databaseContext.CreateTestFileWithExistingUserAsync(UserId.Value, existingFileId);

                    await databaseContext.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Value);

                    await databaseContext.CreateTestFileWithExistingUserAsync(UserId.Value, ImageId.Value);

                    var post = PostTests.UniqueFileOrImage(new Random());
                    post.Id             = PostId.Value;
                    post.ChannelId      = ChannelId.Value;
                    post.QueueId        = QueueId.Value;
                    post.PreviewImageId = existingFileId;
                    await databaseContext.Database.Connection.InsertAsync(post);

                    var postFiles = new List <PostFile>();
                    postFiles.Add(new PostFile(post.Id, existingFileId));

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

                    return(post);
                }

                return(null);
            }
        }
Exemple #3
0
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestEntitiesAsync(CreatorId.Value, ChannelId.Value, QueueId.Value, BlogId.Value);

                await databaseContext.SaveChangesAsync();

                var post = PostTests.UniqueFileOrImage(Random);
                post.ChannelId = ChannelId.Value;
                post.Id        = PostId.Value;
                await databaseContext.Database.Connection.InsertAsync(post);

                await this.CreateUserAsync(databaseContext, UnsubscribedUserId);

                await this.CreateUserAsync(databaseContext, SubscribedUserId);

                await this.CreateUserAsync(databaseContext, GuestListUserId);

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

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, SubscribedUserId.Value, null, ChannelPrice, Now, Now));

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

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

                await databaseContext.Database.Connection.InsertAsync(freeAccessUsers);
            }
        }
Exemple #4
0
        private async Task <Post> CreatePostWithIdAsync(
            TestDatabaseContext testDatabase,
            DateTime liveDate,
            bool scheduledByQueue,
            bool differentCollection,
            Guid postId)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                if (differentCollection)
                {
                    var collection = QueueTests.UniqueEntity(Random);
                    collection.Id     = DifferentQueueId.Value;
                    collection.BlogId = BlogId.Value;
                    await databaseContext.Database.Connection.InsertAsync(collection);
                }

                var post = PostTests.UniqueFileOrImage(Random);
                post.Id        = postId;
                post.ChannelId = ChannelId.Value;
                post.QueueId   = scheduledByQueue ? (differentCollection ? DifferentQueueId.Value : QueueId.Value) : (Guid?)null;
                post.LiveDate  = liveDate;
                await databaseContext.Database.Connection.InsertAsync(post);
            }

            using (var databaseContext = testDatabase.CreateContext())
            {
                return(await databaseContext.Posts.FirstAsync(_ => _.Id == postId));
            }
        }
Exemple #5
0
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                await databaseContext.CreateTestEntitiesAsync(CreatorId.Value, ChannelId.Value, QueueId.Value);

                await databaseContext.CreateTestFileWithExistingUserAsync(CreatorId.Value, FileId.Value);

                var post = PostTests.UniqueFileOrImage(random);
                post.Id             = PostId.Value;
                post.ChannelId      = ChannelId.Value;
                post.QueueId        = QueueId.Value;
                post.PreviewImageId = FileId.Value;
                post.CreationDate   = new SqlDateTime(post.CreationDate).Value;
                post.LiveDate       = new SqlDateTime(post.LiveDate).Value;
                await databaseContext.Database.Connection.InsertAsync(post);

                await databaseContext.Database.Connection.InsertAsync(new PostFile(PostId.Value, FileId.Value));

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

                var comment = CommentTests.Unique(random);
                comment.Id     = CommentId.Value;
                comment.PostId = PostId.Value;
                comment.UserId = UserId.Value;
                await databaseContext.Database.Connection.InsertAsync(comment);

                var like = LikeTests.Unique(random);
                like.PostId = PostId.Value;
                like.UserId = UserId.Value;
                await databaseContext.Database.Connection.InsertAsync(like);
            }
        }
        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);
            }
        }
Exemple #7
0
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool queuePost, bool liveDateInFuture = false)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var userId    = Guid.NewGuid();
                var channelId = Guid.NewGuid();
                await databaseContext.CreateTestEntitiesAsync(userId, channelId, QueueId.Value);

                var post = PostTests.UniqueFileOrImage(new Random());
                post.Id        = PostId.Value;
                post.ChannelId = channelId;
                post.QueueId   = queuePost ? QueueId.Value : (Guid?)null;
                post.LiveDate  = liveDateInFuture ? Now.AddDays(1) : Now.AddDays(-1);
                await databaseContext.Database.Connection.InsertAsync(post);
            }
        }
        private async Task <CommentsResult> CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                await databaseContext.CreateTestEntitiesAsync(CreatorId.Value, ChannelId.Value, QueueId.Value);

                await databaseContext.CreateTestFileWithExistingUserAsync(CreatorId.Value, FileId.Value);

                var post = PostTests.UniqueFileOrImage(random);
                post.Id             = PostId.Value;
                post.ChannelId      = ChannelId.Value;
                post.QueueId        = QueueId.Value;
                post.PreviewImageId = FileId.Value;
                post.CreationDate   = new SqlDateTime(post.CreationDate).Value;
                post.LiveDate       = new SqlDateTime(post.LiveDate).Value;
                await databaseContext.Database.Connection.InsertAsync(post);

                await databaseContext.Database.Connection.InsertAsync(new PostFile(PostId.Value, FileId.Value));

                var user1 = await databaseContext.CreateTestUserAsync(UserId1.Value, random);

                var user2 = await databaseContext.CreateTestUserAsync(UserId2.Value, random);

                var comment1 = CommentTests.Unique(random);
                comment1.PostId       = PostId.Value;
                comment1.UserId       = UserId1.Value;
                comment1.CreationDate = Timestamp.AddSeconds(-1);
                await databaseContext.Database.Connection.InsertAsync(comment1);

                var comment2 = CommentTests.Unique(random);
                comment2.PostId       = PostId.Value;
                comment2.UserId       = UserId2.Value;
                comment2.CreationDate = Timestamp.AddSeconds(-2);
                await databaseContext.Database.Connection.InsertAsync(comment2);

                return(new CommentsResult(new List <CommentsResult.Item>
                {
                    this.GetCommentsResultItem(comment2, user2),
                    this.GetCommentsResultItem(comment1, user1),
                }));
            }
        }
Exemple #9
0
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool createQueuedPosts = false)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestEntitiesAsync(UserId.Value, ChannelId.Value, QueueId.Value, BlogId.Value);

                if (createQueuedPosts)
                {
                    for (var i = 0; i <= QueuedPostCount; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(Random);
                        post.ChannelId = ChannelId.Value;
                        post.QueueId   = QueueId.Value;
                        post.LiveDate  = DateTime.UtcNow.AddDays(i);
                        await databaseContext.Database.Connection.InsertAsync(post);
                    }
                }
            }
        }
Exemple #10
0
        private async Task CreatePostAsync(TestDatabaseContext testDatabase, DateTime liveDate, bool scheduledByQueue, bool differentCollection)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                if (differentCollection)
                {
                    var collection = QueueTests.UniqueEntity(Random);
                    collection.Id     = DifferentQueueId.Value;
                    collection.BlogId = BlogId.Value;
                    await databaseContext.Database.Connection.InsertAsync(collection);
                }

                var post = PostTests.UniqueFileOrImage(Random);
                post.ChannelId      = ChannelId.Value;
                post.QueueId        = scheduledByQueue ? (differentCollection ? DifferentQueueId.Value : QueueId.Value) : (Guid?)null;
                post.PreviewImageId = FileId.Value; // Reuse same file across each post. Not realistic, but doesn't matter for this test.
                post.LiveDate       = liveDate;
                await databaseContext.Database.Connection.InsertAsync(post);
            }
        }
Exemple #11
0
        private async Task <Post> CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                await databaseContext.CreateTestEntitiesAsync(CreatorId.Value, ChannelId.Value, QueueId.Value);

                await databaseContext.CreateTestFileWithExistingUserAsync(CreatorId.Value, FileId.Value);

                var post = PostTests.UniqueFileOrImage(random);
                post.Id             = PostId.Value;
                post.ChannelId      = ChannelId.Value;
                post.QueueId        = QueueId.Value;
                post.PreviewImageId = FileId.Value;
                post.CreationDate   = new SqlDateTime(post.CreationDate).Value;
                post.LiveDate       = new SqlDateTime(post.LiveDate).Value;
                await databaseContext.Database.Connection.InsertAsync(post);

                await databaseContext.Database.Connection.InsertAsync(new PostFile(PostId.Value, FileId.Value));

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

                var comment = CommentTests.Unique(random);
                comment.Id     = CommentId.Value;
                comment.PostId = PostId.Value;
                comment.UserId = UserId.Value;
                await databaseContext.Database.Connection.InsertAsync(comment);

                var like = LikeTests.Unique(random);
                like.PostId = PostId.Value;
                like.UserId = UserId.Value;
                await databaseContext.Database.Connection.InsertAsync(like);

                await databaseContext.CreateTestChannelSubscriptionWithExistingReferences(CreatorId.Value, ChannelId.Value);

                var weeklyReleaseTimes = WeeklyReleaseTimeTests.GenerateSortedWeeklyReleaseTimes(QueueId.Value, 3);
                await databaseContext.Database.Connection.InsertAsync(weeklyReleaseTimes);

                return(post);
            }
        }
        private async Task CreateCollectionWithPostsAsync(TestDatabaseContext testDatabase, bool queuePosts = false, bool queueInFuture = false)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var userId    = Guid.NewGuid();
                var channelId = Guid.NewGuid();
                await databaseContext.CreateTestEntitiesAsync(userId, channelId, QueueId.Value);

                var posts = new List <Post>();
                for (var i = 0; i < PostCount; i++)
                {
                    var post = PostTests.UniqueFileOrImage(Random);
                    post.ChannelId = channelId;
                    post.QueueId   = queuePosts ? QueueId.Value : (Guid?)null;
                    post.LiveDate  = queueInFuture ? Now.AddDays(1) : Now.AddDays(-1);
                    posts.Add(post);
                }

                await databaseContext.Database.Connection.InsertAsync(posts);
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random  = new Random();
                var creator = UserTests.UniqueEntity(random);
                creator.Id = CreatorId1.Value;

                var subscription = BlogTests.UniqueEntity(random);
                subscription.Creator   = creator;
                subscription.CreatorId = creator.Id;

                var channel1 = ChannelTests.UniqueEntity(random);
                channel1.Id     = ChannelId1.Value;
                channel1.Blog   = subscription;
                channel1.BlogId = subscription.Id;

                var channel2 = ChannelTests.UniqueEntity(random);
                channel2.Id     = ChannelId2.Value;
                channel2.Blog   = subscription;
                channel2.BlogId = subscription.Id;

                var post1 = PostTests.UniqueNote(random);
                post1.Id        = PostId1.Value;
                post1.LiveDate  = PostDate1;
                post1.Channel   = channel1;
                post1.ChannelId = channel1.Id;

                var post2 = PostTests.UniqueNote(random);
                post2.Id        = PostId2.Value;
                post2.LiveDate  = PostDate2;
                post2.Channel   = channel2;
                post2.ChannelId = channel2.Id;

                databaseContext.Posts.Add(post1);
                databaseContext.Posts.Add(post2);

                await databaseContext.SaveChangesAsync();
            }
        }
        private async Task <Post> CreateEntitiesAsync(TestDatabaseContext testDatabase, bool liveDateInFuture, bool scheduledByQueue)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var channelId = Guid.NewGuid();
                var queueId   = Guid.NewGuid();
                await databaseContext.CreateTestEntitiesAsync(Guid.NewGuid(), channelId, queueId);

                var post = PostTests.UniqueFileOrImage(new Random());
                post.Id        = PostId.Value;
                post.ChannelId = channelId;
                post.QueueId   = scheduledByQueue ? queueId : (Guid?)null;
                post.LiveDate  = Now.AddDays(liveDateInFuture ? 10 : -10);
                await databaseContext.Database.Connection.InsertAsync(post);
            }

            using (var databaseContext = testDatabase.CreateContext())
            {
                var postId = PostId.Value;
                return(await databaseContext.Posts.FirstAsync(_ => _.Id == postId));
            }
        }
Exemple #15
0
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase, bool createQueuedPosts = false)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestEntitiesAsync(UserId.Value, ChannelId.Value, QueueId.Value, BlogId.Value);

                await databaseContext.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Value);

                if (createQueuedPosts)
                {
                    for (var i = 0; i <= QueuedPostCount; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(Random);
                        post.ChannelId      = ChannelId.Value;
                        post.QueueId        = QueueId.Value;
                        post.PreviewImageId = FileId.Value; // Reuse same file across each post. Not realistic, but doesn't matter for this test.
                        post.LiveDate       = DateTime.UtcNow.AddDays(i);
                        await databaseContext.Database.Connection.InsertAsync(post);
                    }
                }
            }
        }
        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);
            }
        }
Exemple #17
0
        private async Task CreateEntitiesAsync(
            TestDatabaseContext testDatabase,
            bool createLivePosts       = false,
            bool createScheduledPosts  = false,
            bool createLiveQueuedPosts = false,
            bool createQueuedPosts     = false)
        {
            const int PostsToCreateForEachCategory = 5;

            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                await databaseContext.CreateTestEntitiesAsync(
                    UserId.Value,
                    ChannelId.Value,
                    QueueId.Value);

                var posts = new List <Post>();
                if (createLivePosts)
                {
                    for (var i = 0; i < PostsToCreateForEachCategory; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(random);
                        post.ChannelId = ChannelId.Value;
                        post.QueueId   = null;
                        post.LiveDate  = Now.AddDays(random.Next(30) * -1);
                        posts.Add(post);
                    }
                }

                if (createScheduledPosts)
                {
                    for (var i = 0; i < PostsToCreateForEachCategory; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(random);
                        post.ChannelId = ChannelId.Value;
                        post.QueueId   = null;
                        post.LiveDate  = Now.AddDays(random.Next(30));
                        posts.Add(post);
                    }
                }

                if (createLiveQueuedPosts)
                {
                    for (var i = 0; i < PostsToCreateForEachCategory; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(random);
                        post.ChannelId = ChannelId.Value;
                        post.QueueId   = QueueId.Value;
                        post.LiveDate  = Now.AddDays(random.Next(30) * -1);
                        posts.Add(post);
                    }
                }

                if (createQueuedPosts)
                {
                    for (var i = 0; i < PostsToCreateForEachCategory; i++)
                    {
                        var post = PostTests.UniqueFileOrImage(random);
                        post.ChannelId = ChannelId.Value;
                        post.QueueId   = QueueId.Value;
                        post.LiveDate  = i == 0 ? LatestQueuedPostTime : Now.AddDays(random.Next(30));
                        posts.Add(post);
                    }
                }

                await databaseContext.Database.Connection.InsertAsync(posts);
            }
        }
        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);
            }
        }
Exemple #19
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 CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var entities = await databaseContext.CreateTestEntitiesAsync(CreatorId.Value, ChannelId.Value, QueueId.Value, BlogId.Value);

                entities.Channel.Price = ChannelPrice;
                await databaseContext.SaveChangesAsync();

                var pastPost = PostTests.UniqueFileOrImage(Random);
                pastPost.ChannelId = ChannelId.Value;
                pastPost.Id        = PastPostId.Value;
                pastPost.LiveDate  = Past;
                await databaseContext.Database.Connection.InsertAsync(pastPost);

                var futurePost = PostTests.UniqueFileOrImage(Random);
                futurePost.ChannelId = ChannelId.Value;
                futurePost.Id        = FuturePostId.Value;
                futurePost.LiveDate  = Future;
                await databaseContext.Database.Connection.InsertAsync(futurePost);

                await this.CreateUserAsync(databaseContext, UnsubscribedUserId);

                await this.CreateUserAsync(databaseContext, SubscribedUserId);

                await this.CreateUserAsync(databaseContext, SubscribedLowPriceUserId);

                await this.CreateUserAsync(databaseContext, SubscribedHighPriceUserId);

                await this.CreateUserAsync(databaseContext, SubscribedUserIdNoBalance);

                await this.CreateUserAsync(databaseContext, SubscribedUserIdZeroBalance);

                await this.CreateUserAsync(databaseContext, SubscribedUserIdZeroBalancePaymentInProgress);

                await this.CreateUserAsync(databaseContext, GuestListUserId);

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

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.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(ChannelId.Value, null, SubscribedUserIdNoBalance.Value, null, ChannelPrice, Now, Now));
                origins.Add(new UserPaymentOrigin(SubscribedUserIdNoBalance.Value, null, null, default(PaymentOriginKeyType), null, null, null, null, PaymentStatus.Failed));

                // The query should round down to zero for account balance.
                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, SubscribedUserIdZeroBalance.Value, null, ChannelPrice, Now, Now));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserIdZeroBalance.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(-1), 10));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserIdZeroBalance.Value, LedgerAccountType.FifthweekCredit, Now, 0.8m));

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, SubscribedUserIdZeroBalancePaymentInProgress.Value, null, ChannelPrice, Now, Now));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserIdZeroBalancePaymentInProgress.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(-1), 10));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedUserIdZeroBalancePaymentInProgress.Value, LedgerAccountType.FifthweekCredit, Now, 0.8m));
                origins.Add(new UserPaymentOrigin(SubscribedUserIdZeroBalancePaymentInProgress.Value, null, null, default(PaymentOriginKeyType), null, null, null, null, PaymentStatus.Retry1));

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, SubscribedLowPriceUserId.Value, null, ChannelPrice / 2, Now, Now));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedLowPriceUserId.Value, LedgerAccountType.FifthweekCredit, Now, 10));

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, SubscribedHighPriceUserId.Value, null, ChannelPrice * 2, Now, Now));
                calculatedAccountBalances.Add(new CalculatedAccountBalance(SubscribedHighPriceUserId.Value, LedgerAccountType.FifthweekCredit, Now, 10));

                channelSubscriptions.Add(new ChannelSubscription(ChannelId.Value, null, GuestListUserId.Value, null, 0, Now, Now));

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

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

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

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

                await databaseContext.Database.Connection.InsertAsync(origins);
            }
        }
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                // Create header file
                var blogId            = BlogId.Random();
                var headerImageFileId = FileId.Random();
                await databaseContext.CreateTestBlogAsync(UserId.Value, blogId.Value, headerImageFileId.Value);

                var headerImageFile = databaseContext.Files.First(v => v.Id == headerImageFileId.Value);
                headerImageFile.UploadStartedDate = IncludedDate;

                var channelId = ChannelId.Random();
                var channel   = ChannelTests.UniqueEntity(Random);
                channel.BlogId = blogId.Value;
                channel.Blog   = databaseContext.Blogs.First(v => v.Id == blogId.Value);
                channel.Id     = channelId.Value;
                databaseContext.Channels.Add(channel);

                // Create profile image file
                var user = databaseContext.Users.First(v => v.Id == UserId.Value);
                var profileImageFileId = FileId.Random();
                var profileImageFile   = this.CreateTestFileWithExistingUserAsync(UserId.Value, profileImageFileId.Value, IncludedDate);
                databaseContext.Files.Add(profileImageFile);
                user.ProfileImageFileId = profileImageFileId.Value;

                // Create image post file.
                var post1       = PostTests.UniqueFileOrImage(Random);
                var imageFileId = FileId.Random();
                var image       = this.CreateTestFileWithExistingUserAsync(UserId.Value, imageFileId.Value, IncludedDate);
                databaseContext.Files.Add(image);
                post1.PreviewImageId = imageFileId.Value;
                post1.PreviewImage   = image;
                post1.ChannelId      = channelId.Value;
                post1.Channel        = channel;
                databaseContext.Posts.Add(post1);

                var post1File = new PostFile(post1.Id, post1, imageFileId.Value, image);
                databaseContext.PostFiles.Add(post1File);

                // Create file post file.
                var post2      = PostTests.UniqueFileOrImage(Random);
                var fileFileId = FileId.Random();
                var file       = this.CreateTestFileWithExistingUserAsync(UserId.Value, fileFileId.Value, IncludedDate);
                databaseContext.Files.Add(file);
                post2.ChannelId = channelId.Value;
                post2.Channel   = channel;
                databaseContext.Posts.Add(post2);

                var post2File = new PostFile(post2.Id, post2, fileFileId.Value, file);
                databaseContext.PostFiles.Add(post2File);

                // Create files excluded because of date.
                var lateFile1 = this.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Random().Value, EndDate);
                databaseContext.Files.Add(lateFile1);
                var lateFile2 = this.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Random().Value, Now);
                databaseContext.Files.Add(lateFile2);

                // Create orphaned files.
                var orphanedFile1 = this.CreateTestFileWithExistingUserAsync(UserId.Value, OrphanedFileId1.Value, IncludedDate);
                databaseContext.Files.Add(orphanedFile1);
                var orphanedFile2 = this.CreateTestFileWithExistingUserAsync(UserId.Value, OrphanedFileId2.Value, IncludedDate);
                databaseContext.Files.Add(orphanedFile2);

                await databaseContext.SaveChangesAsync();
            }
        }