Skip to content

Commit

Permalink
Add unit tests for publish_plot (#28)
Browse files Browse the repository at this point in the history
* Add dependencies to get postprocessing.publish_plot working

* Refactor to allow for getting testing config file separately

* Add unit tests for postprocessing.publish_plot

* Remove second listing of requests library
  • Loading branch information
peterfpeterson authored Dec 4, 2023
1 parent 36bf63d commit 9f422b4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ channels:
- defaults
dependencies:
- python=2.7
- requests
- pip<21.0
# if pip doesn't work https://stackoverflow.com/questions/49940813/pip-no-module-named-internal
- gcc_linux-64
- plotly
- pytest
- pytest-cov
- pytest-mock
- requests
- pip: # will need to be installed separately if pip is broken
- twisted==12.2.0
- stompest==2.1.6
Expand Down
16 changes: 9 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ def docker_exec_and_cat(filename):
return filecontents


def getDevConfigurationFile():
srcdir = os.path.dirname(os.path.realpath(__file__)) # directory this file is in
# go up 1 level to get out of tests directory
srcdir = os.path.split(srcdir)[0]

return os.path.join(srcdir, "configuration/post_process_consumer.conf.development")


def getDevConfiguration(dev_output_dir=""):
"""
Create a Configuration object with a now developer directory
@param dev_output_dir: Location of the output directory
"""
from postprocessing.Configuration import Configuration

srcdir = os.path.dirname(os.path.realpath(__file__)) # directory this file is in
# go up 1 level to get out of tests directory
srcdir = os.path.split(srcdir)[0]

# load the developer configuration file
config = Configuration(
os.path.join(srcdir, "configuration/post_process_consumer.conf.development")
)
config = Configuration(getDevConfigurationFile())
if dev_output_dir:
config.dev_output_dir = dev_output_dir
config.dev_instrument_shared = os.path.join(dev_output_dir, "shared")
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/postprocessing/test_publish_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from postprocessing import publish_plot
import pytest
from tests.conftest import getDevConfigurationFile


def test_get_user():
info = publish_plot.get_user(getDevConfigurationFile())
assert info
# these aren't sepecified so just make sure the keys are present
assert "username" in info
assert "password" in info


# plot1d test data
data1d = [[0, 1], [2, 3], [4, 5]]


@pytest.mark.parametrize("x_log, y_log", [(False, False), (True, True)])
def test_plot1d(x_log, y_log):
# single spectrum
assert publish_plot.plot1d(
run_number=1234,
instrument="instr",
data_list=[data1d],
x_log=x_log,
y_log=y_log,
publish=False,
)
# two spectra
assert publish_plot.plot1d(
run_number=1234, instrument="instr", data_list=[data1d, data1d], publish=False
)


def test_plot1d_fail():
with pytest.raises(RuntimeError):
publish_plot.plot1d(
run_number=1234, instrument="instr", data_list=None, publish=False
)

# when plot fails to publish the function returns None
assert not publish_plot.plot1d(
run_number=1234, instrument="instr", data_list=[data1d], publish=True
)


data2d = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]


@pytest.mark.parametrize("x_log, y_log", [(False, False), (True, True)])
def test_plot_heatmap(x_log, y_log):
assert publish_plot.plot_heatmap(
1234,
data2d[0],
data2d[1],
data2d[2],
x_log=x_log,
y_log=y_log,
instrument="instr",
publish=False,
)


def test_plot_heatmap_fail():
# when plot fails to publish the function returns None

assert not publish_plot.plot_heatmap(
1234, data2d[0], data2d[1], data2d[2], instrument="instr", publish=True
)


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 9f422b4

Please sign in to comment.