Skip to content

Commit

Permalink
Create meta test
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Nov 7, 2014
1 parent 6d42ecc commit 3031e3b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions yamale/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .yamale import make_schema, make_data, validate
from .validator import YamaleTestCase

__version__ = '1.1.4'
20 changes: 20 additions & 0 deletions yamale/tests/test_meta_test.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions yamale/validator.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3031e3b

Please sign in to comment.