@Test
public void testQueryAndPatterns() {

    assertEquality(MatchType.NO_MATCH,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2))),
            keyField("p").nest(concatenateFields("a", "c", "b")));
    assertEquality(MatchType.NO_MATCH,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2))),
            keyField("p").nest(keyField("a"), keyField("q").nest(keyField("c"), keyField("d")), keyField("b")));
    assertEquality(MatchType.EQUALITY,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2))),
            keyField("p").nest(keyField("a"), keyField("b"), keyField("q").nest(keyField("c"), keyField("d"))));
    assertEqualityCoveringKey(MatchType.NO_MATCH,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2))),
            keyField("p").nest(keyField("a"), keyField("b"), keyField("q").nest(keyField("c"), keyField("d"))));
    assertEquality(MatchType.EQUALITY,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2),
                            queryField("c").equalsValue(3))),
            keyField("p").nest(concatenateFields("c", "b", "a", "extra")));
    assertEqualityCoveringKey(MatchType.NO_MATCH,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2),
                            queryField("c").equalsValue(3))),
            keyField("p").nest(concatenateFields("c", "b", "a", "extra")));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            queryField("p").matches(
                    Query.and(
                            queryField("a").equalsValue(1),
                            queryField("b").equalsValue(2),
                            queryField("c").equalsValue(3))),
            keyField("p").nest(concatenateFields("c", "b")));
    assertEquality(MatchType.INEQUALITY,
            Query.and(
                    queryField("a").lessThanOrEquals(1),
                    queryField("b").equalsValue(2),
                    queryField("c").equalsValue(3)),
            concatenateFields("c", "b", "a"));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("a").lessThanOrEquals(1),
                    queryField("b").equalsValue(2)),
            concatenateFields("a", "b", "c", "d"));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2),
                    queryField("c").equalsValue(3),
                    queryField("DoesNotExist").lessThan(4)),
            concatenateFields("c", "b", "a"));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2),
                    queryField("c").equalsValue(3),
                    queryField("DoesNotExist").lessThan(4)),
            concatenateFields("c", "b", "a"));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("a").isNull(),
                    queryField("b").equalsValue(2)),
            concatenateFields("a", "b"));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(queryField("c").equalsValue(1)),
                    queryField("b").equalsValue(2)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(queryField("c").equalsValue(1)),
                    queryField("b").equalsValue(2),
                    queryField("a").equalsValue(1)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("p").matches(queryField("d").equalsValue(1)),
                    queryField("b").equalsValue(2)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("q").matches(queryField("c").equalsValue(1)),
                    queryField("b").equalsValue(2)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEquality(MatchType.INEQUALITY,
            Query.and(
                    queryField("p").matches(queryField("c").equalsValue(1)),
                    queryField("b").lessThanOrEquals(2)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("p").matches(queryField("c").lessThanOrEquals(1)),
                    queryField("b").equalsValue(2)),
            concat(
                    keyField("p").nest(keyField("c")),
                    keyField("b")));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("p").matches(queryField("c1").equalsValue(1)),
                    queryField("p").matches(queryField("c2").equalsValue(2))),
            concat(
                    keyField("p").nest(keyField("c1")),
                    keyField("p").nest(keyField("c2"))));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2)
            ),
            keyField("a"));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2)
            ),
            keyField("a"));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2)
            ),
            keyField("b"));
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("a").equalsValue(1),
                    queryField("b").equalsValue(2)
            ),
            keyField("b"));
    assertEquality(MatchType.EQUALITY,
            Query.and(
                    queryField("a").equalsValue(1),
                    new RecordTypeKeyComparison("ErsatzRecordType")
            ),
            concat(recordType(), keyField("a")));
    assertEquality(MatchType.NO_MATCH,
            Query.and(
                    queryField("a").equalsValue(1),
                    new RecordTypeKeyComparison("ErsatzRecordType")
            ),
            recordType());
    assertEqualityCoveringKey(MatchType.EQUALITY,
            Query.and(
                    queryField("a").equalsValue(1),
                    new RecordTypeKeyComparison("ErsatzRecordType")
            ),
            recordType());
}