Skip to content

Commit cad1c3d

Browse files
committed
Mock mimetype test when run in github action. Python image for github action does not support mimetype
1 parent c6385c5 commit cad1c3d

File tree

8 files changed

+290
-162
lines changed

8 files changed

+290
-162
lines changed

.github/workflows/publish-release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
run: |
3636
echo ${{ github.workspace }}
3737
cd ${{ github.workspace }}/tests
38-
poetry run pytest
38+
poetry run pytest --github-action-run=True
3939
- name: Build
4040
id: build-step
4141
run: |

.github/workflows/publish-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: |
3434
echo ${{ github.workspace }}
3535
cd ${{ github.workspace }}/tests
36-
poetry run pytest
36+
poetry run pytest --github-action-run=True
3737
- name: Build
3838
id: build-step
3939
run: |

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ jobs:
4848
run: |
4949
echo ${{ github.workspace }}
5050
cd ${{ github.workspace }}/tests
51-
poetry run pytest
51+
poetry run pytest --cov=dwcahandler --github-action-run=True
5252

poetry.lock

+246-145
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pandas = "^2.2.0"
1414
requests = "^2.32.0"
1515
pytest = "^8.2.0"
1616
pytest-mock = "^3.12.0"
17+
pytest-cov = "^5.0.0"
1718
metapype = "^0.0.26"
1819
flake8 = "^7.1.1"
1920

src/dwcahandler/dwca/core_dwca.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -678,15 +678,16 @@ def get_media_type(media_format: str):
678678
def get_multimedia_format_type(row: dict):
679679
url = row['identifier']
680680
media_format = None
681-
try:
682-
mime_type = mimetypes.guess_type(url)
683-
if mime_type and len(mime_type) > 0 and mime_type[0]:
684-
media_format = mime_type[0]
685-
except Exception as error:
686-
log.error("Error getting mimetype from url %s: %s", url, error)
681+
if url:
682+
try:
683+
mime_type = mimetypes.guess_type(url)
684+
if mime_type and len(mime_type) > 0 and mime_type[0]:
685+
media_format = mime_type[0]
686+
except Exception as error:
687+
log.error("Error getting mimetype from url %s: %s", url, error)
687688

688689
media_type = ''
689-
if 'type' not in row or not row['type']:
690+
if 'type' not in row or not row['type'] or row['type'] is nan:
690691
media_type = get_media_type(media_format)
691692
else:
692693
media_type = row['type']

tests/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
def pytest_addoption(parser):
3+
parser.addoption(
4+
"--github-action-run", action="store", default=False, help="Set this to True if it's been called from github action"
5+
)

tests/test_multimedia_content.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import pandas as pd
2+
import dwcahandler
23
from dwcahandler.dwca import CsvFileType, CoreOrExtType
34
from dwcahandler.dwca.core_dwca import Dwca
45
from operator import attrgetter
6+
import logging
7+
import pytest
58

6-
MIMETYPE_IMAGE_URL = 'https://www.gstatic.com/webp/gallery/1.webp'
9+
logging.basicConfig(level=logging.DEBUG)
10+
log = logging.getLogger("test_multimedia_content")
11+
12+
MIMETYPE_IMAGE_URL = "https://www.gstatic.com/webp/gallery/1.webp"
713
INVALID_MIMETYPE_URL = "https://invalid.url.jpeg"
814
IMAGE_URL = "https://images.ala.org.au/image/proxyImageThumbnailLarge?imageId=a36b5634-0277-47c7-b4e3-383e24ce8d1a"
915
AUDIO_URL = "https://images.ala.org.au/image/proxyImage?imageId=480f5f5e-e96c-4ae3-8230-c53a37bc542e"
@@ -20,13 +26,26 @@
2026
keys=['occurrenceID'])
2127

2228

29+
def mock_guess_type(url):
30+
if url == MIMETYPE_IMAGE_URL:
31+
return ('image/webp', None)
32+
elif url == INVALID_MIMETYPE_URL:
33+
return ('image/jpeg', None)
34+
return (None, None)
35+
36+
37+
@pytest.fixture
38+
def mock_mime_types(monkeypatch, request):
39+
if request.config.getoption("--github-action-run"):
40+
monkeypatch.setattr(dwcahandler.dwca.core_dwca.mimetypes, "guess_type", mock_guess_type)
41+
42+
2343
class TestMultimediaExtension:
2444

2545
def test_extract_associate_media(self):
2646
"""
2747
Test for associated media to be expanded into multimedia extension
2848
"""
29-
3049
occ_associated_media_df = pd.DataFrame(data=[["1", "species1", IMAGE_URL],
3150
["2", "species2", AUDIO_URL],
3251
["3", "species3", f"{VIDEO_URL}|{MIMETYPE_IMAGE_URL}"]],
@@ -60,7 +79,7 @@ def test_extract_associate_media(self):
6079
assert sorted(list(map(attrgetter('field_name'), dwca.meta_content.meta_elements[1].fields))) == \
6180
sorted(['coreid', 'identifier'])
6281

63-
def test_fill_additional_multimedia_info(self):
82+
def test_fill_additional_multimedia_info(self, mock_mime_types):
6483
"""
6584
Test for fill additional multimedia info if format and type is not provided
6685
:return:
@@ -88,12 +107,14 @@ def test_fill_additional_multimedia_info(self):
88107
["3", MIMETYPE_IMAGE_URL, 'image/webp', 'StillImage']],
89108
columns=['occurrenceID', 'identifier', 'format', 'type'])
90109

110+
dwca.ext_content[0].df_content.fillna('', inplace=True)
111+
expected_multimedia_df.fillna('', inplace=True)
112+
91113
# Test that the multimedia extension will now contain the format and type
92114
pd.testing.assert_frame_equal(dwca.ext_content[0].df_content.drop(
93115
columns=['coreid']), expected_multimedia_df)
94116

95-
96-
def test_fill_multimedia_info_with_format_type_partially_supplied(self):
117+
def test_fill_multimedia_info_with_format_type_partially_supplied(self, mock_mime_types):
97118
"""
98119
Test fill_additional_multimedia_info if format or type is already present.
99120
Calling fill_additional_multimedia_info should not change the existing values in the content
@@ -156,8 +177,7 @@ def test_fill_multimedia_info_with_format_type_partially_supplied(self):
156177
pd.testing.assert_frame_equal(dwca.ext_content[0].df_content.drop(
157178
columns=['coreid']), expected_multimedia_df)
158179

159-
160-
def test_fill_multimedia_info_type_from_forrmat(self):
180+
def test_fill_multimedia_info_type_from_forrmat(self, mock_mime_types):
161181
"""
162182
Test fill_additional_multimedia_info if only format is already present.
163183
Calling fill_additional_multimedia_info should not change the existing values in the content

0 commit comments

Comments
 (0)