Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: TT-418 renamed tests and problems solved
  • Loading branch information
mandres2015 committed Nov 26, 2021
commit 68240137f3335fcbaf5a0ee48daa8ebbc3c1e82e
21 changes: 10 additions & 11 deletions V2/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ service: azure-time-tracker

frameworkVersion: "2"

#region Config
#region start Config

provider:
name: azure
Expand Down Expand Up @@ -38,13 +38,13 @@ package:
- "!.gitignore"
- "!.git/**"

#endregion Fin Config
#endregion end Config

#region Functions
#region start Functions

functions:

#region Activities
#region start Activities

get_activities:
handler: time_tracker/activities/interface.get_activities
Expand Down Expand Up @@ -86,9 +86,9 @@ functions:
route: activities/
authLevel: anonymous

#endregion fin Activities
#endregion end Activities

#region Time-Entries
#region start Time-Entries

create_time_entry:
handler: time_tracker/time_entries/interface.create_time_entry
Expand All @@ -110,9 +110,9 @@ functions:
route: time-entries/{id}
authLevel: anonymous

#endregion Fin TimeEntries
#endregion end Time-Entries

#region Customer
#region start Customer

create_customer:
handler: time_tracker/customers/interface.create_customer
Expand Down Expand Up @@ -154,7 +154,6 @@ functions:
route: customers/{id}
authLevel: anonymous

#endregion Fin Customer
#endregion end Customer

#endregion Fin Funciones

#endregion end Functions
69 changes: 60 additions & 9 deletions V2/tests/api/azure/customer_azure_endpoints_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from http import HTTPStatus
import json
from faker import Faker

Expand All @@ -24,7 +25,7 @@ def test__create_customer_azure_endpoint__creates_a_customer__when_customer_has_
customer_json_data = json.loads(response.get_body())
customer_body['id'] = customer_json_data['id']

assert response.status_code == 201
assert response.status_code == HTTPStatus.CREATED
assert customer_json_data == customer_body


Expand All @@ -45,7 +46,7 @@ def test__create_customer_azure_endpoint__returns_a_status_400__when_dont_reciev

response = azure_customers._create_customer.create_customer(req)

assert response.status_code == 400
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'


Expand All @@ -65,10 +66,24 @@ def test__delete_customer_azure_endpoint__returns_a_customer_with_true_deleted__
response = azure_customers._delete_customer.delete_customer(req)
customer_json_data = json.loads(response.get_body().decode("utf-8"))

assert response.status_code == 200
assert response.status_code == HTTPStatus.OK
assert customer_json_data['deleted'] is True


def test__delete_customer_azure_endpoint__returns_not_found__when_its_id_is_not_found():
req = func.HttpRequest(
method='DELETE',
body=None,
url=CUSTOMER_URL,
route_params={"id": Faker().pyint()},
)

response = azure_customers._delete_customer.delete_customer(req)

assert response.status_code == HTTPStatus.NOT_FOUND
assert response.get_body() == b'Not found'


def test__update_customer_azure_endpoint__returns_an_updated_customer__when_customer_has_all_necesary_attributes(
test_db, customer_factory, insert_customer
):
Expand All @@ -88,7 +103,7 @@ def test__update_customer_azure_endpoint__returns_an_updated_customer__when_cust
response = azure_customers._update_customer.update_customer(req)
customer_json_data = json.loads(response.get_body())

assert response.status_code == 200
assert response.status_code == HTTPStatus.OK
assert customer_json_data == inserted_customer


Expand All @@ -107,8 +122,30 @@ def test__update_customer_azure_endpoint__returns_update_a_customer__when_custom

response = azure_customers._update_customer.update_customer(req)

assert response.status_code == 409
assert response.get_body() == b'This customer does not exist'
assert response.status_code == HTTPStatus.CONFLICT
assert response.get_body() == b'This customer does not exist or is duplicated'


def test__update_customer_azure_endpoint__returns_invalid_format__when_customer_doesnt_have_all_necesary_attributes(
customer_factory, insert_customer, test_db
):
existent_customer = customer_factory()
inserted_customer = insert_customer(existent_customer, test_db).__dict__

inserted_customer.pop("name")

body = json.dumps(inserted_customer).encode("utf-8")
req = func.HttpRequest(
method='PUT',
body=body,
url=CUSTOMER_URL,
route_params={"id": inserted_customer["id"]},
)

response = azure_customers._update_customer.update_customer(req)

assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_body() == b'Invalid format or structure of the attributes of the customer'


