-
Notifications
You must be signed in to change notification settings - Fork 0
TT-357 Create V2 Activities Azure DAO #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
713559a
feat: TT-357 Change Json Implementation for SQL
ararcos f17ea3c
fix: TT-357 Resolution of comments
ararcos 3b0ed1a
fix: TT-357 Update requirements
ararcos badbce4
Refactor: TT-357 correction of FlakeV8
ararcos f8f633e
fix: TT-357 change of an environment variable to a constant
ararcos dcf14df
refactor: TT-357 Refactor update and create activity
dsgarcia8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: TT-357 Change Json Implementation for SQL
- Loading branch information
commit 713559afa795bd15113905826db35ae64ea02b6f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: '3.9' | ||
services: | ||
database: | ||
image: postgres:14 | ||
ports: | ||
- "5433:5432" | ||
environment: | ||
- POSTGRES_USER=${DB_USER} | ||
- POSTGRES_PASSWORD=${DB_PASS} | ||
- POSTGRES_DB=${DB_NAME} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,23 @@ | ||
import json | ||
import pytest | ||
import shutil | ||
|
||
import time_tracker.activities._domain as domain | ||
import time_tracker.activities._infrastructure as infrastructure | ||
from time_tracker._infrastructure import DB | ||
|
||
@pytest.fixture | ||
def create_temp_activities(tmpdir_factory): | ||
temporary_directory = tmpdir_factory.mktemp("tmp") | ||
json_file = temporary_directory.join("activities.json") | ||
activities = [ | ||
{ | ||
'id': 'c61a4a49-3364-49a3-a7f7-0c5f2d15072b', | ||
'name': 'Development', | ||
'description': 'Development', | ||
'deleted': 'b4327ba6-9f96-49ee-a9ac-3c1edf525172', | ||
'status': 'active', | ||
'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', | ||
}, | ||
{ | ||
'id': '94ec92e2-a500-4700-a9f6-e41eb7b5507c', | ||
'name': 'Management', | ||
'description': 'Description of management', | ||
'deleted': '7cf6efe5-a221-4fe4-b94f-8945127a489a', | ||
'status': 'active', | ||
'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', | ||
}, | ||
{ | ||
'id': 'd45c770a-b1a0-4bd8-a713-22c01a23e41b', | ||
'name': 'Operations', | ||
'description': 'Operation activities performed.', | ||
'deleted': '7cf6efe5-a221-4fe4-b94f-8945127a489a', | ||
'status': 'active', | ||
'tenant_id': 'cc925a5d-9644-4a4f-8d99-0bee49aadd05', | ||
}, | ||
] | ||
@pytest.fixture(name='activity_factory') | ||
def _activity_factory() -> domain.Activity: | ||
def _make_activity(data: dict): | ||
activity = domain.Activity(**data) | ||
return activity | ||
return _make_activity | ||
|
||
with open(json_file, 'w') as outfile: | ||
json.dump(activities, outfile) | ||
@pytest.fixture(name='create_fake_dao') | ||
def _create_fake_dao() -> domain.ActivitiesDao: | ||
db_fake = DB('sqlite:///:memory:') | ||
dao = infrastructure.ActivitiesSQLDao(db_fake) | ||
return dao | ||
|
||
yield activities, json_file | ||
shutil.rmtree(temporary_directory) | ||
@pytest.fixture(name='create_fake_database') | ||
def _create_fake_database() -> domain.ActivitiesDao: | ||
db_fake = DB('sqlite:///:memory:') | ||
return db_fake |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,144 @@ | ||
from time_tracker.activities._application import _activities as activities | ||
import pytest | ||
import json | ||
from faker import Faker | ||
|
||
import azure.functions as func | ||
import json | ||
|
||
|
||
ACTIVITY_URL = "/api/activities/" | ||
|
||
import sqlalchemy | ||
from sqlalchemy.sql.sqltypes import Integer | ||
|
||
import time_tracker.activities._application._activities as azure_activities | ||
import time_tracker.activities._domain as domain | ||
import time_tracker.activities._infrastructure as infrastructure | ||
from time_tracker._infrastructure import DB | ||
|
||
ACTIVITY_URL = '/api/activities/' | ||
|
||
DEMO_DATA = [ | ||
{ | ||
'id': 1, | ||
'name': 'Activity Demo create', | ||
'description': 'test demo create an new activity', | ||
'deleted': None, | ||
'status': None, | ||
}, | ||
{ | ||
'id': 2, | ||
'name': 'Activity Demo create', | ||
'description': 'test demo create an new activity', | ||
'deleted': None, | ||
'status': None, | ||
}, | ||
] | ||
|
||
|
||
@pytest.fixture(name='insert_activity') | ||
def _insert_activity() -> domain.Activity: | ||
def _new_activity(activity: dict, database: DB): | ||
dao = infrastructure.ActivitiesSQLDao(database) | ||
new_activity = dao.create(activity) | ||
return new_activity | ||
return _new_activity | ||
|
||
def test__activity_azure_endpoint__returns_all_activities( | ||
create_temp_activities, | ||
create_fake_database, activity_factory, insert_activity | ||
): | ||
activities_json, tmp_directory = create_temp_activities | ||
activities._get_activities.JSON_PATH = tmp_directory | ||
req = func.HttpRequest(method="GET", body=None, url=ACTIVITY_URL) | ||
fake_database = create_fake_database | ||
existent_activities= [activity_factory(DEMO_DATA[0]), activity_factory(DEMO_DATA[1])] | ||
inserted_activities = [insert_activity(existent_activities[0].__dict__, fake_database).__dict__, insert_activity(existent_activities[1].__dict__, fake_database).__dict__] | ||
|
||
response = activities.get_activities(req) | ||
azure_activities._get_activities.DATABASE = fake_database | ||
req = func.HttpRequest(method='GET', body=None, url=ACTIVITY_URL) | ||
response = azure_activities._get_activities.get_activities(req) | ||
activities_json_data = response.get_body().decode("utf-8") | ||
|
||
ararcos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert response.status_code == 200 | ||
assert activities_json_data == json.dumps(activities_json) | ||
assert activities_json_data == json.dumps(inserted_activities) | ||
|
||
|
||
def test__activity_azure_endpoint__returns_an_activity__when_activity_matches_its_id( | ||
create_temp_activities, | ||
create_fake_database, activity_factory, insert_activity | ||
): | ||
activities_json, tmp_directory = create_temp_activities | ||
activities._get_activities.JSON_PATH = tmp_directory | ||
fake_database = create_fake_database | ||
existent_activity= activity_factory(DEMO_DATA[0]) | ||
inserted_activity = insert_activity(existent_activity.__dict__, fake_database).__dict__ | ||
|
||
azure_activities._get_activities.DATABASE = fake_database | ||
req = func.HttpRequest( | ||
method="GET", | ||
method='GET', | ||
body=None, | ||
url=ACTIVITY_URL, | ||
route_params={"id": activities_json[0]["id"]}, | ||
route_params={"id": inserted_activity["id"]}, | ||
) | ||
|
||
response = activities.get_activities(req) | ||
response = azure_activities._get_activities.get_activities(req) | ||
activitiy_json_data = response.get_body().decode("utf-8") | ||
|
||
assert response.status_code == 200 | ||
assert activitiy_json_data == json.dumps(activities_json[0]) | ||
assert activitiy_json_data == json.dumps(inserted_activity) | ||
|
||
|
||
def test__activity_azure_endpoint__returns_an_activity_with_inactive_status__when_an_activity_matching_its_id_is_found( | ||
create_temp_activities, | ||
): | ||
activities_json, tmp_directory = create_temp_activities | ||
activities._delete_activity.JSON_PATH = tmp_directory | ||
create_fake_database,activity_factory, insert_activity | ||
): | ||
fake_database = create_fake_database | ||
existent_activity= activity_factory(DEMO_DATA[0]) | ||
inserted_activity = insert_activity(existent_activity.__dict__, fake_database).__dict__ | ||
|
||
azure_activities._delete_activity.DATABASE = fake_database | ||
req = func.HttpRequest( | ||
method="DELETE", | ||
method='DELETE', | ||
body=None, | ||
url=ACTIVITY_URL, | ||
route_params={"id": activities_json[0]["id"]}, | ||
route_params={"id": inserted_activity["id"]}, | ||
) | ||
|
||
response = activities.delete_activity(req) | ||
response = azure_activities._delete_activity.delete_activity(req) | ||
activity_json_data = json.loads(response.get_body().decode("utf-8")) | ||
|
||
assert response.status_code == 200 | ||
assert activity_json_data["status"] == "inactive" | ||
assert activity_json_data['status'] == 0 | ||
assert activity_json_data['deleted'] == True | ||
|
||
|
||
def test__update_activity_azure_endpoint__returns_an_activity__when_found_an_activity_to_update( | ||
create_temp_activities, | ||
create_fake_database, activity_factory, insert_activity | ||
): | ||
activities_json, tmp_directory = create_temp_activities | ||
activities._update_activity.JSON_PATH = tmp_directory | ||
activity_data = {"description": Faker().sentence()} | ||
fake_database = create_fake_database | ||
existent_activity= activity_factory(DEMO_DATA[0]) | ||
inserted_activity = insert_activity(existent_activity.__dict__, fake_database).__dict__ | ||
|
||
azure_activities._update_activity.DATABASE = fake_database | ||
activity_body = {"description": Faker().sentence()} | ||
req = func.HttpRequest( | ||
method="PUT", | ||
body=json.dumps(activity_data).encode("utf-8"), | ||
method='PUT', | ||
body=json.dumps(activity_body).encode("utf-8"), | ||
url=ACTIVITY_URL, | ||
route_params={"id": activities_json[0]["id"]}, | ||
route_params={"id": inserted_activity["id"]}, | ||
) | ||
|
||
response = activities.update_activity(req) | ||
response = azure_activities._update_activity.update_activity(req) | ||
activitiy_json_data = response.get_body().decode("utf-8") | ||
new_activity = {**activities_json[0], **activity_data} | ||
inserted_activity.update(activity_body) | ||
|
||
assert response.status_code == 200 | ||
assert activitiy_json_data == json.dumps(new_activity) | ||
assert activitiy_json_data == json.dumps(inserted_activity) | ||
|
||
|
||
def test__activity_azure_endpoint__creates_an_activity__when_activity_has_all_attributes( | ||
create_temp_activities, | ||
): | ||
activities_json, tmp_directory = create_temp_activities | ||
activities._create_activity._JSON_PATH = tmp_directory | ||
|
||
activity_body = { | ||
"id": None, | ||
"name": Faker().user_name(), | ||
"description": Faker().sentence(), | ||
"deleted": Faker().uuid4(), | ||
"status": "active", | ||
"tenant_id": Faker().uuid4(), | ||
} | ||
body = json.dumps(activity_body).encode("utf-8") | ||
req = func.HttpRequest( | ||
method="POST", | ||
body=body, | ||
url=ACTIVITY_URL, | ||
) | ||
|
||
response = activities.create_activity(req) | ||
activitiy_json_data = response.get_body() | ||
assert response.status_code == 201 | ||
assert activitiy_json_data == body | ||
create_fake_database, | ||
): | ||
azure_activities._create_activity.DATABASE = create_fake_database | ||
activity_body = {'id': None, 'name': Faker().user_name(), 'description': Faker().sentence(),'deleted': False ,'status': 1} | ||
body = json.dumps(activity_body).encode("utf-8") | ||
req = func.HttpRequest( | ||
method='POST', | ||
body= body, | ||
url=ACTIVITY_URL, | ||
) | ||
|
||
response = azure_activities._create_activity.create_activity(req) | ||
activitiy_json_data = json.loads(response.get_body()) | ||
activity_body['id'] = activitiy_json_data['id'] | ||
|
||
assert response.status_code == 201 | ||
assert activitiy_json_data == activity_body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
# flake8: noqa | ||
from tests.api.api_fixtures import create_temp_activities | ||
from tests.api.api_fixtures import _activity_factory, _create_fake_dao, _create_fake_database |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.