summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Waldon <brian@waldon.cc>2015-08-03 10:12:27 -0700
committerBrian Waldon <brian@waldon.cc>2015-08-03 10:12:27 -0700
commit3811a1e742411f3cabea5d171880b2291e94f1f9 (patch)
tree60bd1b24db9635ce95066f9bf84bc9e476007636
parenta73b25215a772dbeb6dcac5feb02f4bd44131268 (diff)
parent5c077e013655aa03a1cc25206ac35e8083d339dd (diff)
downloadwarlock-3811a1e742411f3cabea5d171880b2291e94f1f9.tar.gz
Merge pull request #18 from wackywendell/master
Make 'name' optional
-rw-r--r--test/test_core.py22
-rw-r--r--tox.ini2
-rw-r--r--warlock/core.py8
3 files changed, 29 insertions, 3 deletions
diff --git a/test/test_core.py b/test/test_core.py
index c1f6118..21ceca1 100644
--- a/test/test_core.py
+++ b/test/test_core.py
@@ -41,6 +41,15 @@ complex_fixture = {
}
+nameless_fixture = {
+ 'properties': {
+ 'name': {'type': 'string'},
+ 'population': {'type': 'integer'},
+ },
+ 'additionalProperties': False,
+}
+
+
class TestCore(unittest.TestCase):
def test_create_invalid_object(self):
Country = warlock.model_factory(fixture)
@@ -96,6 +105,19 @@ class TestCore(unittest.TestCase):
exc = warlock.InvalidOperation
self.assertRaises(exc, sweden.update, {'population': 'N/A'})
self.assertRaises(exc, sweden.update, {'overloard': 'Bears'})
+
+ def test_naming(self):
+ Country = warlock.model_factory(fixture)
+ self.assertEqual(Country.__name__, 'Country')
+
+ Country2 = warlock.model_factory(fixture, name='Country2')
+ self.assertEqual(Country2.__name__, 'Country2')
+
+ nameless = warlock.model_factory(nameless_fixture)
+ self.assertEqual(nameless.__name__, 'Model')
+
+ nameless2 = warlock.model_factory(nameless_fixture, name='Country3')
+ self.assertEqual(nameless2.__name__, 'Country3')
def test_deepcopy(self):
"""Make sure we aren't leaking references."""
diff --git a/tox.ini b/tox.ini
index 7cf3514..a50b8c2 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py26,py27,py33,pep8
+envlist = py26,py27,py33,py34,pep8
[testenv]
deps=pytest
diff --git a/warlock/core.py b/warlock/core.py
index fa996d2..affdaa9 100644
--- a/warlock/core.py
+++ b/warlock/core.py
@@ -19,10 +19,11 @@ import copy
from . import model
-def model_factory(schema, base_class=model.Model):
+def model_factory(schema, base_class=model.Model, name=None):
"""Generate a model class based on the provided JSON Schema
:param schema: dict representing valid JSON schema
+ :param name: A name to give the class, if `name` is not in `schema`
"""
schema = copy.deepcopy(schema)
@@ -31,5 +32,8 @@ def model_factory(schema, base_class=model.Model):
self.__dict__['schema'] = schema
base_class.__init__(self, *args, **kwargs)
- Model.__name__ = str(schema['name'])
+ if name is not None:
+ Model.__name__ = name
+ elif 'name' in schema:
+ Model.__name__ = str(schema['name'])
return Model