def test__requires_statement(self): table = MySQLTable(name=self.TBL_NAME) ref_table = MySQLTable(name=self.REF_TABLE) ref_column = MySQLColumn(name=self.REF_COL) desc_column = MySQLColumn(name=self.DESC_COL) mysql_column = MySQLColumn( name=self.COL_NAME, table=table, referenced_table=ref_table, referenced_column=ref_column, descriptor_column=desc_column, ) fmt = " ".join(["db.{tbl}.{col}.requires =", """IS_IN_DB(db, db.{ref_tbl}.{ref_col},'%({desc})s')"""]) stmt = mysql_column.requires_statement() self.assertEqual( stmt, fmt.format( tbl=self.TBL_NAME, col=self.COL_NAME, ref_tbl=self.REF_TABLE, ref_col=self.REF_COL, desc=self.DESC_COL ), ) return
def test__documentation(self): # W0511 Used when a warning note as FIXME or XXX is detected. # pylint: disable=W0511 tests = [ {"name": "my_column_name", "type": "text", "expect": "my_column_name text # FIXME"}, { # '1234567890123456789012345678901234567890' "name": "my_column_name", "type": "text", "name_width": 16, "type_width": 6, "expect": "my_column_name text # FIXME", }, { # '1234567890123456789012345678901234567890' "name": "my_column_name", "type": "integer(11)", "name_width": 16, "type_width": 11, "ref_table": "city", "ref_column": "id", "expect": "my_column_name integer(11) # FIXME References city.id", }, ] for t in tests: mysql_column = MySQLColumn(name=t["name"], data_type=t["type"]) kwargs = {} for w in ["name_width", "type_width"]: if w in t: kwargs[w] = t[w] if "ref_table" in t: ref_table = MySQLTable(name=t["ref_table"]) ref_column = MySQLColumn(name=t["ref_column"]) mysql_column.referenced_table = ref_table mysql_column.referenced_column = ref_column self.assertEqual(mysql_column.documentation(**kwargs), t["expect"]) return
def test__defaults_for(self): defaults = ['writeable=False', 'IS_NOT_EMPTY()'] by_type = FieldPropertyDefaultsByType() self.assertTrue(by_type) # Creates object # No column provided, returns empty list self.assertEqual(by_type.defaults_for(), []) column = MySQLColumn() # column_type not set, returns empty list self.assertEqual(by_type.defaults_for(column=column), []) by_type.column_type = 'string' # column.data_type not set, returns empty list self.assertEqual(by_type.defaults_for(column=column), []) column.data_type = 'integer' # column.data_type != column_type, returns empty list self.assertEqual(by_type.defaults_for(column=column), []) column.data_type = 'string' # defaults not set, returns empty list self.assertEqual(by_type.defaults_for(column=column), []) by_type.defaults = defaults # column.data_type == column_type, returns expected self.assertEqual(by_type.defaults_for(column=column), defaults) class FakeClass(object): """Fake class with no type attribute""" def __init__(self): return column = FakeClass() # column has no 'type' attribute, raises exception self.assertRaises(AttributeError, by_type.defaults_for, column=column) return
def test__set_referenced_table(self): table = MySQLTable(name="session") table.columns.append(MySQLColumn(name="id", table=table)) table.columns.append(MySQLColumn(name="name", table=table)) table.columns.append(MySQLColumn(name="group_id", table=table)) table.columns.append(MySQLColumn(name="status_id", table=table)) mysql_column = MySQLColumn(name=self.COL_NAME, table=table) # Control - referenced table initially None self.assertEqual(mysql_column.referenced_table, None) tables = [] mysql_column.set_referenced_table(tables=tables) # Table list is empty, referenced table still None self.assertEqual(mysql_column.referenced_table, None) table_2 = MySQLTable(name="employee") table_2.columns.append(MySQLColumn(name="id", table=table_2)) table_2.columns.append(MySQLColumn(name="name", table=table_2)) table_2.columns.append(MySQLColumn(name="group_id", table=table_2)) table_2.columns.append(MySQLColumn(name="status_id", table=table_2)) table_3 = MySQLTable(name="location") table_3.columns.append(MySQLColumn(name="id", table=table_3)) table_3.columns.append(MySQLColumn(name="name", table=table_3)) table_3.columns.append(MySQLColumn(name="group_id", table=table_3)) table_3.columns.append(MySQLColumn(name="status_id", table=table_3)) tables.append(table) tables.append(table_2) tables.append(table_3) mysql_column.name = "oneword" mysql_column.set_referenced_table(tables=tables) # One word column doesn't reference table self.assertEqual(mysql_column.referenced_table, None) mysql_column.name = "login_id" mysql_column.set_referenced_table(tables=tables) # Reference like name doesn't match any references tables self.assertEqual(mysql_column.referenced_table, None) mysql_column.name = "location_id" mysql_column.set_referenced_table(tables=tables) # Match sets expected. self.assertEqual(mysql_column.referenced_table, table_3) mysql_column.name = "session_id" mysql_column.set_referenced_table(tables=tables) # Matches it own table, sets expected. self.assertEqual(mysql_column.referenced_table, table) return
def test__set_referenced_column(self): # Test with ????_id style column table = MySQLTable(name="session") mysql_column = MySQLColumn(name="employee_id", table=table) # Control - referenced column initially None self.assertEqual(mysql_column.referenced_column, None) mysql_column.set_referenced_column() # no referenced table, referenced column is None. self.assertEqual(mysql_column.referenced_column, None) table_2 = MySQLTable(name="employee") mysql_column.referenced_table = table_2 mysql_column.set_referenced_column() # referenced table has no columns, referenced column is None. self.assertEqual(mysql_column.referenced_column, None) t2_col_1 = MySQLColumn(name="id", table=table_2) t2_col_2 = MySQLColumn(name="name", table=table_2) t2_col_3 = MySQLColumn(name="group_id", table=table_2) t2_col_4 = MySQLColumn(name="status_id", table=table_2) table_2.columns.append(t2_col_1) table_2.columns.append(t2_col_2) table_2.columns.append(t2_col_3) table_2.columns.append(t2_col_4) mysql_column.set_referenced_column() # match, referenced column is expected self.assertEqual(mysql_column.referenced_column, t2_col_1) # Test with non ????_id style column table = MySQLTable(name="session") mysql_column.name = "service_username" table_3 = MySQLTable(name="service") mysql_column.referenced_table = table_3 mysql_column.referenced_column = None # Control - referenced column initially None self.assertEqual(mysql_column.referenced_column, None) t3_col_1 = MySQLColumn(name="name", table=table_3) t3_col_2 = MySQLColumn(name="group_id", table=table_3) t3_col_3 = MySQLColumn(name="status_id", table=table_3) table_3.columns.append(t3_col_1) table_3.columns.append(t3_col_2) table_3.columns.append(t3_col_3) mysql_column.set_referenced_column() # No match, no id, uses first column self.assertEqual(mysql_column.referenced_column, t3_col_1) mysql_column.referenced_column = None # Control - referenced column initially None self.assertEqual(mysql_column.referenced_column, None) t3_col_4 = MySQLColumn(name="username", table=table_3) table_3.columns.append(t3_col_4) mysql_column.set_referenced_column() # Non-???_id column match, uses expected self.assertEqual(mysql_column.referenced_column, t3_col_4) mysql_column.referenced_column = None # Control - referenced column initially None self.assertEqual(mysql_column.referenced_column, None) t3_col_4.name = "id" mysql_column.set_referenced_column() # No match, uses id field self.assertEqual(mysql_column.referenced_column, t3_col_4) return
def test__set_descriptor_column(self): table = MySQLTable(name=self.TBL_NAME) mysql_column = MySQLColumn(name=self.COL_NAME, table=table) mysql_column.set_descriptor_column() # no referenced column, descriptor column is None. self.assertEqual(mysql_column.descriptor_column, None) table_2 = MySQLTable(name="employee") mysql_column.referenced_table = table_2 t2_col_1 = MySQLColumn(name="id", table=table_2) t2_col_2 = MySQLColumn(name="group_id", table=table_2) table_2.columns.append(t2_col_1) table_2.columns.append(t2_col_2) mysql_column.referenced_column = t2_col_1 mysql_column.set_descriptor_column() # no referenced table, uses referenced column self.assertEqual(mysql_column.descriptor_column, t2_col_1) mysql_column.referenced_table = table_2 mysql_column.referenced_column = t2_col_2 mysql_column.set_descriptor_column() # referenced, no non-id column, uses referenced column self.assertEqual(mysql_column.descriptor_column, t2_col_2) # For each name in the names list, a column is added to the referenced # table with that name. Each subsequent name is higher in the priority # for matching the descriptor column, so its column should become the # descriptor column. names = ["identity", "given_name", "title", "last_name", "name"] for name in names: new_column = MySQLColumn(name=name, table=table_2) table_2.columns.append(new_column) mysql_column.set_descriptor_column() # referenced, uses new column self.assertEqual(mysql_column.descriptor_column, new_column) return