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_app_config(): """ Test if app loads configuration values from environmnet variables properly """ mock_env = { 'FLASK_CONFIG_PREFIXES': 'AWS,DB', 'AWS_SECRET': 'S3CR3T', 'DB_USERNAME': '******', 'PREFIX_NOT_EXISTS': 'SHOULD_NOT_EXIST', 'FLASK_BH_DEBUG_LEVEL': 'DEBUG' } os_environ_mock = patch.dict(os.environ, mock_env) with os_environ_mock: app = get_or_create_app(__name__) assert 'AWS_SECRET' in app.config assert 'DB_USERNAME' in app.config assert 'PREFIX_NOT_EXISTS' not in app.config
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_app_creation(): """ Test if flask_bh app is created properly """ # Raises an exception for uninitialized apps with pytest.raises(RuntimeError): current_app._get_current_object() app = get_or_create_app(__name__) with app.app_context(): """ verifies that current_app._get_current_object() returns same object previusly created see: with app.app_context(): """ app2 = current_app._get_current_object() assert app == app2 assert app.name == __name__
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 test_signup_username_password(): with os_environ_mock: app = get_or_create_app(__name__) with app.app_context(): config_app(app) with app.test_client() as c: p1 = BaseTestFactory.create_random_string() p2 = BaseTestFactory.create_random_string() assert p1 != p2 data = { 'username': BaseTestFactory.create_random_email(), 'password': p1, 'password2': p2, } result = c.post('/users/signup', data=json.dumps(data), content_type='application/json') assert 400 == result.status_code j_content = json.loads(result.get_data().decode('utf-8')) assert "Passwords must be equal" in j_content['msg'] assert 0 == User.objects.count() assert 0 == OAuth2User.objects.count() pwd = BaseTestFactory.create_random_string() data2 = { 'username': BaseTestFactory.create_random_email(), 'password': pwd, 'password2': pwd, } serializer = SignUpSerializer(data=data2) result = c.post('/users/signup', data=serializer.payload, content_type='application/json') assert 201 == result.status_code assert 1 == User.objects.count() assert 1 == OAuth2User.objects.count()
""" Imports App and Runs it """ from flask.ext.script import Manager from flask import g, session, request from app import get_or_create_app from app.db import db_session, init_db from models.auth import User from util.jinja_filters.jinja_filters import pluralize, time_ago smtrade = get_or_create_app() manager = Manager(smtrade) @smtrade.teardown_appcontext def shutdown_session(exception=None): db_session.remove() @smtrade.before_request def before_request(): g.db = db_session() if session.get('current_user_id', None) and not request.endpoint == 'testing.server_shutdown': g.user = g.db.query(User).filter_by(id=int(session.get('current_user_id'))).first() else: g.user = None