Load the yaml files optionally from external dir

This commit is contained in:
Honza Kral
2013-07-10 13:25:09 +02:00
parent f1b4eec524
commit 1ce8c07db3
4 changed files with 30 additions and 139 deletions
@@ -27,6 +27,8 @@ server = None
pidfile = tempfile.mktemp()
def setup():
if 'YAML_TEST_DIR' not in os.environ:
raise SkipTest('')
global server
# check installed
+28 -42
View File
@@ -3,7 +3,7 @@ Dynamically generated set of TestCases based on set of yaml files decribing
some integration tests. These files are shared among all official Elasticsearch
clients.
"""
from os import walk
from os import walk, environ
from os.path import dirname, abspath, join
import yaml
from unittest import TestCase, SkipTest
@@ -23,6 +23,10 @@ class InvalidActionType(SkipTest):
class YamlTestCase(TestCase):
def setUp(self):
self.client = Elasticsearch(['localhost:9900'])
self.last_response = None
def run_code(self, test):
""" Execute an instruction based on it's type. """
for action in test:
@@ -92,59 +96,41 @@ class YamlTestCase(TestCase):
# clean up everything
self.client.indices.delete()
def test_from_yaml(self):
for test in self._definition:
for name, definition in test.items():
print name
self.run_code(definition)
def construct_case(filename, name):
"""
Parse a definition of a test case from a yaml file and construct the
TestCase subclass dynamically transforming the individual tests into test
methods. Always use the first one as `setUp`.
TestCase subclass dynamically.
"""
def get_test_method(name, test):
def test_(self):
self.run_code(test)
# remember the name as docstring so it will show up
test_.__doc__ = name
return test_
def get_setUp(name, definition):
def setUp(self):
self.client = Elasticsearch(['localhost:9900'])
self.last_response = None
self.run_code(definition)
# make sure the cluster is ready
self.client.cluster.health(wait_for_status='yellow')
self.client.indices.refresh()
setUp.__doc__ = name
return setUp
with open(filename) as f:
tests = list(yaml.load_all(f))
# take the first test as setUp method
attrs = {'setUp' : get_setUp(*list(tests.pop(0).items())[0])}
# create test methods for the rest
for i, test in enumerate(tests):
if not test:
continue
attrs['test_%d' % i] = get_test_method(*list(test.items())[0])
# dump all tests into one test method
attrs = {
'_definition': tests,
'_yaml_file': filename
}
return type(name, (YamlTestCase, ), attrs)
yaml_dir = join(abspath(dirname(__file__)), 'yaml')
yaml_dir = environ.get('YAML_TEST_DIR', None)
if yaml_dir:
# find all the test definitions in yaml files ...
for (path, dirs, files) in walk(yaml_dir):
for filename in files:
if not filename.endswith('.yaml'):
continue
# ... parse them
name = 'Test' + ''.join(s.title() for s in path.split('/')) + filename.rsplit('.', 1)[0][3:].title()
# and insert them into locals for test runner to find them
locals()[name] = construct_case(join(path, filename), name)
for (path, dirs, files) in walk(yaml_dir):
for filename in files:
if not filename.endswith('.yaml'):
continue
# ... parse them
name = 'Test' + ''.join(s.title() for s in path[len(yaml_dir) + 1:].split('/')) + filename.rsplit('.', 1)[0][3:].title()
# and insert them into locals for test runner to find them
locals()[name] = construct_case(join(path, filename), name)
@@ -1,52 +0,0 @@
---
"Analyze API setup":
- do:
indices.create:
index: test
body:
mappings:
test:
properties:
text:
type: string
analyzer: whitespace
---
"Analyze API text format":
- do:
indices.analyze:
format: text
text: tHE BLACK and white! AND red
- is:
tokens: "[black:4->9:<ALPHANUM>]\n\n4: \n[white:14->19:<ALPHANUM>]\n\n6: \n[red:25->28:<ALPHANUM>]\n"
---
"Analyze API JSON format":
- do:
indices.analyze:
text: Foo Bar
- length: { tokens: 2 }
- is: { tokens.0.token: foo }
- is: { tokens.1.token: bar }
---
"Analyze API JSON format - tokenizer and filter":
- do:
indices.analyze:
filters: lowercase
text: Foo Bar
tokenizer: keyword
- length: { tokens: 1 }
- is: { tokens.0.token: foo bar }
---
"Analyze API JSON format - index and field":
- do:
indices.analyze:
field: text
index: test
text: Foo Bar!
- length: { tokens: 2 }
- is: { tokens.0.token: Foo }
- is: { tokens.1.token: Bar! }
@@ -1,45 +0,0 @@
---
"Mapping setup":
- do:
indices.create:
index: test
- do:
indices.put_mapping:
index: test
type: test-type
body:
test-type:
properties:
text:
type: string
---
"Test get mapping retrieves existing mapping":
- do:
indices.get_mapping:
index: test
type: test-type
- is: { test-type.properties.text.type: string }
---
"Test delete mapping removes mapping":
- do:
indices.delete_mapping:
index: test
type: test-type
- do:
indices.get_mapping:
index: test
- length: { test: 0 }
---
"Test type exists":
- do:
indices.exists_type:
index: test
type: test-type
- is: true
---
"Test type doesn't exists":
- do:
indices.exists_type:
index: test
type: not-test-type
- is: false