public static async Task DatabaseTest_FindAll(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindAll");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title       = "title",
                Description = "desc",
                Body        = "body",
            });

            var postFound = await posts.FindAsync(Query.Query.Eq("Title", "title"));

            Assert.IsNotNull(postFound);
            Assert.AreEqual(1, postFound.Count());
            var post = postFound.Single();

            Assert.AreEqual("title", post.Title);
            Assert.AreEqual("desc", post.Description);
            Assert.AreEqual("body", post.Body);
        }
        public static async Task DatabaseTest_FindAll_NotContainsArray(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindAll_ContainsArray");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title = "title",
                Tags  = new[] { "tag1", "tag2" },
            });

            var postFound = await posts.FindAsync(Query.Query.NotContains("Tags", "tag1"));

            Assert.IsNotNull(postFound);
            Assert.AreEqual(0, postFound.Count());
        }
        public static async Task DatabaseTest_InsertWithoutId(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("Test_InsertWithoutId");

            Assert.AreEqual("Test_InsertWithoutId", db.Name);

            var posts = await db.GetCollectionAsync <PostWithoutId>("posts-no-id");

            Assert.AreEqual(posts.Name, "posts-no-id");

            var post = await posts.InsertAsync(new PostWithoutId()
            {
                Title       = "title",
                Description = "desc",
                Body        = "body",
            });

            Assert.IsNotNull(post);
        }
        public static async Task DatabaseTest_FindByObjectId(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindByObjectId");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            var id = Guid.NewGuid();
            await posts.InsertAsync(new Post()
            {
                Id          = id,
                Title       = "title",
                Description = "desc",
                Body        = "body",
            });

            var post = await posts.FindAsync(id);

            Assert.IsNotNull(post);
            Assert.AreEqual("title", post.Title);
            Assert.AreEqual("desc", post.Description);
            Assert.AreEqual("body", post.Body);
        }
        public static async Task DatabaseTest_Index(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_Index");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title    = "title of post 1",
                Tags     = new[] { "tag1", "tag2" },
                Updated  = DateTime.Now.AddDays(-1),
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content",
                        Updated = DateTime.Now.AddDays(-1),
                        Replies = new[]
                        { new Reply()
                          {
                              Author = new User()
                              {
                                  Username = "******"
                              }, Content = "reply to comment"
                          } }
                    }
                })
            });

            await posts.InsertAsync(new Post()
            {
                Title         = "title of post 2",
                Tags          = new[] { "tag2" },
                Updated       = DateTime.Now,
                FavoriteCount = 23,
                Comments      = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content to post 2",
                        Updated = DateTime.Now,
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "reply to comment of post 2"
                            }
                        }
                    },
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "second comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "admin reply to user 2 comment of post 2"
                            }
                        }
                    }
                })
            });

            await posts.EnsureIndexAsync("Updated");

            //delete even if not exists
            await posts.DeleteIndexAsync("FavoriteCount");

            await posts.EnsureIndexAsync("FavoriteCount");

            //inspect query plan to find index usage
            await posts.FindAllAsync(SortDescription.OrderByDescending("FavoriteCount"));

            await posts.DeleteIndexAsync("FavoriteCount");

            //inspect query plan to not find index usage
            await posts.FindAllAsync(SortDescription.OrderByDescending("FavoriteCount"));
        }
        public static async Task DatabaseTest_FindAll_Collection4(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindAll_Collection4");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title   = "title of post 1",
                Tags    = new[] { "tag1", "tag2" },
                Updated = DateTime.Now.AddDays(-1),
                Author  = new User()
                {
                    Username = "******"
                },
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content",
                        Updated = DateTime.Now.AddDays(-1),
                        Replies = new[]
                        { new Reply()
                          {
                              Author = new User()
                              {
                                  Username = "******"
                              }, Content = "reply to comment"
                          } }
                    }
                })
            });

            await posts.InsertAsync(new Post()
            {
                Title  = "title of post 2",
                Tags   = new[] { "tag2" },
                Author = new User()
                {
                    Username = "******"
                },
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "reply to comment of post 2"
                            }
                        }
                    },
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "second comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "admin reply to user 2 comment of post 2"
                            }
                        }
                    }
                })
            });

            await posts.InsertAsync(new Post()
            {
                Title   = "title of post 3",
                Tags    = new[] { "tag3", "tag5" },
                Updated = DateTime.Now.AddDays(-3),
                Author  = new User()
                {
                    Username = "******"
                },
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content to post3",
                        Updated = DateTime.Now.AddDays(-3),
                    }
                })
            });

            var adminPosts = await posts.FindAsync(_ => _.Author.Username == "admin");

            Assert.IsNotNull(adminPosts);
            Assert.AreEqual(2, adminPosts.Length);

            var userPostsAndReplies = await posts.FindAsync(_ =>
                                                            _.Author.Username == "user" || _.Comments[0].Replies[0].Author.Username == "user");

            Assert.IsNotNull(userPostsAndReplies);
            Assert.AreEqual(2, userPostsAndReplies.Length);

            var now      = DateTime.Now;
            var oldPosts = await posts.FindAsync(_ => _.Updated < now);

            Assert.IsNotNull(oldPosts);
            Assert.AreEqual(2, oldPosts.Length);
        }
        public static async Task DatabaseTest_FindAll_Collection3_Sort(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindAll_Collection3_Sort");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title    = "title of post 1",
                Tags     = new[] { "tag1", "tag2" },
                Updated  = DateTime.Now.AddDays(-1),
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content",
                        Updated = DateTime.Now.AddDays(-1),
                        Replies = new[]
                        { new Reply()
                          {
                              Author = new User()
                              {
                                  Username = "******"
                              }, Content = "reply to comment"
                          } }
                    }
                })
            });

            await posts.InsertAsync(new Post()
            {
                Title    = "title of post 2",
                Tags     = new[] { "tag2" },
                Updated  = DateTime.Now,
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content to post 2",
                        Updated = DateTime.Now,
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "reply to comment of post 2"
                            }
                        }
                    },
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "second comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "admin reply to user 2 comment of post 2"
                            }
                        }
                    }
                })
            });

            var query = Query.Query.Eq("Comments.Author.Username", "admin");

            var postFoundCount = await posts.CountAsync(query);

            Assert.AreEqual(2, postFoundCount);

            var postsFound = await posts.FindAsync(query, new[] { new SortDescription("Updated", SortOrder.Descending) });

            Assert.IsNotNull(postsFound);
            Assert.AreEqual(2, postsFound.Count());
            var post = postsFound.First();

            Assert.AreEqual("title of post 2", post.Title);
            Assert.IsNull(post.Description);

            postsFound = await posts.FindAsync(query,
                                               new[] { new SortDescription("Comments.Updated", SortOrder.Ascending) });

            Assert.IsNotNull(postsFound);
            Assert.AreEqual(2, postsFound.Count());
            post = postsFound.First();
            Assert.AreEqual("title of post 1", post.Title);
            Assert.IsNull(post.Description);

            postsFound = await posts.FindAllAsync(SortDescription.OrderById());

            Assert.IsNotNull(postsFound);
            Assert.AreEqual(2, postsFound.Count());

            postsFound = await posts.FindAsync(_ => _.Comments[0].Author.Username == "admin",
                                               SortDescription.OrderBy <Post>(_ => _.Comments[0].Updated));

            Assert.IsNotNull(postsFound);
            Assert.AreEqual(2, postsFound.Count());
            post = postsFound.First();
            Assert.AreEqual("title of post 1", post.Title);
            Assert.IsNull(post.Description);
        }
        public static async Task DatabaseTest_FindAll_Collection2(NsConnection nsClient)
        {
            var db = await nsClient.GetDatabaseAsync("DatabaseTest_FindAll_Collection2");

            await db.DeleteCollectionAsync("posts");

            var posts = await db.GetCollectionAsync <Post>("posts");

            await posts.InsertAsync(new Post()
            {
                Title    = "title of post 2",
                Tags     = new[] { "tag2" },
                Comments = new List <Comment>(new[]
                {
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "reply to comment of post 2"
                            }
                        }
                    },
                    new Comment()
                    {
                        Author = new User()
                        {
                            Username = "******"
                        },
                        Content = "second comment content to post 2",
                        Replies = new[]
                        {
                            new Reply()
                            {
                                Author = new User()
                                {
                                    Username = "******"
                                },
                                Content = "admin reply to user 2 comment of post 2"
                            }
                        }
                    }
                })
            });

            var query = Query.Query.Or(
                Query.Query.Eq("Comments.Author.Username", "admin"),
                Query.Query.Eq("Comments.Replies.Author.Username", "admin"));

            var postFoundCount = await posts.CountAsync(query);

            Assert.AreEqual(1, postFoundCount);

            var postsFound = await posts.FindAsync(query);

            Assert.IsNotNull(postsFound);
            Assert.AreEqual(1, postsFound.Count());
            var post = postsFound.Single();

            Assert.AreEqual("title of post 2", post.Title);
            Assert.IsNull(post.Description);
        }