From df945d5a21694a4e40b6bf91eb7e51a140312b39 Mon Sep 17 00:00:00 2001 From: Jenkins CI Date: Thu, 23 Nov 2023 16:25:02 +0100 Subject: [PATCH] test: add crud unit tests --- .github/workflows/ci.yml | 3 +++ .github/workflows/sonarcloud.yml | 12 ++++++++---- Makefile | 6 ++++++ crud/Dockerfile | 2 ++ crud/docker-compose.yml | 12 ++++++++++++ crud/endpoints/products.py | 7 +++++-- crud/endpoints_tests/__init__.py | 0 crud/endpoints_tests/test_health.py | 6 ++++++ crud/endpoints_tests/test_products.py | 22 ++++++++++++++++++++++ crud/requirements.txt | 4 +++- sonar-project.properties | 6 +++--- 11 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 crud/endpoints_tests/__init__.py create mode 100644 crud/endpoints_tests/test_health.py create mode 100644 crud/endpoints_tests/test_products.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e4ad471..da826a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 540de37..8655173 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -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/sonarcloud-github-action@v2.0.2 diff --git a/Makefile b/Makefile index 8bf2783..ce83e70 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/crud/Dockerfile b/crud/Dockerfile index 059e52b..a5d2316 100644 --- a/crud/Dockerfile +++ b/crud/Dockerfile @@ -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 \ No newline at end of file diff --git a/crud/docker-compose.yml b/crud/docker-compose.yml index f67c881..1f64f1b 100644 --- a/crud/docker-compose.yml +++ b/crud/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/crud/endpoints/products.py b/crud/endpoints/products.py index 785ad4e..900b437 100644 --- a/crud/endpoints/products.py +++ b/crud/endpoints/products.py @@ -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 @@ -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}) diff --git a/crud/endpoints_tests/__init__.py b/crud/endpoints_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/crud/endpoints_tests/test_health.py b/crud/endpoints_tests/test_health.py new file mode 100644 index 0000000..3ae8bbe --- /dev/null +++ b/crud/endpoints_tests/test_health.py @@ -0,0 +1,6 @@ +from endpoints.health import Health + +def test_health(): + health = Health() + response = health.get() + assert response == ({'status': 'ok'}, 200) diff --git a/crud/endpoints_tests/test_products.py b/crud/endpoints_tests/test_products.py new file mode 100644 index 0000000..dfee4a6 --- /dev/null +++ b/crud/endpoints_tests/test_products.py @@ -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) \ No newline at end of file diff --git a/crud/requirements.txt b/crud/requirements.txt index 1130131..2347f29 100644 --- a/crud/requirements.txt +++ b/crud/requirements.txt @@ -1,3 +1,5 @@ flask~=3.0 flask_restful~=0.3 -pymongo~=4.6 \ No newline at end of file +pymongo~=4.6 +pytest~=7.4 +coverage~=7.3 \ No newline at end of file diff --git a/sonar-project.properties b/sonar-project.properties index d0f04fa..cf93f77 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -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 \ No newline at end of file