def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_customer_recive_invalid_id(
Expand All @@ -122,7 +159,7 @@ def test__delete_customers_azure_endpoint__returns_a_status_code_400__when_custo

response = azure_customers._delete_customer.delete_customer(req)

assert response.status_code == 400
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_body() == b'Invalid Format ID'


Expand All @@ -138,7 +175,7 @@ def test__customers_azure_endpoint__returns_all_customers(
customers_json_data = response.get_body().decode("utf-8")
customer_list = json.loads(customers_json_data)

assert response.status_code == 200
assert response.status_code == HTTPStatus.OK
assert customers_json_data <= json.dumps(inserted_customer)
assert customer_list.pop() == inserted_customer

Expand All @@ -159,5 +196,19 @@ def test__customer_azure_endpoint__returns_a_customer__when_customer_matches_its
response = azure_customers._get_customers.get_customers(req)
customer_json_data = response.get_body().decode("utf-8")

assert response.status_code == 200
assert response.status_code == HTTPStatus.OK
assert customer_json_data == json.dumps(inserted_customer)


def test__customer_azure_endpoint__returns_invalid_id__when_customer_not_matches_its_id():
req = func.HttpRequest(
method='GET',
body=None,
url=CUSTOMER_URL,
route_params={"id": "Invalid ID"},
)

response = azure_customers._get_customers.get_customers(req)

assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.get_body() == b'The id has an invalid format'
3 changes: 2 additions & 1 deletion V2/tests/integration/daos/customers_dao_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import typing

import pytest
from faker import Faker

import time_tracker.customers._domain as domain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import json
import typing
from http import HTTPStatus

import azure.functions as func

Expand Down Expand Up @@ -32,10 +33,10 @@ def create_customer(req: func.HttpRequest) -> func.HttpResponse:

if created_customer:
body = json.dumps(created_customer.__dict__)
status_code = 201
status_code = HTTPStatus.CREATED
else:
body = b'This customer already exists'
status_code = 409
status_code = HTTPStatus.CONFLICT

return func.HttpResponse(
body=body,
Expand All @@ -45,7 +46,7 @@ def create_customer(req: func.HttpRequest) -> func.HttpResponse:
except ValueError:
return func.HttpResponse(
body=b'Invalid format or structure of the attributes of the customer',
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
mimetype="application/json"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from http import HTTPStatus

import azure.functions as func

Expand All @@ -20,19 +21,19 @@ def delete_customer(req: func.HttpRequest) -> func.HttpResponse:
if not deleted_customer:
return func.HttpResponse(
body="Not found",
status_code=404,
status_code=HTTPStatus.NOT_FOUND,
mimetype=DATATYPE
)

return func.HttpResponse(
body=json.dumps(deleted_customer.__dict__, default=str),
status_code=200,
status_code=HTTPStatus.OK,
mimetype=DATATYPE,
)

except ValueError:
return func.HttpResponse(
body=b"Invalid Format ID",
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
mimetype=DATATYPE
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from http import HTTPStatus
import json

import azure.functions as func
Expand All @@ -9,13 +10,13 @@

def get_customers(req: func.HttpRequest) -> func.HttpResponse:
customer_id = req.route_params.get('id')
status_code = 200
status_code = HTTPStatus.OK

try:
if customer_id:
response = _get_by_id(int(customer_id))
if response == b'This customer does not exist':
status_code = 404
status_code = HTTPStatus.NOT_FOUND
else:
response = _get_all()

Expand All @@ -24,7 +25,7 @@ def get_customers(req: func.HttpRequest) -> func.HttpResponse:
)
except ValueError:
return func.HttpResponse(
body=b"The id has an invalid format", status_code=400, mimetype="application/json"
body=b"The id has an invalid format", status_code=HTTPStatus.BAD_REQUEST, mimetype="application/json"
)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import json
import typing
from http import HTTPStatus

import azure.functions as func

Expand Down Expand Up @@ -29,10 +30,10 @@ def update_customer(req: func.HttpRequest) -> func.HttpResponse:

if updated_customer:
body = json.dumps(updated_customer.__dict__)
status_code = 200
status_code = HTTPStatus.OK
else:
body = b'This customer does not exist'
status_code = 409
body = b'This customer does not exist or is duplicated'
status_code = HTTPStatus.CONFLICT

return func.HttpResponse(
body=body,
Expand All @@ -42,7 +43,7 @@ def update_customer(req: func.HttpRequest) -> func.HttpResponse:
except ValueError:
return func.HttpResponse(
body=b'Invalid format or structure of the attributes of the customer',
status_code=400,
status_code=HTTPStatus.BAD_REQUEST,
mimetype="application/json"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import typing

import sqlalchemy as sq
import sqlalchemy.sql as sql

import time_tracker.customers._domain as domain
from time_tracker._infrastructure import _db
Expand All @@ -25,8 +24,8 @@ def __init__(self, database: _db.DB):
)

def get_by_id(self, id: int) -> domain.Customer:
query = sql.select(self.customer).where(
sql.and_(self.customer.c.id == id, self.customer.c.deleted.is_(False))
query = sq.sql.select(self.customer).where(
sq.sql.and_(self.customer.c.id == id, self.customer.c.deleted.is_(False))
)
customer = self.db.get_session().execute(query).one_or_none()
return self.__create_customer_dto(dict(customer)) if customer else None
Expand Down Expand Up @@ -70,12 +69,9 @@ def delete(self, customer_id: int) -> domain.Customer:

def update(self, id: int, data: domain.Customer) -> domain.Customer:
try:
new_customer = {
"name": data.name,
"description": data.description,
"status": data.status,
"deleted": data.deleted
}
new_customer = data.__dict__
new_customer.pop("id")

customer_validated = {key: value for (key, value) in new_customer.items() if value is not None}
query = self.customer.update().where(self.customer.c.id == id).values(customer_validated)
self.db.get_session().execute(query)
Expand Down