Just click on the following button and login/signup:
In case of missing pytest
command just open a Shell
tab and follow the "Local setup" steps.
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
For some macOS if pip install fails run this command and then repeat the setup:
export C_INCLUDE_PATH=/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/Headers
pytest
- carts/tests.py: Tests
- carts/views.py: Implementation
- carts/urls.py: List of API urls
Remember:
- Write a test
- Run all tests and see it fail
- Write the minimum code to pass the test
- Run all tests and see it pass
- Refactor the code
- Repeat
- Write a new test case for the
GET /cart/
endpoint - Expect the response to be a
200
status code - Expect the response.json() to be
{
"lines": []
}
- Run the tests and see it fail with
AssertionError: assert 404 == 200
- Implement the
GET /cart/
endpoint with a hardcoded response - Add the following code to
carts/views.py
:
def cart_detail(request):
return JsonResponse(data={"lines": []})
- Run the tests and see it pass
- Refactor the code
- Run the tests and see it pass
- Repeat
- Write a new test case for the
GET /cart/
endpoint - Add a product to the cart
CartLine.objects.create(reference="50776", quantity=5)
- Expect the response to be a
200
status code - Expect the response.json() to be
{
"lines": [
{
"reference": "50776",
"quantity": 5
}
]
}
- Run the tests and see it fail with
AssertionError: assert {'lines': []} == {'lines': [{'reference': '50776', 'quantity': 5}]}
- Fix the implementation to retrieve the products in the cart
- Run the tests and see it pass
- Refactor the code
- Run the tests and see it pass
- Repeat
- Write a new test case for the
POST /cart/products/add/
endpoint - Use the following data to add a product to the cart
{
"reference": "33333",
"quantity": 2
}
- Expect the response to be a
200
status code - Expect the response.json() to be
{
"lines": [
{
"reference": "33333",
"quantity": 2
}
]
}
- Run the tests and see it fail
- Write the minimum code to pass the test
- Run the tests and see it pass
- Refactor the code
- Run the tests and see it pass
- Repeat
- Given a cart with 2 products, one with reference
33333
and quantity4
and another with reference11111
and quantity2
. - When I substract a product with reference
33333
and quantity2
. - Then the cart should have 2 products, one with reference
33333
and quantity2
and another with reference11111
and quantity2
.
- Write a new test case for the
POST /cart/products/substract/
endpoint - Use the following data to substract a product from the cart
{
"reference": "11111",
"quantity": 2
}
- Expect the response.json() to be
{
"lines": [
{
"reference": "33333",
"quantity": 1
},
{
"reference": "11111",
"quantity": 2
}
]
}
- Run the tests and see it fail
- Write the minimum code to pass the test
- Run the tests and see it pass
- Refactor the code
- Run the tests and see it pass
- Repeat
- Given a cart with 2 products, one with reference
33333
and quantity4
and another with reference11111
and quantity1
. - When I retrieve the cart
- Then the cart should have a total quantity of
5
. - And the response should be
{
"lines": [
{
"reference": "33333",
"quantity": 4
},
{
"reference": "11111",
"quantity": 1
}
],
"total_quantity": 5
}
- Run the tests and see it fail
- Write the minimum code to pass the test
- Run the tests and see it pass
- Refactor the code
- Run the tests and see it pass
- Repeat