Merge pull request #4 from rasmusvt/rasmus_pytest

Merge setup of pytest into master
This commit is contained in:
Rasmus Vester Thøgersen 2022-04-06 17:30:34 +02:00 committed by GitHub Enterprise
commit 4ea0fcce4b
11 changed files with 220 additions and 3 deletions

View file

@ -1,13 +1,11 @@
import beamtime.auxillary as aux import beamtime.auxillary as aux
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,AutoMinorLocator) from matplotlib.ticker import (MultipleLocator)
from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition, mark_inset)
import importlib import importlib
import matplotlib.patches as mpatches import matplotlib.patches as mpatches
from matplotlib.lines import Line2D from matplotlib.lines import Line2D
import matplotlib.lines as mlines import matplotlib.lines as mlines
from cycler import cycler
import itertools import itertools

View file

9
beamtime/test/pytest.ini Normal file
View file

@ -0,0 +1,9 @@
# pytest.ini
[pytest]
minversion = 6.0
testpaths =
.
filterwarnings =
ignore::DeprecationWarning

View file

@ -0,0 +1,78 @@
import beamtime.auxillary as aux
import os
def test_swap_values():
dict = {'test1': 1, 'test2': 2}
key1 = 'test1'
key2 = 'test2'
oldval1 = dict[key1]
oldval2 = dict[key2]
new_dict = aux.swap_values(dict=dict, key1=key1, key2=key2)
assert (dict[key1] == oldval2) and (dict[key2] == oldval1)
def test_ceil() -> None:
assert aux.ceil(1.05, 0.5) == 1.5
assert aux.ceil(1.05, 1) == 2.0
assert aux.ceil(1.1, 0.2) == 1.2
def test_floor() -> None:
assert aux.floor(2.02, 1) == 2.0
assert aux.floor(2.02, 0.01) == 2.02
assert aux.floor(2.013, 0.01) == 2.01
def test_options() -> None:
options = {}
required_options = ['test1', 'test2', 'test3', 'test4']
default_options = {
'test1': 1,
'test2': 2,
'test3': 3,
'test4': 4,
'test5': 5,
}
options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
assert options['test1'] == default_options['test1']
assert len(options.items()) == len(required_options)
assert 'test5' not in options.keys()
def test_save_options() -> None:
options = {'test1': 1, 'test2': 2}
path = 'tmp.dat'
aux.save_options(options, path)
assert os.path.isfile(path)
os.remove(path)
def test_load_options() -> None:
options = {'test1': 1, 'test2': 2}
path = 'tmp.dat'
aux.save_options(options, path)
loaded_options = aux.load_options(path)
assert (loaded_options['test1'] == 1) and (loaded_options['test2'] == 2)
os.remove(path)

View file

@ -0,0 +1,132 @@
import beamtime.plotting as btp
from cycler import cycler
import itertools
import numpy as np
import matplotlib.pyplot as plt
def test_generate_colours() -> None:
assert type(btp.generate_colours('black', kind='single')) == itertools.cycle
palettes = [('qualitative', 'Dark2_8')]
colour_cycle = btp.generate_colours(palettes)
assert type(colour_cycle) == itertools.cycle
# Test that it actually loaded 8 colours when given a set of 8 colours to
same_colour = None
for i in range(10):
colour = next(colour_cycle)
if i == 0:
first_colour = colour
if colour == first_colour:
repeat_colour_index = i
assert repeat_colour_index == 8
def test_update_rc_params() -> None:
rc_params = {
'lines.linewidth': 100
}
prev_params = plt.rcParams['lines.linewidth']
# Update run commands if any is passed (will pass an empty dictionary if not passed)
btp.update_rc_params(rc_params)
new_params = plt.rcParams['lines.linewidth']
assert new_params == 100
assert prev_params != new_params
# Reset run commands
plt.rcdefaults()
def test_scale_figure() -> None:
width, height = 1, 1
format_params = {
'upscaling_factor': 2,
'compress_width': 1,
'compress_height': 1
}
width1, height1 = btp.scale_figure(format_params=format_params, width=width, height=height)
assert width1 == 2 and height1 == 2
format_params = {
'upscaling_factor': 1,
'compress_width': 0.5,
'compress_height': 1
}
width2, height2 = btp.scale_figure(format_params=format_params, width=width, height=height)
assert width2 == 0.5 and height2 == 1
format_params = {
'upscaling_factor': 2,
'compress_width': 0.5,
'compress_height': 0.2
}
width2, height2 = btp.scale_figure(format_params=format_params, width=width, height=height)
assert width2 == 1 and height2 == 0.4
def test_determine_width() -> None:
conversion_cm_inch = 0.3937008 # cm to inch
format_params = {
'column_type': 'single',
'single_column_width': 5,
'double_column_width': 10,
'width_ratio': '1:1'
}
assert np.round(btp.determine_width(format_params),6) == np.round(5*conversion_cm_inch,6)
format_params['column_type'] = 'double'
assert np.round(btp.determine_width(format_params), 6) == np.round(10*conversion_cm_inch, 6)
format_params['column_type'] = 'single'
format_params['width_ratio'] = '1:2'
assert np.round(btp.determine_width(format_params), 6) == np.round(2.5*conversion_cm_inch, 6)
def test_determine_height() -> None:
width = 1
format_params = {
'aspect_ratio': '1:1'
}
assert btp.determine_height(format_params=format_params, width=width) == 1
format_params['aspect_ratio'] = '3:1'
assert (btp.determine_height(format_params=format_params, width=width) - 0.333333333333333) < 10e-7
assert True

View file

View file