@Test
public void testNestedAnd() {

    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2)
                    )),
                    queryField("c").equalsValue(3)
            ),
            keyField("p").nest(concatenateFields("a", "b")));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2)
                    )),
                    queryField("c").equalsValue(3)
            ),
            keyField("p").nest(concatenateFields("a", "b")));
    assertEqualityCoveringKey(MatchType.INEQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").greaterThan(2)
                    )),
                    queryField("c").equalsValue(3)
            ),
            keyField("p").nest(concatenateFields("a", "b")));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2)
                    )),
                    queryField("c").equalsValue(3)
            ),
            concat(keyField("p").nest(concatenateFields("a", "b")), keyField("c")));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2)
                    )),
                    queryField("c").equalsValue(3)
            ),
            concat(keyField("c"), keyField("p").nest(concatenateFields("a", "b")), keyField("c")));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").matches(Query.and(
                                    queryField("c").equalsValue(2),
                                    queryField("d").equalsValue(3)
                            ))
                    )),
                    queryField("e").equalsValue(4)
            ),
            concat(keyField("e"), keyField("p").nest("a")));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                queryField("p").matches(Query.and(
                        queryField("a").equalsValue(1),
                        queryField("b").matches(Query.and(
                                queryField("c").equalsValue(2),
                                queryField("d").equalsValue(3)
                        ))
                )),
                queryField("e").equalsValue(4)
            ),
            concat(keyField("e"), keyField("p").nest("a")));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").matches(Query.and(
                                    queryField("c").equalsValue(2),
                                    queryField("d").equalsValue(3)
                            ))
                    )),
                    queryField("e").equalsValue(4)
            ),
            concat(keyField("e"), keyField("p").nest(concat(keyField("b").nest(concatenateFields("c", "d")), keyField("a")))));
    assertEquality(MatchType.INEQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").lessThan(1),
                            queryField("b").matches(Query.and(
                                    queryField("c").equalsValue(2),
                                    queryField("d").equalsValue(3)
                            ))
                    )),
                    queryField("e").equalsValue(4)
            ),
            concat(keyField("e"), keyField("p").nest(concat(keyField("b").nest(concatenateFields("c", "d")), keyField("a")))));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").matches(Query.and(
                                    queryField("c").equalsValue(2),
                                    queryField("d").equalsValue(3)
                            ))
                    )),
                    queryField("e").equalsValue(4)
            ),
            concat(keyField("e"), keyField("p").nest(concat(keyField("b").nest(concatenateFields("c", "d")), keyField("a")))));
}