aurweb/test/test_user.py
Kevin Morris a836892cde aurweb.db: add query, create, delete helpers
Takes sqlalchemy kwargs or stanzas:

query(Model, Model.Column == value)
query(Model, and_(Model.Column == value, Model.Column != "BAD!"))

Updated tests to reflect the new utility and a comment about upcoming
function deprecation is added to get_account_type().

From here on, phase out the use of get_account_type().

+ aurweb.db: Added create utility function
+ aurweb.db: Added delete utility function

The `delete` function can be used to delete a record by search
kwargs directly.

Example:
    delete(User, User.ID == 6)

All three functions added in this commit are typically useful to
perform these operations without having to import aurweb.db.session.
Removes a bit of redundancy overall.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-06-05 20:11:17 -07:00

54 lines
1.5 KiB
Python

import pytest
import aurweb.config
from aurweb.db import query
from aurweb.models.account_type import AccountType
from aurweb.models.user import User
from aurweb.testing import setup_test_db
@pytest.fixture(autouse=True)
def setup():
setup_test_db("Users")
def test_user():
""" Test creating a user and reading its columns. """
from aurweb.db import session
# First, grab our target AccountType.
account_type = session.query(AccountType).filter(
AccountType.AccountType == "User").first()
user = User(
AccountType=account_type,
RealName="Test User", Username="test",
Email="test@example.org", Passwd="abcd",
IRCNick="tester",
Salt="efgh", ResetKey="blahblah")
session.add(user)
session.commit()
assert user in account_type.users
# Make sure the user was created and given an ID.
assert bool(user.ID)
# Search for the user via query API.
result = session.query(User).filter(User.ID == user.ID).first()
# Compare the result and our original user.
assert result.ID == user.ID
assert result.AccountType.ID == user.AccountType.ID
assert result.Username == user.Username
assert result.Email == user.Email
# Ensure we've got the correct account type.
assert user.AccountType.ID == account_type.ID
assert user.AccountType.AccountType == account_type.AccountType
# Test out user string functions.
assert repr(user) == f"<User(ID='{user.ID}', " + \
"AccountType='User', Username='test')>"
session.delete(user)