Exemplo n.º 1
0
import slddb
from slddb import __version__, dbconfig
# for flask use database file in startup folder
DB_FILE = 'slddb.db'
dbconfig.DB_FILE = DB_FILE
slddb.DB_FILE = DB_FILE
from slddb import SLDDB
from slddb.dbconfig import DB_FILE
import sqlite3

if __name__ == '__main__':
    db = SLDDB(DB_FILE)
    try:
        db.create_database()
        db.add_elements()
    except sqlite3.OperationalError:
        db.update_fields()
Exemplo n.º 2
0
class TestCreateDB(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.db = SLDDB(':memory:')
        cls.db.create_database()

    @classmethod
    def tearDownClass(cls):
        del (cls.db)

    def test_tables(self):
        c = self.db.db.cursor()
        c.execute("SELECT name FROM sqlite_master WHERE type='table'")
        items = c.fetchall()
        for i, tbl in enumerate([dbconfig.DB_MATERIALS_NAME]):
            with self.subTest(msg=tbl, i=i):
                self.assertTrue((tbl, ) in items)

    def test_element_search(self):
        with self.subTest('database search', i=0):
            s1 = element_table.get_element('Si')
            s2 = element_table.get_element(14)
        with self.subTest('equality', i=0):
            self.assertEqual(s1.Z, s2.Z)
            self.assertEqual(s1.symbol, s2.symbol)
            self.assertEqual(s1.mass, s2.mass)
            self.assertEqual(s1.b, s2.b)
            testing.assert_array_equal(s1._xdata, s2._xdata)

    def test_add_field(self):
        global dbconfig
        # call without changes
        self.db.update_fields()

        # call with appending column
        dbconfig.DB_MATERIALS_FIELDS.append('testadd')
        dbconfig.DB_MATERIALS_CONVERTERS.append(
            dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.append(
            dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])

        self.db.update_fields()

        # call with inserted column
        dbconfig.DB_MATERIALS_FIELDS.insert(5, 'testadd2')
        dbconfig.DB_MATERIALS_CONVERTERS.insert(
            5, dbconfig.DB_MATERIALS_CONVERTERS[-1])
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.insert(
            5, dbconfig.DB_MATERIALS_FIELD_DEFAULTS[-1])
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db.update_fields()

        #reset database
        dbconfig.DB_MATERIALS_FIELDS.pop(-1)
        dbconfig.DB_MATERIALS_FIELDS.pop(5)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(-1)
        dbconfig.DB_MATERIALS_CONVERTERS.pop(5)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(-1)
        dbconfig.DB_MATERIALS_FIELD_DEFAULTS.pop(5)
        dbconfig.db_lookup = dict([
            (field, (i, converter, default))
            for i, (field, converter, default) in enumerate(
                zip(dbconfig.DB_MATERIALS_FIELDS,
                    dbconfig.DB_MATERIALS_CONVERTERS,
                    dbconfig.DB_MATERIALS_FIELD_DEFAULTS))
        ])
        self.db = SLDDB(':memory:')
        self.db.create_database()

    def test_backup(self):
        self.db.backup(':memory:')