30
30
from apache_beam .utils .timestamp import Timestamp
31
31
from apache_beam .transforms .window import IntervalWindow
32
32
33
+ from nose_parameterized import parameterized
34
+
35
+ STANDARD_CODERS_YAML = os .path .join (
36
+ os .path .dirname (__file__ ), '..' , 'tests' , 'data' , 'standard_coders.yaml' )
37
+
38
+
39
+ def _load_test_cases (test_yaml ):
40
+ """Load test data from yaml file and return an iterable of test cases.
41
+
42
+ See ``standard_coders.yaml`` for more details.
43
+ """
44
+ if not os .path .exists (test_yaml ):
45
+ raise ValueError ('Could not find the test spec: %s' % test_yaml )
46
+ for ix , spec in enumerate (yaml .load_all (open (test_yaml ))):
47
+ spec ['index' ] = ix
48
+ name = spec .get ('name' , spec ['coder' ]['urn' ].split (':' )[- 2 ])
49
+ yield [name , spec ]
50
+
33
51
34
52
class StandardCodersTest (unittest .TestCase ):
35
53
@@ -54,30 +72,8 @@ class StandardCodersTest(unittest.TestCase):
54
72
'urn:beam:coders:stream:0.1' : lambda x , parser : map (parser , x )
55
73
}
56
74
57
- # We must prepend an underscore to this name so that the open-source unittest
58
- # runner does not execute this method directly as a test.
59
- @classmethod
60
- def _create_test (cls , spec ):
61
- counter = 0
62
- name = spec .get ('name' , spec ['coder' ]['urn' ].split (':' )[- 2 ])
63
- unique_name = 'test_' + name
64
- while hasattr (cls , unique_name ):
65
- counter += 1
66
- unique_name = 'test_%s_%d' % (name , counter )
67
- setattr (cls , unique_name , lambda self : self ._run_coder_test (spec ))
68
-
69
- # We must prepend an underscore to this name so that the open-source unittest
70
- # runner does not execute this method directly as a test.
71
- @classmethod
72
- def _create_tests (cls , coder_test_specs ):
73
- if not os .path .exists (STANDARD_CODERS_YAML ):
74
- raise ValueError (
75
- "Could not find the test spec: %s" % STANDARD_CODERS_YAML )
76
- for ix , spec in enumerate (yaml .load_all (open (coder_test_specs ))):
77
- spec ['index' ] = ix
78
- cls ._create_test (spec )
79
-
80
- def _run_coder_test (self , spec ):
75
+ @parameterized .expand (_load_test_cases (STANDARD_CODERS_YAML ))
76
+ def test_standard_coder (self , name , spec ):
81
77
coder = self .parse_coder (spec ['coder' ])
82
78
parse_value = self .json_value_parser (spec ['coder' ])
83
79
nested_list = [spec ['nested' ]] if 'nested' in spec else [True , False ]
@@ -124,11 +120,6 @@ def quote(s):
124
120
open (STANDARD_CODERS_YAML , 'w' ).write (doc_sep .join (docs ))
125
121
126
122
127
- STANDARD_CODERS_YAML = os .path .join (
128
- os .path .dirname (__file__ ), '..' , 'tests' , 'data' , 'standard_coders.yaml' )
129
- StandardCodersTest ._create_tests (STANDARD_CODERS_YAML )
130
-
131
-
132
123
def encode_nested (coder , value , nested = True ):
133
124
out = coder_impl .create_OutputStream ()
134
125
coder .get_impl ().encode_to_stream (value , out , nested )
0 commit comments