forked from GISGIT/GEE-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_test.py
More file actions
executable file
·105 lines (81 loc) · 2.79 KB
/
Copy pathfunction_test.py
File metadata and controls
executable file
·105 lines (81 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
"""Tests for the ee.function module."""
import unittest
import ee
# A function to experiment on.
TEST_FUNC = ee.Function()
TEST_FUNC.getSignature = lambda: { # pylint: disable-msg=g-long-lambda
'description': 'Method description.',
'returns': 'Image',
'args': [
{
'type': 'Image',
'name': 'a',
'description': 'Arg A doc.'},
{
'type': 'Image',
'name': 'b',
'description': 'Arg B doc.',
'optional': True
}
]
}
EXPECTED_DOC = """Method description.
Args:
a: Arg A doc.
b: Arg B doc."""
class FunctionTest(unittest.TestCase):
def testNameArgs(self):
"""Verifies that Functions can convert positional to named arguments."""
self.assertEqual({}, TEST_FUNC.nameArgs([]))
self.assertEqual({'a': 42}, TEST_FUNC.nameArgs([42]))
self.assertEqual({'a': 42, 'b': 13}, TEST_FUNC.nameArgs([42, 13]))
self.assertEqual({'a': 3, 'b': 5}, TEST_FUNC.nameArgs([3], {'b': 5}))
self.assertRaisesWithRegexpMatch('Too many', TEST_FUNC.nameArgs, [1, 2, 3])
def testPromoteArgs(self):
"""Verifies that Functions can promote and verify their arguments."""
old_promoter = ee.Function._promoter
ee.Function._registerPromoter(lambda obj, type_name: [type_name, obj])
# Regular call.
self.assertEqual({
'a': ['Image', 42],
'b': ['Image', 13]
}, TEST_FUNC.promoteArgs({
'a': 42,
'b': 13
}))
# Allow missing optional argument.
self.assertEqual({'a': ['Image', 42]}, TEST_FUNC.promoteArgs({'a': 42}))
# Disallow unknown arguments.
self.assertRaisesWithRegexpMatch(
'Required argument', TEST_FUNC.promoteArgs, {})
# Disallow unknown arguments.
self.assertRaisesWithRegexpMatch(
'Unrecognized', TEST_FUNC.promoteArgs, {'a': 42, 'c': 13})
# Clean up.
ee.Function._registerPromoter(old_promoter)
def testCall(self):
"""Verifies the full function invocation flow."""
old_promoter = ee.Function._promoter
ee.Function._registerPromoter(lambda obj, type_name: [type_name, obj])
return_type, return_value = TEST_FUNC.call(42, 13)
self.assertEqual('Image', return_type)
self.assertEqual(TEST_FUNC, return_value.func)
self.assertEqual({
'a': ['Image', 42],
'b': ['Image', 13]
}, return_value.args)
# Clean up.
ee.Function._registerPromoter(old_promoter)
def testToString(self):
"""Verifies function docstring generation."""
self.assertEqual(EXPECTED_DOC, str(TEST_FUNC))
def assertRaisesWithRegexpMatch(self, msg, func, *args):
try:
func(*args)
except ee.EEException as e:
self.assertTrue(msg in str(e))
else:
self.fail('Expected an exception.')
if __name__ == '__main__':
unittest.main()