Upload firebase_test.py
Browse files- tests/firebase_test.py +96 -0
tests/firebase_test.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
import sys
|
3 |
+
from pathlib import Path
|
4 |
+
from unittest.mock import patch, MagicMock
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
sys.path.append(str(Path(__file__).resolve().parent.parent))
|
8 |
+
from src.configs.database.firebase import (
|
9 |
+
read_all_users,
|
10 |
+
write_user_data,
|
11 |
+
update_user_data,
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
# Mock data for tests
|
16 |
+
MOCK_USER_DATA = {
|
17 |
+
"uuid7-1": {
|
18 |
+
"id": "uuid7-1",
|
19 |
+
"name": "Alice",
|
20 |
+
"score": 90,
|
21 |
+
"interview_question": "What are your strengths?",
|
22 |
+
"job_requirements": "Team player",
|
23 |
+
"feedback": "Excellent response.",
|
24 |
+
"created_at": "2024-01-01T10:00:00",
|
25 |
+
"updated_at": "2024-01-01T10:00:00",
|
26 |
+
},
|
27 |
+
"uuid7-2": {
|
28 |
+
"id": "uuid7-2",
|
29 |
+
"name": "Bob",
|
30 |
+
"score": 85,
|
31 |
+
"interview_question": "Describe your work ethic.",
|
32 |
+
"job_requirements": "Self-starter",
|
33 |
+
"feedback": "Good examples provided.",
|
34 |
+
"created_at": "2024-01-02T10:00:00",
|
35 |
+
"updated_at": "2024-01-02T10:00:00",
|
36 |
+
},
|
37 |
+
}
|
38 |
+
|
39 |
+
|
40 |
+
@patch("src.configs.database.firebase.users_ref") # Mock Firebase reference
|
41 |
+
def test_write_user_data(mock_users_ref):
|
42 |
+
mock_users_ref.child.return_value.set = MagicMock()
|
43 |
+
|
44 |
+
name = "Charlie"
|
45 |
+
score = 88
|
46 |
+
interview_question = "How do you handle challenges?"
|
47 |
+
job_requirements = "Problem solver"
|
48 |
+
feedback = "Well-articulated response."
|
49 |
+
|
50 |
+
entry_id = write_user_data(
|
51 |
+
name, score, interview_question, job_requirements, feedback
|
52 |
+
)
|
53 |
+
|
54 |
+
# Check that Firebase `set` was called with correct data
|
55 |
+
mock_users_ref.child.assert_called_with(entry_id)
|
56 |
+
mock_users_ref.child(entry_id).set.assert_called_once()
|
57 |
+
|
58 |
+
|
59 |
+
@patch("src.configs.database.firebase.users_ref")
|
60 |
+
def test_read_all_users(mock_users_ref):
|
61 |
+
# Mock the Firebase `get` method
|
62 |
+
mock_users_ref.get.return_value = MOCK_USER_DATA
|
63 |
+
|
64 |
+
df = read_all_users()
|
65 |
+
|
66 |
+
assert isinstance(df, pd.DataFrame)
|
67 |
+
assert not df.empty
|
68 |
+
assert "name" in df.columns
|
69 |
+
|
70 |
+
|
71 |
+
@patch("src.configs.database.firebase.users_ref")
|
72 |
+
def test_update_user_data(mock_users_ref):
|
73 |
+
mock_users_ref.child.return_value.get.return_value = MOCK_USER_DATA["uuid7-1"]
|
74 |
+
mock_users_ref.child.return_value.update = MagicMock()
|
75 |
+
|
76 |
+
uuid = "uuid7-1"
|
77 |
+
update_dict = {"score": 95, "feedback": "Updated feedback."}
|
78 |
+
|
79 |
+
result = update_user_data(uuid, update_dict)
|
80 |
+
|
81 |
+
assert result is True
|
82 |
+
mock_users_ref.child.assert_called_with(uuid)
|
83 |
+
mock_users_ref.child(uuid).update.assert_called_once_with(update_dict)
|
84 |
+
|
85 |
+
|
86 |
+
@patch("src.configs.database.firebase.users_ref")
|
87 |
+
def test_update_user_data_no_record(mock_users_ref):
|
88 |
+
mock_users_ref.child.return_value.get.return_value = None
|
89 |
+
|
90 |
+
uuid = "non-existent-uuid"
|
91 |
+
update_dict = {"score": 95, "feedback": "Updated feedback."}
|
92 |
+
|
93 |
+
result = update_user_data(uuid, update_dict)
|
94 |
+
|
95 |
+
assert result is False
|
96 |
+
mock_users_ref.child.assert_called_with(uuid)
|