Пример #1
0
        public void ByAuthorTest()
        {
            var cursor          = new Mock <IAsyncCursor <Article> >();
            var isFirstMoveNext = true;

            cursor.Setup(x => x.MoveNext(It.IsAny <CancellationToken>())).Returns(() =>
            {
                if (isFirstMoveNext)
                {
                    isFirstMoveNext = false;
                    return(true);
                }
                return(false);
            });
            cursor.SetupGet(x => x.Current).Returns(new[] { CreateArticle() });

            var mongoInitializer = new MockMongoWrapper <IMongoInitializer>()
                                   .SetupDatabase(x => x.SampleDb, x => x
                                                  .SetupCollection <Article>(
                                                      m => m.Setup(c => c.FindSync(It.IsAny <FilterDefinition <Article> >(), It.IsAny <FindOptions <Article, Article> >(), It.IsAny <CancellationToken>())).Returns(cursor.Object)
                                                      ))
                                   .Object;

            var count = new GetUserCommentsCountCommand(mongoInitializer).ByAuthor(2, 1);

            Assert.AreEqual(1, count);
        }
Пример #2
0
        private static void Main()
        {
            MongoInitializer = new MongoInitializer();

            var user = new User
            {
                Id = MongoInitializer.SampleDb.Users.NewId()
            };

            MongoInitializer.SampleDb.Users.InsertOne(user);
            MongoInitializer.SampleDb.Articles.InsertOne(new Article
            {
                AuthorId = user.Id
            });

            var article = MongoInitializer.SampleDb.Articles.AsQueryable().First(x => x.AuthorId == user.Id);

            MongoInitializer.SampleDb.Articles.UpdateById(article.Id, x => x.PushEach(a => a.Comments, new List <Comment>
            {
                new Comment(),
                new Comment
                {
                    UserId = user.Id
                }
            }));

            MongoInitializer.SampleDb.Articles.UpdateArrayById(article.Id,
                                                               x => x.Comments,
                                                               QueryExtensions.EQ <Comment>(a => a.UserId, user.Id),
                                                               (builder, array) => builder.Set(array, c => c.Text, "new text"));

            var count = new GetUserCommentsCountCommand(MongoInitializer).ByArticle(article.Id, user.Id);

            Console.WriteLine(count);
        }
Пример #3
0
        public void ByArticleTest()
        {
            var cursor = new Mock <IAsyncCursor <Article> >();

            cursor.Setup(x => x.MoveNext(It.IsAny <CancellationToken>())).Returns(true);
            cursor.SetupGet(x => x.Current).Returns(new[] { CreateArticle() });

            var mongoInitializer = new MockMongoWrapper <IMongoInitializer>()
                                   .SetupDatabase(x => x.SampleDb, x => x
                                                  .SetupCollection <User>()
                                                  .SetupCollection <Article>(
                                                      m => m.Setup(c => c.FindSync(It.IsAny <FilterDefinition <Article> >(), It.IsAny <FindOptions <Article, Article> >(), It.IsAny <CancellationToken>())).Returns(cursor.Object)
                                                      ))
                                   .SetupDatabase(x => x.LogDb,
                                                  x => x.SetupCollection <Log>())
                                   .Object;



            var count = new GetUserCommentsCountCommand(mongoInitializer).ByArticle(ObjectId.GenerateNewId().ToString(), 1);

            Assert.AreEqual(1, count);
        }