def test_schema_single_meta(self): Base = declarative_base() class A(Base): __tablename__ = 'A' id = Column(Integer, primary_key=True) class B(Base): __tablename__ = 'B' id = Column(Integer, primary_key=True) engine = create_engine('sqlite://') cli = Sqla(orm.sessionmaker(bind=engine), metadata=Base.metadata) output = StringIO() with mock.patch.object(sys, 'stdout', output): cli.command_schema() created = self._created_tables(output.getvalue()) self.assertEqual(len(created), 2) self.assertEqual(created.count('A'), 1) self.assertEqual(created.count('B'), 1) output = StringIO() with mock.patch.object(sys, 'stdout', output): cli.command_schema('A') created = self._created_tables(output.getvalue()) self.assertEqual(created, ['A']) output = StringIO() with mock.patch.object(sys, 'stdout', output): try: cli.command_schema('C') except SystemExit: pass created = self._created_tables(output.getvalue()) self.assertEqual(created, [])
def test_schema_several_meta(self): Base1 = declarative_base() class A1(Base1): __tablename__ = 'A' id = Column(Integer, primary_key=True) class B1(Base1): __tablename__ = 'B' id = Column(Integer, primary_key=True) Base2 = declarative_base() class A2(Base2): __tablename__ = 'A' id = Column(Integer, primary_key=True) engine1 = create_engine('sqlite://') engine2 = create_engine('sqlite://') binds = { A1.__table__: engine1, B1.__table__: engine1, A2.__table__: engine2, } meta = { 'm1': Base1.metadata, 'm2': Base2.metadata, 'm3': MetaData(), } cli = Sqla(orm.sessionmaker(binds=binds), metadata=meta) output = StringIO() with mock.patch.object(sys, 'stdout', output): cli.command_schema() created = self._created_tables(output.getvalue()) self.assertEqual(len(created), 3) self.assertEqual(created.count('A'), 2) self.assertEqual(created.count('B'), 1) output = StringIO() with mock.patch.object(sys, 'stdout', output): cli.command_schema('m1') created = self._created_tables(output.getvalue()) self.assertEqual(len(created), 2) self.assertEqual(created.count('A'), 1) self.assertEqual(created.count('B'), 1) output = StringIO() with mock.patch.object(sys, 'stdout', output): cli.command_schema('m1.B') created = self._created_tables(output.getvalue()) self.assertEqual(created, ['B']) output = StringIO() with mock.patch.object(sys, 'stdout', output): try: cli.command_schema('m2.B') except SystemExit: pass created = self._created_tables(output.getvalue()) self.assertEqual(created, []) output = StringIO() with mock.patch.object(sys, 'stdout', output): try: cli.command_schema('m3.A') except SystemExit: pass created = self._created_tables(output.getvalue()) self.assertEqual(created, [])
def test_schema_several_meta(self): Base1 = declarative_base() class A1(Base1): __tablename__ = "A" id = Column(Integer, primary_key=True) class B1(Base1): __tablename__ = "B" id = Column(Integer, primary_key=True) Base2 = declarative_base() class A2(Base2): __tablename__ = "A" id = Column(Integer, primary_key=True) engine1 = create_engine("sqlite://") engine2 = create_engine("sqlite://") binds = {A1.__table__: engine1, B1.__table__: engine1, A2.__table__: engine2} meta = {"m1": Base1.metadata, "m2": Base2.metadata, "m3": MetaData()} cli = Sqla(orm.sessionmaker(binds=binds), metadata=meta) output = StringIO() with mock.patch.object(sys, "stdout", output): cli.command_schema() created = self._created_tables(output.getvalue()) self.assertEqual(len(created), 3) self.assertEqual(created.count("A"), 2) self.assertEqual(created.count("B"), 1) output = StringIO() with mock.patch.object(sys, "stdout", output): cli.command_schema("m1") created = self._created_tables(output.getvalue()) self.assertEqual(len(created), 2) self.assertEqual(created.count("A"), 1) self.assertEqual(created.count("B"), 1) output = StringIO() with mock.patch.object(sys, "stdout", output): cli.command_schema("m1.B") created = self._created_tables(output.getvalue()) self.assertEqual(created, ["B"]) output = StringIO() with mock.patch.object(sys, "stdout", output): try: cli.command_schema("m2.B") except SystemExit: pass created = self._created_tables(output.getvalue()) self.assertEqual(created, []) output = StringIO() with mock.patch.object(sys, "stdout", output): try: cli.command_schema("m3.A") except SystemExit: pass created = self._created_tables(output.getvalue()) self.assertEqual(created, [])