Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding Values class and tests.

  • Loading branch information...
commit 644285005ccd99088c26ebf0df6977671021b8a4 1 parent 5c6f79c
Gregor Müllegger authored
49 autofixture/values.py
View
@@ -0,0 +1,49 @@
+from UserDict import UserDict, IterableUserDict
+
+
+class ValuesMetaclass(type):
+ def __new__(mcs, name, bases, attrs):
+ parent_value_attrs = {}
+ # left most base class overwrites righter base classes
+ for base in bases[::-1]:
+ if hasattr(base, '_value_attrs'):
+ parent_value_attrs.update(base._value_attrs)
+ defined_value_attrs = {}
+ for key in attrs:
+ if not key.startswith('__'):
+ defined_value_attrs[key] = attrs[key]
+
+ for key in defined_value_attrs:
+ del attrs[key]
+
+ attrs['_value_attrs'] = {}
+ attrs['_value_attrs'].update(parent_value_attrs)
+ attrs['_value_attrs'].update(defined_value_attrs)
+ return super(ValuesMetaclass, mcs).__new__(mcs, name, bases, attrs)
+
+
+class ValuesBase(IterableUserDict, object):
+ def __init__(self, *parents, **values):
+ self.data = self._value_attrs.copy()
+ for parent in parents:
+ if isinstance(parent, (dict, UserDict)):
+ self.data.update(parent)
+ else:
+ for attr in dir(parent):
+ if not attr.startswith('__'):
+ self.data[attr] = getattr(parent, attr)
+ self.data.update(**values)
+
+ def __add__(self, other):
+ return self.__class__(self, other)
+
+ def __radd__(self, other):
+ return self.__class__(other, self)
+
+ def __iadd__(self, other):
+ self.data.update(other)
+ return self
+
+
+class Values(ValuesBase):
+ __metaclass__ = ValuesMetaclass
0  autofixture_tests/values_tests/__init__.py
View
No changes.
0  autofixture_tests/values_tests/models.py
View
No changes.
109 autofixture_tests/values_tests/tests.py
View
@@ -0,0 +1,109 @@
+from django.test import TestCase
+from autofixture.values import Values
+
+
+class ValuesTests(TestCase):
+ def test_init(self):
+ values = Values({'a': 1, 'c': 4}, a=2, b=3)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ values = Values(values)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ class Data:
+ a = 1
+ b = 3
+ _c = 4
+ values = Values(Data, a=2)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['_c'], 4)
+
+ values = Values(Data(), a=2)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['_c'], 4)
+
+ values = Values(Values(a=1, c=4), a=2, b=3)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ def test_add_operation(self):
+ values = Values(a=1, b=3)
+ values = values + {'a': 2, 'c': 4}
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ def test_radd_operation(self):
+ values = Values(a=1, b=3)
+ values = {'a': 2, 'c': 4} + values
+ self.assertEqual(values['a'], 1)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ def test_iadd_operation(self):
+ values = Values(a=1, b=3)
+ values += {'a': 2, 'c': 4}
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ def test_subclassing(self):
+ class AB(Values):
+ a = 1
+ b = 2
+
+ values = AB()
+ self.assertEqual(values['a'], 1)
+ self.assertEqual(values['b'], 2)
+ self.assertRaises(KeyError, values.__getitem__, 'c')
+ self.assertRaises(AttributeError, getattr, values, 'a')
+ self.assertRaises(AttributeError, getattr, values, 'b')
+ self.assertRaises(AttributeError, getattr, values, 'c')
+
+ values = AB(b=3, c=4)
+ self.assertEqual(values['a'], 1)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ values += {'a': 2}
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
+
+ def test_sub_subclassing(self):
+ class AB(Values):
+ a = 1
+ b = 2
+
+ class ABCD(AB):
+ c = 3
+ d = 4
+
+ values = ABCD(a=2, c=4)
+ self.assertEqual(values['a'], 2)
+ self.assertEqual(values['b'], 2)
+ self.assertEqual(values['c'], 4)
+ self.assertEqual(values['d'], 4)
+
+ def test_multiple_inheritance(self):
+ class A(Values):
+ a = 1
+
+ class AB(Values):
+ a = 2
+ b = 3
+
+ class ABC(A, AB):
+ c = 4
+
+ values = ABC()
+ self.assertEqual(values['a'], 1)
+ self.assertEqual(values['b'], 3)
+ self.assertEqual(values['c'], 4)
1  runtests.py
View
@@ -16,6 +16,7 @@ def runtests(*args):
'autofixture_tests',
'autofixture_test',
'generator_test',
+ 'values_tests',
'sample_app',
],
verbosity=1, interactive=True)
1  settings.py
View
@@ -85,6 +85,7 @@
'autofixture_tests.sample_app',
'autofixture_tests.autofixture_test',
'autofixture_tests.generator_test',
+ 'autofixture_tests.values_tests',
'autofixture',
)
Please sign in to comment.
Something went wrong with that request. Please try again.