You can perform also multiple insert operations at once:
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()
The following example shows how to use foreing keys for relational data:
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)[:])
Update operations are also supported:
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()
The get_for_update method is useful if you want to retrieve an object with lock for updates:
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()
The following examples show how to delete records from the database:
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()
The ORM supports the usage of raw SQL sentences:
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]
Application Configuration
This application translates enviroment variables to Flask configuration values. This approachs allows the usage of key value pairs tools
such as terraform vault (https://blog.backendhelpers.co/en/2019/vault-intro/index.html) for secret management. In order to tell Flask what
kind of enviroment variables to look for, all you need to do is to define the following variable:
请发表评论