MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / TestConverter

Class TestConverter

pymysql/tests/test_converters.py:11–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class TestConverter(TestCase):
12 def test_escape_string(self):
13 self.assertEqual(converters.escape_string("foo\nbar"), "foo\\nbar")
14
15 def test_convert_datetime(self):
16 expected = datetime.datetime(2007, 2, 24, 23, 6, 20)
17 dt = converters.convert_datetime("2007-02-24 23:06:20")
18 self.assertEqual(dt, expected)
19
20 def test_convert_datetime_with_fsp(self):
21 expected = datetime.datetime(2007, 2, 24, 23, 6, 20, 511581)
22 dt = converters.convert_datetime("2007-02-24 23:06:20.511581")
23 self.assertEqual(dt, expected)
24
25 def _test_convert_timedelta(self, with_negate=False, with_fsp=False):
26 d = {"hours": 789, "minutes": 12, "seconds": 34}
27 s = "%(hours)s:%(minutes)s:%(seconds)s" % d
28 if with_fsp:
29 d["microseconds"] = 511581
30 s += ".%(microseconds)s" % d
31
32 expected = datetime.timedelta(**d)
33 if with_negate:
34 expected = -expected
35 s = "-" + s
36
37 tdelta = converters.convert_timedelta(s)
38 self.assertEqual(tdelta, expected)
39
40 def test_convert_timedelta(self):
41 self._test_convert_timedelta(with_negate=False, with_fsp=False)
42 self._test_convert_timedelta(with_negate=True, with_fsp=False)
43
44 def test_convert_timedelta_with_fsp(self):
45 self._test_convert_timedelta(with_negate=False, with_fsp=True)
46 self._test_convert_timedelta(with_negate=False, with_fsp=True)
47
48 def test_escape_timedelta(self):
49 # MySQL TIME allows negatives, and a timedelta is the registered param
50 # encoder, so a negative value must escape to its real magnitude with a
51 # single leading sign -- not with complemented sub-hour fields.
52 cases = {
53 datetime.timedelta(hours=1, minutes=30): "'01:30:00'",
54 datetime.timedelta(days=1, hours=2): "'26:00:00'",
55 -datetime.timedelta(minutes=30): "'-00:30:00'",
56 -datetime.timedelta(hours=1, minutes=30): "'-01:30:00'",
57 datetime.timedelta(seconds=83579, microseconds=51000): "'23:12:59.051000'",
58 -datetime.timedelta(
59 seconds=83579, microseconds=51000
60 ): "'-23:12:59.051000'",
61 }
62 for tdelta, expected in cases.items():
63 self.assertEqual(converters.escape_timedelta(tdelta), expected)
64 # the escaped literal must re-parse to the same duration
65 self.assertEqual(
66 converters.convert_timedelta(
67 converters.escape_timedelta(tdelta).strip("'")
68 ),

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected