Ejemplo n.º 1
0
    def test_formfield(self):
        info = meta.column_info(sa.Column(sa.Boolean()), name="test")
        formfield = info.formfield()
        self.assertIsInstance(formfield, djangofields.NullBooleanField)

        info = meta.column_info(sa.Column(sa.Boolean(), nullable=False),
                                name="test")
        formfield = info.formfield()
        self.assertIsInstance(formfield, djangofields.BooleanField)
Ejemplo n.º 2
0
    def test_formfield(self):
        info = meta.column_info(sa.Column(sa.String()), name="test")
        formfield = info.formfield()
        self.assertIsInstance(formfield, djangofields.CharField)

        info = meta.column_info(sa.Column(sa.Text()), name="test")
        formfield = info.formfield()
        self.assertIsInstance(formfield, djangofields.CharField)
        self.assertIsInstance(formfield.widget, widgets.Textarea)
Ejemplo n.º 3
0
    def test_override_form_class(self):
        column = fields.EnumField(choices=["a", "b"],
                                  form_class=djangofields.ChoiceField)

        form_field = meta.column_info(column).formfield()
        self.assertIs(type(form_field), djangofields.ChoiceField)
        self.assertIsNot(type(form_field), djangofields.TypedChoiceField)

        column = fields.URLField(form_class=djangofields.CharField, length=100)
        form_field = meta.column_info(column).formfield()
        self.assertIs(type(form_field), djangofields.CharField)
        self.assertIsNot(type(form_field), djangofields.URLField)
Ejemplo n.º 4
0
    def test_clean(self):
        info = meta.column_info(sa.Column(
            sa.Numeric(precision=14, scale=2, asdecimal=True)),
                                name="test")
        tests = [
            (None, None),
            ("", None),
            ("1", Decimal("1")),
            (Decimal("1"), Decimal("1")),
            ("abc", ValidationError),
            (1, Decimal("1")),
            (4123411130.3419398, Decimal("4123411130.3419")),
            ("20,000", Decimal("20000")),
            ("1.e-8", Decimal("1E-8")),
            ("1.-8", ValidationError),
            # excess whitespace tests
            ("\t\t\t\t\n", None),
            ("\t\tabc\t\t\n", ValidationError),
            ("\t\t20,000\t\t\n", Decimal("20000")),
            ("  \t 23\t", Decimal("23")),
        ]
        _run_tests(self, info, tests)

        with self.settings(THOUSAND_SEPARATOR=".", DECIMAL_SEPARATOR=","):
            tests = [("20.000", Decimal("20000")),
                     ("20,000", Decimal("20.000"))]
            _run_tests(self, info, tests)
Ejemplo n.º 5
0
    def test_text_field(self):
        column = fields.TextField()
        self.assertIsInstance(column.type, sa.Text)

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.CharField)
        self.assertIsInstance(form_field.widget, djangoforms.Textarea)
Ejemplo n.º 6
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Date()), name="test")
     tests = [
         (None, None),
         ("Hello", ValidationError),
         (date(2006, 10, 25), date(2006, 10, 25)),
         (datetime(2006, 10, 25, 14, 30), date(2006, 10, 25)),
         (datetime(2006, 10, 25, 14, 30, 59), date(2006, 10, 25)),
         (datetime(2006, 10, 25, 14, 30, 59, 200), date(2006, 10, 25)),
         (datetime(2006,
                   10,
                   25,
                   14,
                   30,
                   59,
                   200,
                   tzinfo=pytz.timezone("EST")), date(2006, 10, 25)),
         ("2006-10-25", date(2006, 10, 25)),
         ("10/25/2006", date(2006, 10, 25)),
         ("10/25/06", date(2006, 10, 25)),
         ("Oct 25 2006", date(2006, 10, 25)),
         ("October 25 2006", date(2006, 10, 25)),
         ("October 25, 2006", date(2006, 10, 25)),
         ("25 October 2006", date(2006, 10, 25)),
         ("25 October, 2006", date(2006, 10, 25)),
         (1335172500.12, date(2012, 4, 23)),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 7
0
 def test_enum_field_enum_choices_with_constraint_name(self):
     column = fields.EnumField(choices=DummyEnum, constraint_name="dummy")
     self.assertIsInstance(column.type, sa.Enum)
     self.assertEqual(column.type.enum_class, DummyEnum)
     self.assertEqual(column.type.name, "dummy")
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, formfields.EnumField)
Ejemplo n.º 8
0
 def test_formfield(self):
     info = meta.column_info(sa.Column(
         sa.Numeric(precision=14, scale=2, asdecimal=True)),
                             name="test")
     formfield = info.formfield()
     self.assertIsInstance(formfield, djangofields.DecimalField)
     self.assertEqual(formfield.max_digits, 14)
     self.assertEqual(formfield.decimal_places, 2)
