Skip to content

Commit 0fdd49b

Browse files
committed
refactored tests for the data extraction module and added Makefile
1 parent bbc296c commit 0fdd49b

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Default commit message
2+
COMMIT_MSG ?= "refactored tests for the data extraction module and added Makefile"
3+
4+
# Target to add changes to staging
5+
add:
6+
git add .
7+
8+
# Target to commit with a message
9+
commit: add
10+
git commit -m $(COMMIT_MSG)
11+
12+
# Target to push to the default branch
13+
push: commit
14+
git push
15+
16+
# Run all targets
17+
all: push

main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import argparse
44

5-
from src.extract.extract import run_extract
5+
from src.extract.extract import run_extraction
66

77
if __name__ == "__main__":
88
parser = argparse.ArgumentParser(description="Connect to MongoDB.")
@@ -20,12 +20,12 @@
2020
"--collections",
2121
type=str,
2222
nargs="+",
23-
default=["users"],
24-
help="MongoDB collection names",
23+
default=["category", "events", "listing", "sales", "users", "venue"],
24+
help="Tickit DB collection names",
2525
)
2626
args = parser.parse_args()
2727

28-
run_extract(
28+
run_extraction(
2929
host=args.host,
3030
port=args.port,
3131
database=args.database,

src/extract/extract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_data_from_collection(db: Database, collection_name: str) -> tuple[String
4646
return StringIO(json_data), f"raw-files/{collection_name}.json"
4747

4848

49-
def run_extract(host: str, port: int, database: str, collections: list[str], bucket_name: str):
49+
def run_extraction(host: str, port: int, database: str, collections: list[str], bucket_name: str):
5050
"""Run the Extraction Module Logic"""
5151
db = connect_to_mongodb(host, port, database)
5252
for collection in collections:

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
"""Fixtures for module tests."""
22

3+
from unittest.mock import MagicMock, patch
4+
35
import pytest
6+
from pymongo.collection import Collection
7+
from pymongo.database import Database
48

59

610
@pytest.fixture
711
def mock_connection(mocker):
812
"""Fixture to mock MongoClient."""
913
return mocker.patch("src.extract.extract.MongoClient")
14+
15+
16+
@pytest.fixture
17+
def mock_logger():
18+
with patch("src.extract.extract.logger") as mock:
19+
yield mock
20+
21+
22+
@pytest.fixture
23+
def mock_db():
24+
mock = MagicMock(spec=Database)
25+
mock_collection = MagicMock(spec=Collection)
26+
mock.__getitem__.return_value = mock_collection
27+
return mock, mock_collection

tests/test_extract.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Tests for the extract module."""
22

3+
import json
4+
from io import StringIO
35
from unittest.mock import MagicMock
46

57
import pytest
68
from pymongo.errors import ConnectionFailure
79

8-
from src.extract.extract import connect_to_mongodb
10+
from src.extract.extract import connect_to_mongodb, get_data_from_collection
911

1012

1113
@pytest.mark.unit
@@ -38,3 +40,49 @@ def test_failed_connection(mock_connection):
3840

3941
# Ensure result is None when connection fails
4042
assert result is None
43+
44+
45+
def test_get_data_from_collection_success(mock_db, mock_logger):
46+
mock_db_instance, mock_collection = mock_db
47+
documents = [{"_id": 1, "name": "test1"}, {"_id": 2, "name": "test2"}]
48+
mock_collection.find.return_value = documents
49+
50+
result = get_data_from_collection(mock_db_instance, "test_collection")
51+
52+
mock_db_instance.__getitem__.assert_called_once_with("test_collection")
53+
mock_collection.find.assert_called_once()
54+
assert result is not None
55+
assert isinstance(result[0], StringIO)
56+
assert result[1] == "raw-files/test_collection.json"
57+
expected_data = [{"name": "test1"}, {"name": "test2"}]
58+
assert json.loads(result[0].getvalue()) == expected_data
59+
mock_logger.info.assert_called_with("Extracting data from test_collection")
60+
61+
62+
def test_get_data_from_collection_no_documents(mock_db, mock_logger):
63+
mock_db_instance, mock_collection = mock_db
64+
mock_collection.find.return_value = []
65+
66+
result = get_data_from_collection(mock_db_instance, "test_collection")
67+
68+
mock_db_instance.__getitem__.assert_called_once_with("test_collection")
69+
mock_collection.find.assert_called_once()
70+
assert result is None
71+
mock_logger.error.assert_called_with(f"No documents found in {mock_collection}")
72+
73+
74+
def test_get_data_from_collection_no_id_field(mock_db, mock_logger):
75+
mock_db_instance, mock_collection = mock_db
76+
documents = [{"name": "test1"}, {"name": "test2"}]
77+
mock_collection.find.return_value = documents
78+
79+
result = get_data_from_collection(mock_db_instance, "test_collection")
80+
81+
mock_db_instance.__getitem__.assert_called_once_with("test_collection")
82+
mock_collection.find.assert_called_once()
83+
assert result is not None
84+
assert isinstance(result[0], StringIO)
85+
assert result[1] == "raw-files/test_collection.json"
86+
expected_data = [{"name": "test1"}, {"name": "test2"}]
87+
assert json.loads(result[0].getvalue()) == expected_data
88+
mock_logger.info.assert_called_with("Extracting data from test_collection")

0 commit comments

Comments
 (0)