# Path: hstore_flattenfields/models.py
# class DynamicField(models.Model):
#     """
#     Created to represent the Django Model's field information,
#     we use him to fill the Field instances when the ``refer``
#     instances will be build.
# 
#     :param refer: The name of the Model of the ``DynamicField``.
#     :param name: The name of the ``DynamicField``.
#     :param verbose_name: The Verbose Name of the ``DynamicField``.
#     :param order: The 0-indexed order which the ``DynamicField`` is places inside the ``Form``.
#     :param content_type: The ``ContentType`` which the ``ContentPane`` will be shown.
#     :param group: The ``DynamicFieldGroup`` instance which the ``ContentPane`` is owned.
# 
#     """
#     refer = models.CharField(max_length=120, blank=False, db_index=True, verbose_name=_("Class name"))
#     name = models.CharField(max_length=120, blank=False, db_index=True,unique=True, verbose_name=_("Field name"))
#     verbose_name = models.CharField(max_length=120, blank=False, verbose_name=_("Verbose name"))
#     typo = models.CharField(max_length=20, blank=False, db_index=True, verbose_name=_("Field type"), choices=single_list_to_tuple(FIELD_TYPES))
#     max_length = models.IntegerField(null=True, blank=True, verbose_name=_("Length"))
#     order = models.IntegerField(null=True, blank=True, default=None, verbose_name=_("Order"))
#     blank = models.BooleanField(default=True, verbose_name=_("Blank"))
#     choices = models.TextField(null=True, blank=True, verbose_name=_("Choices"))
#     default_value = models.CharField(max_length=80, null=True, blank=True, verbose_name=_("Default value"))
#     help_text = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Help Text'))
#     html_attrs = hstore.DictionaryField(db_index=True, null=True, blank=True, default=None, verbose_name=_("html Attributes"))
# 
#     # relations
#     group = models.ForeignKey(DynamicFieldGroup, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Groups"))
#     content_pane = models.ForeignKey(ContentPane, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Panel"))
# 
#     class Meta:
#         verbose_name = _('Dynamic Field')
#         verbose_name_plural = _('Dynamic Fields')
#         # ordering = ['pk']
# 
#     def __unicode__(self):
#         return self.verbose_name or self.name
# 
#     @property
#     def has_blank_option(self):
#         return self.blank and self.typo not in FIELD_TYPES_WITHOUT_BLANK_OPTION


from django.test import TestCase
from django.core.exceptions import ValidationError
from decimal import Decimal
from datetime import date, datetime
from django.db.models.fields import NOT_PROVIDED
from hstore_flattenfields.db.fields import *
from hstore_flattenfields.models import DynamicField
from tests.app.models import *
    def test_value_to_string(self):
        f = HstoreIntegerField(name="something_dfield_int")
        DynamicField.objects.create(id=1, refer="Something", typo="Integer", name="something_dfield_int", verbose_name=u"Dynamic Field Int")
        something = Something.objects.create(something_dfield_int=3)

        self.assertEqual(f.value_to_string(something), "3")
        _update_obj(something, "something_dfield_int", -3)
        self.assertEqual(f.value_to_string(something), "-3")
        _update_obj(something, "something_dfield_int", None)
        self.assertEqual(f.value_to_string(something), "")
        _update_obj(something, "something_dfield_int", "aaa")
        self.assertEqual(f.value_to_string(something), "")

    def test_clean(self):
        f = HstoreIntegerField(name="something_dfield_int")
        self.assertEqual(
            f.clean(42, Something()),
            42
        )


class HstoreDateFieldTests(TestCase):
    def test_to_python(self):
        f = HstoreDateField()
        self.assertEqual(f.to_python("2013-05-25"), date(2013, 05, 25))
        self.assertRaises(ValueError, f.to_python, "2013-52-99")
        self.assertEqual(f.to_python(None), None)

    def test_value_to_string(self):
        f = HstoreDateField(name="something_dfield_date")

