move aurweb.testing to its own package

+ Added aurweb.testing.setup_test_db(*tables)
+ Added aurweb.testing.models.make_user(**kwargs)
+ Added aurweb.testing.models.make_session(**kwargs)
+ Added aurweb.testing.requests.Client
+ Added aurweb.testing.requests.Request
* Updated test_l10n.py to use our new Request

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2020-12-24 20:48:35 -08:00
parent 64bc93926f
commit 5185df629e
4 changed files with 41 additions and 14 deletions

View file

@ -0,0 +1,30 @@
import aurweb.db
def setup_test_db(*args):
""" This function is to be used to setup a test database before
using it. It takes a variable number of table strings, and for
each table in that set of table strings, it deletes all records.
The primary goal of this method is to configure empty tables
that tests can use from scratch. This means that tests using
this function should make sure they do not depend on external
records and keep their logic self-contained.
Generally used inside of pytest fixtures, this function
can be used anywhere, but keep in mind its functionality when
doing so.
Examples:
setup_test_db("Users", "Sessions")
test_tables = ["Users", "Sessions"];
setup_test_db(*test_tables)
"""
engine = aurweb.db.get_engine()
conn = engine.connect()
tables = list(args)
for table in tables:
conn.execute(f"DELETE FROM {table}")
conn.close()

25
aurweb/testing/models.py Normal file
View file

@ -0,0 +1,25 @@
import warnings
from sqlalchemy import exc
import aurweb.db
def make_user(**kwargs):
with warnings.catch_warnings():
warnings.simplefilter("ignore", exc.SAWarning)
from aurweb.models.user import User
user = User(**kwargs)
aurweb.db.session.add(user)
aurweb.db.session.commit()
return user
def make_session(**kwargs):
with warnings.catch_warnings():
warnings.simplefilter("ignore", exc.SAWarning)
from aurweb.models.session import Session
session = Session(**kwargs)
aurweb.db.session.add(session)
aurweb.db.session.commit()
return session

View file

@ -0,0 +1,8 @@
class Client:
host = "127.0.0.1"
class Request:
client = Client()
cookies = dict()
headers = dict()