Skip to content

Commit c0e06d6

Browse files
jcs-instructornitsanavni4dsherwoodkevinmaesmander11
committed
. t Create fizzbuzz function with db injected
Co-Authored-By: Nitsan Avni <nitsanav@gmail.com> Co-Authored-By: Joel Silberman <42779942+jcs-instructor@users.noreply.github.com> Co-Authored-By: 4dsherwood <4dsherwood@users.noreply.github.com> Co-Authored-By: Kevin Maes <kevin@kevinmaes.com> Co-Authored-By: Matt <matthew.anderson11@gmail.com>
1 parent 1618ee4 commit c0e06d6

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

katas/fizzbuzz/test_fizzbuzz.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import mongomock
22
import pytest
3+
from typing import Callable
34

45
@pytest.fixture
56
def mock_mongo_client():
67
return mongomock.MongoClient()
78

9+
@pytest.fixture
10+
def fizzbuzz_db(mock_mongo_client):
11+
db = mock_mongo_client.mydatabase
12+
rules = db.fizzbuzz_rules
13+
rules.insert_many(
14+
[
15+
{"word": "Fizz", "divisor": 3},
16+
{"word": "Buzz", "divisor": 5},
17+
]
18+
)
19+
return db
20+
821
def test_example_using_mock_db(mock_mongo_client):
922
db = mock_mongo_client.mydatabase
1023
collection = db.ourcollection
@@ -14,14 +27,34 @@ def test_example_using_mock_db(mock_mongo_client):
1427

1528
assert result["age"] == 30
1629

30+
1731
def test_have_fizz_buzz_specs_in_DB(mock_mongo_client):
1832
db = mock_mongo_client.mydatabase
19-
collection = db.ourcollection
33+
rules = db.fizzbuzz_rules
34+
rules.insert_many(
35+
[
36+
{"word": "Fizz", "divisor": 3},
37+
{"word": "Buzz", "divisor": 5},
38+
]
39+
)
40+
result = rules.find_one({"word": "Fizz"})
41+
42+
assert result["divisor"] == 3
43+
44+
def test_fizz_divisor_is_3(fizzbuzz_db):
45+
assert fizzbuzz_db.fizzbuzz_rules.find_one({"word": "Fizz"})["divisor"] == 3
46+
47+
48+
def test_buzz_divisor_is_5(fizzbuzz_db):
49+
assert fizzbuzz_db.fizzbuzz_rules.find_one({"word": "Buzz"})["divisor"] == 5
50+
51+
FizzbuzzFn = Callable[[int], str]
2052

21-
# input: 9
22-
# output: fizz
23-
# denominator: 3
24-
collection.insert_one({"word": "Fizz", "denominator": 3})
25-
result = collection.find_one({"word": "Fizz"})
53+
def make_fizzbuzz(db) -> FizzbuzzFn:
54+
def fizzbuzz_fn(number):
55+
return "Fizz"
56+
return fizzbuzz_fn
2657

27-
assert result["denominator"] == 3
58+
def test_fizzbuzz(fizzbuzz_db):
59+
fizzbuzz_fn = make_fizzbuzz(fizzbuzz_db)
60+
assert "Fizz" == fizzbuzz_fn(3)

session-notes/session-notes-2024-04-18.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ Nitsan
101101

102102
### Final Retro
103103

104+
Nitsan
105+
- Now see a clear path, how we can TDD multiple paths
106+
- Interesting: how does the mock mongo relate to using an actual mongo connection?
107+
- Could do end-to-end testing with a real database, not just mock
108+
109+
Joel
110+
- Like how things are unfolding
111+
- Found it a bit confusing - injecting something to a function in python; putting the production code in the test code itself
112+
- A better name for `ret` (maybe `fizzbuzz_factory` -> returing a `fizzbuzz_function`)
113+
104114
### How we spent our time today:
105115
- Time-Archivist will record the total time for each section of today's session:
106116
- __ minutes coding

0 commit comments

Comments
 (0)