Exemplo n.º 1
0
    def test_io_success(self):
        """ Tests that a basic usage works as expected """
        k1 = uuid4()
        k2 = uuid4()
        now = datetime.now()
        then = now + timedelta(days=1)
        m1 = TestMapModel.create(
            self.conn,
            int_map={
                1: k1,
                2: k2
            },
            text_map={
                'now': now,
                'then': then
            },
        )
        m2 = TestMapModel.get(self.conn, partition=m1.partition)

        self.assertTrue(isinstance(m2.int_map, dict))
        self.assertTrue(isinstance(m2.text_map, dict))

        self.assertTrue(1 in m2.int_map)
        self.assertTrue(2 in m2.int_map)
        self.assertEqual(m2.int_map[1], k1)
        self.assertEqual(m2.int_map[2], k2)

        self.assertAlmostEqual(get_total_seconds(now - m2.text_map['now']), 0,
                               2)
        self.assertAlmostEqual(get_total_seconds(then - m2.text_map['then']),
                               0, 2)
Exemplo n.º 2
0
    def to_database(self, value):
        value = super(DateTime, self).to_database(value)
        if value is None:
            return
        if not isinstance(value, datetime):
            if isinstance(value, date):
                value = datetime(value.year, value.month, value.day)
            else:
                raise ValidationError(
                    "{0} '{1}' is not a datetime object".format(
                        self.column_name, value))
        epoch = datetime(1970, 1, 1, tzinfo=value.tzinfo)
        offset = get_total_seconds(
            epoch.tzinfo.utcoffset(epoch)) if epoch.tzinfo else 0

        return int((get_total_seconds(value - epoch) - offset) * 1000)
Exemplo n.º 3
0
    def test_datetime_precision(self):
        """ Tests that millisecond resolution is preserved when saving datetime objects """
        now = datetime.now()
        pk = 1000
        obj = DateTimeQueryTestModel.create(self.conn, user=pk, day=now, data="energy cheese")
        load = DateTimeQueryTestModel.get(self.conn, user=pk)

        self.assertAlmostEqual(get_total_seconds(now - load.day), 0, 2)
        obj.delete(self.conn)