3
3
some integration tests. These files are shared among all official Elasticsearch
4
4
clients.
5
5
"""
6
- from os import walk
6
+ from os import walk , environ
7
7
from os .path import dirname , abspath , join
8
8
import yaml
9
9
from unittest import TestCase , SkipTest
@@ -23,6 +23,10 @@ class InvalidActionType(SkipTest):
23
23
24
24
25
25
class YamlTestCase (TestCase ):
26
+ def setUp (self ):
27
+ self .client = Elasticsearch (['localhost:9900' ])
28
+ self .last_response = None
29
+
26
30
def run_code (self , test ):
27
31
""" Execute an instruction based on it's type. """
28
32
for action in test :
@@ -92,59 +96,41 @@ def tearDown(self):
92
96
# clean up everything
93
97
self .client .indices .delete ()
94
98
99
+ def test_from_yaml (self ):
100
+ for test in self ._definition :
101
+ for name , definition in test .items ():
102
+ print name
103
+ self .run_code (definition )
104
+
95
105
96
106
97
107
def construct_case (filename , name ):
98
108
"""
99
109
Parse a definition of a test case from a yaml file and construct the
100
- TestCase subclass dynamically transforming the individual tests into test
101
- methods. Always use the first one as `setUp`.
110
+ TestCase subclass dynamically.
102
111
"""
103
- def get_test_method (name , test ):
104
- def test_ (self ):
105
- self .run_code (test )
106
-
107
- # remember the name as docstring so it will show up
108
- test_ .__doc__ = name
109
- return test_
110
-
111
-
112
- def get_setUp (name , definition ):
113
- def setUp (self ):
114
- self .client = Elasticsearch (['localhost:9900' ])
115
- self .last_response = None
116
- self .run_code (definition )
117
-
118
- # make sure the cluster is ready
119
- self .client .cluster .health (wait_for_status = 'yellow' )
120
- self .client .indices .refresh ()
121
-
122
- setUp .__doc__ = name
123
- return setUp
124
-
125
112
with open (filename ) as f :
126
113
tests = list (yaml .load_all (f ))
127
114
128
-
129
- # take the first test as setUp method
130
- attrs = {'setUp' : get_setUp (* list (tests .pop (0 ).items ())[0 ])}
131
- # create test methods for the rest
132
- for i , test in enumerate (tests ):
133
- if not test :
134
- continue
135
- attrs ['test_%d' % i ] = get_test_method (* list (test .items ())[0 ])
115
+ # dump all tests into one test method
116
+ attrs = {
117
+ '_definition' : tests ,
118
+ '_yaml_file' : filename
119
+ }
136
120
137
121
return type (name , (YamlTestCase , ), attrs )
138
122
139
123
140
- yaml_dir = join (abspath (dirname (__file__ )), 'yaml' )
124
+ yaml_dir = environ .get ('YAML_TEST_DIR' , None )
125
+
126
+ if yaml_dir :
141
127
# find all the test definitions in yaml files ...
142
- for (path , dirs , files ) in walk (yaml_dir ):
143
- for filename in files :
144
- if not filename .endswith ('.yaml' ):
145
- continue
146
- # ... parse them
147
- name = 'Test' + '' .join (s .title () for s in path .split ('/' )) + filename .rsplit ('.' , 1 )[0 ][3 :].title ()
148
- # and insert them into locals for test runner to find them
149
- locals ()[name ] = construct_case (join (path , filename ), name )
128
+ for (path , dirs , files ) in walk (yaml_dir ):
129
+ for filename in files :
130
+ if not filename .endswith ('.yaml' ):
131
+ continue
132
+ # ... parse them
133
+ name = 'Test' + '' .join (s .title () for s in path [ len ( yaml_dir ) + 1 :] .split ('/' )) + filename .rsplit ('.' , 1 )[0 ][3 :].title ()
134
+ # and insert them into locals for test runner to find them
135
+ locals ()[name ] = construct_case (join (path , filename ), name )
150
136
0 commit comments