From 1ce8c07db306ccccb5d2f4e8619cd3b429ed0912 Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Wed, 10 Jul 2013 13:25:09 +0200 Subject: [PATCH] Load the yaml files optionally from external dir --- test_elasticsearch/test_server/__init__.py | 2 + test_elasticsearch/test_server/test_common.py | 70 ++++++++----------- .../test_server/yaml/10_analyze.yaml | 52 -------------- .../test_server/yaml/20_mapping.yaml | 45 ------------ 4 files changed, 30 insertions(+), 139 deletions(-) delete mode 100644 test_elasticsearch/test_server/yaml/10_analyze.yaml delete mode 100644 test_elasticsearch/test_server/yaml/20_mapping.yaml diff --git a/test_elasticsearch/test_server/__init__.py b/test_elasticsearch/test_server/__init__.py index ecebddb7..4541bcfa 100644 --- a/test_elasticsearch/test_server/__init__.py +++ b/test_elasticsearch/test_server/__init__.py @@ -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 diff --git a/test_elasticsearch/test_server/test_common.py b/test_elasticsearch/test_server/test_common.py index 9de0c599..c1afdfa1 100644 --- a/test_elasticsearch/test_server/test_common.py +++ b/test_elasticsearch/test_server/test_common.py @@ -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) diff --git a/test_elasticsearch/test_server/yaml/10_analyze.yaml b/test_elasticsearch/test_server/yaml/10_analyze.yaml deleted file mode 100644 index 3d5ca473..00000000 --- a/test_elasticsearch/test_server/yaml/10_analyze.yaml +++ /dev/null @@ -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:]\n\n4: \n[white:14->19:]\n\n6: \n[red:25->28:]\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! } - diff --git a/test_elasticsearch/test_server/yaml/20_mapping.yaml b/test_elasticsearch/test_server/yaml/20_mapping.yaml deleted file mode 100644 index 8c9fa204..00000000 --- a/test_elasticsearch/test_server/yaml/20_mapping.yaml +++ /dev/null @@ -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