Skip to content

Commit

Permalink
make test_render_layer work for similarity_threshold=0 (single fields…
Browse files Browse the repository at this point in the history
… per geom)

Signed-off-by: minff <[email protected]>
  • Loading branch information
minff committed Oct 13, 2023
1 parent 1c9c65f commit 1437327
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
19 changes: 12 additions & 7 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
# import unittest
# class TestParser(BaseTestAsync, unittest.TestCase):
class TestParser(BaseTestAsync):
def __init__(self, *a, **kw):
def __init__(self, *a, test_parser_kw=None, **kw):
super().__init__(*a, **kw)
self.similarity_threshold = 0
self.mixed_case_duplicate = False
test_parser_kw = test_parser_kw or dict()
self.similarity_threshold = test_parser_kw.get("similarity_threshold", 80)
self.mixed_case_duplicate = test_parser_kw.get("mixed_case_duplicate", False)
self.has_many_fields = test_parser_kw.get("has_many_fields", False)

# util for debug
def assertEqual(self, first, second, msg=None):
Expand Down Expand Up @@ -466,13 +468,16 @@ def _assert_parsed_map(self, obj_feat, map_feat, map_fields):
]
feat_by_geom = sum(map_feat[geom_str], [])
self._assert_parsed_feat(obj_feat_by_geom, feat_by_geom)
self._assert_parsed_geom_unorder(obj_feat_by_geom, feat_by_geom, None, geom_str)

if not self.has_many_fields:
# element-wise assert
self._assert_parsed_geom(obj_feat_by_geom, feat_by_geom, None, geom_str)

for feat, fields in zip(map_feat[geom_str], map_fields[geom_str]):
self._assert_parsed_geom_unorder(obj_feat_by_geom, feat, fields, geom_str)
self._assert_parsed_fields_unorder(obj_feat_by_geom, feat, fields)

if not self.mixed_case_duplicate:
if not self.mixed_case_duplicate and not self.has_many_fields:
# element-wise assert
self._assert_parsed_geom(obj_feat_by_geom, feat, fields, geom_str)
self._assert_parsed_fields(obj_feat_by_geom, feat, fields)

def _assert_len_map_feat_fields(self, map_feat, map_fields):
Expand Down
65 changes: 49 additions & 16 deletions test/test_render_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
len_of_struct_sorted,
flatten,
format_map_fields,
add_test_fn_params,
)
from test import test_parser
from qgis.testing import unittest
Expand All @@ -38,20 +39,47 @@ def __init__(self, *a, **kw):
super().__init__(*a, **kw)
test_parser.TestParser._id = lambda x: self._id()

def get_test_parser_kw(self):
kw = dict()
params = self._subtest and self._subtest.params
if "similarity_threshold" in params:
similarity_threshold = params["similarity_threshold"]
kw["similarity_threshold"] = similarity_threshold
if "shuffle" in self._id() or similarity_threshold > 0:
return dict(has_many_fields=True)
return kw

def get_similarity_threshold_kw(self):
kw = self.get_test_parser_kw()
return (
dict(similarity_threshold=kw["similarity_threshold"])
if "similarity_threshold" in kw
else dict()
)

def test_render_mixed_json_to_layer_chunk(self):
folder = "xyzjson-small"
fnames = [
"mixed-xyz.geojson",
]
for fname in fnames:
self.subtest_render_mixed_json_to_layer(
folder,
fname,
ref_len_fields={None: [4], "MultiPoint": [20], "MultiPolygon": [27]},
similarity_threshold=0,
)
self.subtest_render_mixed_json_to_layer(
folder,
fname,
ref_len_fields={"MultiPoint": [3, 6, 18], "MultiPolygon": [27], None: [4]},
similarity_threshold=80,
)

def subtest_render_mixed_json_to_layer(self, folder, fname, ref_len_fields=None):
with self.subTest(folder=folder, fname=fname):
def subtest_render_mixed_json_to_layer(
self, folder, fname, ref_len_fields=None, similarity_threshold=0
):
with self.subTest(folder=folder, fname=fname, similarity_threshold=similarity_threshold):
resource = TestFolder(folder)
txt = resource.load(fname)
obj = json.loads(txt)
Expand Down Expand Up @@ -117,7 +145,7 @@ def subtest_render_mixed_json_to_layer_chunk(self, obj, chunk_size=100, empty_ch
with self.subTest(chunk_size=chunk_size):
layer = self.new_layer()
o = dict(obj)
obj_feat = obj["features"]
obj_feat = list(obj["features"])
lst_map_feat = list()
map_fields = dict()
lst_chunk: list = [
Expand All @@ -129,8 +157,14 @@ def subtest_render_mixed_json_to_layer_chunk(self, obj, chunk_size=100, empty_ch
lst_chunk.insert(i, list())
for chunk in lst_chunk:
o["features"] = chunk
map_feat, _ = parser.xyz_json_to_feature_map(o, map_fields, similarity_threshold=0)
test_parser.TestParser()._assert_parsed_map(chunk, map_feat, map_fields)
map_feat, _ = parser.xyz_json_to_feature_map(
o,
map_fields,
**self.get_similarity_threshold_kw(),
)
test_parser.TestParser(
test_parser_kw=self.get_test_parser_kw()
)._assert_parsed_map(chunk, map_feat, map_fields)
lst_map_feat.append(map_feat)
self._render_layer(layer, map_feat, map_fields)

Expand Down Expand Up @@ -160,7 +194,9 @@ def _assert_rendered_fields(self, vlayer, fields):
def _test_render_mixed_json_to_layer(self, obj):
layer = self.new_layer()
# map_feat, map_fields = parser.xyz_json_to_feature_map(obj)
map_feat, map_fields = test_parser.TestParser().do_test_parse_xyzjson_map(obj)
map_feat, map_fields = test_parser.TestParser(
test_parser_kw=self.get_test_parser_kw()
).do_test_parse_xyzjson_map(obj)
self._render_layer(layer, map_feat, map_fields)
self.assert_layer(layer, obj, map_fields)
return map_feat, map_fields
Expand Down Expand Up @@ -189,17 +225,14 @@ def _assert_len_ref_fields(self, map_fields, ref, strict=False):

def _assert_len_fields(self, map_fields, ref, strict=False):
len_ = len_of_struct if strict else len_of_struct_sorted
self.assertEqual(
len_(map_fields),
ref,
"\n".join(
[
"len of map_fields is not correct (vs. ref). "
+ "Please revised parser, similarity threshold.",
format_map_fields(map_fields),
]
),
msg = "\n".join(
[
"len of map_fields is not correct (vs. ref). "
+ "Please revised parser, similarity threshold.",
format_map_fields(map_fields),
]
)
self.assertEqual(len_(map_fields), ref, msg)

def assert_layer(self, layer, obj, map_fields):
lst_vlayer = list(layer.iter_layer())
Expand Down

0 comments on commit 1437327

Please sign in to comment.