From e2c0bcaa90b6fc0393b4ba59fc05135d4821bea8 Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Thu, 23 May 2024 08:54:18 -0700 Subject: [PATCH] Feature, FeatureCollection, Image, and ImageCollection copyProperties: add testing PiperOrigin-RevId: 636566626 --- python/ee/tests/feature_test.py | 32 ++++++++++++++++++ python/ee/tests/featurecollection_test.py | 40 ++++++++++++++++++++++- python/ee/tests/imagecollection_test.py | 27 ++++++++++++++- 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/python/ee/tests/feature_test.py b/python/ee/tests/feature_test.py index 3255f7752..34b6d9871 100644 --- a/python/ee/tests/feature_test.py +++ b/python/ee/tests/feature_test.py @@ -24,12 +24,20 @@ 'arguments': {'crs': {'constantValue': EPSG_4326}}, } } +# ee.Feature(None).serialize())['values']['0'] FEATURE_NONE_GRAPH = { 'functionInvocationValue': { 'functionName': 'Feature', 'arguments': {}, } } +# ee.Feature(None, {'a': 'b'}).serialize())['values']['0'] +FEATURE_A_GRAPH = { + 'functionInvocationValue': { + 'functionName': 'Feature', + 'arguments': {'metadata': {'constantValue': {'a': 'b'}}}, + } +} def right_maxerror_proj(function_name: str) -> Dict[str, Any]: @@ -250,6 +258,30 @@ def test_convex_hull(self): result = json.loads(expression.serialize()) self.assertEqual(expect, result) + def test_copy_properties(self): + source = ee.Feature(None, {'a': 'b'}) + properties = ['c', 'd'] + exclude = ['e', 'f'] + expect = make_expression_graph({ + 'arguments': { + 'destination': FEATURE_NONE_GRAPH, + 'source': FEATURE_A_GRAPH, + 'properties': {'constantValue': properties}, + 'exclude': {'constantValue': exclude}, + }, + # Note this is Element rather than Feature + 'functionName': 'Element.copyProperties', + }) + expression = ee.Feature(None).copyProperties(source, properties, exclude) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + + expression = ee.Feature(None).copyProperties( + source=source, properties=properties, exclude=exclude + ) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + def test_cutLines(self): expect = make_expression_graph({ 'arguments': { diff --git a/python/ee/tests/featurecollection_test.py b/python/ee/tests/featurecollection_test.py index 9e6f1e93d..50697f542 100644 --- a/python/ee/tests/featurecollection_test.py +++ b/python/ee/tests/featurecollection_test.py @@ -39,6 +39,19 @@ def make_expression_graph( } } +FEATURES_A = { + 'functionInvocationValue': { + 'functionName': 'Collection.loadTable', + 'arguments': {'tableId': {'constantValue': 'a'}}, + } +} + +FEATURES_B = { + 'functionInvocationValue': { + 'functionName': 'Collection.loadTable', + 'arguments': {'tableId': {'constantValue': 'b'}}, + } +} class FeatureCollectionTestCase(apitestcase.ApiTestCase): @@ -203,6 +216,32 @@ def test_cluster(self): result = json.loads(expression.serialize()) self.assertEqual(expect, result) + def test_copy_properties(self): + source = ee.FeatureCollection('b') + properties = ['c', 'd'] + exclude = ['e', 'f'] + expect = make_expression_graph({ + 'arguments': { + 'destination': FEATURES_A, + 'source': FEATURES_B, + 'properties': {'constantValue': properties}, + 'exclude': {'constantValue': exclude}, + }, + # Note this is Element rather than FeatureCollection + 'functionName': 'Element.copyProperties', + }) + expression = ee.FeatureCollection('a').copyProperties( + source, properties, exclude + ) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + + expression = ee.FeatureCollection('a').copyProperties( + source=source, properties=properties, exclude=exclude + ) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + def test_inverse_distance(self): a_range = 2 property_name = 'property name' @@ -356,6 +395,5 @@ def test_random_points(self): self.assertEqual(expect, result) - if __name__ == '__main__': unittest.main() diff --git a/python/ee/tests/imagecollection_test.py b/python/ee/tests/imagecollection_test.py index e55f2d3eb..cf36f6f8c 100644 --- a/python/ee/tests/imagecollection_test.py +++ b/python/ee/tests/imagecollection_test.py @@ -185,6 +185,32 @@ def test_combine(self): result = json.loads(expression.serialize()) self.assertEqual(expect, result) + def test_copy_properties(self): + source = ee.ImageCollection('b') + properties = ['c', 'd'] + exclude = ['e', 'f'] + expect = make_expression_graph({ + 'arguments': { + 'destination': IMAGES_A, + 'source': IMAGES_B, + 'properties': {'constantValue': properties}, + 'exclude': {'constantValue': exclude}, + }, + # Note this is Element rather than ImageCollection + 'functionName': 'Element.copyProperties', + }) + expression = ee.ImageCollection('a').copyProperties( + source, properties, exclude + ) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + + expression = ee.ImageCollection('a').copyProperties( + source=source, properties=properties, exclude=exclude + ) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + def test_forma_trend(self): covariates = ee.ImageCollection('b') window_size = 3 @@ -376,6 +402,5 @@ def test_to_bands(self): self.assertEqual(expect, result) - if __name__ == '__main__': unittest.main()