def test_table_engine_exceptions(self): try: tinyAPI.Table('db', 'abc').engine('def') self.fail('Was able to set the engine to an invalid value.') except TableBuilderException as e: self.assertEqual('the engine "def" is invalid', e.get_message())
def test_table_foreign_key_and_dependencies_active_column(self): text = '''create table abc ( id bigint unsigned not null auto_increment unique ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' table = tinyAPI.Table('db', 'abc') \ .id('id', True, True) \ .fk('def') self.assertEqual(text, table.get_definition()) text = ''' alter table abc add constraint abc_0_fk foreign key (id) references def (id) on delete cascade''' fks = table.get_foreign_key_definitions() self.assertEqual(1, len(fks)) self.assertEqual(text, fks[0]) deps = table.get_dependencies() self.assertEqual(1, len(deps)) self.assertTrue('def' in deps)
def test_add_column_dupe_exceptions(self): try: tinyAPI.Table('db', 'abc').bit('def').bint('def') self.fail('Was able to add two columns with the same name.'); except TableBuilderException as e: self.assertEqual('the column "def" already exists', e.get_message())
def test_longitude_column(self): text = '''create table abc ( longitude float(53) default null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc').long('longitude').get_definition()) text = '''create table abc ( long_x float(53) not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc').long('long_x', True).get_definition())
def test_table_defv_active_column_is_set_exceptions(self): try: tinyAPI.Table('db', 'abc').defv(1) self.fail('Was able to set the default value for a olumn even ' + 'though no column was defined.') except TableBuilderException as e: self.assertEqual( 'call to "defv" invalid until column is defined', e.get_message())
def test_table_foreign_key_exceptions(self): try: tinyAPI.Table('db', 'abc').fk('def', True, ['ghi']) self.fail('Was able to create a foreign key even though the column ' + 'provided did not exist.') except TableBuilderException as e: self.assertEqual( 'column "ghi" cannot be used in foreign key because it has ' + 'not been defined', e.get_message())
def test_table_fk_active_column_is_set_exceptions(self): try: tinyAPI.Table('db', 'abc').fk('def') self.fail('Was able to set a column as a foreign key even though ' + 'no column was defined.') except TableBuilderException as e: self.assertEqual( 'call to "fk" invalid until column is defined', e.get_message())
def test_table_ai_active_column_is_set_exceptions(self): try: tinyAPI.Table('db', 'abc').ai() self.fail('Was able to set a column as auto-increment even though ' + 'no column was defined.') except TableBuilderException as e: self.assertEqual( 'call to "ai" invalid until column is defined', e.get_message())
def test_table_idx_active_column_is_set_exceptions(self): try: tinyAPI.Table('db', 'abc').idx() self.fail('Was able to set a column as an index even though no ' + 'column was defined.') except TableBuilderException as e: self.assertEqual( 'call to "idx" invalid until column is defined', e.get_message())
def test_table_get_definition_exceptions(self): try: tinyAPI.Table('db', 'abc').get_definition() self.fail('Was able to get table definition even though no ' + 'columns were provided.') except TableBuilderException as e: self.assertEqual( 'the table cannot be defined because it has no columns', e.get_message())
def test_email_column_exceptions(self): try: tinyAPI.Table('db', 'abc').email('invalid') self.fail('Was able to create an email column with an invalid ' + 'name.') except TableBuilderException as e: self.assertEqual( 'email column must be named "email_address" or start with ' + '"em_"', e.get_message())
def test_temporary_table(self): text = '''create temporary table abc ( id bigint unsigned not null auto_increment unique ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .temp() .id('id', True, True) .get_definition())
def test_table_active_column_is_primary_key(self): text = '''create table abc ( def int default null primary key ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .int('def') .pk() .get_definition())
def test_email_column(self): text = '''create table abc ( em_address varchar(100) character set utf8 collate utf8_unicode_ci not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc') \ .email('em_address', True) \ .get_definition())
def test_money_column(self): text = '''create table abc ( def float(53) not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc') .money('def', True) .get_definition())
def test_date_created_with_precision(self): text = '''create table abc ( date_created datetime(6) not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc') \ .created(6) \ .get_definition())
def test_date_updated_with_precision(self): text = '''create table abc ( date_updated timestamp(6) default current_timestamp(6) on update current_timestamp(6) ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual( text, tinyAPI.Table('db', 'abc') \ .updated(6) \ .get_definition())
def test_latitude_column_exceptions(self): try: tinyAPI.Table('db', 'abc').lat('invalid') self.fail('Was able to create a latitude column with an invalid ' + 'name.') except TableBuilderException as e: self.assertEqual( 'latitude column must be named "latitude" or start with ' + '"lat_"', e.get_message())
def test_table_index_exceptions(self): try: tinyAPI.Table('db', 'abc').idx(['def']) self.fail('Was able to create an index even though the column ' + 'provided did not exist.') except TableBuilderException as e: self.assertEqual( 'column "def" cannot be used in index because it has not been ' + 'defined', e.get_message()) try: tinyAPI.Table('db', 'abc').int('col_a').idx(['col_a x']) self.fail('Was able to create an indexed with an invalid column ' + 'modifier for asc/desc.'); except TableBuilderException as e: self.assertEqual( 'columns can only be modified using "asc" or "desc"', e.get_message())
def test_table_updated(self): text = '''create table abc ( id bigint unsigned not null auto_increment unique, date_updated timestamp default '2000-01-01 00:00:00' on update current_timestamp ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .id('id', True, True) .updated() .get_definition())
def test_table_calling_set_attribute(self): text = '''create table abc ( def int default null, ghi int unsigned zerofill default null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .int('def') .int('ghi', False, None, True, True) .get_definition())
def test_table_composite_unique_key_exceptions(self): try: tinyAPI.Table('db', 'abc') \ .int('def') \ .uk(['def', 'ghi']) \ .get_definition() self.fail('Was able to get the definition for a table even ' + 'though one of the columns in a unique key did not ' + 'exist.') except TableBuilderException as e: self.assertEqual( 'column "ghi" cannot be used in unique key because it has not ' + 'been defined', e.get_message())
def test_foreign_key_full_definition(self): text = ''' alter table abc add constraint abc_0_fk foreign key (col_a, col_b) references def (col_c, col_d)''' fks = tinyAPI.Table('db', 'abc') \ .int('col_a') \ .int('col_b') \ .fk('def', False, ['col_a', 'col_b'], ['col_c', 'col_d']) \ .get_foreign_key_definitions() self.assertEqual(1, len(fks)) self.assertEqual(text, fks[0])
def test_table_composite_primary_key(self): text = '''create table abc ( def int default null, ghi int default null, primary key abc_pk (def, ghi) ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .int('def') .int('ghi') .pk(['def', 'ghi']) .get_definition())
def test_table_multi_numeric_columns(self): text = '''create table abc ( id bigint unsigned not null auto_increment unique, def tinyint(1) default null, ghi float(12) default null ) engine = myisam default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .engine('myisam') .id('id', True, True) .bool('def') .float('ghi', False, 12) .get_definition())
def test_table_getting_index_definitions(self): table = tinyAPI.Table('db', 'abc') \ .int('col_a') \ .int('col_b') \ .idx() \ .idx(['col_a asc', 'col_b desc']) indexes = table.get_index_definitions() self.assertEqual(2, len(indexes)) self.assertEqual( "create index abc_0_idx\n on abc\n (col_b)", indexes[0]) self.assertEqual( "create index abc_1_idx\n on abc\n" + " (col_a asc, col_b desc)", indexes[1])
def test_table_multiple_composite_unique_keys(self): text = '''create table abc ( def int default null, ghi int default null, jkl int default null, unique key abc_0_uk (def, ghi), unique key abc_1_uk (ghi, jkl) ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .int('def') .int('ghi') .int('jkl') .uk(['def', 'ghi']) .uk(['ghi', 'jkl']) .get_definition())
def test_table_date_columns_year_4(self): text = '''create table abc ( def date not null, ghi datetime not null, jkl timestamp not null, mno time not null, pqr year(4) not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .dt('def', True) .dtt('ghi', True) .ts('jkl', True) .ti('mno', True) .yr('pqr', True, 4) .get_definition())
def test_table_helper_attribute_methods(self): text = '''create table abc ( def int default null, ghi int unique default null, jkl int auto_increment default null, mno int default '123' ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .int('def') .int('ghi') .uk() .int('jkl') .ai() .int('mno') .defv(123) .get_definition())
def test_string_types_in_table(self): text = '''create table abc ( def char(15) character set utf8 collate utf8_unicode_ci not null, ghi varchar(16) character set utf8 collate utf8_unicode_ci not null, jkl binary(17) character set utf8 collate utf8_unicode_ci not null, mno varbinary(18) character set utf8 collate utf8_unicode_ci not null, pqr tinyblob not null, stu blob(19) not null, vwx mediumblob not null, yza longblob not null, bcd tinytext character set utf8 collate utf8_unicode_ci not null, efg text(20) character set utf8 collate utf8_unicode_ci not null, hij mediumtext character set utf8 collate utf8_unicode_ci not null, klm longtext character set utf8 collate utf8_unicode_ci not null, nop enum('a', 'b') character set utf8 collate utf8_unicode_ci not null, qrs set('c', 'd') character set utf8 collate utf8_unicode_ci not null ) engine = innodb default charset = utf8 collate = utf8_unicode_ci;''' self.assertEqual(text, tinyAPI.Table('db', 'abc') .char('def', 15, True) .vchar('ghi', 16, True) .bin('jkl', 17, True) .vbin('mno', 18, True) .tblob('pqr', True) .blob('stu', 19, True) .mblob('vwx', True) .lblob('yza', True) .ttext('bcd', True) .text('efg', True, 20) .mtext('hij', True) .ltext('klm', True) .enum('nop', ['a', 'b'], True) .set('qrs', ['c', 'd'], True) .get_definition())