44
55import cql2
66import pytest
7- from cql2 import Expr , ParseError , ValidationError
87
98
109def test_parse_file (fixtures : Path ) -> None :
@@ -16,52 +15,95 @@ def test_parse_file_str(fixtures: Path) -> None:
1615
1716
1817def test_init (example01_text : str ) -> None :
19- Expr (example01_text )
18+ cql2 . Expr (example01_text )
2019
2120
2221def test_parse_json (example01_text : str , example01_json : dict [str , Any ]) -> None :
2322 cql2 .parse_json (json .dumps (example01_json ))
24- with pytest .raises (ParseError ):
23+ with pytest .raises (cql2 . ParseError ):
2524 cql2 .parse_json (example01_text )
2625
2726
2827def test_parse_text (example01_text : str , example01_json : dict [str , Any ]) -> None :
2928 cql2 .parse_text (example01_text )
30- with pytest .raises (ParseError ):
29+ with pytest .raises (cql2 . ParseError ):
3130 cql2 .parse_text (json .dumps (example01_json ))
3231
3332
3433def test_to_json (example01_text : str ) -> None :
35- Expr (example01_text ).to_json () == {
34+ cql2 . Expr (example01_text ).to_json () == {
3635 "op" : "=" ,
3736 "args" : [{"property" : "landsat:scene_id" }, "LC82030282019133LGN00" ],
3837 }
3938
4039
4140def test_to_text (example01_json : dict [str , Any ]) -> None :
42- Expr (example01_json ).to_text () == "landsat:scene_id = 'LC82030282019133LGN00'"
41+ cql2 . Expr (example01_json ).to_text () == "landsat:scene_id = 'LC82030282019133LGN00'"
4342
4443
4544def test_to_sql (example01_text : str ) -> None :
46- sql_query = Expr (example01_text ).to_sql ()
45+ sql_query = cql2 . Expr (example01_text ).to_sql ()
4746 assert sql_query .query == '("landsat:scene_id" = $1)'
4847 assert sql_query .params == ["LC82030282019133LGN00" ]
4948
5049
5150def test_validate () -> None :
52- expr = Expr (
51+ expr = cql2 . Expr (
5352 {
5453 "op" : "t_before" ,
5554 "args" : [{"property" : "updated_at" }, {"timestamp" : "invalid-timestamp" }],
5655 }
5756 )
58- with pytest .raises (ValidationError ):
57+ with pytest .raises (cql2 . ValidationError ):
5958 expr .validate ()
6059
6160
6261def test_add () -> None :
63- assert Expr ("True" ) + Expr ("false" ) == Expr ("true AND false" )
62+ assert cql2 . Expr ("True" ) + cql2 . Expr ("false" ) == cql2 . Expr ("true AND false" )
6463
6564
6665def test_eq () -> None :
67- assert Expr ("True" ) == Expr ("true" )
66+ assert cql2 .Expr ("True" ) == cql2 .Expr ("true" )
67+
68+
69+ @pytest .mark .parametrize (
70+ "expr, item, should_match" ,
71+ [
72+ pytest .param (
73+ "boolfield and 1 + 2 = 3" ,
74+ {
75+ "properties" : {
76+ "eo:cloud_cover" : 10 ,
77+ "datetime" : "2020-01-01 00:00:00Z" ,
78+ "boolfield" : True ,
79+ }
80+ },
81+ True ,
82+ id = "pass on bool & cql2 arithmetic" ,
83+ ),
84+ pytest .param (
85+ "eo:cloud_cover <= 9" ,
86+ {
87+ "properties" : {
88+ "eo:cloud_cover" : 10 ,
89+ "datetime" : "2020-01-01 00:00:00Z" ,
90+ },
91+ },
92+ False ,
93+ id = "fail on property value comparison" ,
94+ ),
95+ pytest .param (
96+ "eo:cloud_cover <= 9" ,
97+ {
98+ "properties" : {
99+ "eo:cloud_cover" : 8 ,
100+ "datetime" : "2020-01-01 00:00:00Z" ,
101+ },
102+ },
103+ True ,
104+ id = "pass on property value comparison" ,
105+ ),
106+ ],
107+ )
108+ def test_matches (expr , item , should_match ) -> None :
109+ assert cql2 .Expr (expr ).matches (item ) == should_match
0 commit comments