|
1 |
| -from datetime import datetime, date, timedelta |
2 |
| -from superset import utils |
3 |
| -import unittest |
4 |
| - |
5 |
| -from mock import Mock, patch |
6 |
| - |
7 |
| -class UtilsTestCase(unittest.TestCase): |
8 |
| - def test_json_int_dttm_ser(self): |
9 |
| - dttm = datetime(2020, 1, 1) |
10 |
| - ts = 1577836800000.0 |
11 |
| - json_int_dttm_ser = utils.json_int_dttm_ser |
12 |
| - assert json_int_dttm_ser(dttm) == ts |
13 |
| - assert json_int_dttm_ser(date(2020, 1, 1)) == ts |
14 |
| - assert json_int_dttm_ser(datetime(1970, 1, 1)) == 0 |
15 |
| - assert json_int_dttm_ser(date(1970, 1, 1)) == 0 |
16 |
| - assert json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1) |
17 |
| - |
18 |
| - with self.assertRaises(TypeError): |
19 |
| - utils.json_int_dttm_ser("this is not a date") |
20 |
| - |
21 |
| - @patch('superset.utils.datetime') |
22 |
| - def test_parse_human_timedelta(self, mock_now): |
23 |
| - mock_now.return_value = datetime(2016, 12, 1) |
24 |
| - self.assertEquals(utils.parse_human_timedelta('now'), timedelta(0)) |
| 1 | +from datetime import datetime, date, timedelta, time |
| 2 | +from decimal import Decimal |
| 3 | +from superset.utils import ( |
| 4 | + json_int_dttm_ser, json_iso_dttm_ser, base_json_conv, parse_human_timedelta |
| 5 | +) |
| 6 | +import unittest |
| 7 | +import uuid |
| 8 | + |
| 9 | +from mock import Mock, patch |
| 10 | +import numpy |
| 11 | + |
| 12 | + |
| 13 | +class UtilsTestCase(unittest.TestCase): |
| 14 | + def test_json_int_dttm_ser(self): |
| 15 | + dttm = datetime(2020, 1, 1) |
| 16 | + ts = 1577836800000.0 |
| 17 | + assert json_int_dttm_ser(dttm) == ts |
| 18 | + assert json_int_dttm_ser(date(2020, 1, 1)) == ts |
| 19 | + assert json_int_dttm_ser(datetime(1970, 1, 1)) == 0 |
| 20 | + assert json_int_dttm_ser(date(1970, 1, 1)) == 0 |
| 21 | + assert json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1) |
| 22 | + |
| 23 | + with self.assertRaises(TypeError): |
| 24 | + json_int_dttm_ser("this is not a date") |
| 25 | + |
| 26 | + def test_json_iso_dttm_ser(self): |
| 27 | + dttm = datetime(2020, 1, 1) |
| 28 | + dt = date(2020, 1, 1) |
| 29 | + t = time() |
| 30 | + assert json_iso_dttm_ser(dttm) == dttm.isoformat() |
| 31 | + assert json_iso_dttm_ser(dt) == dt.isoformat() |
| 32 | + assert json_iso_dttm_ser(t) == t.isoformat() |
| 33 | + |
| 34 | + with self.assertRaises(TypeError): |
| 35 | + json_iso_dttm_ser("this is not a date") |
| 36 | + |
| 37 | + def test_base_json_conv(self): |
| 38 | + assert isinstance(base_json_conv(numpy.bool_(1)), bool) == True |
| 39 | + assert isinstance(base_json_conv(numpy.int64(1)), int) == True |
| 40 | + assert isinstance(base_json_conv(set([1])), list) == True |
| 41 | + assert isinstance(base_json_conv(Decimal('1.0')), float) == True |
| 42 | + assert isinstance(base_json_conv(uuid.uuid4()), str) == True |
| 43 | + |
| 44 | + @patch('superset.utils.datetime') |
| 45 | + def test_parse_human_timedelta(self, mock_now): |
| 46 | + mock_now.return_value = datetime(2016, 12, 1) |
| 47 | + self.assertEquals(parse_human_timedelta('now'), timedelta(0)) |
0 commit comments