aurweb/test/test_exceptions.py
Kevin Morris 38dc2bb99d Sanitize and modernize pytests
Some of these tests were written before some of our convenient
tooling existed. Additionally, some of the tests were not
cooperating with PEP-8 guidelines or isorted.

This commit does the following:
    - Replaces all calls to make_(user|session) with
      aurweb.db.create(Model, ...).
    - Replace calls to session.add(...) + session.commit() with
      aurweb.db.create.
    - Removes the majority of calls to (session|aurweb.db).delete(...).
    - Replaces session.query calls with aurweb.db.query.
    - Initializes all mutable globals in pytest fixture setup().
    - Makes mutable global declarations more concise:
      `var1, var2 = None, None` -> `var1 = var2 = None`
    - Defines a warning exclusion for test/test_ssh_pub_key.py.
    - Removes the aurweb.testing.models module.
    - Removes some useless pytest.fixture yielding.

As of this commit, developers should use the following guidelines
when writing tests:
    - Always use aurweb.db.(create|delete|query) for database
      operations, where possible.
    - Always define mutable globals in the style: `var1 = var2 = None`.
    - `yield` the most dependent model in pytest setup fixture **iff**
      you must delete records after test runs to maintain database
      integrity. Example: test/test_account_type.py.

This all makes the test code look and behave much cleaner.
Previously, aurweb.testing.setup_test_db was buggy and leaving
objects around in SQLAlchemy's IdentityMap.

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

99 lines
2.9 KiB
Python

from aurweb import exceptions
def test_aurweb_exception():
try:
raise exceptions.AurwebException("test")
except exceptions.AurwebException as exc:
assert str(exc) == "test"
def test_maintenance_exception():
try:
raise exceptions.MaintenanceException("test")
except exceptions.MaintenanceException as exc:
assert str(exc) == "test"
def test_banned_exception():
try:
raise exceptions.BannedException("test")
except exceptions.BannedException as exc:
assert str(exc) == "test"
def test_already_voted_exception():
try:
raise exceptions.AlreadyVotedException("test")
except exceptions.AlreadyVotedException as exc:
assert str(exc) == "already voted for package base: test"
def test_broken_update_hook_exception():
try:
raise exceptions.BrokenUpdateHookException("test")
except exceptions.BrokenUpdateHookException as exc:
assert str(exc) == "broken update hook: test"
def test_invalid_arguments_exception():
try:
raise exceptions.InvalidArgumentsException("test")
except exceptions.InvalidArgumentsException as exc:
assert str(exc) == "test"
def test_invalid_packagebase_exception():
try:
raise exceptions.InvalidPackageBaseException("test")
except exceptions.InvalidPackageBaseException as exc:
assert str(exc) == "package base not found: test"
def test_invalid_comment_exception():
try:
raise exceptions.InvalidCommentException("test")
except exceptions.InvalidCommentException as exc:
assert str(exc) == "comment is too short: test"
def test_invalid_reason_exception():
try:
raise exceptions.InvalidReasonException("test")
except exceptions.InvalidReasonException as exc:
assert str(exc) == "invalid reason: test"
def test_invalid_user_exception():
try:
raise exceptions.InvalidUserException("test")
except exceptions.InvalidUserException as exc:
assert str(exc) == "unknown user: test"
def test_not_voted_exception():
try:
raise exceptions.NotVotedException("test")
except exceptions.NotVotedException as exc:
assert str(exc) == "missing vote for package base: test"
def test_packagebase_exists_exception():
try:
raise exceptions.PackageBaseExistsException("test")
except exceptions.PackageBaseExistsException as exc:
assert str(exc) == "package base already exists: test"
def test_permission_denied_exception():
try:
raise exceptions.PermissionDeniedException("test")
except exceptions.PermissionDeniedException as exc:
assert str(exc) == "permission denied: test"
def test_repository_name_exception():
try:
raise exceptions.InvalidRepositoryNameException("test")
except exceptions.InvalidRepositoryNameException as exc:
assert str(exc) == "invalid repository name: test"