#!/usr/bin/python
import unittest
import re
from schemasync.utils import REGEX_TABLE_AUTO_INC, REGEX_TABLE_COMMENT
from schemasync.utils import REGEX_MULTI_SPACE, REGEX_DISTANT_SEMICOLIN, REGEX_FILE_COUNTER
class TestTableCommentRegex(unittest.TestCase):
def test_single_column_comment_case_insensitive(self):
"""Test REGEX_TABLE_COMMENT lowercase (comment '*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL comment 'this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_single_column_comment_space_seperator(self):
"""Test REGEX_TABLE_COMMENT space seperator (COMMENT '*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT 'this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_single_column_comment_space_seperator_multiple_spaces(self):
"""Test REGEX_TABLE_COMMENT multiple spaces as the seperator (COMMENT '*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT 'this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_single_column_comment_equals_seperator(self):
"""Test REGEX_TABLE_COMMENT = seperator (COMMENT='*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT='this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_single_column_comment_equals_seperator_with_spaces(self):
"""Test REGEX_TABLE_COMMENT = seperator surrounded by spaces (COMMENT = '*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT = 'this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_multiple_mixed_seperated_column_comment(self):
"""Test REGEX_TABLE_COMMENT multiple column comments (COMMENT '*', COMMENT='*')"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT 'this is your first name',`last_name` varchar(100) NOT NULL COMMENT='this is your last name',`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 2)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL ,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_table_comment(self):
"""Test REGEX_TABLE_COMMENT Table comment"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB COMMENT 'table comment' DEFAULT CHARSET=utf8"""
(sql, count) = re.subn(REGEX_TABLE_COMMENT, '', table)
self.assertEqual(count, 1)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_multiple_column_comment_with_table_comment(self):
"""Test REGEX_TABLE_COMMENT multiple column comments and the Table comment"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT 'this is your first name',`last_name` varchar(100) NOT NULL COMMENT='this is your last name',`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB COMMENT 'table comment' DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 3)
self.assertEqual(sql, """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL ,`last_name` varchar(100) NOT NULL ,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8""")
def test_no_comments(self):
"""Test REGEX_TABLE_COMMENT no comments"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL,`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count) = REGEX_TABLE_COMMENT.subn('', table)
self.assertEqual(count, 0)
self.assertEqual(sql, table)
class TestTableAutoIncrementRegex(unittest.TestCase):
def test_auto_inc_regex_space_seperator(self):
"""Test REGEX_TABLE_AUTO_INC table option AUTO_INCREMENT 1"""
table = """CREATE TABLE `person` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL COMMENT 'this is your first name',`last_name` varchar(100) NOT NULL,`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`)) AUTO_INCREMENT 1 ENGINE=InnoDB DEFAULT CHARSET=utf8"""
(sql, count
评论0
最新资源