Exemplo n.º 1
0
 def test_dump_aware_time(self):
     """Establish that JSON dumping a timezone aware time produces
     an error.
     """
     t = time(12, 0, 0, tzinfo=pytz.UTC)
     with self.assertRaises(ValueError):
         json.dumps(t)
Exemplo n.º 2
0
 def test_invalid_input(self):
     """Test that invalid input still raises TypeError as
     we expect.
     """
     d = object()
     with self.assertRaises(TypeError):
         json.dumps(d)
Exemplo n.º 3
0
 def test_dump_aware_time(self):
     """Establish that JSON dumping a timezone aware time produces
     an error.
     """
     t = time(12, 0, 0, tzinfo=pytz.UTC)
     with self.assertRaises(ValueError):
         json.dumps(t)
Exemplo n.º 4
0
 def test_invalid_input(self):
     """Test that invalid input still raises TypeError as
     we expect.
     """
     d = object()
     with self.assertRaises(TypeError):
         json.dumps(d)
Exemplo n.º 5
0
 def test_iter(self):
     """Establish that JSON dumping an object with an __iter__ method
     works as expected.
     """
     class Foo(object):
         def __iter__(self):
             yield 4
             yield 5
             yield 6
     self.assertEqual(json.dumps(Foo()), '[4, 5, 6]')
Exemplo n.º 6
0
    def test_iter(self):
        """Establish that JSON dumping an object with an __iter__ method
        works as expected.
        """
        class Foo(object):
            def __iter__(self):
                yield 4
                yield 5
                yield 6

        self.assertEqual(json.dumps(Foo()), '[4, 5, 6]')
Exemplo n.º 7
0
    def test_dumps(self):
        """Ensure that json.dumps dumps both new things (datetime)
        and old things (string, bool) correctly.
        """
        # Define the data to be dumped.
        d = adict({
            'a': datetime(2012, 4, 21, 16, tzinfo=pytz.UTC),
            'b': True,
            'c': 'foo',
        })

        # Dump the data, and verify the dump.
        payload = json.dumps(d)
        self.assertEqual(
            payload,
            '{"a": 1335024000, "b": true, "c": "foo"}',
        )
Exemplo n.º 8
0
    def test_dumps(self):
        """Ensure that json.dumps dumps both new things (datetime)
        and old things (string, bool) correctly.
        """
        # Define the data to be dumped.
        d = adict({
            'a': datetime(2012, 4, 21, 16, tzinfo=pytz.UTC),
            'b': True,
            'c': 'foo',
        })

        # Dump the data, and verify the dump.
        payload = json.dumps(d)
        self.assertEqual(
            payload,
            '{"a": 1335024000, "b": true, "c": "foo"}',
        )
Exemplo n.º 9
0
 def render(self, name, value, attrs=None):
     return super(JSONWidget, self).render(
         name,
         json.dumps(value, indent=4, sort_keys=True),
         attrs,
     )
Exemplo n.º 10
0
 def render(self, name, value, attrs=None):
     return super(JSONWidget, self).render(
         name,
         json.dumps(value, indent=4, sort_keys=True),
         attrs,
     )
Exemplo n.º 11
0
 def test_dump_date(self):
     """Establish that JSON dumping a date works as expected."""
     d = date(2012, 4, 21)
     self.assertEqual(json.dumps(d), '"2012-04-21"')
Exemplo n.º 12
0
 def test_dump_decimal(self):
     """Establish that JSON dumping a decimal.Decimal object works
     as expected.
     """
     d = decimal.Decimal(1.5)
     self.assertEqual(json.dumps(d), '"1.5"')
Exemplo n.º 13
0
 def test_dump_timedelta(self):
     """Establish that JSON dumping a timedelta works as expected."""
     td = timedelta(days=1)
     self.assertEqual(json.dumps(td), '"86400.0"')
Exemplo n.º 14
0
 def test_dump_time(self):
     """Establish that JSON dumping a time works as expected."""
     t = time(12, 0, 0)
     self.assertEqual(json.dumps(t), '"12:00:00"')
Exemplo n.º 15
0
 def test_dump_time_with_microseconds(self):
     """Establish that JSON dumping a time with microseconds works
     as expected.
     """
     t = time(12, 0, 0, 1000)
     self.assertEqual(json.dumps(t), '"12:00:00.001"')
Exemplo n.º 16
0
 def test_dump_time(self):
     """Establish that JSON dumping a time works as expected."""
     t = time(12, 0, 0)
     self.assertEqual(json.dumps(t), '"12:00:00"')
Exemplo n.º 17
0
 def test_dump_date(self):
     """Establish that JSON dumping a date works as expected."""
     d = date(2012, 4, 21)
     self.assertEqual(json.dumps(d), '"2012-04-21"')
Exemplo n.º 18
0
 def test_dump_time_with_microseconds(self):
     """Establish that JSON dumping a time with microseconds works
     as expected.
     """
     t = time(12, 0, 0, 1000)
     self.assertEqual(json.dumps(t), '"12:00:00.001"')
Exemplo n.º 19
0
 def test_dump_timedelta(self):
     """Establish that JSON dumping a timedelta works as expected."""
     td = timedelta(days=1)
     self.assertEqual(json.dumps(td), '"86400.0"')
Exemplo n.º 20
0
 def test_dump_decimal(self):
     """Establish that JSON dumping a decimal.Decimal object works
     as expected.
     """
     d = decimal.Decimal(1.5)
     self.assertEqual(json.dumps(d), '"1.5"')