MCPcopy Create free account
hub / github.com/danfengcao/binlog2sql / TestBinlog2sqlUtil

Class TestBinlog2sqlUtil

tests/test_binlog2sql_util.py:12–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class TestBinlog2sqlUtil(unittest.TestCase):
13
14 def test_is_valid_datetime(self):
15 self.assertTrue(is_valid_datetime('2015-12-12 12:12:12'))
16 self.assertFalse(is_valid_datetime('2015-12-12 12:12'))
17 self.assertFalse(is_valid_datetime('2015-12-12'))
18 self.assertFalse(is_valid_datetime(None))
19
20 @mock.patch('binlog2sql.binlog2sql_util.os.path')
21 def test_create_unique_file(self, mock_path):
22 filename = "test.sql"
23 mock_path.exists.return_value = False
24 self.assertEqual(create_unique_file(filename), filename)
25 mock_path.exists.return_value = True
26 try:
27 create_unique_file(filename)
28 except Exception as e:
29 self.assertEqual(str(e), "cannot create unique file %s.[0-1000]" % filename)
30
31 def test_command_line_args(self):
32 try:
33 command_line_args(['--flashback', '--no-primary-key'])
34 except Exception as e:
35 self.assertEqual(str(e), "Lack of parameter: start_file")
36 try:
37 command_line_args(['--start-file', 'mysql-bin.000058', '--flashback', '--no-primary-key'])
38 except Exception as e:
39 self.assertEqual(str(e), "Only one of flashback or no_pk can be True")
40 try:
41 command_line_args(['--start-file', 'mysql-bin.000058', '--flashback', '--stop-never'])
42 except Exception as e:
43 self.assertEqual(str(e), "Only one of flashback or stop-never can be True")
44 try:
45 command_line_args(['--start-file', 'mysql-bin.000058', '--start-datetime', '2016-12-12'])
46 except Exception as e:
47 self.assertEqual(str(e), "Incorrect datetime argument")
48
49 def test_compare_items(self):
50 self.assertEqual(compare_items(('data', '12345')), '`data`=%s')
51 self.assertEqual(compare_items(('data', None)), '`data` IS %s')
52
53 def test_fix_object(self):
54 self.assertEqual(fix_object('ascii'), 'ascii')
55 self.assertEqual(fix_object(u'unicode'), u'unicode'.encode('utf-8'))
56
57 def test_generate_sql_pattern(self):
58 row = {'values': {'data': 'hello', 'id': 1}}
59 mock_write_event = mock.create_autospec(WriteRowsEvent)
60 mock_write_event.schema = 'test'
61 mock_write_event.table = 'tbl'
62 mock_write_event.primary_key = 'id'
63 pattern = generate_sql_pattern(binlog_event=mock_write_event, row=row, flashback=False, no_pk=False)
64 self.assertEqual(pattern, {'values': ['hello', 1],
65 'template': 'INSERT INTO `test`.`tbl`(`data`, `id`) VALUES (%s, %s);'})
66 pattern = generate_sql_pattern(binlog_event=mock_write_event, row=row, flashback=True, no_pk=False)
67 self.assertEqual(pattern, {'values': ['hello', 1],
68 'template': 'DELETE FROM `test`.`tbl` WHERE `data`=%s AND `id`=%s LIMIT 1;'})
69 pattern = generate_sql_pattern(binlog_event=mock_write_event, row=row, flashback=False, no_pk=True)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected