def test_muti_crud(): mock_env = { 'FLASK_CONFIG_PREFIXES': 'SQLALCHEMY', 'SQLALCHEMY_DEFAULT': 'postgresql://*****:*****@pgdb:5432/ds_test', 'SQLALCHEMY_DB2': 'postgresql://*****:*****@pgdb:5432/ds2_test' } os_environ_mock = patch.dict(os.environ, mock_env) with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Example(name='Rock') rock.add(connection_name='SQLALCHEMY_DEFAULT') db.pool.commit(connection_name='SQLALCHEMY_DEFAULT') assert 1 == Example.objects.count( connection_name='SQLALCHEMY_DEFAULT') assert 0 == Example.objects.count(connection_name='SQLALCHEMY_DB2') rock2 = Example(name='Rock2') rock2.add(connection_name='SQLALCHEMY_DB2') db.pool.commit(connection_name='SQLALCHEMY_DB2') assert 1 == Example.objects.count(connection_name='SQLALCHEMY_DB2') r1 = Example.objects.get(connection_name='SQLALCHEMY_DEFAULT', name='Rock') r2 = Example.objects.get(connection_name='SQLALCHEMY_DB2', name='Rock2') assert r1.name != r2.name rock.delete() db.pool.commit() assert 0 == Example.objects.count( connection_name='SQLALCHEMY_DEFAULT') l1 = list( Example.objects.filter_by( connection_name='SQLALCHEMY_DEFAULT')) l2 = list( Example.objects.filter_by(connection_name='SQLALCHEMY_DB2')) assert 0 == len(l1) assert 1 == len(l2) db.cleandb()
def test_delete(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Genre(name='Rock', description='rock yeah!!!') rock.add() db.pool.commit() assert 1 == Genre.objects.count() rock.delete() db.pool.commit() assert 0 == Genre.objects.count()
def test_raw_sql(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Genre(name='Rock', description='rock yeah!!!') rock.add() db.pool.commit() pink = Artist( genre_id=rock.id, name='Pink Floyd', description='Awsome') pink.add() db.pool.commit() dark = Album( artist_id=pink.id, name='Dark side of the moon', description='Interesting') dark.add() db.pool.commit() rolling = Artist( genre_id=rock.id, name='Rolling Stones', description='Acceptable') rolling.add() db.pool.commit() sql = """ SELECT a.name as artist_name, a.description artist_description, g.name as artist_genre FROM artist a INNER JOIN genre g ON a.genre_id = g.id ORDER BY a.id DESC; """ result = Genre.objects.raw_sql(sql).fetchall() assert 2 == len(result) assert 'Rolling Stones' == result[0][0] sql = """ SELECT a.name as artist_name, a.description artist_description, g.name as artist_genre FROM artist a INNER JOIN genre g ON a.genre_id = g.id WHERE a.id = :artist_id ORDER BY a.id DESC; """ result = Genre.objects.raw_sql(sql, artist_id=pink.id).fetchall() assert 1 == len(result) assert 'Pink Floyd' == result[0][0]
def test_get_for_update(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Genre(name='Rock', description='rock yeah!!!') rock.add() db.pool.commit() rock2 = Genre.objects.get_for_update(id=rock.id) rock2.name = 'updated name' rock2.update() assert rock2.id == rock.id rock2.objects.db.pool.close()
def test_simple_insert(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() assert 0 == Genre.objects.count() genre = Genre(name='name1', description='dsc1') genre.add() db.pool.commit() assert 1 == Genre.objects.count() genre2 = Genre(name='name2', description='dsc2') genre2.add() db.pool.commit() assert 2 == Genre.objects.count()
def test_update(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Genre(name='Rock', description='rock yeah!!!') rock.add() db.pool.commit() description_updated = 'description_updated' rock.description = description_updated rock.update() db.pool.commit() rock2 = Genre.objects.get(id=rock.id) assert rock2.description == description_updated assert 1 == Genre.objects.count()
def test_encrypted_password(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() user = User( username='******', email='*****@*****.**', password='******') user.add() db.pool.commit() id = user.id # objects needs to dereferenciated otherwise # user2 will be just a copy of user user = None user2 = User.objects.get(id=id) assert id == user2.id assert '123' == user2.password
def test_multi_insert(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() assert 0 == Genre.objects.count() data = [ Genre( name='genre{}'.format(x), description='descript{}'.format(x)) for x in range(100) ] Genre.objects.add_all(data) db.pool.commit() assert 100 == Genre.objects.count()
def test_postgresql_connection(): """ checks if connection is open """ mock_env = { 'FLASK_CONFIG_PREFIXES': 'SQLALCHEMY', 'SQLALCHEMY_DEFAULT': 'postgresql://*****:*****@pgdb:5432/ds_test', } os_environ_mock = patch.dict(os.environ, mock_env) with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) result = db.pool.connections['SQLALCHEMY_DEFAULT'].session.execute( 'SELECT 19;') assert result.fetchone()[0] == 19 db.pool.connections['SQLALCHEMY_DEFAULT'].session.close()
def test_relationships(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): db = init_db(app) db.syncdb() db.cleandb() rock = Genre(name='Rock', description='rock yeah!!!') rock.add() db.pool.commit() pink = Artist( genre_id=rock.id, name='Pink Floyd', description='Awsome') pink.add() db.pool.commit() dark = Album( artist_id=pink.id, name='Dark side of the moon', description='Interesting') dark.add() db.pool.commit() rolling = Artist( genre_id=rock.id, name='Rolling Stones', description='Acceptable') rolling.add() db.pool.commit() hits = Album( artist_id=rolling.id, name='Greatest hits', description='Interesting') hits.add() db.pool.commit() assert 2 == Album.objects.count() wall = Album( artist_id=pink.id, name='The Wall', description='Interesting') wall.add() db.pool.commit() assert 2 == len(pink.albums) assert 2 == len(Artist.objects.filter_by(genre_id=rock.id)[:])
def config_app(app): db = init_db(app) db.syncdb() db.cleandb() config_users(app)