Beispiel #1
0
class TestSQLiteDBAPI:

    def setup_method(self):
        self.db = SQLiteDB(':memory:')

    def table_exists(self, model):
        with closing(self.db._connection.cursor()) as cursor:
            query = cursor.execute(
                "SELECT name FROM sqlite_master WHERE type='table' AND name='{}'".format(model.__name__.lower())
            )
            return bool(len(query.fetchall()))

    def count_tables(self):
        with closing(self.db._connection.cursor()) as cursor:
            query = cursor.execute("SELECT count(*) as number FROM sqlite_master WHERE type='table'").fetchall()
            return query[0][0]
            
    def test_is_slotted(self):
        with pytest.raises(AttributeError):
            self.db.__dict__
            
    def test_has_atomic_method(self):
        assert hasattr(self.db, 'atomic') is True

    def test_register_a_custom_model(self):
        assert self.table_exists(Trainer) == False
        self.db.register(Trainer)
        assert self.table_exists(Trainer) == True

    def test_register_several_custom_models(self):
        assert self.count_tables() == 0
        self.db.register(Trainer, Pokemon)
        assert self.table_exists(Trainer) == True
        assert self.table_exists(Pokemon) == True

    def test_store_a_database_reference_into_model_when_registered(self):
        self.db.register(Trainer)
        try:
            Trainer._db
        except AttributeError:
            pytest.fail()

    def test_no_database_reference_in_unregistered_model(self):
        class CustomModel(Model):
            pass

        with pytest.raises(AttributeError):
            CustomModel._db

    def test_register_an_existing_table(self):
        self.db.register(Trainer)
        try:
            self.db.register(Trainer)
        except:
            pytest.fail()

    def test_pragma_foreign_key_is_on(self):
        result = self.db._connection.execute('PRAGMA foreign_keys').fetchone()[0]
        assert result == 1

    def teardown_method(self):
        try:
            os.remove(DB_NAME)
        except:
            return
Beispiel #2
0
class BaseTestCase:

    TRAINERS = {
        'Giovanni': {
            'name': 'Giovanni',
            'age': 42
        },
        'James': {
            'name': 'James',
            'age': 21
        },
        'Jessie': {
            'name': 'Jessie',
            'age': 17
        },
    }

    POKEMONS = {
        'Kangaskhan': {
            'name': 'Kangaskhan',
            'level': 29,
            'trainer': 1
        },
        'Koffing': {
            'name': 'Koffing',
            'level': 9,
            'trainer': 2
        },
        'Wobbuffet': {
            'name': 'Wobbuffet',
            'level': 19,
            'trainer': 3
        },
    }

    ATTACKS = {
        'Rage': {
            'name': 'Rage',
            'accuracy': 1.0
        },
        'Smog': {
            'name': 'Smog',
            'accuracy': 0.7
        },
        'Safeguard': {
            'name': 'Safeguard',
            'accuracy': 0.10
        }
    }

    def setup_method(self):
        self.db = SQLiteDB(DB_NAME)
        self.db.register(Trainer, Pokemon, Attack)

    def add_attack(self, names):
        try:
            names = names.split()
        except:
            pass

        for name in names:
            InsertQuery(self.db).table(Attack).from_dicts(
                self.ATTACKS[name]).execute()

    def add_trainer(self, names):
        try:
            names = names.split()
        except:
            pass

        for name in names:
            InsertQuery(self.db).table(Trainer).from_dicts(
                self.TRAINERS[name]).execute()

    def add_pokemon(self, names):
        try:
            names = names.split()
        except:
            pass

        for name in names:
            InsertQuery(self.db).table(Pokemon).from_dicts(
                self.POKEMONS[name]).execute()