def test_create_hierarchy(self):
        db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres",
                                              "postgres")
        py2sql.Py2SQL.db_connect(db_con_info)

        py2sql.Py2SQL.save_hierarchy(TestSaveDeleteObject.Foo)

        tables = py2sql.Py2SQL.db_tables()
        self.assertTrue("foo" in tables and "foo_der1" in tables
                        and "foo_der11" in tables and "foo_der2" in tables
                        and "miss" not in tables)

        foo_der11_columns = [
            j for i in py2sql.Py2SQL._select_from_table('foo_der11') for j in i
        ]
        self.assertTrue('id' in foo_der11_columns
                        and 'foo_1' in foo_der11_columns
                        and 'foo_2' in foo_der11_columns
                        and 'der_1' in foo_der11_columns
                        and 'der_11' in foo_der11_columns)

        foo_der1_columns = [
            j for i in py2sql.Py2SQL._select_from_table('foo_der1') for j in i
        ]
        self.assertTrue('id' in foo_der1_columns
                        and 'foo_1' in foo_der1_columns
                        and 'foo_2' in foo_der1_columns
                        and 'der_1' in foo_der1_columns)

        py2sql.Py2SQL.delete_hierarchy(TestSaveDeleteObject.Foo)

        py2sql.Py2SQL.db_disconnect()
Example #2
0
class TestClassSaveDelete(unittest.TestCase):

    db_name = "test"
    table_name = "bar"
    db_config = py2sql.DBConnectionInfo(db_name, "localhost",
                                        "adminadminadmin", "postgres")

    def test_save_class_called_twice(self):
        py2sql.Py2SQL.db_connect(self.db_config)

        self.assertEqual([], py2sql.Py2SQL.db_table_structure("bar"))
        try:
            py2sql.Py2SQL.save_class(Bar)
            self.assertEqual([(0, 'id', 'integer'), (1, 'done', 'boolean')],
                             py2sql.Py2SQL.db_table_structure("bar"))
            py2sql.Py2SQL.save_class(Bar)
        except:
            self.fail(
                "save_class() should not throw after being called twice.")
        self.assertEqual([(0, 'id', 'integer'), (1, 'done', 'boolean')],
                         py2sql.Py2SQL.db_table_structure("bar"))

        py2sql.Py2SQL.delete_class(Bar)
        self.assertEqual([], py2sql.Py2SQL.db_table_structure("bar"))
        py2sql.Py2SQL.db_disconnect()
Example #3
0
class TestGeneralAccess(unittest.TestCase):
    db_config = py2sql.DBConnectionInfo("test", "localhost", "adminadminadmin",
                                        "postgres")

    def tearDown(self):
        py2sql.Py2SQL.db_connect(self.db_config)
        py2sql.Py2SQL._drop_table("test")
        py2sql.Py2SQL.db_disconnect()

    def test_run_all_readonly_functions(self):
        py2sql.Py2SQL.db_connect(self.db_config)
        py2sql.Py2SQL.db_engine()
        py2sql.Py2SQL.db_name()
        py2sql.Py2SQL.db_size()
        py2sql.Py2SQL.db_tables()

        py2sql.Py2SQL._create_table(
            "test",
            "(id serial primary key not null , foo varchar(100), bar text, zoo bytea)"
        )

        self.assertEqual(
            # serial is really pseudo-type, it is mapped into integer
            [(0, 'id', 'integer'), (1, 'foo', 'character varying'),
             (2, 'bar', 'text'), (3, 'zoo', 'bytea')],
            py2sql.Py2SQL.db_table_structure("test"))

        # it is never 0, even with no data
        self.assertGreater(py2sql.Py2SQL.db_table_size("test"), 0)

        py2sql.Py2SQL.db_disconnect()
    def test_delete_hierarchy(self):
        db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres",
                                              "postgres")
        py2sql.Py2SQL.db_connect(db_con_info)

        py2sql.Py2SQL.save_hierarchy(TestSaveDeleteObject.Foo)
        py2sql.Py2SQL.delete_hierarchy(TestSaveDeleteObject.Foo)
        tables = py2sql.Py2SQL.db_tables()
        self.assertFalse("foo" in tables or "foo_der1" in tables
                         or "foo_der11" in tables or "foo_der2" in tables
                         or "miss" in tables)

        py2sql.Py2SQL.db_disconnect()