# Path: hstore_flattenfields/models.py
# class DynamicField(models.Model):
#     """
#     Created to represent the Django Model's field information,
#     we use him to fill the Field instances when the ``refer``
#     instances will be build.
# 
#     :param refer: The name of the Model of the ``DynamicField``.
#     :param name: The name of the ``DynamicField``.
#     :param verbose_name: The Verbose Name of the ``DynamicField``.
#     :param order: The 0-indexed order which the ``DynamicField`` is places inside the ``Form``.
#     :param content_type: The ``ContentType`` which the ``ContentPane`` will be shown.
#     :param group: The ``DynamicFieldGroup`` instance which the ``ContentPane`` is owned.
# 
#     """
#     refer = models.CharField(max_length=120, blank=False, db_index=True, verbose_name=_("Class name"))
#     name = models.CharField(max_length=120, blank=False, db_index=True,unique=True, verbose_name=_("Field name"))
#     verbose_name = models.CharField(max_length=120, blank=False, verbose_name=_("Verbose name"))
#     typo = models.CharField(max_length=20, blank=False, db_index=True, verbose_name=_("Field type"), choices=single_list_to_tuple(FIELD_TYPES))
#     max_length = models.IntegerField(null=True, blank=True, verbose_name=_("Length"))
#     order = models.IntegerField(null=True, blank=True, default=None, verbose_name=_("Order"))
#     blank = models.BooleanField(default=True, verbose_name=_("Blank"))
#     choices = models.TextField(null=True, blank=True, verbose_name=_("Choices"))
#     default_value = models.CharField(max_length=80, null=True, blank=True, verbose_name=_("Default value"))
#     help_text = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Help Text'))
#     html_attrs = hstore.DictionaryField(db_index=True, null=True, blank=True, default=None, verbose_name=_("html Attributes"))
# 
#     # relations
#     group = models.ForeignKey(DynamicFieldGroup, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Groups"))
#     content_pane = models.ForeignKey(ContentPane, null=True, blank=True, related_name="dynamic_fields", verbose_name=_("Panel"))
# 
#     class Meta:
#         verbose_name = _('Dynamic Field')
#         verbose_name_plural = _('Dynamic Fields')
#         # ordering = ['pk']
# 
#     def __unicode__(self):
#         return self.verbose_name or self.name
# 
#     @property
#     def has_blank_option(self):
#         return self.blank and self.typo not in FIELD_TYPES_WITHOUT_BLANK_OPTION


# Path: tests/app/tests/test_model_fields.py
from django.test import TestCase
from django.core.exceptions import ValidationError
from decimal import Decimal
from datetime import date, datetime
from django.db.models.fields import NOT_PROVIDED
from hstore_flattenfields.db.fields import *
from hstore_flattenfields.models import DynamicField
from tests.app.models import *

    def test_value_to_string(self):
        f = HstoreIntegerField(name="something_dfield_int")
        DynamicField.objects.create(id=1, refer="Something", typo="Integer", name="something_dfield_int", verbose_name=u"Dynamic Field Int")
        something = Something.objects.create(something_dfield_int=3)

        self.assertEqual(f.value_to_string(something), "3")
        _update_obj(something, "something_dfield_int", -3)
        self.assertEqual(f.value_to_string(something), "-3")
        _update_obj(something, "something_dfield_int", None)
        self.assertEqual(f.value_to_string(something), "")
        _update_obj(something, "something_dfield_int", "aaa")
        self.assertEqual(f.value_to_string(something), "")

    def test_clean(self):
        f = HstoreIntegerField(name="something_dfield_int")
        self.assertEqual(
            f.clean(42, Something()),
            42
        )


class HstoreDateFieldTests(TestCase):
    def test_to_python(self):
        f = HstoreDateField()
        self.assertEqual(f.to_python("2013-05-25"), date(2013, 05, 25))
        self.assertRaises(ValueError, f.to_python, "2013-52-99")
        self.assertEqual(f.to_python(None), None)

    def test_value_to_string(self):
        f = HstoreDateField(name="something_dfield_date")

        DynamicField.objects.create(id=1, refer="Something", typo="Date", name="something_dfield_date", verbose_name=u"Dynamic Field Date")