Skip to content

Commit aaa34b4

Browse files
committed
Proof-of-concept for mock ddb fixtures
1 parent 4d93b85 commit aaa34b4

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

backend/recipe_routes_test.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
from backend.models import Recipe, Step
2-
from backend.recipe_routes import RecipeApi, RecipeCollectionApi
3-
from datetime import datetime, timezone
4-
from dateutil import parser as dateutil_parser
5-
from flask_restful import reqparse
6-
import json
1+
# from backend.models import Recipe, Step
2+
# from backend.recipe_routes import RecipeApi, RecipeCollectionApi
3+
# from datetime import datetime, timezone
4+
# from dateutil import parser as dateutil_parser
5+
# from flask_restful import reqparse
6+
# import json
77

88

99
class TestRecipeCollectionApi:
10-
def test_get(self):
10+
def test_get(self, mock_recipe):
1111
# TODO: Write unit tests for recipe routes
1212
pass
1313

14-
def test_post(self):
14+
def test_post(self, mock_recipe):
15+
# example = mock_recipe(name="my-mock-recipe1", difficulty="Intermediate")
16+
# example.save()
17+
# assert mock_recipe.count() == 1
1518
pass
1619

1720

18-
class TestRecipeApi:
19-
def test_get(self):
20-
pass
21-
22-
def test_put(self):
23-
pass
24-
25-
def test_delete(self):
26-
pass
21+
# class TestRecipeApi:
22+
# def test_get(self):
23+
# pass
24+
#
25+
# def test_put(self):
26+
# pass
27+
#
28+
# def test_delete(self):
29+
# pass

conftest.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Config file for extending pytest functionality to packages w/o native support."""
22
from pytest import fixture
3+
from moto import mock_dynamodb
34
from os import environ
45

56

@@ -12,17 +13,54 @@ def mock_logs():
1213
yield capture
1314

1415

15-
@fixture(scope='function')
16-
def aws_credentials():
16+
@fixture(scope='session')
17+
def mock_aws_credentials():
1718
"""Mocked AWS Credentials for moto."""
1819
environ['AWS_ACCESS_KEY_ID'] = 'testing'
1920
environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
2021
environ['AWS_SECURITY_TOKEN'] = 'testing'
2122
environ['AWS_SESSION_TOKEN'] = 'testing'
22-
environ['AWS_REGION'] = 'us-west-1'
23+
# environ['AWS_REGION'] = 'us-west-1'
2324

2425

25-
@fixture(scope='function')
26-
def dynamodb(aws_credentials):
26+
@fixture(scope='class')
27+
def mock_recipe(mock_aws_credentials):
28+
"""Fixture for mocking a local dynamodb instance w/test credentials."""
29+
with mock_dynamodb():
30+
# TODO: Figure out how to set test-config params for the pynamodb models
31+
from backend.models import Recipe
32+
33+
# Recipe.Meta.host = 'http://localhost:8009'
34+
Recipe.Meta.table_name = 'MockRecipe'
35+
Recipe.Meta.aws_access_key_id = 'testing2'
36+
Recipe.Meta.aws_secret_access_key = 'testing2'
37+
Recipe.Meta.aws_session_token = 'testing2'
38+
Recipe.Meta.aws_security_token = 'testing2'
39+
40+
Recipe.create_table(read_capacity_units=5, write_capacity_units=5)
41+
print(f"new recipe count ======> {Recipe.count()}")
42+
43+
yield Recipe
44+
45+
# Teardown
46+
Recipe.delete_table()
47+
48+
49+
@fixture(scope='class')
50+
def mock_replacement(mock_aws_credentials):
2751
"""Fixture for mocking a local dynamodb instance w/test credentials."""
28-
pass
52+
with mock_dynamodb():
53+
from backend.models import Replacement
54+
55+
# Replacement.Meta.host = 'http://localhost:8009'
56+
Replacement.Meta.table_name = 'MockReplacement'
57+
Replacement.Meta.aws_access_key_id = 'testing3'
58+
Replacement.Meta.aws_secret_access_key = 'testing3'
59+
Replacement.Meta.aws_session_token = 'testing3'
60+
Replacement.Meta.aws_security_token = 'testing3'
61+
Replacement.create_table(read_capacity_units=5, write_capacity_units=5)
62+
63+
yield Replacement
64+
65+
# Teardown
66+
Replacement.delete_table()

0 commit comments

Comments
 (0)