Skip to content

Commit

Permalink
Proof-of-concept for mock ddb fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
brystmar committed Sep 29, 2020
1 parent 4d93b85 commit aaa34b4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
37 changes: 20 additions & 17 deletions backend/recipe_routes_test.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
from backend.models import Recipe, Step
from backend.recipe_routes import RecipeApi, RecipeCollectionApi
from datetime import datetime, timezone
from dateutil import parser as dateutil_parser
from flask_restful import reqparse
import json
# from backend.models import Recipe, Step
# from backend.recipe_routes import RecipeApi, RecipeCollectionApi
# from datetime import datetime, timezone
# from dateutil import parser as dateutil_parser
# from flask_restful import reqparse
# import json


class TestRecipeCollectionApi:
def test_get(self):
def test_get(self, mock_recipe):
# TODO: Write unit tests for recipe routes
pass

def test_post(self):
def test_post(self, mock_recipe):
# example = mock_recipe(name="my-mock-recipe1", difficulty="Intermediate")
# example.save()
# assert mock_recipe.count() == 1
pass


class TestRecipeApi:
def test_get(self):
pass

def test_put(self):
pass

def test_delete(self):
pass
# class TestRecipeApi:
# def test_get(self):
# pass
#
# def test_put(self):
# pass
#
# def test_delete(self):
# pass
50 changes: 44 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Config file for extending pytest functionality to packages w/o native support."""
from pytest import fixture
from moto import mock_dynamodb
from os import environ


Expand All @@ -12,17 +13,54 @@ def mock_logs():
yield capture


@fixture(scope='function')
def aws_credentials():
@fixture(scope='session')
def mock_aws_credentials():
"""Mocked AWS Credentials for moto."""
environ['AWS_ACCESS_KEY_ID'] = 'testing'
environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
environ['AWS_SECURITY_TOKEN'] = 'testing'
environ['AWS_SESSION_TOKEN'] = 'testing'
environ['AWS_REGION'] = 'us-west-1'
# environ['AWS_REGION'] = 'us-west-1'


@fixture(scope='function')
def dynamodb(aws_credentials):
@fixture(scope='class')
def mock_recipe(mock_aws_credentials):
"""Fixture for mocking a local dynamodb instance w/test credentials."""
with mock_dynamodb():
# TODO: Figure out how to set test-config params for the pynamodb models
from backend.models import Recipe

# Recipe.Meta.host = 'http://localhost:8009'
Recipe.Meta.table_name = 'MockRecipe'
Recipe.Meta.aws_access_key_id = 'testing2'
Recipe.Meta.aws_secret_access_key = 'testing2'
Recipe.Meta.aws_session_token = 'testing2'
Recipe.Meta.aws_security_token = 'testing2'

Recipe.create_table(read_capacity_units=5, write_capacity_units=5)
print(f"new recipe count ======> {Recipe.count()}")

yield Recipe

# Teardown
Recipe.delete_table()


@fixture(scope='class')
def mock_replacement(mock_aws_credentials):
"""Fixture for mocking a local dynamodb instance w/test credentials."""
pass
with mock_dynamodb():
from backend.models import Replacement

# Replacement.Meta.host = 'http://localhost:8009'
Replacement.Meta.table_name = 'MockReplacement'
Replacement.Meta.aws_access_key_id = 'testing3'
Replacement.Meta.aws_secret_access_key = 'testing3'
Replacement.Meta.aws_session_token = 'testing3'
Replacement.Meta.aws_security_token = 'testing3'
Replacement.create_table(read_capacity_units=5, write_capacity_units=5)

yield Replacement

# Teardown
Replacement.delete_table()

0 comments on commit aaa34b4

Please sign in to comment.