From ac0daf5b967b241eec7c28528e9853a0fb1bd8b3 Mon Sep 17 00:00:00 2001 From: Stewart Henderson Date: Wed, 18 Dec 2019 15:31:12 +0000 Subject: [PATCH] Initial test coverage --- src/shared/tests/conftest.py | 30 ++++++++ src/shared/tests/unit/test_db.py | 122 +++++++++++++++++++++++++++++++ src/test_requirements.txt | 3 +- tox.ini | 2 +- 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 src/shared/tests/conftest.py create mode 100644 src/shared/tests/unit/test_db.py diff --git a/src/shared/tests/conftest.py b/src/shared/tests/conftest.py new file mode 100644 index 0000000..0d19d45 --- /dev/null +++ b/src/shared/tests/conftest.py @@ -0,0 +1,30 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +import os +import sys +import signal +import subprocess +import uuid +import logging +import json +import psutil +import pytest +import stripe + +from unittest.mock import Mock, MagicMock, PropertyMock + +from sub.shared.cfg import CFG +from shared.log import get_logger +from shared.dynamodb import dynamodb + + +logger = get_logger() + +THIS_PATH = os.path.join(os.path.realpath(os.path.dirname(__file__))) + + +def pytest_configure(): + os.environ["AWS_ACCESS_KEY_ID"] = "fake" + os.environ["AWS_SECRET_ACCESS_KEY"] = "fake" diff --git a/src/shared/tests/unit/test_db.py b/src/shared/tests/unit/test_db.py new file mode 100644 index 0000000..96b7735 --- /dev/null +++ b/src/shared/tests/unit/test_db.py @@ -0,0 +1,122 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +import random +import string + +from shared.db import ( + HubEvent, + SubHubDeletedAccount, + SubHubAccount, + _create_account_model, + _create_deleted_account_model, + _create_hub_model, +) + +from shared.dynamodb import dynamodb +from past.builtins import xrange + +#################### +# SubHubAccountModel +# SubHubAccount +#################### + + +def generate_random_table(): + return "".join( + [random.choice(string.ascii_letters + string.digits) for n in xrange(32)] + ) + + +def test_create_account_model(dynamodb): + model = _create_account_model(generate_random_table(), "local", dynamodb) + assert model is not None + + +def test_new_user_account_model(dynamodb): + subhub_account = SubHubAccount(generate_random_table(), "local", dynamodb) + customer_model = subhub_account.new_user( + "uid", "origin_system", "customer_identifier" + ) + assert customer_model is not None + +###################################################### +# TODO(med): These tests rely on the container +# being clean which isn't guaranteed right not. These +# merely serve as a placeholder for future tests that +# need to be written. +###################################################### + +# def test_get_user_account_model(dynamodb): +# subhub_account = SubHubAccount(generate_random_table(), "local", dynamodb) +# customer_model = subhub_account.new_user( +# "uid", "origin_system", "customer_identifier" +# ) +# user_model = subhub_account.get_user("uid") +# assert user_model is not None + + +# def test_append_customer_id_account_model(dynamodb): +# subhub_account = SubHubAccount(generate_random_table(), "local", dynamodb) +# customer_model = subhub_account.new_user( +# "uid", "origin_system", "customer_identifier" +# ) +# user_model = subhub_account.append_custid("uid", "customer_identifier2") +# assert subhub_account.get_user("uid").cust_id == "customer_identifier2" + + +# def test_remove_from_db_account_model(dynamodb): +# subhub_account = SubHubAccount(generate_random_table(), "local", dynamodb) +# customer_model = subhub_account.new_user( +# "uid", "origin_system", "customer_identifier" +# ) +# user_model = subhub_account.append_custid("uid", "customer_identifier2") +# subhub_account.remove_from_db("uid") +# assert subhub_account.get_user("uid") is None + + +# def test_mark_deleted_account_model(dynamodb): +# subhub_account = SubHubAccount(generate_random_table(), "local", dynamodb) +# customer_model = subhub_account.new_user( +# "uid", "origin_system", "customer_identifier" +# ) +# subhub_account.mark_deleted("uid") +# user_model = subhub_account.get_user("uid") +# assert subhub_account.customer_status == "deleted" + + +#################### +# HubEventModel +#################### + + +def test_create_create_hub_model(dynamodb): + model = _create_hub_model(generate_random_table(), "local", dynamodb) + assert model is not None + + +def test_append_event_hub_event(dynamodb): + pass + + +def test_remove_from_db_hub_event(dynamodb): + pass + + +#################### +# SubHubDeletedAccountModel +#################### + + +def test_create_deleted_account_model(dynamodb): + model = _create_deleted_account_model(generate_random_table(), "local", dynamodb) + assert model is not None + + +def test_new_deleted_user_created(dynamodb): + subhub_deleted_account_model = SubHubDeletedAccount( + generate_random_table(), "local", dynamodb + ) + user_model = subhub_deleted_account_model.new_user("uid", "origin_system", None) + assert user_model is not None diff --git a/src/test_requirements.txt b/src/test_requirements.txt index 9cc7042..d58c7ea 100644 --- a/src/test_requirements.txt +++ b/src/test_requirements.txt @@ -1,5 +1,6 @@ backoff==1.8.0 docker==4.1.0 +future==0.18.2 jsoncompare==0.1.2 locustio==0.11.0 mock==3.0.5 @@ -16,4 +17,4 @@ pytest-cov==2.7.1 pytest-mock==1.10.4 pytest-watch==4.2.0 requests==2.22.0 -responses==0.10.6 +responses==0.10.6 \ No newline at end of file diff --git a/tox.ini b/tox.ini index 7e9fcad..33af550 100644 --- a/tox.ini +++ b/tox.ini @@ -49,4 +49,4 @@ commands = py.test --cov-config={toxinidir}/.coveragerc --cov-report term-missing --cov-append --cov-branch --no-cov-on-fail --cov=src/hub -k src/hub --ignore=src/hub/shared py.test --cov-config={toxinidir}/.coveragerc --cov-report term-missing --cov-append --cov-branch --no-cov-on-fail --cov=src/shared -k src/shared [pytest] -norecursedirs = docs *.egg-info .git appdir .tox .venv env services +norecursedirs = docs *.egg-info .git appdir .tox .venv env services *.eggs