|
| 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