From 3031e3bb6173660a006edc22af96d1fe5d843022 Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Thu, 6 Nov 2014 17:10:30 -0800 Subject: [PATCH] Create meta test --- Makefile | 5 +++- README.md | 20 +++++++++++++++ yamale/__init__.py | 1 + yamale/tests/test_meta_test.py | 20 +++++++++++++++ yamale/validator.py | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 yamale/tests/test_meta_test.py create mode 100644 yamale/validator.py diff --git a/Makefile b/Makefile index 6f7f0e9..f500e04 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,7 @@ bamboo: tag: @./setup.py tag -.PHONY: test tag bamboo coverage +clean: + @rm -rf .tox *.egg-info dist .coverage + +.PHONY: test tag bamboo coverage clean diff --git a/README.md b/README.md index ebdf5b6..c679ab9 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,26 @@ questions: - id: 'id_str1' ``` +Writing Tests +------------- +To validate YAML files when you run your program's tests use Yamale's YamaleTestCase + +Example: + +```python +class TestYaml(YamaleTestCase): + base_dir = os.path.dirname(os.path.realpath(__file__)) + schema = 'fixtures/custom_types.yaml' + yaml = 'fixtures/custom_types_good.yaml' + + def runTest(self): + self.validate() +``` + +`base_dir`: the path which is prefixed to other paths +`schema`: the path to your schema YAML file +`yaml`: the path or glob to the files you want to validate. Can also be an list of paths and/or globs. + Developers ---------- ### Testing diff --git a/yamale/__init__.py b/yamale/__init__.py index d9399a1..72cbe1e 100644 --- a/yamale/__init__.py +++ b/yamale/__init__.py @@ -1,3 +1,4 @@ from .yamale import make_schema, make_data, validate +from .validator import YamaleTestCase __version__ = '1.1.4' diff --git a/yamale/tests/test_meta_test.py b/yamale/tests/test_meta_test.py new file mode 100644 index 0000000..0ccbdfa --- /dev/null +++ b/yamale/tests/test_meta_test.py @@ -0,0 +1,20 @@ +import os +from yamale import YamaleTestCase + + +class TestYaml(YamaleTestCase): + base_dir = os.path.dirname(os.path.realpath(__file__)) + schema = 'fixtures/custom_types.yaml' + yaml = 'fixtures/custom_types_good.yaml' + + def runTest(self): + self.validate() + + +class TestBadYaml(YamaleTestCase): + base_dir = os.path.dirname(os.path.realpath(__file__)) + schema = 'fixtures/custom_types.yaml' + yaml = 'fixtures/custom_types_bad.yaml' + + def runTest(self): + self.validate(exception=ValueError) diff --git a/yamale/validator.py b/yamale/validator.py new file mode 100644 index 0000000..3226946 --- /dev/null +++ b/yamale/validator.py @@ -0,0 +1,46 @@ +import glob +import os +from unittest import TestCase + +import yamale + + +class YamaleTestCase(TestCase): + schema = None + yaml = None + base_dir = None + + def validate(self, exception=None): + schema = self.schema + yaml = self.yaml + base_dir = self.base_dir + + if schema is None: + return + + if type(yaml) != list: + yaml = [yaml] + + if base_dir is not None: + schema = os.path.join(base_dir, schema) + yaml = [os.path.join(base_dir, y) for y in yaml] + + yamale_schema = yamale.make_schema(schema) + datas = self.create_data(yaml) + + if exception is None: + self.assertIsNotNone(yamale.validate(yamale_schema, datas)) + else: + self.assertRaises(exception, yamale.validate, yamale_schema, datas) + + def create_data(self, yamls): + paths = set() + for g in yamls: + paths = paths.union(set(glob.glob(g))) + + data = [] + for d in [yamale.make_data(path) for path in paths]: + for c in d: + data.append(c) + + return data