Exemple #1
0
 protected void AssertLightLoadedPost(PostStoreEntityId collectionId, PostStoreEntityType entityType,
                                      IBoardPost post, string messagePrefix, IBoardPostEntity opEntity, PostStoreEntityId opId, bool counterRetrieved, int postCounterNum)
 {
     AssertBareEntityLoadedPost(collectionId, entityType, post, messagePrefix, opEntity, opId);
     if (opEntity is IBoardPostLight opLight)
     {
         Assert.AreEqual(post.BoardSpecificDate, opLight.BoardSpecificDate, $"{messagePrefix}: opLight.BoardSpecificDate");
         var dateDiff = Math.Abs((post.Date - opLight.Date).TotalSeconds);
         Assert.IsTrue(dateDiff < 1.5, $"{messagePrefix}: opLight.Date");
         if (post.Likes != null)
         {
             Assert.IsNotNull(opLight.Likes, $"{messagePrefix}: opLight.Likes != null");
             Assert.AreEqual(post.Likes.Likes, opLight.Likes.Likes, $"{messagePrefix}: opLight.Likes.Likes");
             Assert.AreEqual(post.Likes.Dislikes, opLight.Likes.Dislikes, $"{messagePrefix}: opLight.Likes.Dislikes");
         }
         else
         {
             Assert.IsNull(opLight.Likes, $"{messagePrefix}: opLight.Likes == null");
         }
         if (post.Tags != null)
         {
             Assert.IsNotNull(opLight.Tags, $"{messagePrefix}: opLight.Tags != null");
             CollectionAssert.AreEquivalent(post.Tags.Tags.Distinct().ToList(), opLight.Tags.Tags.Distinct().ToList(), $"{messagePrefix}: opLight.Tags");
         }
         else
         {
             Assert.IsNull(opLight.Tags, $"{messagePrefix}: opLight.Tags == null");
         }
         Assert.IsNotNull(opLight.Flags, $"{messagePrefix}: opLight.Flags != null");
         CollectionAssert.AreEquivalent(post.Flags.Distinct().ToList(), opLight.Flags.Distinct().ToList(), $"{messagePrefix}: opLight.Flags");
         if (counterRetrieved)
         {
             Assert.AreEqual(postCounterNum, opLight.Counter, $"{messagePrefix}: opLight.Counter");
         }
     }
     else
     {
         Assert.Fail($"{messagePrefix}: не типа IBoardPostLight");
     }
 }
        private async ValueTask <Nothing> SetChildren(IBoardPostEntity entity)
        {
            if (entity == null)
            {
                return(Nothing.Value);
            }

            ValueTask <List <IBoardPostEntity> > DoLoad(IList <(PostStoreEntityId id, int counter)> toLoad)
            {
                return(OpenSession(session =>
                {
                    var result = new List <IBoardPostEntity>();
                    var loadContext = CreateLoadContext(session, true, true);
                    var loadContextToDispose = loadContext;
                    using (loadContextToDispose)
                    {
                        foreach (var id in toLoad)
                        {
                            Api.MakeKey(loadContext.table.Session, loadContext.table, id.id.Id, MakeKeyGrbit.NewKey);
                            if (Api.TrySeek(loadContext.table.Session, loadContext.table, SeekGrbit.SeekEQ))
                            {
                                var r = LoadBoardEntity(session, ref loadContext, FullLoadMode);
                                if (r is IBoardPostLight l)
                                {
                                    l.Counter = id.counter;
                                }
                                result.Add(r);
                            }
                        }
                    }
                    return result;
                }));
            }

            var collection       = entity as IBoardPostCollection;
            var threadCollection = entity as IBoardPageThreadCollection;

            if ((entity.EntityType == PostStoreEntityType.Thread || entity.EntityType == PostStoreEntityType.ThreadPreview || entity.EntityType == PostStoreEntityType.Catalog || entity.EntityType == PostStoreEntityType.BoardPage) &&
                entity is IPostModelStoreChildrenLoadStageInfo loadStage && (collection?.Posts != null || threadCollection?.Threads != null) &&
                entity.StoreId != null)
            {
                if (loadStage.Stage == ChildrenLoadStageId.Completed)
                {
                    var childrenIds = await OpenSession(session0 => EnumEntityChildren(session0, entity.StoreId.Value));

                    IList <IBoardPostEntity> loaded;
                    if (childrenIds.Count > 4)
                    {
                        var toLoad      = childrenIds.DistributeToProcess(3);
                        var tasks       = toLoad.Select(DoLoad).ToArray();
                        var taskResults = await CoreTaskHelper.WhenAllValueTasks(tasks);

                        loaded = taskResults.SelectMany(r => r).ToList();
                    }
                    else
                    {
                        loaded = await DoLoad(childrenIds);
                    }

                    if (collection?.Posts != null)
                    {
                        IEnumerable <IBoardPost> toAdd;
                        if (entity.EntityType == PostStoreEntityType.Catalog)
                        {
                            int GetCatalogSequence(IBoardPost p)
                            {
                                if (p is IBoardPostEntityWithSequence s)
                                {
                                    return(s.OnPageSequence);
                                }
                                return(int.MaxValue);
                            }

                            toAdd = loaded.OfType <IBoardPost>().Deduplicate(p => p.Link, BoardLinkEqualityComparer.Instance).OrderBy(GetCatalogSequence);
                        }
                        else
                        {
                            toAdd = loaded.OfType <IBoardPost>().Deduplicate(p => p.Link, BoardLinkEqualityComparer.Instance).OrderBy(p => p.Link, BoardLinkComparer.Instance);
                        }
                        foreach (var item in toAdd)
                        {
                            collection.Posts.Add(item);
                        }
                    }
                    if (threadCollection?.Threads != null)
                    {
                        var toAdd = loaded.OfType <IThreadPreviewPostCollection>().Deduplicate(p => p.Link, BoardLinkEqualityComparer.Instance).OrderBy(p => p.OnPageSequence);
                        foreach (var item in toAdd)
                        {
                            threadCollection.Threads.Add(item);
                        }
                    }
                }
            }

            return(Nothing.Value);
        }
