@@ -86,6 +86,12 @@ def sample_papers() -> Generator[list[Paper], None, None]:
8686 ]
8787 )
8888
89+ papers [0 ].flags = {"valid" }
90+ papers [1 ].flags = {"invalid" }
91+ papers [2 ].flags = {"valid" , "invalid" }
92+ papers [3 ].flags = {"reviewed" }
93+ papers [4 ].flags = {"valid" , "reviewed" }
94+
8995 yield papers
9096
9197
@@ -190,6 +196,27 @@ def __getattr__(self, name):
190196 )
191197
192198
199+ def check_papers (
200+ data_regression : DataRegressionFixture , papers : list [Paper ], basename : str = None
201+ ):
202+ # Using file_regression and json.dumps to avoid
203+ # yaml.representer.RepresenterError on DatePrecision
204+ # papers = sort_keys(papers[:5])
205+ # [p.pop("acquired") for p in papers]
206+ papers = serialize (list [Paper ], papers )
207+
208+ for paper in papers :
209+ # MongoPaper uses ObjectId which will not be the same each time the test is
210+ # run
211+ paper ["id" ] = None if not isinstance (paper .get ("id" , None ), int ) else paper ["id" ]
212+ paper .pop ("_id" , None )
213+ paper .pop ("version" , None )
214+ # Sort flags to ensure consistent ordering
215+ paper ["flags" ] = sorted (paper ["flags" ])
216+
217+ data_regression .check (papers , basename = basename )
218+
219+
193220def test_add_papers (collection : PaperCollection , sample_papers : list [Paper ]):
194221 """Test adding multiple papers."""
195222 collection .add_papers (sample_papers )
@@ -406,6 +433,48 @@ def test_search_partial_matches(
406433 assert len (results ) == 4
407434
408435
436+ @pytest .mark .coll_w_remote
437+ def test_search_by_flags (collection : PaperCollection , sample_papers : list [Paper ]):
438+ """Test searching by flag inclusion and exclusion."""
439+ collection .add_papers (sample_papers )
440+
441+ # Test include_flags: papers must have ALL specified flags
442+ results = sorted (collection .search (include_flags = ["valid" ]), key = lambda x : x .title )
443+ assert len (results ) == 3
444+ assert all ({"valid" } <= p .flags for p in results )
445+
446+ # Test include_flags with multiple flags: papers must have ALL specified flags
447+ results = sorted (
448+ collection .search (include_flags = ["valid" , "reviewed" ]), key = lambda x : x .title
449+ )
450+ assert len (results ) == 1
451+ assert all ({"valid" , "reviewed" } <= p .flags for p in results )
452+
453+ # Test exclude_flags: papers must NOT have ANY of the specified flags
454+ results = sorted (collection .search (exclude_flags = ["invalid" ]), key = lambda x : x .title )
455+ assert len (results ) == len (sample_papers ) - 2
456+ assert all (not p .flags & {"invalid" } for p in results )
457+
458+ # Test exclude_flags with multiple flags
459+ results = sorted (
460+ collection .search (exclude_flags = ["valid" , "invalid" ]), key = lambda x : x .title
461+ )
462+ assert len (results ) == len (sample_papers ) - 4
463+ assert all (not p .flags & {"valid" , "invalid" } for p in results )
464+
465+ # Test combining include_flags and exclude_flags
466+ results = sorted (
467+ collection .search (include_flags = ["valid" ], exclude_flags = ["invalid" ]),
468+ key = lambda x : x .title ,
469+ )
470+ assert len (results ) == 2
471+ assert all ({"valid" } <= p .flags and not p .flags & {"invalid" } for p in results )
472+
473+ # Test with no flags (should return all papers)
474+ results = sorted (collection .search (), key = lambda x : x .title )
475+ assert len (results ) == 10
476+
477+
409478def test_file_collection_is_persistent (tmp_path : Path , sample_papers : list [Paper ]):
410479 collection = FileCollection (file = tmp_path / "collection.json" )
411480
@@ -442,10 +511,4 @@ def test_make_collection_item(
442511 assert paper .id == papers [0 ].id
443512 assert eq (paper , papers [0 ])
444513
445- paper = serialize (paper_cls , paper )
446- # MongoPaper uses ObjectId which will not be the same each time the test is
447- # run
448- paper ["id" ] = None if not isinstance (paper ["id" ], int ) else paper ["id" ]
449- paper .pop ("_id" , None )
450- paper .pop ("version" )
451- data_regression .check (paper )
514+ check_papers (data_regression , [paper ])
0 commit comments