And() public static method

Tests that all the subqueries are true (see $and in newer versions of the server).
public static And ( ) : QueryComplete
return QueryComplete
        public void TestAndWithEmptyQuery()
        {
            var emptyQuery = new QueryDocument();

            var query    = Query.And(emptyQuery);
            var expected = "{ }";

            Assert.AreEqual(expected, query.ToJson());

            query    = Query.And(emptyQuery, emptyQuery);
            expected = "{ }";
            Assert.AreEqual(expected, query.ToJson());

            query    = Query.And(emptyQuery, Query.EQ("x", 1));
            expected = "{ \"x\" : 1 }";
            Assert.AreEqual(expected, query.ToJson());

            query    = Query.And(Query.EQ("x", 1), emptyQuery);
            expected = "{ \"x\" : 1 }";
            Assert.AreEqual(expected, query.ToJson());

            query    = Query.And(emptyQuery, Query.EQ("x", 1), emptyQuery);
            expected = "{ \"x\" : 1 }";
            Assert.AreEqual(expected, query.ToJson());

            query    = Query.And(Query.EQ("x", 1), emptyQuery, Query.EQ("y", 2));
            expected = "{ \"x\" : 1, \"y\" : 2 }";
            Assert.AreEqual(expected, query.ToJson());
        }
        public void TestAnd()
        {
            var query = Query.And(
                Query.EQ("a", 1),
                Query.EQ("b", 2)
                );
            var expected = "{ \"a\" : 1, \"b\" : 2 }";

            Assert.AreEqual(expected, query.ToJson());
        }
        public void TestEqualsTwoElements()
        {
            var query = Query.And(
                Query.EQ("x", 3),
                Query.EQ("y", "foo")
                );
            var expected = "{ \"x\" : 3, \"y\" : \"foo\" }";

            Assert.AreEqual(expected, query.ToJson());
        }
Example #4
0
        public void TestDuplicateEq()
        {
            // now that server supports $and this is actually syntactically valid
            var query = Query.And(
                Query.EQ("value", 6),
                Query.EQ("value", 20)
                );
            var expected = "{ '$and' : [{ 'value' : 6 }, { 'value' : 20 }] }".Replace("'", "\"");
            var json     = query.ToJson();

            Assert.AreEqual(expected, json);
        }
        public void TestElementMatch()
        {
            var query = Query.ElemMatch("x",
                                        Query.And(
                                            Query.EQ("a", 1),
                                            Query.GT("b", 1)
                                            )
                                        );
            var expected = "{ \"x\" : { \"$elemMatch\" : { \"a\" : 1, \"b\" : { \"$gt\" : 1 } } } }";

            Assert.AreEqual(expected, query.ToJson());
        }
        public void TestAndNullSecond()
        {
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                Query.And(
                    Query.EQ("x", 1),
                    Query.Null
                    );
            });

            Assert.IsTrue(ex.Message.StartsWith("One of the queries is null."));
        }
        public void TestNestedOr()
        {
            var query = Query.And(
                Query.EQ("name", "bob"),
                Query.Or(
                    Query.EQ("a", 1),
                    Query.EQ("b", 2)
                    )
                );
            var expected = "{ \"name\" : \"bob\", \"$or\" : [{ \"a\" : 1 }, { \"b\" : 2 }] }";

            Assert.AreEqual(expected, query.ToJson());
        }
Example #8
0
        public void TestAndInNotIn()
        {
            var query = Query.And(
                Query.In("value", 1, 2, 3, 4),
                Query.NotIn("value", 11, 12, 13, 14)
                );

            Assert.AreEqual(
                new BsonDocument
            {
                { "value", new BsonDocument
                  {
                      { "$in", new BsonArray {
                                1, 2, 3, 4
                            } },
                      { "$nin", new BsonArray {
                                11, 12, 13, 14
                            } }
                  } }
            },
                query.ToBsonDocument());
        }
Example #9
0
        public void TestAndGtLt()
        {
            var query = Query.And(
                Query.NotIn("value", 1, 2, 3),
                Query.EQ("OtherValue", 1),
                Query.GT("value", 6),
                Query.LT("value", 20)
                );

            Assert.AreEqual(
                new BsonDocument
            {
                { "value", new BsonDocument
                  {
                      { "$nin", new BsonArray {
                                1, 2, 3
                            } },
                      { "$gt", 6 },
                      { "$lt", 20 }
                  } },
                { "OtherValue", 1 }
            },
                query.ToBsonDocument());
        }
        public void TestAndNoArgs()
        {
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => { Query.And(); });

            Assert.IsTrue(ex.Message.StartsWith("Query.And cannot be called with zero queries."));
        }