Skip to content

Commit 1ce8c07

Browse files
committed
Load the yaml files optionally from external dir
1 parent f1b4eec commit 1ce8c07

File tree

4 files changed

+30
-139
lines changed

4 files changed

+30
-139
lines changed

test_elasticsearch/test_server/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
pidfile = tempfile.mktemp()
2828

2929
def setup():
30+
if 'YAML_TEST_DIR' not in os.environ:
31+
raise SkipTest('')
3032
global server
3133

3234
# check installed

test_elasticsearch/test_server/test_common.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
some integration tests. These files are shared among all official Elasticsearch
44
clients.
55
"""
6-
from os import walk
6+
from os import walk, environ
77
from os.path import dirname, abspath, join
88
import yaml
99
from unittest import TestCase, SkipTest
@@ -23,6 +23,10 @@ class InvalidActionType(SkipTest):
2323

2424

2525
class YamlTestCase(TestCase):
26+
def setUp(self):
27+
self.client = Elasticsearch(['localhost:9900'])
28+
self.last_response = None
29+
2630
def run_code(self, test):
2731
""" Execute an instruction based on it's type. """
2832
for action in test:
@@ -92,59 +96,41 @@ def tearDown(self):
9296
# clean up everything
9397
self.client.indices.delete()
9498

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+
95105

96106

97107
def construct_case(filename, name):
98108
"""
99109
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.
102111
"""
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-
125112
with open(filename) as f:
126113
tests = list(yaml.load_all(f))
127114

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+
}
136120

137121
return type(name, (YamlTestCase, ), attrs)
138122

139123

140-
yaml_dir = join(abspath(dirname(__file__)), 'yaml')
124+
yaml_dir = environ.get('YAML_TEST_DIR', None)
125+
126+
if yaml_dir:
141127
# 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)
150136

test_elasticsearch/test_server/yaml/10_analyze.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

test_elasticsearch/test_server/yaml/20_mapping.yaml

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)