Exemple #3
0
        protected void AssertFullLoadedPost(PostStoreEntityId collectionId, PostStoreEntityType entityType,
                                            IBoardPost post, string messagePrefix, IBoardPostEntity opEntity, PostStoreEntityId opId,
                                            bool counterRetrieved, int postCounterNum)
        {
            AssertLightLoadedPost(collectionId, entityType, post, messagePrefix, opEntity, opId, counterRetrieved, postCounterNum);
            if (opEntity is IBoardPost opPost)
            {
                Assert.AreEqual(post.UniqueId, opPost.UniqueId, $"{messagePrefix}: opPost.UniqueId");
                PostModelsTests.AssertDocuments(_provider, post.Comment, opPost.Comment);
                if (post.Country != null)
                {
                    Assert.IsNotNull(opPost.Country, $"{messagePrefix}: opPost.Country != null");
                    Assert.AreEqual(post.Country.ImageLink?.GetLinkHash(), opPost.Country.ImageLink?.GetLinkHash(), $"{messagePrefix}: opPost.Country");
                }
                else
                {
                    Assert.IsNull(opPost.Country, $"{messagePrefix}: opPost.Country == null");
                }
                if (post.Icon != null)
                {
                    Assert.IsNotNull(opPost.Icon, $"{messagePrefix}: opPost.Icon != null");
                    Assert.AreEqual(post.Icon.ImageLink?.GetLinkHash(), opPost.Icon.ImageLink?.GetLinkHash(), $"{messagePrefix}: opPost.Icon.ImageLink");
                    Assert.AreEqual(post.Icon.Description, opPost.Icon.Description, $"{messagePrefix}: opPost.Icon.Description");
                }
                else
                {
                    Assert.IsNull(opPost.Icon, $"{messagePrefix}: opPost.Icon == null");
                }
                Assert.AreEqual(post.Email, opPost.Email, $"{messagePrefix}: opPost.Email");
                Assert.AreEqual(post.Hash, opPost.Hash, $"{messagePrefix}: opPost.Email");
                if (post.Poster != null)
                {
                    Assert.IsNotNull(opPost.Poster, $"{messagePrefix}: opPost.Poster != null");
                    Assert.AreEqual(post.Poster.Name, opPost.Poster.Name, $"{messagePrefix}: opPost.Poster.Name");
                    Assert.AreEqual(post.Poster.Tripcode, opPost.Poster.Tripcode, $"{messagePrefix}: opPost.Poster.Tripcode");
                    Assert.AreEqual(post.Poster.NameColor, opPost.Poster.NameColor, $"{messagePrefix}: opPost.Poster.NameColor");
                    Assert.AreEqual(post.Poster.NameColorStr, opPost.Poster.NameColorStr, $"{messagePrefix}: opPost.Poster.NameColorStr");
                }
                else
                {
                    Assert.IsNull(opPost.Poster, $"{messagePrefix}: opPost.Poster == null");
                }
                Assert.IsNotNull(opPost.Quotes, $"{messagePrefix}: opPost.Quotes != null");
                CollectionAssert.AreEquivalent(post.Quotes.Distinct(BoardLinkEqualityComparer.Instance).Select(l => l.GetLinkHash()).ToList(), opPost.Quotes.Distinct(BoardLinkEqualityComparer.Instance).Select(l => l.GetLinkHash()).ToList(), $"{messagePrefix}: opPost.Quotes");
                Assert.IsNotNull(opPost.MediaFiles, $"{messagePrefix}: opPost.MediaFiles != null");

                Assert.AreEqual(post.MediaFiles.Count, opPost.MediaFiles.Count, $"{messagePrefix}: opPost.MediaFiles.Count");
                for (var i = 0; i < post.MediaFiles.Count; i++)
                {
                    var pMedia  = post.MediaFiles[i];
                    var opMedia = opPost.MediaFiles[i];
                    Assert.AreEqual(pMedia is IPostMediaWithSize, opMedia is IPostMediaWithSize, $"{messagePrefix}: opPost.MediaFiles[{i}] is IPostMediaWithSize");
                    Assert.AreEqual(pMedia is IPostMediaWithThumbnail, opMedia is IPostMediaWithThumbnail, $"{messagePrefix}: opPost.MediaFiles[{i}] is IPostMediaWithThumbnail");
                    Assert.AreEqual(pMedia is IPostMediaWithInfo, opMedia is IPostMediaWithInfo, $"{messagePrefix}: opPost.MediaFiles[{i}] is IPostMediaWithInfo");
                    Assert.AreEqual(pMedia.MediaLink?.GetLinkHash(), opMedia.MediaLink?.GetLinkHash(), $"{messagePrefix}: opPost.MediaFiles[{i}].MediaLink");
                    Assert.AreEqual(pMedia.FileSize, opMedia.FileSize, $"{messagePrefix}: opPost.MediaFiles[{i}].FileSize");
                    Assert.AreEqual(pMedia.MediaType, opMedia.MediaType, $"{messagePrefix}: opPost.MediaFiles[{i}].MediaType");
                    if (pMedia is IPostMediaWithSize pMedia2 && opMedia is IPostMediaWithSize opMedia2)
                    {
                        Assert.AreEqual(pMedia2.Size, opMedia2.Size, $"{messagePrefix}: opPost.MediaFiles[{i}].Size");
                    }
                    if (pMedia is IPostMediaWithThumbnail pMedia3 && opMedia is IPostMediaWithThumbnail opMedia3)
                    {
                        Assert.AreEqual(pMedia3.Thumbnail?.MediaLink.GetLinkHash(), opMedia3.Thumbnail?.MediaLink.GetLinkHash(), $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.MediaLink");
                        Assert.AreEqual(pMedia3.Thumbnail?.FileSize, opMedia3.Thumbnail?.FileSize, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.FileSize");
                        Assert.AreEqual(pMedia3.Thumbnail?.MediaType, opMedia3.Thumbnail?.MediaType, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.MediaType");
                        Assert.AreEqual(pMedia3.Thumbnail?.Size, opMedia3.Thumbnail?.Size, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.Size");
                    }
                    if (pMedia is IPostMediaWithInfo pMedia4 && opMedia is IPostMediaWithInfo opMedia4)
                    {
                        Assert.AreEqual(pMedia4.Nsfw, opMedia4.Nsfw, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.Nsfw");
                        Assert.AreEqual(pMedia4.DisplayName, opMedia4.DisplayName, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.DisplayName");
                        Assert.AreEqual(pMedia4.Duration, opMedia4.Duration, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.Duration");
                        Assert.AreEqual(pMedia4.FullName, opMedia4.FullName, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.FullName");
                        Assert.AreEqual(pMedia4.Hash, opMedia4.Hash, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.Hash");
                        Assert.AreEqual(pMedia4.Name, opMedia4.Name, $"{messagePrefix}: opPost.MediaFiles[{i}].Thumbnail.Name");
                    }
                }

                var dateDiff = Math.Abs((post.LoadedTime - opPost.LoadedTime).TotalSeconds);
                Assert.IsTrue(dateDiff < 1.5, $"{messagePrefix}: opPost.LoadedTime");
            }
            else
            {
                Assert.Fail($"{messagePrefix}: не типа IBoardPost");
            }
        }
Exemple #4
0
 protected void AssertBareEntityLoadedPost(PostStoreEntityId collectionId, PostStoreEntityType entityType,
                                           IBoardPost post, string messagePrefix, IBoardPostEntity opEntity, PostStoreEntityId opId)
 {
     AssertLinkOnlyLoadedPost(collectionId, entityType, post, messagePrefix, opEntity, opId);
     Assert.AreEqual(post.Subject, opEntity.Subject, $"{messagePrefix}: opEntity.Subject");
     if (post.Thumbnail != null)
     {
         Assert.IsNotNull(opEntity.Thumbnail, $"{messagePrefix}: opEntity.Thumbnail != null");
         Assert.AreEqual(post.Thumbnail.MediaLink?.GetLinkHash(), opEntity.Thumbnail.MediaLink?.GetLinkHash(), $"{messagePrefix}: opEntity.Thumbnail.MediaLink");
         Assert.AreEqual(post.Thumbnail.Size, opEntity.Thumbnail.Size, $"{messagePrefix}: opEntity.Thumbnail.Size");
         Assert.AreEqual(post.Thumbnail.FileSize, opEntity.Thumbnail.FileSize, $"{messagePrefix}: opEntity.Thumbnail.FileSize");
         Assert.AreEqual(post.Thumbnail.MediaType, opEntity.Thumbnail.MediaType, $"{messagePrefix}: opEntity.Thumbnail.MediaType");
     }
     else
     {
         Assert.IsNull(opEntity.Thumbnail, "opEntity.Thumbnail == null");
     }
 }
Exemple #5
0
 protected void AssertLinkOnlyLoadedPost(PostStoreEntityId collectionId, PostStoreEntityType entityType,
                                         IBoardPost post, string messagePrefix, IBoardPostEntity opLinkOnly, PostStoreEntityId opId)
 {
     Assert.IsNotNull(opLinkOnly, $"{messagePrefix}: opLinkOnly != null");
     Assert.AreEqual(post.Link.GetLinkHash(), opLinkOnly.Link.GetLinkHash(), $"{messagePrefix}: opLinkOnly.Link");
     Assert.AreEqual(post.ParentLink.GetLinkHash(), opLinkOnly.ParentLink.GetLinkHash(),
                     $"{messagePrefix}: opLinkOnly.ParentLink");
     Assert.AreEqual(entityType, opLinkOnly.EntityType, $"{messagePrefix}: opLinkOnly.EntityType");
     Assert.IsNotNull(opLinkOnly.StoreId, $"{messagePrefix}: opLinkOnly.StoreId");
     Assert.AreEqual(opId, opLinkOnly.StoreId.Value, $"{messagePrefix}: opLinkOnly.StoreId");
     Assert.IsNotNull(opLinkOnly.StoreParentId, $"{messagePrefix}: opLinkOnly.StoreParentId");
     Assert.AreEqual(collectionId, opLinkOnly.StoreParentId.Value, $"{messagePrefix}: opLinkOnly.StoreParentId");
 }