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
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__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