-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from sdsc-ordes/test/tests
Test/tests: Implemented first unit tests, using both plain pytest and AppTest
- Loading branch information
Showing
15 changed files
with
578 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a single version of Python | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python | ||
|
||
name: Execute tests with pytest | ||
|
||
on: | ||
push: | ||
branches: [ "dev" ] | ||
pull_request: | ||
branches: [ "dev", "main" ] | ||
permissions: | ||
contents: read | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi | ||
if [ -f tests/requirements.txt ]; then pip install -r tests/requirements.txt; fi | ||
# if [ -f pyproject.toml ]; then pip install -r pyproject.toml; fi | ||
#- name: Lint with flake8 | ||
# run: | | ||
# # stop the build if there are Python syntax errors or undefined names | ||
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
run: | | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
pythonpath = "src" | ||
testpaths = | ||
tests | ||
|
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# a minimal snippet for the whale viewer, for testing purposes | ||
# - using AppTest to validate that the display_whale functionality | ||
# is ok | ||
# - currently placed in the src directory (not optimal) because | ||
# I couldn't get pytest to pick it up from the tests directory. | ||
# - TODO: find a cleaner solution for organisation (maybe just config to pytest?) | ||
|
||
import streamlit as st | ||
|
||
# to run streamlit from this subdir, we need the the src dir on the path | ||
# NOTE: pytest doesn't need this to run the tests, but to develop the test | ||
# harness is hard without running streamlit | ||
import sys | ||
from os import path | ||
# src (parent from here) | ||
src_dir = path.dirname( path.dirname( path.abspath(__file__) ) ) | ||
sys.path.append(src_dir) | ||
|
||
|
||
import whale_viewer as sw_wv | ||
|
||
# a menu to pick one of the images | ||
title = st.title("Whale Viewer testing") | ||
species = st.selectbox("Species", sw_wv.WHALE_CLASSES) | ||
|
||
if species is not None: | ||
# and display the image + reference | ||
st.write(f"Selected species: {species}") | ||
sw_wv.display_whale([species], 0, st) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Test report | ||
|
||
- overall status: ![CI test build](https://github.com/sdsc-ordes/saving-willy/actions/workflows/python-pytest.yml/badge.svg) | ||
- more detailed test report: TODO | ||
|
||
## Test coverage | ||
|
||
- TODO | ||
- For a summary: one way is using https://github.com/GaelGirodon/ci-badges-action, can add it as a post-pytest step to the CI | ||
- For a table: try this https://github.com/coroo/pytest-coverage-commentator |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# tests | ||
pytest~=8.3.4 | ||
pytest-cov~=6.0.0 | ||
# linting | ||
#flake8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from streamlit.testing.v1 import AppTest | ||
import pytest # for the exception testing | ||
|
||
import whale_viewer as sw_wv # for data | ||
|
||
|
||
def test_selectbox_ok(): | ||
''' | ||
test the snippet demoing whale viewer - relating to AppTest'able elements | ||
we validate that | ||
- there is one selectbox present, with initial value "beluga" and index 0 | ||
- the two markdown elems generated dynamically by the selection corresponds | ||
- then changing the selection, we do the same checks again | ||
- finally, we check there are the right number of options (26) | ||
''' | ||
at = AppTest.from_file("src/apptest/demo_whale_viewer.py").run() | ||
assert len(at.selectbox) == 1 | ||
assert at.selectbox[0].value == "beluga" | ||
assert at.selectbox[0].index == 0 | ||
|
||
# let's check that the markdown is right | ||
# the first markdown should be "Selected species: beluga" | ||
assert at.markdown[0].value == "Selected species: beluga" | ||
# the second markdown should be "### :whale: #1: Beluga" | ||
print("markdown 1: ", at.markdown[1].value) | ||
assert at.markdown[1].value == "### :whale: #1: Beluga" | ||
|
||
# now let's select a different element. index 4 is commersons_dolphin | ||
v4 = "commersons_dolphin" | ||
v4_str = v4.replace("_", " ").title() | ||
|
||
at.selectbox[0].set_value(v4).run() | ||
assert at.selectbox[0].value == v4 | ||
assert at.selectbox[0].index == 4 | ||
# the first markdown should be "Selected species: commersons_dolphin" | ||
assert at.markdown[0].value == f"Selected species: {v4}" | ||
# the second markdown should be "### :whale: #1: Commersons Dolphin" | ||
assert at.markdown[1].value == f"### :whale: #1: {v4_str}" | ||
|
||
# test there are the right number of options | ||
print("PROPS=> ", dir(at.selectbox[0])) # no length unfortunately, | ||
# test it dynamically intead. | ||
# should be fine | ||
at.selectbox[0].select_index(len(sw_wv.WHALE_CLASSES)-1).run() | ||
# should fail | ||
with pytest.raises(Exception): | ||
at.selectbox[0].select_index(len(sw_wv.WHALE_CLASSES)).run() | ||
|
||
def test_img_props(): | ||
''' | ||
test the snippet demoing whale viewer - relating to the image | ||
we validate that | ||
- one image is displayed | ||
- the caption corresponds to the data in WHALE_REFERENCES | ||
- the url is a mock url | ||
- then changing the image, we do the same checks again | ||
''' | ||
at = AppTest.from_file("src/apptest/demo_whale_viewer.py").run() | ||
ix = 0 # we didn't interact with the dropdown, so it should be the first one | ||
# could fetch the property - maybe better in case code example changes | ||
ix = at.selectbox[0].index | ||
|
||
elem = at.get("imgs") # hmm, apparently the naming is not consistent with the other AppTest f/w. | ||
# type(elem[0]) -> "streamlit.testing.v1.element_tree.UnknownElement" haha | ||
assert len(elem) == 1 | ||
img0 = elem[0] | ||
|
||
# we can't check the image, but maybe the alt text? | ||
#assert at.image[0].alt == "beluga" # no, doesn't have that property. | ||
|
||
# for v1.39, the proto comes back something like this: | ||
exp_proto = ''' | ||
imgs { | ||
caption: "https://www.fisheries.noaa.gov/species/beluga-whale" | ||
url: "/mock/media/6a21db178fcd99b82817906fc716a5c35117f4daa1d1c1d3c16ae1c8.png" | ||
} | ||
width: -3 | ||
''' | ||
# from the proto string we can look for <itemtype>: "<value>" pairs and make a dictionary | ||
import re | ||
|
||
def parse_proto(proto_str): | ||
pattern = r'(\w+):\s*"([^"]+)"' | ||
matches = re.findall(pattern, proto_str) | ||
return {key: value for key, value in matches} | ||
|
||
parsed_proto = parse_proto(str(img0.proto)) | ||
# we're expecting the caption to be WHALE_REFERENCES[ix] | ||
print(parsed_proto) | ||
assert "caption" in parsed_proto | ||
assert parsed_proto["caption"] == sw_wv.WHALE_REFERENCES[ix] | ||
assert "url" in parsed_proto | ||
assert parsed_proto["url"].startswith("/mock/media") | ||
|
||
print(sw_wv.WHALE_REFERENCES[ix]) | ||
|
||
# now let's switch to another index | ||
ix = 15 | ||
v15 = sw_wv.WHALE_CLASSES[ix] | ||
v15_str = v15.replace("_", " ").title() | ||
at.selectbox[0].set_value(v15).run() | ||
|
||
elem = at.get("imgs") | ||
img0 = elem[0] | ||
print("[INFO] image 0 after adjusting dropdown:") | ||
print(img0.type, type(img0.proto))#, "\t", i0.value) # it doesn't have a value | ||
print(img0.proto) | ||
|
||
|
||
parsed_proto = parse_proto(str(img0.proto)) | ||
# we're expecting the caption to be WHALE_REFERENCES[ix] | ||
print(parsed_proto) | ||
assert "caption" in parsed_proto | ||
assert parsed_proto["caption"] == sw_wv.WHALE_REFERENCES[ix] | ||
assert "url" in parsed_proto | ||
assert parsed_proto["url"].startswith("/mock/media") | ||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.