def test_add_data_no_fail_contains_unique(self):
     def iterator():
         yield {'col1': 0, 'col2': 1, 'col3': 1}
         yield {'col1': 1, 'col2': 1, 'col3': 2}
         yield {'col1': 0, 'col2': 1, 'col3': 3}
         yield {'col1': 2, 'col2': 1, 'col3': 4}
     structure = {
         'col1': {'type': 'Integer'},
         'col2': {'type': 'Integer'},
         'col3': {'type': 'Integer'},
         }
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     table = database.Table.from_config(
         "hello",
         {'Columns': structure,
          'UniqueConstraint': {
              'name': 'something', 'Columns': ['col1', 'col2']
              },
          },
         db)
     table.create(db)
     table.add_data(iterator())
     rows = self.execute("""
         SELECT col1, col2
         FROM "hello"
     """)
     self.assertEqual(
         [(x, 1,) for x in range(3)],
         rows,
         )
 def test_create_empty_tables_noconfig(self):
     config_file = self._create_test_config(None)
     db = database.create_engine(config_file)
     structure = {'table': {}}
     with tempfile.NamedTemporaryFile(delete=False) as db_structure_file:
         json.dump(structure, db_structure_file)
     with self.assertRaises(RuntimeError):
         database._create_empty_tables(db, db_structure_file.name)
Example #3
0
 def _prepare_db(self):
     self.config_file = self._create_test_config(None)
     db = database.create_engine(self.config_file)
     try:
         database._create_superlevel(db)
     except RuntimeError:
         pass
     database._create_empty_tables(db, store_data.resources.db_structure)
 def test_create_superlevel(self):
     config_file = self._create_test_config()
     db = database.create_engine(config_file)
     database._create_superlevel(db)
     self.execute(
         "CREATE TABLE hello(id int, value varchar(256))",
         nofetch=True)
     databases = self.execute("SELECT datname FROM pg_database;")
     database_names = set(x[0] for x in databases)
     self.assertTrue(_project_config()['database'] in database_names)
 def test_from_config(self, _, structure):
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     table = database.Table.from_config("hello", {"Columns": structure}, db)
     table.create(db.engine)
     tables = self.execute("""
         SELECT table_name
         FROM information_schema.tables
         WHERE table_schema='public'
     """)
     table_names = set(x[0] for x in tables)
     self.assertTrue("hello" in table_names)
 def test_create_empty_tables(self, _, structure):
     config_file = self._create_test_config(None)
     db = database.create_engine(config_file)
     with tempfile.NamedTemporaryFile(delete=False) as db_structure_file:
         json.dump(structure, db_structure_file)
     database._create_empty_tables(db, db_structure_file.name)
     tables = self.execute("""
         SELECT table_name
         FROM information_schema.tables
         WHERE table_schema='public'
     """)
     table_names = set(x[0] for x in tables)
     for table_name in structure:
         self.assertTrue(table_name in table_names)
 def create_table(self):
     structure = {
         'number': {'type': 'Integer'},
         'name': {'type': 'String'},
         }
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     table = database.Table.from_config("hello", {'Columns': structure}, db)
     table.create(db)
     self.init_data = [{'number': i,
                       'name': 'oneven' if i % 2 else 'even'}
                       for i in range(10)]
     table.add_data(self.init_data)
     return table
 def test_from_database(self):
     structure = {
         'Columns': {
             'first': {'type': 'Integer'},
             'second': {'type': 'Integer'}
             }
         }
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     table_ref = database.Table.from_config("hello", structure, db)
     table_ref.create(db.engine)
     table_target = database.Table.from_database("hello", db.engine)
     self.assertEqual(table_ref.name, table_target.name)
     self.assertEqual(
         [x.name for x in table_ref.columns],
         [x.name for x in table_target.columns])
 def test_add_data_buffer(self):
     def iterator():
         for i in range(10):
             yield {'first': i}
     structure = {
         'first': {'type': 'Integer'},
         }
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     table = database.Table.from_config("hello", {'Columns': structure}, db)
     table.create(db)
     table.add_data(iterator(), buffer_size=10)
     rows = self.execute("""
         SELECT *
         FROM "hello"
     """)
     self.assertEqual(
         [(x,) for x in range(10)],
         rows,
         )
    def test_add_data_fail_unknown_error(self):
        def iterator():
            yield {'col1': 'string'}
        structure = {
            'col1': {'type': 'Integer'},
            }
        config_file = self._create_test_config(database=None)
        db = database.create_engine(config_file)
        table = database.Table.from_config(
            "hello",
            {'Columns': structure,
             },
            db)
        table.create(db)

        class inserter(object):
            def execute(self, *args, **kwargs):
                raise sqlalchemy.exc.IntegrityError("", {}, Exception)
        table.insert = mock.Mock(return_value=inserter())
        with self.assertRaises(sqlalchemy.exc.IntegrityError):
            table.add_data(iterator())
 def test_from_config_no_columns(self):
     config_file = self._create_test_config(database=None)
     db = database.create_engine(config_file)
     with self.assertRaises(RuntimeError):
         database.Table.from_config("hello", {}, db)
 def test_create_superlevel_failure(self):
     config_file = self._create_test_config(None)
     db = database.create_engine(config_file)
     with self.assertRaises(RuntimeError):
         database._create_superlevel(db)
 def test_init_database(self, _, structure):
     config_file = self._create_test_config()
     db = database.create_engine(config_file)
     with tempfile.NamedTemporaryFile(delete=False) as db_structure_file:
         json.dump(structure, db_structure_file)
     database.init_database(db, db_structure_file.name)
 def test_init(self):
     config_file = self._create_test_config()
     database.create_engine(config_file)