Skip to content

Commit

Permalink
test: add crud unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipuigbou committed Nov 23, 2023
1 parent 199eceb commit df945d5
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
- name: Run behave dry-run
run: make crud-tests-dry-run

- name: Run unit test crud
run: make run-crud-unit-tests

- name: Run test crud
run: make run-crud-tests

Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ jobs:
# Disable shallow clones, as recommended by SonarQube for improved analysis and reports
fetch-depth: 0

- name: Run apis
- name: Run apis unit test
run: make run-apis-coverage

- name: List folder
run: ls -la apis/_output/reports
- name: Run crud unit test
run: make run-crud-unit-tests

- name: Fix code coverage paths
- name: Fix apis code coverage paths
run: |
mkdir apis/reports ; cp apis/_output/reports/coverage.xml apis/reports/coverage_fixed.xml ; sed -i 's/root/\/github\/workspace\/apis/g' apis/reports/coverage_fixed.xml
- name: Fix apis code coverage paths
run: |
mkdir crud/reports ; cp crud/_output/reports/coverage.xml crud/reports/coverage_fixed.xml ; sed -i 's/root/\/github\/workspace\/crud/g' crud/reports/coverage_fixed.xml
- name: Analyze with SonarCloud

uses: SonarSource/[email protected]
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ build-crud: ## build crud
run-crud: ## run crud
docker compose -f $(CRUD_DIR)/docker-compose.yml run backend

.PHONY: run-crud-unit-tests
run-crud-unit-tests: ## run crud unit tests
docker compose -f $(CRUD_DIR)/docker-compose.yml down
docker compose -f $(CRUD_DIR)/docker-compose.yml run unit-test


.PHONY: run-crud-tests
run-crud-tests: ## run crud tests
docker compose -f $(CRUD_DIR)/docker-compose.yml down
Expand Down
2 changes: 2 additions & 0 deletions crud/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ RUN pip install -r /root/requirements.txt

COPY ./backend.py /root/backend.py
COPY ./endpoints /root/endpoints
COPY ./endpoints_tests /root/endpoints_tests

WORKDIR /root

EXPOSE 8888
12 changes: 12 additions & 0 deletions crud/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ services:
- mongo
volumes:
- ./tests/_output:/root/_output

unit-test:
image: unit-test-image
build:
context: .
dockerfile: Dockerfile
command: sh -c "sleep 5 && coverage run -m pytest --junitxml=/root/_output/reports/junit-crud.xml /root/endpoints_tests && coverage xml -o /root/_output/reports/coverage.xml && coverage report"
depends_on:
- backend
- mongo
volumes:
- ./tests/_output:/root/_output
7 changes: 5 additions & 2 deletions crud/endpoints/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def get(self, id=None):
return {"status": "error reading products"}, 500

def post(self):
json_data = request.get_json(force=True)
try:
json_data = request.get_json(force=True)
except:
return {"status": "error parsing json"}, 500
try:
self.db.products.insert_one(json_data)
return {"status": "product inserted"}, 200
Expand All @@ -34,7 +37,7 @@ def post(self):

def put(self, id=None):
if id is None:
return {"status": "product not updated"}, 500
return {"status": "product not updated because id is not provided"}, 500
json_data = request.get_json(force=True)
try:
self.db.products.update_one({"_id": ObjectId(id)}, {"$set": json_data})
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions crud/endpoints_tests/test_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from endpoints.health import Health

def test_health():
health = Health()
response = health.get()
assert response == ({'status': 'ok'}, 200)
22 changes: 22 additions & 0 deletions crud/endpoints_tests/test_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from endpoints.products import Products
from pytest import raises
def test_get_products():
products = Products()
response = products.get()
assert response == ([], 200)

def test_delete_products_missing_id():
products = Products()
with raises(TypeError) as excinfo:
products.delete()
assert "missing 1 required positional argument" in str(excinfo.value)

def test_post_products_missing_id():
products = Products()
response = products.post()
assert response == ({'status': 'error parsing json'}, 500)

def test_put_products_missing_json():
products = Products()
response = products.put()
assert response == ({'status': 'product not updated because id is not provided'}, 500)
4 changes: 3 additions & 1 deletion crud/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
flask~=3.0
flask_restful~=0.3
pymongo~=4.6
pymongo~=4.6
pytest~=7.4
coverage~=7.3
6 changes: 3 additions & 3 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ sonar.projectKey=jordipuigbou_behave-test
sonar.organization=jordipuigbou
sonar.sources=.
sonar.sourceEncoding=UTF-8
sonar.test.inclusions=apis/test.py
sonar.python.coverage.reportPaths=apis/reports/coverage_fixed.xml
sonar.test.inclusions=apis/test.py, crud/endpoints_tests/test_*.py
sonar.python.coverage.reportPaths=apis/reports/coverage_fixed.xml, crud/reports/coverage_fixed.xml
sonar.python.version=3
sonar.python.xunit.reportPath=apis/_output/reports/junit-apis.xml
sonar.python.xunit.reportPath=apis/_output/reports/junit-apis.xml, crud/_output/reports/junit-crud.xml
sonar.python.xunit.skipDetails=true

0 comments on commit df945d5

Please sign in to comment.