Skip to content

Commit

Permalink
Merge pull request #76 from ubclaunchpad/cheukyin699/#72-pytest-conds…
Browse files Browse the repository at this point in the history
…kips

Pytest tests for dynamodb things is optional
  • Loading branch information
Cheuk Yin Ng committed Nov 3, 2018
2 parents 91e6a65 + 000eae9 commit 8a6ef19
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 46 deletions.
110 changes: 69 additions & 41 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ this can be run with:
./scripts/build_check.sh
```

The above tests would be run with the assumption that other applications, such
as the local database, is also running. To run tests that explicitly do **not**
involve the running of any database, run pytest with the following arguments:

```bash
pytest -m "not db"
```

You can also install it as a
[pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for git:

Expand Down
3 changes: 1 addition & 2 deletions db/facade.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Database Facade."""
from .dynamodb import DynamoDB


class DBFacade:
Expand All @@ -10,7 +9,7 @@ class DBFacade:
or Postgres are also being considered.
"""

def __init__(self, db=DynamoDB()):
def __init__(self, db):
"""Initialize facade using DynamoDB settings (for now)."""
self.ddb = db

Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
db: mark a test as involving use of a database
12 changes: 11 additions & 1 deletion scripts/build_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ set -euxo pipefail
pipenv run pycodestyle .
pipenv run pydocstyle .
mdl .
pipenv run pytest tests/

# We use nmap to check if dynamodb is running locally
# TODO: later should combine this with environmental variable checking (to see
# if we can use a remote server instead).
# XXX: Find a better way to check if dynamodb is running locally
if nmap localhost | egrep "8000.*http-alt"; then
pipenv run pytest tests/
else
printf "Warning: DynamoDB not detected. Running without the tests.\n"
pipenv run pytest tests/ -m "not db"
fi
5 changes: 5 additions & 0 deletions tests/db/dynamodb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from db.dynamodb import DynamoDB
from tests.util import create_test_user
from model.user import User
import pytest


@pytest.mark.db
def test_string_rep():
"""Test string representation of the DynamoDB class."""
assert str(DynamoDB()) == "DynamoDB"


@pytest.mark.db
def test_store_invalid_user():
"""Test handling of invalid user."""
ddb = DynamoDB()
Expand All @@ -17,6 +20,7 @@ def test_store_invalid_user():
assert not success


@pytest.mark.db
def test_store_retrieve_user():
"""Test to see if we can store and retrieve the same user."""
ddb = DynamoDB()
Expand All @@ -31,6 +35,7 @@ def test_store_retrieve_user():
ddb.delete_user('abc_123')


@pytest.mark.db
def test_query_user():
"""Test to see if we can store and query the same user."""
ddb = DynamoDB()
Expand Down
5 changes: 3 additions & 2 deletions tests/db/facade_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from tests.util import create_test_user


def test_string_rep():
@mock.patch('db.dynamodb.DynamoDB', autospec=True)
def test_string_rep(ddb):
"""Test string representation of the DBFacade class."""
assert str(DBFacade()) == "Database Facade"
assert str(DBFacade(ddb)) == "Database Facade"


@mock.patch('db.dynamodb.DynamoDB', autospec=True)
Expand Down

0 comments on commit 8a6ef19

Please sign in to comment.