Skip to content

Commit

Permalink
Try and run pytest in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
pblowey committed Aug 13, 2024
1 parent 3787252 commit 5a8d998
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test CI

on:
push:
pull_request:
schedule:
# Run every Monday at 8am to check latest versions of dependencies
- cron: "0 8 * * WED"

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: |
ghcr.io/${{ github.repository }}
tags: |
type=edge,branch=main
type=ref,event=branch
type=semver,pattern={{version}}
type=sha,prefix=
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: false
tags: test
labels: ${{ steps.meta.outputs.labels }}

- name: Test
run: |
docker exec test bash -c "pytest"
File renamed without changes.
39 changes: 39 additions & 0 deletions tests/test_internals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pymca_zocalo.internals import find_cut_off_energy
import tempfile
from pytest import raises
from unittest.mock import patch, mock_open


@patch("pymca_zocalo.internals.open")
def test_GIVEN_input_file_not_found_WHEN_find_cut_off_energy_called_THEN_raises_exception(mock_open):
mock_open.side_effect = FileNotFoundError()
with raises(FileNotFoundError):
find_cut_off_energy("test_file_name", 0)


@patch("pymca_zocalo.internals.open", new_callable=mock_open, read_data="1 0")
def test_GIVEN_energy_of_1_and_cutoff_of_0_WHEN_find_cut_off_energy_called_THEN_returns_1(mock_file):
line_of_greater_energy = find_cut_off_energy("test_file_name", 0)
assert line_of_greater_energy == 0

@patch("pymca_zocalo.internals.open")
def test_different_energies(mock_file):
test_data = "0 1\n5 10\n"
mock_open(mock_file, read_data=test_data)
line_of_greater_energy = find_cut_off_energy("test_file_name", 2)
assert line_of_greater_energy == 1


@patch("pymca_zocalo.internals.open")
def test_file_has_non_floats_different_energies(mock_file):
test_data = "hyadasd 1\n5 10\n"
mock_open(mock_file, read_data=test_data)
line_of_greater_energy = find_cut_off_energy("test_file_name", 2)
assert line_of_greater_energy == 1

@patch("pymca_zocalo.internals.open")
def test_file_nothing_in_then_gives_none(mock_file):
test_data = ""
mock_open(mock_file, read_data=test_data)
line_of_greater_energy = find_cut_off_energy("test_file_name", 2)
assert line_of_greater_energy == None

0 comments on commit 5a8d998

Please sign in to comment.