@@ -627,119 +627,110 @@ def test_open_range(self):
627
627
dbcore .query .NotQuery (q )
628
628
629
629
630
- class NotQueryTest (DummyDataTestCase ):
631
- """Test `query.NotQuery` against the dummy data:
632
- - `test_type_xxx`: tests for the negation of a particular XxxQuery class.
633
- - `test_get_yyy`: tests on query strings (similar to `GetTest`)
634
- """
630
+ class TestNotQuery :
631
+ """Test `query.NotQuery` against the dummy data."""
635
632
636
- def assertNegationProperties (self , q ):
637
- """Given a Query `q`, assert that:
638
- - q OR not(q) == all items
639
- - q AND not(q) == 0
640
- - not(not(q)) == q
641
- """
633
+ @pytest .fixture (autouse = True , scope = "class" )
634
+ def lib (self ):
635
+ test_case = DummyDataTestCase ()
636
+ test_case .setUp ()
637
+ return test_case .lib
638
+
639
+ @pytest .mark .parametrize (
640
+ "q, expected_results" ,
641
+ [
642
+ (
643
+ dbcore .query .BooleanQuery ("comp" , True ),
644
+ {"beets 4 eva" },
645
+ ),
646
+ (
647
+ dbcore .query .DateQuery ("added" , "2000-01-01" ),
648
+ {"foo bar" , "baz qux" , "beets 4 eva" },
649
+ ),
650
+ (
651
+ dbcore .query .FalseQuery (),
652
+ {"foo bar" , "baz qux" , "beets 4 eva" },
653
+ ),
654
+ (
655
+ dbcore .query .MatchQuery ("year" , "2003" ),
656
+ {"foo bar" , "baz qux" },
657
+ ),
658
+ (
659
+ dbcore .query .NoneQuery ("rg_track_gain" ),
660
+ set (),
661
+ ),
662
+ (
663
+ dbcore .query .NumericQuery ("year" , "2001..2002" ),
664
+ {"beets 4 eva" },
665
+ ),
666
+ (
667
+ dbcore .query .AnyFieldQuery (
668
+ "baz" , ["album" ], dbcore .query .MatchQuery
669
+ ),
670
+ {"beets 4 eva" },
671
+ ),
672
+ (
673
+ dbcore .query .AndQuery (
674
+ [
675
+ dbcore .query .BooleanQuery ("comp" , True ),
676
+ dbcore .query .NumericQuery ("year" , "2002" ),
677
+ ]
678
+ ),
679
+ {"foo bar" , "beets 4 eva" },
680
+ ),
681
+ (
682
+ dbcore .query .OrQuery (
683
+ [
684
+ dbcore .query .BooleanQuery ("comp" , True ),
685
+ dbcore .query .NumericQuery ("year" , "2002" ),
686
+ ]
687
+ ),
688
+ {"beets 4 eva" },
689
+ ),
690
+ (
691
+ dbcore .query .RegexpQuery ("artist" , "^t" ),
692
+ {"foo bar" },
693
+ ),
694
+ (
695
+ dbcore .query .SubstringQuery ("album" , "ba" ),
696
+ {"beets 4 eva" },
697
+ ),
698
+ (
699
+ dbcore .query .TrueQuery (),
700
+ set (),
701
+ ),
702
+ ],
703
+ ids = lambda x : x .__class__ if isinstance (x , dbcore .query .Query ) else "" ,
704
+ )
705
+ def test_query_type (self , lib , q , expected_results ):
706
+ def get_results (* args ):
707
+ return {i .title for i in lib .items (* args )}
708
+
709
+ # not(a and b) <-> not(a) or not(b)
642
710
not_q = dbcore .query .NotQuery (q )
711
+ not_q_results = get_results (not_q )
712
+ assert not_q_results == expected_results
713
+
643
714
# assert using OrQuery, AndQuery
644
715
q_or = dbcore .query .OrQuery ([q , not_q ])
716
+
645
717
q_and = dbcore .query .AndQuery ([q , not_q ])
646
- self . assert_items_matched_all ( self . lib . items ( q_or ))
647
- self . assert_items_matched ( self . lib . items ( q_and ), [] )
718
+ assert get_results ( q_or ) == { "foo bar" , "baz qux" , "beets 4 eva" }
719
+ assert get_results ( q_and ) == set ( )
648
720
649
721
# assert manually checking the item titles
650
- all_titles = {i .title for i in self .lib .items ()}
651
- q_results = {i .title for i in self .lib .items (q )}
652
- not_q_results = {i .title for i in self .lib .items (not_q )}
722
+ all_titles = get_results ()
723
+ q_results = get_results (q )
653
724
assert q_results .union (not_q_results ) == all_titles
654
725
assert q_results .intersection (not_q_results ) == set ()
655
726
656
727
# round trip
657
728
not_not_q = dbcore .query .NotQuery (not_q )
658
- assert {i .title for i in self .lib .items (q )} == {
659
- i .title for i in self .lib .items (not_not_q )
660
- }
729
+ assert get_results (q ) == get_results (not_not_q )
661
730
662
- def test_type_and (self ):
663
- # not(a and b) <-> not(a) or not(b)
664
- q = dbcore .query .AndQuery (
665
- [
666
- dbcore .query .BooleanQuery ("comp" , True ),
667
- dbcore .query .NumericQuery ("year" , "2002" ),
668
- ],
669
- )
670
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
671
- self .assert_items_matched (not_results , ["foo bar" , "beets 4 eva" ])
672
- self .assertNegationProperties (q )
673
-
674
- def test_type_boolean (self ):
675
- q = dbcore .query .BooleanQuery ("comp" , True )
676
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
677
- self .assert_items_matched (not_results , ["beets 4 eva" ])
678
- self .assertNegationProperties (q )
679
-
680
- def test_type_date (self ):
681
- q = dbcore .query .DateQuery ("added" , "2000-01-01" )
682
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
683
- # query date is in the past, thus the 'not' results should contain all
684
- # items
685
- self .assert_items_matched (
686
- not_results , ["foo bar" , "baz qux" , "beets 4 eva" ]
687
- )
688
- self .assertNegationProperties (q )
689
-
690
- def test_type_false (self ):
691
- q = dbcore .query .FalseQuery ()
692
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
693
- self .assert_items_matched_all (not_results )
694
- self .assertNegationProperties (q )
695
-
696
- def test_type_match (self ):
697
- q = dbcore .query .MatchQuery ("year" , "2003" )
698
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
699
- self .assert_items_matched (not_results , ["foo bar" , "baz qux" ])
700
- self .assertNegationProperties (q )
701
-
702
- def test_type_none (self ):
703
- q = dbcore .query .NoneQuery ("rg_track_gain" )
704
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
705
- self .assert_items_matched (not_results , [])
706
- self .assertNegationProperties (q )
707
-
708
- def test_type_numeric (self ):
709
- q = dbcore .query .NumericQuery ("year" , "2001..2002" )
710
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
711
- self .assert_items_matched (not_results , ["beets 4 eva" ])
712
- self .assertNegationProperties (q )
713
-
714
- def test_type_or (self ):
715
- # not(a or b) <-> not(a) and not(b)
716
- q = dbcore .query .OrQuery (
717
- [
718
- dbcore .query .BooleanQuery ("comp" , True ),
719
- dbcore .query .NumericQuery ("year" , "2002" ),
720
- ]
721
- )
722
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
723
- self .assert_items_matched (not_results , ["beets 4 eva" ])
724
- self .assertNegationProperties (q )
725
-
726
- def test_type_regexp (self ):
727
- q = dbcore .query .RegexpQuery ("artist" , "^t" )
728
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
729
- self .assert_items_matched (not_results , ["foo bar" ])
730
- self .assertNegationProperties (q )
731
-
732
- def test_type_substring (self ):
733
- q = dbcore .query .SubstringQuery ("album" , "ba" )
734
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
735
- self .assert_items_matched (not_results , ["beets 4 eva" ])
736
- self .assertNegationProperties (q )
737
-
738
- def test_type_true (self ):
739
- q = dbcore .query .TrueQuery ()
740
- not_results = self .lib .items (dbcore .query .NotQuery (q ))
741
- self .assert_items_matched (not_results , [])
742
- self .assertNegationProperties (q )
731
+
732
+ class NegationPrefixTest (DummyDataTestCase ):
733
+ """Tests negation prefixes."""
743
734
744
735
def test_get_prefixes_keyed (self ):
745
736
"""Test both negation prefixes on a keyed query."""
0 commit comments