+test_begin_subtest "count library function is non-destructive"
+cat<<EOF > EXPECTED
+1: 52 messages
+2: 52 messages
+Exclude 'spam'
+3: 52 messages
+4: 52 messages
+EOF
+test_python <<EOF
+import sys
+import notmuch
+
+query_string = 'tag:inbox or tag:spam'
+tag_string = 'spam'
+
+database = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
+query = notmuch.Query(database, query_string)
+
+print("1: {} messages".format(query.count_messages()))
+print("2: {} messages".format(query.count_messages()))
+print("Exclude '{}'".format(tag_string))
+query.exclude_tag(tag_string)
+print("3: {} messages".format(query.count_messages()))
+print("4: {} messages".format(query.count_messages()))
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+if [ $NOTMUCH_HAVE_SFSEXP -eq 1 ]; then
+
+ test_begin_subtest "and of exact terms (query=sexp)"
+ output=$(notmuch count --query=sexp '(and "wonderful" "wizard")')
+ test_expect_equal "$output" 1
+
+ test_begin_subtest "or of exact terms (query=sexp)"
+ output=$(notmuch count --query=sexp '(or "php" "wizard")')
+ test_expect_equal "$output" 2
+
+ test_begin_subtest "starts-with, case-insensitive (query=sexp)"
+ output=$(notmuch count --query=sexp '(starts-with FreeB)')
+ test_expect_equal "$output" 5
+
+ test_begin_subtest "query that matches no messages (query=sexp)"
+ count=$(notmuch count --query=sexp '(and (from keithp) (to keithp))')
+ test_expect_equal 0 "$count"
+
+ test_begin_subtest "Compound subquery (query=sexp)"
+ output=$(notmuch count --query=sexp '(thread (of (from keithp) (subject Maildir)))')
+ test_expect_equal "$output" 7
+
+fi
+