Ejemplo n.º 9
0
    def test_formfield(self):
        class Demo(enum.Enum):
            one = "1"
            two = "2"

        info = meta.column_info(sa.Column(sa.Enum(Demo)), name="test")
        formfield = info.formfield()
        self.assertIsInstance(formfield, sorceryfields.EnumField)
Ejemplo n.º 10
0
 def test_enum_field_plain_choices(self):
     column = fields.EnumField(choices=["a", "b"])
     self.assertIsInstance(column.type, sa.Enum)
     self.assertIsNone(column.type.enum_class)
     self.assertEqual(column.type.enums, ["a", "b"])
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, djangofields.TypedChoiceField)
     self.assertEqual(form_field.choices, [("a", "a"), ("b", "b")])
Ejemplo n.º 11
0
    def test_url_field(self):
        column = fields.URLField(length=100)
        self.assertIsInstance(column.type, sa.String)
        self.assertIsInstance(column.info["validators"][0],
                              django_validators.URLValidator)

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.URLField)
Ejemplo n.º 12
0
    def test_boolean_field(self):
        column = fields.BooleanField()
        self.assertIsInstance(column.type, sa.Boolean)
        self.assertFalse(column.nullable)
        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.BooleanField)

        column = fields.BooleanField(constraint_name="test")
        self.assertEqual(column.type.name, "test")
Ejemplo n.º 13
0
    def test_char_field(self):
        with self.assertRaises(TypeError):
            column = fields.CharField()

        column = fields.CharField(max_length=120)
        self.assertEqual(column.type.length, 120)
        self.assertIsInstance(column.info["validators"][-1],
                              django_validators.MaxLengthValidator)
        self.assertEqual(column.info["validators"][-1].limit_value, 120)
        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.CharField)
Ejemplo n.º 14
0
    def test_smallinteger_field(self):
        column = fields.SmallIntegerField()
        self.assertIsInstance(column.type, sa.SmallInteger)
        self.assertIsInstance(column.info["validators"][-2],
                              django_validators.MinValueValidator)
        self.assertEqual(column.info["validators"][-2].limit_value, -32768)
        self.assertIsInstance(column.info["validators"][-1],
                              django_validators.MaxValueValidator)
        self.assertEqual(column.info["validators"][-1].limit_value, 32767)

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.IntegerField)
Ejemplo n.º 15
0
 def test_email_field(self):
     column = fields.EmailField(length=100)
     self.assertIsInstance(column.type, sa.String)
     self.assertEqual(
         {
             "form_class": djangofields.EmailField,
             "required": False,
             "validators": [django_validators.validate_email, mock.ANY],
         },
         column.info,
     )
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, djangofields.EmailField)
Ejemplo n.º 16
0
    def test_biginteger_field(self):
        column = fields.BigIntegerField()
        self.assertIsInstance(column.type, sa.BigInteger)
        self.assertIsInstance(column.info["validators"][-2],
                              django_validators.MinValueValidator)
        self.assertEqual(column.info["validators"][-2].limit_value,
                         -9223372036854775808)
        self.assertIsInstance(column.info["validators"][-1],
                              django_validators.MaxValueValidator)
        self.assertEqual(column.info["validators"][-1].limit_value,
                         9223372036854775807)

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.IntegerField)
Ejemplo n.º 17
0
    def test_float_field(self):
        column = fields.FloatField()
        self.assertIsInstance(column.type, sa.Float)
        self.assertIsNone(column.type.precision)
        self.assertFalse(column.type.asdecimal)

        column = fields.FloatField(precision=10)
        self.assertEqual(column.type.precision, 10)

        column = fields.FloatField(max_digits=10)
        self.assertEqual(column.type.precision, 10)

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.FloatField)
Ejemplo n.º 18
0
 def test_column_info_label_and_text(self):
     column = meta.column_info(dbfields.TextField(label="dummy"),
                               name="test")
     self.assertDictEqual(
         column.field_kwargs,
         {
             "help_text": None,
             "label": "dummy",
             "max_length": None,
             "required": False,
             "validators": [],
             "widget": djangoforms.Textarea,
         },
     )