class TestSaveDeleteObject(unittest.TestCase):
    db_name = "test"
    db_config = py2sql.DBConnectionInfo(db_name, "localhost", "postgres", "postgres")

    def test_save_object_with_no_class_raises_exception(self):
        @dataclass
        class Bar:
            done: bool = True
        py2sql.Py2SQL.db_connect(self.db_config)
        b = Bar()
        with self.assertRaises(NotImplementedError):
            py2sql.Py2SQL.save_object(b)
        py2sql.Py2SQL.db_disconnect()
        py2sql.Py2SQL.db_connect(self.db_config)
        py2sql.Py2SQL.delete_class(Bar)
        self.assertEqual([],
            py2sql.Py2SQL.db_table_structure("bar")
        )
        py2sql.Py2SQL.db_disconnect()

    def test_save_class_and_object(self):
        py2sql.Py2SQL.db_connect(self.db_config)
        @dataclass
        class S:
            foo: str = 0
            bar: int = 1
        py2sql.Py2SQL.save_class(S)
        s = S("one", 1)
        py2sql.Py2SQL.save_object(s)
        py2sql.Py2SQL.db_disconnect()
        py2sql.Py2SQL.db_connect(self.db_config)
        py2sql.Py2SQL.delete_class(S)
        self.assertEqual([],
            py2sql.Py2SQL.db_table_structure("s")
        )
        py2sql.Py2SQL.db_disconnect()

    def test_save_and_delete(self):
        @dataclass
        class Bar():
            bar: int = 2
        b = Bar()
        db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres", "postgres")
        py2sql.Py2SQL.db_connect(db_con_info)
        py2sql.Py2SQL.save_class(Bar)
        py2sql.Py2SQL.save_object(b)
        self.assertTrue("bar" in py2sql.Py2SQL.db_tables())
        py2sql.Py2SQL.delete_object(b)
        py2sql.Py2SQL.delete_class(Bar)
        self.assertTrue("bar" not in py2sql.Py2SQL.db_tables())
        py2sql.Py2SQL.db_disconnect()
 def test_save_and_delete(self):
     @dataclass
     class Bar():
         bar: int = 2
     b = Bar()
     db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres", "postgres")
     py2sql.Py2SQL.db_connect(db_con_info)
     py2sql.Py2SQL.save_class(Bar)
     py2sql.Py2SQL.save_object(b)
     self.assertTrue("bar" in py2sql.Py2SQL.db_tables())
     py2sql.Py2SQL.delete_object(b)
     py2sql.Py2SQL.delete_class(Bar)
     self.assertTrue("bar" not in py2sql.Py2SQL.db_tables())
     py2sql.Py2SQL.db_disconnect()
class TestSaveDeleteObject(unittest.TestCase):
    db_name = "test"
    db_config = py2sql.DBConnectionInfo(db_name, "localhost", "postgres",
                                        "postgres")

    @dataclass
    class Foo:
        foo_1: str = "s1"
        foo_2: int = 2

    @dataclass
    class Miss:
        miss: str = "-1"

    @dataclass
    class Foo_der1(Foo):
        der_1: int = 3

    @dataclass
    class Foo_der11(Foo_der1):
        der_11: str = "dd"

    @dataclass
    class Foo_der2(Foo):
        der_2: bool = True

    def test_create_hierarchy(self):
        db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres",
                                              "postgres")
        py2sql.Py2SQL.db_connect(db_con_info)

        py2sql.Py2SQL.save_hierarchy(TestSaveDeleteObject.Foo)

        tables = py2sql.Py2SQL.db_tables()
        self.assertTrue("foo" in tables and "foo_der1" in tables
                        and "foo_der11" in tables and "foo_der2" in tables
                        and "miss" not in tables)

        foo_der11_columns = [
            j for i in py2sql.Py2SQL._select_from_table('foo_der11') for j in i
        ]
        self.assertTrue('id' in foo_der11_columns
                        and 'foo_1' in foo_der11_columns
                        and 'foo_2' in foo_der11_columns
                        and 'der_1' in foo_der11_columns
                        and 'der_11' in foo_der11_columns)

        foo_der1_columns = [
            j for i in py2sql.Py2SQL._select_from_table('foo_der1') for j in i
        ]
        self.assertTrue('id' in foo_der1_columns
                        and 'foo_1' in foo_der1_columns
                        and 'foo_2' in foo_der1_columns
                        and 'der_1' in foo_der1_columns)

        py2sql.Py2SQL.delete_hierarchy(TestSaveDeleteObject.Foo)

        py2sql.Py2SQL.db_disconnect()

    def test_delete_hierarchy(self):
        db_con_info = py2sql.DBConnectionInfo("test", "localhost", "postgres",
                                              "postgres")
        py2sql.Py2SQL.db_connect(db_con_info)

        py2sql.Py2SQL.save_hierarchy(TestSaveDeleteObject.Foo)
        py2sql.Py2SQL.delete_hierarchy(TestSaveDeleteObject.Foo)
        tables = py2sql.Py2SQL.db_tables()
        self.assertFalse("foo" in tables or "foo_der1" in tables
                         or "foo_der11" in tables or "foo_der2" in tables
                         or "miss" in tables)

        py2sql.Py2SQL.db_disconnect()