Esempio n. 1
0
class TestToBool(FormskitTestCase):

    def setUp(self):
        super().setUp()
        self.field = Field('name1', convert=ToBool())

    def test_convert(self):
        self.field.set_values(['1'])
        self.field.get_raw_value() == '1'
        assert self.field.get_value() is True

        self.field.set_values(['0'])
        self.field.get_raw_value() == '0'
        assert self.field.get_value() is False

    def test_unconvert(self):
        self.field.set_value(True)
        self.field.get_raw_value() == '1'
        assert self.field.get_value() is True

        self.field.set_value(False)
        self.field.get_raw_value() == '0'
        assert self.field.get_value() is False

    def test_make_convert(self):
        self.field.validate()
        assert self.field.get_value() is False
Esempio n. 2
0
class TestToDatetime(FormskitTestCase):
    timestamp = datetime(year=2016, month=1, day=2, hour=15, minute=12)

    def setUp(self):
        super().setUp()
        self.field = Field('name1', convert=ToDatetime())

    def test_convert(self):
        self.field.set_values(['2016-01-02 15:12'])
        self.field.get_raw_value() == '2016-01-02 15:12'
        assert self.field.get_value() == self.timestamp

    def test_convert_fail(self):
        self.field.set_values(['2016-01x-02'])
        self.field.get_raw_value() == '2016-01-02 15:12'
        assert self.field.get_value() is None

    def test_convert_back(self):
        self.field.set_value(self.timestamp)
        self.field.get_raw_value() == '2016-01-02 15:12'
        assert self.field.get_value() == self.timestamp

    def test_convert_back_fail(self):
        self.field.set_value(None)
        self.field.get_raw_value() is None
        assert self.field.get_value() is None

    def test_make_convert(self):
        self.field.set_values(['2016-01-02', '15:12'])
        self.field.validate()
        assert self.field.get_value() == self.timestamp