Ejemplo n.º 19
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.DateTime()), name="test")
     tests = [
         (None, None),
         (
             datetime(2006,
                      10,
                      25,
                      14,
                      30,
                      45,
                      200,
                      tzinfo=pytz.timezone("EST")),
             datetime(2006, 10, 25, 19, 30, 45, 200),
         ),
         (date(2006, 10, 25), datetime(2006, 10, 25, 0, 0)),
         ("2006-10-25 14:30:45.000200",
          datetime(2006, 10, 25, 14, 30, 45, 200)),
         ("2006-10-25 14:30:45.0002", datetime(2006, 10, 25, 14, 30, 45,
                                               200)),
         ("2006-10-25 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
         ("2006-10-25 14:30:00", datetime(2006, 10, 25, 14, 30)),
         ("2006-10-25 14:30", datetime(2006, 10, 25, 14, 30)),
         ("2006-10-25", datetime(2006, 10, 25, 0, 0)),
         ("10/25/2006 14:30:45.000200",
          datetime(2006, 10, 25, 14, 30, 45, 200)),
         ("10/25/2006 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
         ("10/25/2006 14:30:00", datetime(2006, 10, 25, 14, 30)),
         ("10/25/2006 14:30", datetime(2006, 10, 25, 14, 30)),
         ("10/25/2006", datetime(2006, 10, 25, 0, 0)),
         ("10/25/06 14:30:45.000200", datetime(2006, 10, 25, 14, 30, 45,
                                               200)),
         ("10/25/06 14:30:45", datetime(2006, 10, 25, 14, 30, 45)),
         ("10/25/06 14:30:00", datetime(2006, 10, 25, 14, 30)),
         ("10/25/06 14:30", datetime(2006, 10, 25, 14, 30)),
         ("10/25/06", datetime(2006, 10, 25, 0, 0)),
         ("2012-04-23T09:15:00", datetime(2012, 4, 23, 9, 15)),
         ("2012-4-9 4:8:16", datetime(2012, 4, 9, 4, 8, 16)),
         ("2012-04-23T09:15:00Z", datetime(2012, 4, 23, 9, 15, 0, 0)),
         ("2012-4-9 4:8:16-0320", datetime(2012, 4, 9, 7, 28, 16, 0)),
         ("2012-04-23T10:20:30.400+02:30",
          datetime(2012, 4, 23, 7, 50, 30, 400000)),
         ("2012-04-23T10:20:30.400+02",
          datetime(2012, 4, 23, 8, 20, 30, 400000)),
         ("2012-04-23T10:20:30.400-02",
          datetime(2012, 4, 23, 12, 20, 30, 400000)),
         (1335172500.12, datetime(2012, 4, 23, 9, 15, 0, 120000)),
         ("Hello", ValidationError),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 20
0
    def test_slug_field(self):
        column = fields.SlugField(label="test", length=100)
        self.assertIsInstance(column.type, sa.String)
        self.assertEqual(
            {
                "form_class": djangofields.SlugField,
                "label": "test",
                "required": False,
                "validators": [django_validators.validate_slug, mock.ANY],
            },
            column.info,
        )

        form_field = meta.column_info(column).formfield()
        self.assertIsInstance(form_field, djangofields.SlugField)
Ejemplo n.º 21
0
    def test_clean(self):
        class Demo(enum.Enum):
            one = "1"
            two = "2"

        info = meta.column_info(sa.Column(sa.Enum(Demo)), name="test")
        tests = [
            (None, None),
            ("", None),
            ("one", Demo.one),
            ("1", Demo.one),
            (Demo.one, Demo.one),
            (1, ValidationError),
        ]
        _run_tests(self, info, tests)
Ejemplo n.º 22
0
    def test_clean(self):
        info = meta.column_info(sa.Column(sa.String()), name="test")

        tests = [
            (None, None),
            ("abc", "abc"),
            (1234, "1234"),
            ("", ""),
            ("é", "é"),
            (Decimal("1234.44"), "1234.44"),
            # excess whitespace tests
            ("\t\t\t\t\n", ""),
            ("\t\tabc\t\t\n", "abc"),
            ("\t\t20,000\t\t\n", "20,000"),
            ("  \t 23\t", "23"),
        ]
        _run_tests(self, info, tests)
Ejemplo n.º 23
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Boolean()), name="test")
     tests = [
         (None, None),
         ("", None),
         (True, True),
         (1, True),
         ("t", True),
         ("true", True),
         ("True", True),
         ("1", True),
         (0, False),
         ("f", False),
         ("false", False),
         ("False", False),
         ("0", False),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 24
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Float()), name="test")
     tests = [
         (None, None),
         ("", None),
         (1.2, 1.2),
         ("1", 1.0),
         ("abc", ValidationError),
         ("1.0", 1.0),
         ("1.", 1.0),
         ("1.001", 1.001),
         ("1.e-8", 1e-08),
         ("\t\t\t\t\n", None),
         ("\t\tabc\t\t\n", ValidationError),
         ("\t\t20,000.02\t\t\n", 20000.02),
         ("  \t 23\t", 23.0),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 25
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Integer()), name="test")
     tests = [
         (None, None),
         ("", None),
         (1, 1),
         ("1", 1),
         ("abc", ValidationError),
         (1.0, 1),
         (1.01, ValidationError),
         ("1.0", 1),
         ("1.01", ValidationError),
         ("\t\t\t\t\n", None),
         ("\t\tabc\t\t\n", ValidationError),
         ("\t\t20,000.02\t\t\n", ValidationError),
         ("\t\t20,000\t\t\n", 20000),
         ("  \t 23\t", 23),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 26
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Interval()), name="test")
     tests = [
         (None, None),
         ("", None),
         ("Hello", ValidationError),
         (1.2, timedelta(seconds=1, microseconds=200000)),
         (timedelta(seconds=30), timedelta(seconds=30)),
         ("30", timedelta(seconds=30)),
         ("15:30", timedelta(minutes=15, seconds=30)),
         ("1:15:30", timedelta(hours=1, minutes=15, seconds=30)),
         ("1 1:15:30.3",
          timedelta(days=1,
                    hours=1,
                    minutes=15,
                    seconds=30,
                    milliseconds=300)),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 27
0
 def test_clean(self):
     info = meta.column_info(sa.Column(sa.Time()), name="test")
     tests = [
         (None, None),
         ("", None),
         ("Hello", ValidationError),
         (2.3, ValidationError),
         (time(14, 25), time(14, 25)),
         (time(14, 25, 59), time(14, 25, 59)),
         (datetime(2006,
                   10,
                   25,
                   14,
                   30,
                   59,
                   200,
                   tzinfo=pytz.timezone("EST")), time(14, 30, 59, 200)),
         ("14:25", time(14, 25)),
         ("14:25:59", time(14, 25, 59)),
     ]
     _run_tests(self, info, tests)
Ejemplo n.º 28
0
 def test_decimal_field(self):
     column = fields.DecimalField()
     self.assertIsInstance(column.type, sa.Numeric)
     self.assertTrue(column.type.asdecimal)
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, djangofields.DecimalField)
Ejemplo n.º 29
0
 def test_duration_field(self):
     column = fields.DurationField()
     self.assertIsInstance(column.type, sa.Interval)
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, djangofields.DurationField)
Ejemplo n.º 30
0
 def test_datetime_field(self):
     column = fields.DateTimeField()
     self.assertIsInstance(column.type, sa.DateTime)
     form_field = meta.column_info(column).formfield()
     self.assertIsInstance(form_field, djangofields.DateTimeField)