From 8a46e046db3826cfdd37141833a1736bc7159883 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Wed, 10 Jul 2024 10:50:04 -0400 Subject: [PATCH] ci: add coverage checking to pytest runs And get us to 100% by adding a couple tests --- .github/workflows/ci.yaml | 14 ++++++++++++-- cumulus_fhir_support/json.py | 2 +- pyproject.toml | 1 + tests/test_json.py | 6 ++++++ tests/test_schemas.py | 13 +++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 170bd10..4bce559 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,7 +22,7 @@ jobs: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} @@ -33,7 +33,17 @@ jobs: - name: Test with pytest run: | - python -m pytest + python -m pytest --cov=cumulus_fhir_support --cov-report=xml + + - name: Check coverage report + if: github.ref != 'refs/heads/main' + uses: orgoro/coverage@v3.1 + with: + coverageFile: coverage.xml + token: ${{ secrets.GITHUB_TOKEN }} + thresholdAll: .99 + thresholdNew: 1 + thresholdModified: 1 lint: runs-on: ubuntu-latest diff --git a/cumulus_fhir_support/json.py b/cumulus_fhir_support/json.py index 23701b2..04f0984 100644 --- a/cumulus_fhir_support/json.py +++ b/cumulus_fhir_support/json.py @@ -44,7 +44,7 @@ from typing import TYPE_CHECKING, Any, Iterable, Optional, Union if TYPE_CHECKING: - import fsspec + import fsspec # pragma: no cover PathType = Union[str, pathlib.Path] ResourceType = Union[str, Iterable[str], None] diff --git a/pyproject.toml b/pyproject.toml index 71941fd..bf3677e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,7 @@ line-length = 100 tests = [ "ddt", "pytest", + "pytest-cov", ] dev = [ "black >= 24, < 25", diff --git a/tests/test_json.py b/tests/test_json.py index 8d6b41d..e6ede56 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -261,6 +261,12 @@ def fake_open(filename, mode, encoding): mock_fs.ls = fake_ls mock_fs.open = fake_open + # Missing dir + with self.assert_no_logs(): + rows = support.read_multiline_json_from_dir("not-present", "Patient", fsspec_fs=mock_fs) + self.assertEqual([], list(rows)) + + # Dir exists with self.assert_no_logs(): rows = support.read_multiline_json_from_dir("folder", "Patient", fsspec_fs=mock_fs) self.assertEqual(["P2", "P1"], [x["id"] for x in rows]) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index aafa9d3..6908d26 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -1,6 +1,7 @@ """Tests for schemas.py""" import unittest +from unittest import mock import pyarrow @@ -219,3 +220,15 @@ def test_contained_resources_empty(self): self.assertEqual(pyarrow.string(), contained_type.field("id").type) self.assertEqual(pyarrow.string(), contained_type.field("implicitRules").type) self.assertEqual(pyarrow.string(), contained_type.field("language").type) + + @mock.patch("fhirclient.models.fhirelementfactory.FHIRElementFactory.instantiate") + def test_unexpected_fhir_type(self, mock_instantiate): + """Verify that we error out if an unknown FHIR type is provided""" + mock_resource = mock.MagicMock() + mock_resource.elementProperties.return_value = [ + ("fieldBoolean", "fieldBoolean", bool, False, None, False), + ("fieldObject", "fieldObject", object, False, None, False), + ] + mock_instantiate.return_value = mock_resource + with self.assertRaisesRegex(ValueError, "Unexpected type: "): + support.pyarrow_schema_from_rows("AllergyIntolerance")