From 812cb6cf5038993c589438dd8b745d7651372588 Mon Sep 17 00:00:00 2001 From: dbate Date: Fri, 4 Jul 2014 14:07:03 +0100 Subject: [PATCH] Removed superfluos tests --- django_cookbook/model/fields.py | 15 +++--- .../test/unit/test_IterField_GetPrepValue.py | 37 ------------- .../test/unit/test_IterField_PrepToPython.py | 3 ++ .../test/unit/test_IterField_ToPython.py | 52 ------------------- 4 files changed, 9 insertions(+), 98 deletions(-) delete mode 100644 django_cookbook/test/unit/test_IterField_GetPrepValue.py delete mode 100644 django_cookbook/test/unit/test_IterField_ToPython.py diff --git a/django_cookbook/model/fields.py b/django_cookbook/model/fields.py index db84fe5..c7ebeee 100644 --- a/django_cookbook/model/fields.py +++ b/django_cookbook/model/fields.py @@ -1,5 +1,4 @@ -import json - +import pickle from django.core.exceptions import ValidationError from django.db.models import TextField, SubfieldBase from six import with_metaclass @@ -7,16 +6,14 @@ class IterField(with_metaclass(SubfieldBase, TextField)): """ - Stores the an iterable object in the the database. The data is stored as JSON and so all the values given to the - field must be serializable by the "json" module. This includes dict, list, tuple, str, int, float, True, False and - None + Stores an iterable object in the the database. All the values given to the field must be serializable by the "pickle" package. """ def to_python(self, value): if isinstance(value, list) or isinstance(value, dict) or value is None: return value - if not isinstance(value, str): + if not isinstance(value, bytes): raise ValidationError("Invalid input for a IterField instance") if not value: @@ -24,10 +21,10 @@ def to_python(self, value): # We could store the data as a string representation of the iterable which we then "eval" but this would allow # for malicious data to be stores in the field so we need to do some sanity checking on the string. We let the - # json library handle this. - return json.loads(value) + # pickle library handle this. + return pickle.loads(value) def get_prep_value(self, value): - return json.dumps(value, separators=(',', ':')) + return pickle.dumps(value) diff --git a/django_cookbook/test/unit/test_IterField_GetPrepValue.py b/django_cookbook/test/unit/test_IterField_GetPrepValue.py deleted file mode 100644 index fc84074..0000000 --- a/django_cookbook/test/unit/test_IterField_GetPrepValue.py +++ /dev/null @@ -1,37 +0,0 @@ -import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_cookbook.test.settings") - -from django_cookbook.model.fields import IterField -from django.test import TestCase - - -class IterField_GetPrepValue(TestCase): - def test_ValueIsAnEmptyList_StringIsEmptyList(self): - field = IterField() - - self.assertEqual("[]", field.get_prep_value([])) - - def test_ValueIsAnEmptyDictionary_StringIsEmptyDict(self): - field = IterField() - - self.assertEqual("{}", field.get_prep_value({})) - - def test_ValueIsListOfElements_StringIsJsonRepresentationOfThatListWithNoSpaces(self): - field = IterField() - - self.assertEqual("[1,2,3,4,5]", field.get_prep_value([1, 2, 3, 4, 5])) - - def test_ValueIsListOfListsAndDictionaries_StringIsJsonRepresentationOfThatListWithNoSpaces(self): - field = IterField() - - self.assertEqual("[[1,2,3],{\"4\":5}]", field.get_prep_value([[1, 2, 3], {4: 5}])) - - def test_ValueIsDictionaryOfElements_StringIsJsonRepresentationOfThatListWithNoSpaces(self): - field = IterField() - - self.assertEqual("{\"1\":2,\"3\":4,\"5\":6}", field.get_prep_value({1: 2, 3: 4, 5: 6})) - - def test_ValueIsDictionaryOfListsAndDictionaries_StringIsJsonRepresentationOfThatListWithNoSpaces(self): - field = IterField() - - self.assertEqual("{\"1\":{\"2\":3},\"4\":[5,6]}", field.get_prep_value({1: {2: 3}, 4: [5, 6]})) diff --git a/django_cookbook/test/unit/test_IterField_PrepToPython.py b/django_cookbook/test/unit/test_IterField_PrepToPython.py index d8e16db..195261e 100644 --- a/django_cookbook/test/unit/test_IterField_PrepToPython.py +++ b/django_cookbook/test/unit/test_IterField_PrepToPython.py @@ -1,3 +1,6 @@ +import os +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_cookbook.test.settings") + from django.test import TestCase from django_cookbook.model.fields import IterField diff --git a/django_cookbook/test/unit/test_IterField_ToPython.py b/django_cookbook/test/unit/test_IterField_ToPython.py deleted file mode 100644 index 8e8fef7..0000000 --- a/django_cookbook/test/unit/test_IterField_ToPython.py +++ /dev/null @@ -1,52 +0,0 @@ -import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_cookbook.test.settings") - -from django.test import TestCase -from django_cookbook.model.fields import IterField - - -class IterField_ToPython(TestCase): - def test_ValueIsNone_ReturnedValueIsNone(self): - field = IterField() - - self.assertIsNone(field.to_python(None)) - - def test_StringIsEmpty_ReturnedValueIsEmptyList(self): - field = IterField() - - self.assertEqual([], field.to_python("")) - - def test_ValueIsList_ListIsReturned(self): - field = IterField() - - self.assertEqual([1, 2, {3: 4}], field.to_python([1, 2, {3: 4}])) - - def test_ValueIsDictionary_DictionaryIsReturned(self): - field = IterField() - - self.assertEqual({1: 2, 3: [4, 5, 6]}, field.to_python({1: 2, 3: [4, 5, 6]})) - - def test_StringIsEmptyList_ReturnedValueIsEmptyList(self): - field = IterField() - - self.assertEqual([], field.to_python("[]")) - - def test_StringIsListWithOneField_ReturnedValueIsListWithThatField(self): - field = IterField() - - self.assertEqual(["foo"], field.to_python('["foo"]')) - - def test_StringIsListWithMultipleField_ReturnedValueIsListWithThoseFields(self): - field = IterField() - - self.assertEqual(["foo", "bar", "boo"], field.to_python('["foo","bar","boo"]')) - - def test_StringIsListWithMultipleFieldOneOfWhichIsAList_ReturnedValueIsListWithThoseFields(self): - field = IterField() - - self.assertEqual(["foo", ["bar", "boo"], "moo"], field.to_python('["foo",["bar","boo"],"moo"]')) - - def test_StringIsDictionary_ReturnedValueIsADictionaryWithTheCorrectFields(self): - field = IterField() - - self.assertEqual({"foo": "bar", "boo": ["moo", "maa"]}, field.to_python('{"foo":"bar", "boo":["moo","maa"]}')) \ No newline at end of file