Skip to content

Commit c9ae125

Browse files
committed
refactor: TT-180 changes in activity test
1 parent c56d070 commit c9ae125

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime, timedelta
22

33
import jwt
4+
import base64
45
import pytest
56
from faker import Faker
67
from flask import Flask
@@ -14,6 +15,9 @@
1415
from time_tracker_api.time_entries.time_entries_repository import (
1516
TimeEntryCosmosDBRepository,
1617
)
18+
from time_tracker_api.activities.activities_model import (
19+
ActivityCosmosDBRepository,
20+
)
1721

1822
fake = Faker()
1923
Faker.seed()
@@ -222,6 +226,26 @@ def running_time_entry(
222226
)
223227

224228

229+
@pytest.fixture(scope="module")
230+
def activity_repository(app: Flask) -> ActivityCosmosDBRepository:
231+
with app.app_context():
232+
from commons.data_access_layer.cosmos_db import init_app, cosmos_helper
233+
234+
if cosmos_helper is None:
235+
init_app(app)
236+
237+
return ActivityCosmosDBRepository()
238+
239+
240+
@pytest.fixture
241+
def activities_dao():
242+
from time_tracker_api.activities.activities_model import (
243+
create_dao,
244+
)
245+
246+
return create_dao()
247+
248+
225249
@pytest.fixture(scope="session")
226250
def valid_jwt(app: Flask, tenant_id: str, owner_id: str) -> str:
227251
with app.app_context():
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from unittest.mock import Mock, patch
2+
import pytest
3+
from faker import Faker
4+
from typing import List
5+
6+
from commons.data_access_layer.database import EventContext
7+
from time_tracker_api.activities.activities_model import (
8+
ActivityCosmosDBRepository,
9+
ActivityCosmosDBDao,
10+
ActivityCosmosDBModel,
11+
)
12+
13+
14+
def create_activity(
15+
valid_tenant_id: str,
16+
event_context: EventContext,
17+
activity_repository: ActivityCosmosDBRepository,
18+
) -> ActivityCosmosDBModel:
19+
data = {
20+
"id": Faker().uuid4(),
21+
"name": Faker().name(),
22+
"description": Faker().paragraph(nb_sentences=2),
23+
"deleted": Faker().uuid4(),
24+
"tenant_id": valid_tenant_id,
25+
}
26+
27+
created_item = activity_repository.create(
28+
data, event_context, mapper=ActivityCosmosDBModel
29+
)
30+
return created_item
31+
32+
33+
@pytest.mark.parametrize(
34+
"id_list , exception",
35+
[
36+
(123, AssertionError),
37+
((1, 2), AssertionError),
38+
("id_list", AssertionError),
39+
({"id_list": []}, AssertionError),
40+
],
41+
)
42+
def test_create_sql_in_condition_with_invalid_list_should_fail(
43+
id_list, exception, activity_repository: ActivityCosmosDBRepository
44+
):
45+
try:
46+
activity_repository.create_sql_in_condition(id_list)
47+
except Exception as e:
48+
assert type(e) is exception
49+
50+
51+
# valid_id = ["03741215-a9b0-4a22-93cc-9cd6571e0366",
52+
# "d45c770a-b1a0-4bd8-a713-22c01a23e41b",
53+
# "57be59a6-1c01-449c-b3cb-17cd4113c6d2",
54+
# "1263af28-6a06-43b2-b27f-7e2bd0edc58e",
55+
# "a2cb8294-56ae-4fb8-aa7d-9a8056658f9e",
56+
# "320716ae-eb1b-441f-8a83-1c9d006bff92"]
57+
58+
59+
# @pytest.mark.parametrize(
60+
# 'activity_id', valid_id
61+
# )
62+
# def test_find_all_with_id_in_list_should_success(
63+
# event_context: EventContext,
64+
# activity_id: str,
65+
# activity_repository: ActivityCosmosDBRepository,
66+
# ):
67+
# id_list = [activity_id]
68+
# result = activity_repository.find_all_with_id_in_list(
69+
# event_context, id_list)
70+
# assert len(result) > 0
71+
72+
73+
@patch(
74+
'time_tracker_api.activities.activities_model.ActivityCosmosDBRepository.find_all_with_id_in_list',
75+
new_callable=Mock,
76+
)
77+
def test_test_find_all_width_id_in_list(
78+
find_all_with_id_in_list_mock,
79+
event_context: EventContext,
80+
activity_repository: ActivityCosmosDBRepository,
81+
):
82+
83+
activity_repository.find_all_with_id_in_list(event_context, ["123456"])
84+
find_all_with_id_in_list_mock.assert_called_once()
85+
86+
87+
invalid_id = [
88+
"4ff7b319-a3f1-4a99-a0e0-649e3d3e6c65",
89+
"3ee1aef6-079e-4dcd-abe6-bee6143c9473",
90+
"0beb1210-91fa-40f3-ac1b-bac92bc31623",
91+
"0556c46b-8a9b-48c2-b198-137c8afab7de",
92+
]
93+
94+
95+
# @pytest.mark.parametrize(
96+
# "activity_id", invalid_id
97+
# )
98+
# def test_find_all_with_id_in_list_should_not_find(
99+
# event_context: EventContext,
100+
# activity_id: str,
101+
# activity_repository: ActivityCosmosDBRepository
102+
# ):
103+
# id_list = [activity_id]
104+
# result = activity_repository.find_all_with_id_in_list(
105+
# event_context, id_list)
106+
# assert len(result) == 0

0 commit comments

Comments
 (0)