class TestInsert(unittest.TestCase): def setUp(self): self.ins = Insert() def test_empty(self): self.assertEqual(0, len(list(self.ins.next()))) def test_feed(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,2,3,4)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(1, len(lst)) self.assertEqual('INSERT INTO "mytable" VALUES (1,2,3,4);', lst[0]) def test_feed_lower(self): fmt = "insert into `MYTABLE` values {};" values = "(1,2,3,4)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(1, len(lst)) self.assertEqual('INSERT INTO "MYTABLE" VALUES (1,2,3,4);', lst[0]) def test_feed_split_1(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,2,3,4)" stmt = fmt.format(values) self.ins.feed(stmt[:len(stmt)/2]) self.ins.feed(stmt[len(stmt)/2:]) lst = list(self.ins.next()) self.assertEqual(1, len(lst)) self.assertEqual('INSERT INTO "mytable" VALUES (1,2,3,4);', lst[0]) def test_feed_split_2(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,2,3,4),(5,6,7,8)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(2, len(lst)) self.assertEqual('INSERT INTO "mytable" VALUES (1,2,3,4);', lst[0]) self.assertEqual('INSERT INTO "mytable" VALUES (5,6,7,8);', lst[1]) def test_feed_split_3(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,2,3,4),(5,6,7,8),(15,16,17,18)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual('INSERT INTO "mytable" VALUES (1,2,3,4);', lst[0]) self.assertEqual('INSERT INTO "mytable" VALUES (5,6,7,8);', lst[1]) self.assertEqual('INSERT INTO "mytable" VALUES (15,16,17,18);', lst[2]) self.assertEqual(3, len(lst)) def test_feed_split_1_false_2(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,'a 2001(3),(4), b',4)" stmt = fmt.format(values) self.ins.feed(stmt[:len(stmt)/2]) self.ins.feed(stmt[len(stmt)/2:]) lst = list(self.ins.next()) self.assertEqual('''INSERT INTO "mytable" VALUES (1,'a 2001(3),(4), b',4);''', lst[0]) self.assertEqual(1, len(lst), lst) def test_feed_split_2_false_3(self): fmt = "INSERT INTO `mytable` VALUES {};" values = "(1,2,3,4),(5,'\\',77,88),(55,\\'ins\\',',7,8)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(lst[0], '''INSERT INTO "mytable" VALUES (1,2,3,4);''') self.assertEqual(lst[1], """INSERT INTO "mytable" VALUES (5,''',77,88),(55,''ins'',',7,8);""") self.assertEqual(2, len(lst), lst) def test_quote_double(self): fmt = "INSERT INTO `mytable` VALUES {};" values = r"(1,'\\\'\\\'qwerty\\\'\\\'',8)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(lst[0], r"""INSERT INTO "mytable" VALUES (1,'\\''\\''qwerty\\''\\''',8);""") def test_double_quote(self): fmt = "INSERT INTO `mytable` VALUES ({});" values = r"1,'\"qwerty\"',8" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(lst[0], 'INSERT INTO "mytable" VALUES (1,\'"qwerty"\',8);') def test_quote_recursive(self): fmt = "INSERT INTO `mytable` VALUES {};" values = r"(1,'\\\\\\\'qwerty\\\\\\\'',8)" self.ins.feed(fmt.format(values)) lst = list(self.ins.next()) self.assertEqual(lst[0], r"""INSERT INTO "mytable" VALUES (1,'\\\\\\''qwerty\\\\\\''',8);""") def test_sub_quote(self): s = r"' qwerty \'dfssdf\\aaa\\''" r = self.ins.re_quote.sub(self.ins.match_quote, s) self.assertEqual(r"' qwerty ''dfssdf\\aaa\\''", r) s = r"'\'one\''" r = s.replace(r"\'", "''") self.assertEqual("'''one'''", r) s = r"182,'\'one\'',''" r = s.replace(r"\'", "''") self.assertEqual("182,'''one''',''", r) #return s = r"182,'\'Та сторона\'','','Альманах','','',0,'','','','\0','','','','','','','',47560,''" #r = self.ins.sub_quote("''", s) r = self.ins.re_quote.sub(self.ins.match_quote, s) #r = s.replace(r"\'", "''") #print #print s #print r #print r"182,'''Та сторона''','','Альманах','','',0,'','','','\0','','','','','','','',47560,''" self.assertEqual(r"182,'''Та сторона''','','Альманах','','',0,'','','','\0','','','','','','','',47560,''", r)