예제 #1
0
    def test_reading_excel(self):
        """Tests reading a worksheet."""
        databases = XLSReader().read_xls("test_excel_reader.xlsx")

        database = databases.get("xdb")
        self.assertIsNotNone(database)

        table1 = database.get_table("table1")
        self.assertIsNotNone(table1)
        self.assertEqual(table1.table_name, "table1")
        self.assertEqual(table1.schema_name, "s1")
        self.assertEqual(table1.primary_key, ["column_1"])
        self.assertEqual(table1.shard_key.shard_keys, ["column_1"])
        self.assertEqual(table1.shard_key.number_shards, 128)

        self.assertTrue(table1.has_column("column_1"))
        self.assertEqual(table1.get_column("column_1").column_type, "INT")
        self.assertTrue(table1.has_column("column_3"))
        self.assertEqual(table1.get_column("column_3").column_type, "FLOAT")

        table2 = database.get_table("table2")
        self.assertIsNotNone(table2)
        self.assertEqual(table2.table_name, "table2")
        self.assertEqual(table2.schema_name, "s1")
        self.assertEqual(table2.primary_key, ["column_1"])
        self.assertEqual(table2.shard_key.shard_keys, ["column_1", "column_2"])
        self.assertEqual(table2.shard_key.number_shards, 128)

        self.assertTrue(table2.has_column("column_1"))
        self.assertEqual(table2.get_column("column_1").column_type, "INT")
        self.assertTrue(table2.has_column("column_4"))
        self.assertEqual(table2.get_column("column_4").column_type, "DOUBLE")

        fk = table2.get_foreign_key("FK_table2_to_table1")
        self.assertIsNotNone(fk)
        self.assertEqual(fk.from_table, "table2")
        self.assertEqual(fk.from_keys, ["column_1"])
        self.assertEqual(fk.to_table, "table1")
        self.assertEqual(fk.to_keys, ["column_1"])

        rel = table2.get_relationship("REL_table2_to_table1")
        self.assertIsNotNone(rel)
        self.assertEqual(rel.from_table, "table2")
        self.assertEqual(rel.to_table, "table1")
        self.assertEqual(rel.conditions, "table2.column_4 = table1.column_2")

        table3 = database.get_table("table3")
        fk = table3.get_foreign_key("FK_table3_to_table4")
        self.assertIsNotNone(fk)
        self.assertEqual(fk.from_table, "table3")
        self.assertEqual(fk.from_keys, ["column_1", "column_2"])
        self.assertEqual(fk.to_table, "table4")
        self.assertEqual(fk.to_keys, ["column_1", "column_3"])
def read_excel(args):
    """
    Reads the database description from XLS and returns a database model.
    Note that XLSReader can read multiple databases at a time, but convert 
    only supports one.  If there are more than one database, the first will be
    returned and an error message written.
    :param args: The command line arguments.
    :returns: The database read from Excel.
    :rtype: Database
    """

    reader = XLSReader()
    databases = reader.read_xls(filepath=args.from_excel)
    if len(databases) == 0:
        eprint("ERROR:  No databases read.")
        return None

    if len(databases) > 1:
        eprint("WARNING:  multiple databases read.  Only using %s" %
               databases.values()[0].database_name)

    return databases.values()[0]