示例#1
0
 def validate_db_connection(self):
     """Validate the connection to the database. If database does not exist,
     a new one is created.
     :raises: peewee.DatabaseError if existing database is invalid.
     """
     models.database.init(str(self._db_path))
     if self._db_path.exists():
         models.database.connect()
         Ship.select().first()
         models.database.close()
     else:
         # If database path does not exist, attempt to create a new database.
         self.create_db()
示例#2
0
    def add_test_data_to_db():
        """Adds test data to the database, create_db must be called first.
        """
        ships = [
            {'name': 'Achilles', 'sh_class': 'Leander', 'type': 'Light Cruiser', 'country': 'NZL',
             'build_date': '1000-01-01'},
            {'name': 'Admiral Graf Spee', 'sh_class': 'Deutschland', 'type': 'Heavy Cruiser',
             'country': 'GER', 'build_date': '1000-01-01'},
            {'name': 'Cumberland', 'sh_class': 'County-Kent', 'type': 'Heavy Cruiser',
             'country': 'UK', 'build_date': '1000-01-01'},
            {'name': 'Exeter', 'sh_class': 'York', 'type': 'Heavy Cruiser', 'country': 'UK',
             'build_date': '1000-01-01'},
            {'name': 'Ajax', 'sh_class': 'Leander', 'type': 'Light Cruiser', 'country': 'UK',
             'build_date': '1000-01-01'}
        ]

        battles = [
            {'name': 'River Platte', 'conflict': 'WWII', 'date': '1939-12-13'},
            {'name': 'Battle of the Java Sea', 'conflict': 'WWII', 'date': '1942-02-27'}
        ]

        with models.database.atomic():
            Ship.insert_many(ships).execute()
            Battle.insert_many(battles).execute()

            river_platte = Battle.select().where(Battle.name == 'River Platte').get()
            java_sea = Battle.select().where(Battle.name == 'Battle of the Java Sea').get()

            for ship in Ship.select():
                if ship.name == 'Ajax':
                    ShipBattle.create(ship=ship, battle=java_sea)
                ShipBattle.create(ship=ship, battle=river_platte)
        models.database.close()
示例#3
0
 def test_reset_db_message(self, create_db, clean_up):
     db_creator = DbCreatorActor.start(self._db_path)
     db_creator.tell(PopulateDbMessage(None).to_dict())
     db_creator.ask(ResetDbMessage(None).to_dict())
     assert len(Ship.select()) == 0
     assert len(Battle.select()) == 0
     assert len(ShipBattle.select()) == 0
     db_creator.stop()
     clean_up()
示例#4
0
 def test_populate_db_message(self, create_db, clean_up):
     db_creator = DbCreatorActor.start(self._db_path)
     db_creator.ask(PopulateDbMessage(None).to_dict())
     assert self._db_path.exists()
     assert len(Ship.select()) == 5
     assert len(Battle.select()) == 2
     assert len(ShipBattle.select()) == 6
     db_creator.stop()
     clean_up()