From 507c51c0a4cadf1da587e45724f64dcfb71c5ee7 Mon Sep 17 00:00:00 2001 From: Christian Darsow Fromm <cdarsowf@physnet.uni-hamburg.de> Date: Mon, 9 Dec 2019 18:20:28 +0100 Subject: [PATCH] Resolve "Make testing data files cwd invariant" --- Makefile | 2 +- .../test_analysis/test_cavity/test_finesse.py | 7 ++- .../test_cavity/test_linewidth.py | 5 +- .../test_cavity/test_modematching.py | 11 ++-- .../test_servo_design/test_servo_design.py | 13 +++-- src/tests/test_conversion/test_db/test_db.py | 56 +++++++++++++++++++ .../test_gaussian_beam/test_fit_beam_data.py | 7 ++- src/tests/test_io/test___init__/test_read.py | 18 ++++-- src/tests/test_io/test___init__/test_reads.py | 16 ++++-- .../test_data_container.py | 14 +++-- .../test_ascii_header/test_ascii_header.py | 7 ++- .../test_data_container_csv.py | 7 ++- .../test_gwinstek/test_gwinstek.py | 15 +++-- .../test_gwinstek_LSF/test_gwinstek_LSF.py | 7 ++- .../test_hp4395a/test_hp4395a.py | 8 ++- .../test_keysight_binary.py | 6 +- .../test_keysight_csv/test_keysight_csv.py | 8 ++- .../test_keysight_fra/test_keysight_fra.py | 8 ++- .../test_rhode_schwarz/test_rhode_schwarz.py | 10 ++-- .../test_tektronix/test_tektronix.py | 12 ++-- .../test_tektronix_dpx/test_tektronix_dpx.py | 6 +- .../test_tektronix_spectrogram.py | 8 +-- src/tests/test_plots/test_plots.py | 14 ++--- 23 files changed, 185 insertions(+), 80 deletions(-) create mode 100644 src/tests/test_conversion/test_db/test_db.py diff --git a/Makefile b/Makefile index 5fca0b6..e1850db 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ bdist: @export PYTHONPATH=`pwd`/src:$(PYTHONPATH); python3 setup.py bdist_wheel test: - @export PYTHONPATH=`pwd`/src:$(PYTHONPATH); cd src/tests; py.test -x --log-level=INFO --no-cov-on-fail --cov-report=html . + @export PYTHONPATH=`pwd`/src:$(PYTHONPATH); cd src/tests; py.test -x --log-level=INFO --no-cov-on-fail --cov-fail-under=81 --cov-report=html . mypy: @export PYTHONPATH=`pwd`/src:$(PYTHONPATH); mypy src diff --git a/src/tests/test_analysis/test_cavity/test_finesse.py b/src/tests/test_analysis/test_cavity/test_finesse.py index 390ec34..99069dd 100644 --- a/src/tests/test_analysis/test_cavity/test_finesse.py +++ b/src/tests/test_analysis/test_cavity/test_finesse.py @@ -1,16 +1,17 @@ import unittest +from pathlib import Path import matplotlib.pyplot as plt from openqlab import io from openqlab.analysis.cavity import finesse +filedir = Path(__file__).parent + class TestFinesse(unittest.TestCase): def setUp(self): - self.data = io.read( - "test_analysis/test_cavity/opo_scan.csv", importer="KeysightCSV" - ) + self.data = io.read(f"{filedir}/opo_scan.csv", importer="KeysightCSV") plt.figure() def test_finesse_calculation(self): diff --git a/src/tests/test_analysis/test_cavity/test_linewidth.py b/src/tests/test_analysis/test_cavity/test_linewidth.py index 796ea40..cdca200 100644 --- a/src/tests/test_analysis/test_cavity/test_linewidth.py +++ b/src/tests/test_analysis/test_cavity/test_linewidth.py @@ -1,3 +1,4 @@ +from pathlib import Path from unittest import TestCase import matplotlib.pyplot as plt @@ -5,10 +6,12 @@ import matplotlib.pyplot as plt from openqlab import io from openqlab.analysis.cavity import linewidth +filedir = Path(__file__).parent + class TestLinewidth(TestCase): def setUp(self): - base = "test_analysis/test_cavity/" + base = f"{filedir}/" data = io.read( [base + "linewidth_err.csv", base + "linewidth_aux.csv"], importer="KeysightCSV", diff --git a/src/tests/test_analysis/test_cavity/test_modematching.py b/src/tests/test_analysis/test_cavity/test_modematching.py index 1e3bebb..bcbb5bc 100644 --- a/src/tests/test_analysis/test_cavity/test_modematching.py +++ b/src/tests/test_analysis/test_cavity/test_modematching.py @@ -1,16 +1,17 @@ import unittest +from pathlib import Path import matplotlib.pyplot as plt from openqlab import io from openqlab.analysis.cavity import modematching +filedir = Path(__file__).parent + class TestModematching(unittest.TestCase): def setUp(self): - self.data = io.read( - "test_analysis/test_cavity/opo_scan.csv", importer="KeysightCSV" - ) + self.data = io.read(f"{filedir}/opo_scan.csv", importer="KeysightCSV") plt.figure() def test_default_inverted(self): @@ -55,9 +56,7 @@ class TestModematching(unittest.TestCase): modematching(self.data["opo_scan_2"], without_main_peaks=True) def test_cropped_main_peak(self): - data = io.read( - "test_analysis/test_cavity/opo_scan_zoom.csv", importer="KeysightCSV" - ) + data = io.read(f"{filedir}/opo_scan_zoom.csv", importer="KeysightCSV") mm = modematching( data["opo_scan_zoom_2"].loc[0.0015:0.0155], offset=-0.015, diff --git a/src/tests/test_analysis/test_servo_design/test_servo_design.py b/src/tests/test_analysis/test_servo_design/test_servo_design.py index 54ed233..b3c0a2b 100644 --- a/src/tests/test_analysis/test_servo_design/test_servo_design.py +++ b/src/tests/test_analysis/test_servo_design/test_servo_design.py @@ -1,6 +1,7 @@ import logging as log import os import unittest +from pathlib import Path import jsonpickle import matplotlib as mp @@ -18,6 +19,8 @@ from openqlab.analysis.servo_design import ( ServoDesign, ) +filedir = Path(__file__).parent + class TestFilter(unittest.TestCase): def test_integrator(self): @@ -214,7 +217,7 @@ class TestServoDesign(unittest.TestCase): self.sd.plant = 8 columns = ["Gain (dB)", "Phase (deg)"] - fra = io.read("test_analysis/test_servo_design/fra_3.csv") + fra = io.read(f"{filedir}/fra_3.csv") self.sd.plant = fra self.assertListEqual(self.sd.plant.columns.tolist(), columns) @@ -253,7 +256,7 @@ class TestServoDesign(unittest.TestCase): def test_plot_with_plant(self): if self.display_available: self.sd.differentiator(390) - self.sd.plant = io.read("test_analysis/test_servo_design/fra_3.csv") + self.sd.plant = io.read(f"{filedir}/fra_3.csv") plt = self.sd.plot() self.assertIsInstance(plt, mp.figure.Figure) ax = plt.axes[0] @@ -267,7 +270,7 @@ class TestServoDesign(unittest.TestCase): columns = ["Servo A", "Servo P"] self.assertListEqual(df.columns.tolist(), columns) - self.sd.plant = io.read("test_analysis/test_servo_design/fra_3.csv") + self.sd.plant = io.read(f"{filedir}/fra_3.csv") df = self.sd.plot(plot=False) self.assertIsInstance(df, DataFrame) columns = ["Servo A", "Servo P", "Servo+TF A", "Servo+TF P"] @@ -321,7 +324,7 @@ class TestServoDesign(unittest.TestCase): ) def test_correct_latency(self): - self.sd.plant = io.read("test_analysis/test_servo_design/fra_3.csv") + self.sd.plant = io.read(f"{filedir}/fra_3.csv") df = self.sd.plot(plot=False, correct_latency=False) df_corrected = self.sd.plot(plot=False, correct_latency=True) columns = ["Servo A", "Servo P", "Servo+TF A", "Servo+TF P"] @@ -388,7 +391,7 @@ class TestServoDesign(unittest.TestCase): self.assertEqual(self.sd.__str__(), sd.__str__()) # Encode and decode with plant - fra = io.read("test_analysis/test_servo_design/fra_3.csv") + fra = io.read(f"{filedir}/fra_3.csv") self.sd.plant = fra sdjson = jsonpickle.encode(self.sd) sd = jsonpickle.decode(sdjson) diff --git a/src/tests/test_conversion/test_db/test_db.py b/src/tests/test_conversion/test_db/test_db.py new file mode 100644 index 0000000..c016f83 --- /dev/null +++ b/src/tests/test_conversion/test_db/test_db.py @@ -0,0 +1,56 @@ +import unittest + +import numpy as np + +from openqlab.conversion import db + + +class TestModematching(unittest.TestCase): + def test_db_to_lin(self): + self.assertAlmostEqual(db.to_lin(20), 100) + self.assertAlmostEqual(db.to_lin(100), 1e10) + self.assertAlmostEqual(db.to_lin(3), 1.99526, places=5) + self.assertAlmostEqual(db.to_lin(0), 1) + self.assertAlmostEqual(db.to_lin(-20), 0.01) + self.assertAlmostEqual(db.to_lin(-100), 1e-10) + self.assertAlmostEqual(db.to_lin(-3), 0.501187, places=5) + self.assertAlmostEqual(db.to_lin(-0), 1) + + def test_lin_to_db(self): + self.assertAlmostEqual(db.from_lin(100), 20) + self.assertAlmostEqual(db.from_lin(1e10), 100) + self.assertAlmostEqual(db.from_lin(1.99526), 3, places=4) + self.assertAlmostEqual(db.from_lin(1), 0) + self.assertAlmostEqual(db.from_lin(0.01), -20) + self.assertAlmostEqual(db.from_lin(1e-10), -100) + self.assertAlmostEqual(db.from_lin(0.501187), -3, places=4) + + def test_mean(self): + lin_data = np.array([1, 2, 3, 4, 5, 6]) + db_data = db.from_lin(lin_data) + self.assertAlmostEqual(db.to_lin(db.mean(db_data)), 3.5) + + def test_subtract(self): + lin_data1 = np.array([5, 3, 1e4, 8, 5, 6]) + lin_data2 = np.array([1, 2, 1e3, 4, 4, 5]) + result = np.array([4, 1, 9e3, 4, 1, 1]) + + db_data1 = db.from_lin(lin_data1) + db_data2 = db.from_lin(lin_data2) + + np.testing.assert_array_almost_equal( + db.to_lin(db.subtract(db_data1, db_data2)), result, decimal=3 + ) + + def test_average(self): + lin_data = np.array([1, 2, 3, 4, 5, 6, 7]) + db_data = db.from_lin(lin_data) + self.assertAlmostEqual(db.to_lin(db.average(db_data)), 4) + + def test_dBm2Vrms(self): + dbm_data = np.array([0, 1, 3, 6, 10, 100, -3, -10]) + dbv_data = np.array( + [0.224, 0.251, 0.316, 0.446, 0.707, 22360.680, 0.158, 0.071] + ) + + np.testing.assert_array_almost_equal(db.dBm2Vrms(dbm_data), dbv_data, decimal=3) diff --git a/src/tests/test_gaussian_beam/test_fit_beam_data.py b/src/tests/test_gaussian_beam/test_fit_beam_data.py index 702a4bc..9e0bd50 100644 --- a/src/tests/test_gaussian_beam/test_fit_beam_data.py +++ b/src/tests/test_gaussian_beam/test_fit_beam_data.py @@ -1,16 +1,17 @@ +from pathlib import Path from unittest import TestCase -import matplotlib.pyplot as plt import numpy as np -import pandas as pd from openqlab import io from openqlab.analysis.gaussian_beam import GaussianBeam, fit_beam_data +filedir = Path(__file__).parent + class TestFitBeamData(TestCase): def setUp(self): - self.data = io.read("./test_gaussian_beam/data.txt") + self.data = io.read(f"{filedir}/data.txt") self.data.index *= 1e-2 self.data *= 0.5e-6 diff --git a/src/tests/test_io/test___init__/test_read.py b/src/tests/test_io/test___init__/test_read.py index 1d9f688..e5730a7 100644 --- a/src/tests/test_io/test___init__/test_read.py +++ b/src/tests/test_io/test___init__/test_read.py @@ -5,14 +5,16 @@ from pathlib import Path import openqlab.io from openqlab.io.data_container import DataContainer +filedir = Path(__file__).parent + class TestRead(unittest.TestCase): """ Test files starting with a dot (".") will be ignored. """ - files_path = Path(r"test_io/test_importers/data_files") - gzip_files_path = Path(r"test_io/test___init__/gzip_data_files") + files_path = Path(fr"{filedir}/../test_importers/data_files") + gzip_files_path = Path(fr"{filedir}/gzip_data_files") @classmethod def setUpClass(cls): @@ -26,15 +28,19 @@ class TestRead(unittest.TestCase): cls.binary_files = [ Path( - r"test_io/test_importers/data_files/KeysightBinary/keysight_binary.bin" + fr"{filedir}/../test_importers/data_files/KeysightBinary/keysight_binary.bin" + ), + Path( + fr"{filedir}/../test_importers/data_files/TektronixSpectrogram/test2.mat" ), - Path(r"test_io/test_importers/data_files/TektronixSpectrogram/test2.mat"), ] cls.ignore_text_files = [ - Path(r"test_io/test_importers/data_files/KeysightFRA/scope_0_comma.csv"), Path( - r"test_io/test_importers/data_files/KeysightFRA/scope_1_semicolon.csv" + fr"{filedir}/../test_importers/data_files/KeysightFRA/scope_0_comma.csv" + ), + Path( + fr"{filedir}/../test_importers/data_files/KeysightFRA/scope_1_semicolon.csv" ), ] cls.text_files = [ diff --git a/src/tests/test_io/test___init__/test_reads.py b/src/tests/test_io/test___init__/test_reads.py index 9c17ff2..d5ac0aa 100644 --- a/src/tests/test_io/test___init__/test_reads.py +++ b/src/tests/test_io/test___init__/test_reads.py @@ -4,13 +4,15 @@ from pathlib import Path from openqlab import io from openqlab.io.data_container import DataContainer +filedir = Path(__file__).parent + class TestReads(unittest.TestCase): """ Test files starting with a dot (".") will be ignored. """ - files_path = Path(r"test_io/test_importers/data_files") + files_path = Path(fr"{filedir}/../test_importers/data_files") @classmethod def setUpClass(cls): @@ -22,15 +24,19 @@ class TestReads(unittest.TestCase): cls.binary_files = [ Path( - r"test_io/test_importers/data_files/KeysightBinary/keysight_binary.bin" + fr"{filedir}/../test_importers/data_files/KeysightBinary/keysight_binary.bin" + ), + Path( + fr"{filedir}/../test_importers/data_files/TektronixSpectrogram/test2.mat" ), - Path(r"test_io/test_importers/data_files/TektronixSpectrogram/test2.mat"), ] + list((cls.files_path / "Gwinstek_LSF").glob("*")) cls.ignore_text_files = [ - Path(r"test_io/test_importers/data_files/KeysightFRA/scope_0_comma.csv"), Path( - r"test_io/test_importers/data_files/KeysightFRA/scope_1_semicolon.csv" + fr"{filedir}/../test_importers/data_files/KeysightFRA/scope_0_comma.csv" + ), + Path( + fr"{filedir}/../test_importers/data_files/KeysightFRA/scope_1_semicolon.csv" ), ] cls.text_files = [ diff --git a/src/tests/test_io/test_data_container/test_data_container.py b/src/tests/test_io/test_data_container/test_data_container.py index 8dbcdb9..9b41c9e 100644 --- a/src/tests/test_io/test_data_container/test_data_container.py +++ b/src/tests/test_io/test_data_container/test_data_container.py @@ -1,5 +1,6 @@ import unittest from copy import deepcopy +from pathlib import Path import numpy as np import pandas as pd @@ -7,6 +8,9 @@ import pandas as pd from openqlab import io from openqlab.io.data_container import DataContainer, MetaDataContainer +filedir = Path(__file__).parent +datadir = f"{filedir}/../../ContainerTestData" + def print_wrapper(func): flag = False # change to True if results should be printed to console @@ -71,7 +75,7 @@ class TestDataContainer(unittest.TestCase): ) def test_plot_with_xUnit_and_subplots(self): - dc = io.read("./ContainerTestData/plot_test.csv") + dc = io.read(f"{datadir}/plot_test.csv") dc.columns = ["impulse", "error", "amplitude"] dc.plot(subplots=True, figsize=(8, 8), grid=True) @@ -1231,7 +1235,7 @@ class TestDataContainer(unittest.TestCase): yield result def test_csv(self): - filename = "ContainerTestData/TestCSV.csv" + filename = f"{datadir}/TestCSV.csv" self.base.to_csv(filename) result = DataContainer.from_csv(filename) self.assertIsInstance(result, DataContainer) @@ -1239,7 +1243,7 @@ class TestDataContainer(unittest.TestCase): self.assertEqual(result.header, self.header1) data = io.read( - "./ContainerTestData/SuccedingImport/SpectrumAnalyzer/RhodeSchwarz/2010830P_002" + f"{datadir}/SuccedingImport/SpectrumAnalyzer/RhodeSchwarz/2010830P_002" ) data.header = {} data.to_csv(filename) @@ -1249,7 +1253,7 @@ class TestDataContainer(unittest.TestCase): self.assertEquals(result.__repr__(), data.__repr__()) def test_json(self): - filename = "./ContainerTestData/TestCSV.csv" + filename = f"{datadir}/TestCSV.csv" self.base.to_json(filename) result = DataContainer.from_json(filename) self.assertIsInstance(result, DataContainer) @@ -1257,7 +1261,7 @@ class TestDataContainer(unittest.TestCase): self.assertEqual(result.header, self.header1) data = io.read( - "./ContainerTestData/SuccedingImport/SpectrumAnalyzer/RhodeSchwarz/2010830P_002" + f"{datadir}/SuccedingImport/SpectrumAnalyzer/RhodeSchwarz/2010830P_002" ) data.header = {} data.to_json(filename) diff --git a/src/tests/test_io/test_importers/test_ascii_header/test_ascii_header.py b/src/tests/test_io/test_importers/test_ascii_header/test_ascii_header.py index 2f51932..5eee253 100644 --- a/src/tests/test_io/test_importers/test_ascii_header/test_ascii_header.py +++ b/src/tests/test_io/test_importers/test_ascii_header/test_ascii_header.py @@ -5,11 +5,14 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.ascii_header import ASCII_Header from openqlab.io.importers.utils import UnknownFileType +filedir = Path(__file__).parent +datadir = f"{filedir}/../data_files" + class TestASCII_Header(unittest.TestCase): importer = ASCII_Header - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/ASCII_Header") + files_path = Path(datadir) + supported_files_path = Path(fr"{datadir}/ASCII_Header") multiline_comment = "This is a multiline comment.\nIt continues at the next line.\nAnd another line\nwith newline." def read_file(self, file): diff --git a/src/tests/test_io/test_importers/test_data_container_csv/test_data_container_csv.py b/src/tests/test_io/test_importers/test_data_container_csv/test_data_container_csv.py index 890d04a..7cb8f83 100644 --- a/src/tests/test_io/test_importers/test_data_container_csv/test_data_container_csv.py +++ b/src/tests/test_io/test_importers/test_data_container_csv/test_data_container_csv.py @@ -7,11 +7,14 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.data_container_csv import DataContainerCSV from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent +datadir = f"{filedir}/../data_files" + class TestDataContainerCSV(unittest.TestCase): importer = DataContainerCSV - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/DataContainerCSV") + files_path = Path(datadir) + supported_files_path = Path(fr"{datadir}/DataContainerCSV") def read_file(self, file): try: diff --git a/src/tests/test_io/test_importers/test_gwinstek/test_gwinstek.py b/src/tests/test_io/test_importers/test_gwinstek/test_gwinstek.py index 0938f60..447eb95 100644 --- a/src/tests/test_io/test_importers/test_gwinstek/test_gwinstek.py +++ b/src/tests/test_io/test_importers/test_gwinstek/test_gwinstek.py @@ -7,11 +7,14 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.gwinstek import Gwinstek from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent +datadir = f"{filedir}/../data_files" + class TestGwinstek(unittest.TestCase): importer = Gwinstek - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/Gwinstek") + files_path = Path(datadir) + supported_files_path = Path(fr"{datadir}/Gwinstek") def read_file(self, file): try: @@ -52,8 +55,8 @@ class TestGwinstek(unittest.TestCase): def test_missing_data(self): files = [ - r"test_io/test_importers/test_gwinstek/missing_data.csv", - r"test_io/test_importers/test_gwinstek/missing_data_1.csv", + fr"{filedir}/missing_data.csv", + fr"{filedir}/missing_data_1.csv", ] for file in files: try: @@ -63,7 +66,7 @@ class TestGwinstek(unittest.TestCase): raise AssertionError(f"{file} did not raise {EmptyDataError}") from e def test_wrong_savemode(self): - file = r"test_io/test_importers/test_gwinstek/wrong_savemode.csv" + file = fr"{filedir}/wrong_savemode.csv" try: with self.assertRaises(ImportFailed): self.read_file(file) @@ -71,7 +74,7 @@ class TestGwinstek(unittest.TestCase): raise AssertionError(f"{file} did not raise {ImportFailed}") from e def test_missing_savemode(self): - file = r"test_io/test_importers/test_gwinstek/missing_savemode.csv" + file = fr"{filedir}/missing_savemode.csv" try: with self.assertRaises(ImportFailed): self.read_file(file) diff --git a/src/tests/test_io/test_importers/test_gwinstek_LSF/test_gwinstek_LSF.py b/src/tests/test_io/test_importers/test_gwinstek_LSF/test_gwinstek_LSF.py index 2f17175..36c0d65 100644 --- a/src/tests/test_io/test_importers/test_gwinstek_LSF/test_gwinstek_LSF.py +++ b/src/tests/test_io/test_importers/test_gwinstek_LSF/test_gwinstek_LSF.py @@ -7,11 +7,14 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.gwinstek_lsf import Gwinstek_LSF from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent +datadir = f"{filedir}/../data_files" + class TestGwinstek_LSF(unittest.TestCase): importer = Gwinstek_LSF - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/Gwinstek_LSF") + files_path = Path(datadir) + supported_files_path = Path(fr"{datadir}/Gwinstek_LSF") def read_file(self, file): try: diff --git a/src/tests/test_io/test_importers/test_hp4395a/test_hp4395a.py b/src/tests/test_io/test_importers/test_hp4395a/test_hp4395a.py index 81ec2b9..38a8978 100644 --- a/src/tests/test_io/test_importers/test_hp4395a/test_hp4395a.py +++ b/src/tests/test_io/test_importers/test_hp4395a/test_hp4395a.py @@ -8,11 +8,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.hp4395a import HP4395A from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestHP4395A(unittest.TestCase): importer = HP4395A - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/HP4395A") + files_path = Path(f"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/HP4395A") def read_file(self, file): try: @@ -49,7 +51,7 @@ class TestHP4395A(unittest.TestCase): raise AssertionError(f"{file} did not raise {UnknownFileType}") from e def test_missing_data(self): - files = [r"test_io/test_importers/test_hp4395a/missing_data.txt"] + files = [fr"{filedir}/missing_data.txt"] for file in files: try: with self.assertRaises(EmptyDataError): diff --git a/src/tests/test_io/test_importers/test_keysight_binary/test_keysight_binary.py b/src/tests/test_io/test_importers/test_keysight_binary/test_keysight_binary.py index 1f27f8b..0683397 100644 --- a/src/tests/test_io/test_importers/test_keysight_binary/test_keysight_binary.py +++ b/src/tests/test_io/test_importers/test_keysight_binary/test_keysight_binary.py @@ -6,11 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.keysight_binary import KeysightBinary from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestKeysightBinary(unittest.TestCase): importer = KeysightBinary - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/KeysightBinary") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/KeysightBinary") def read_file(self, file): try: diff --git a/src/tests/test_io/test_importers/test_keysight_csv/test_keysight_csv.py b/src/tests/test_io/test_importers/test_keysight_csv/test_keysight_csv.py index 59ee838..a0d1305 100644 --- a/src/tests/test_io/test_importers/test_keysight_csv/test_keysight_csv.py +++ b/src/tests/test_io/test_importers/test_keysight_csv/test_keysight_csv.py @@ -8,11 +8,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.keysight_csv import KeysightCSV from openqlab.io.importers.utils import UnknownFileType +filedir = Path(__file__).parent + class TestKeysightCSV(unittest.TestCase): importer = KeysightCSV - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/KeysightCSV") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/KeysightCSV") def read_file(self, file): try: @@ -49,7 +51,7 @@ class TestKeysightCSV(unittest.TestCase): raise AssertionError(f"{file} did not raise {UnknownFileType}") from e def test_missing_data(self): - file = r"test_io/test_importers/test_keysight_csv/missing_data.csv" + file = fr"{filedir}/missing_data.csv" try: with self.assertRaises(EmptyDataError): self.read_file(file) diff --git a/src/tests/test_io/test_importers/test_keysight_fra/test_keysight_fra.py b/src/tests/test_io/test_importers/test_keysight_fra/test_keysight_fra.py index b5147eb..6fed903 100644 --- a/src/tests/test_io/test_importers/test_keysight_fra/test_keysight_fra.py +++ b/src/tests/test_io/test_importers/test_keysight_fra/test_keysight_fra.py @@ -6,11 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.keysight_fra import KeysightFRA from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestKeysightFRA(unittest.TestCase): importer = KeysightFRA - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/KeysightFRA") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/KeysightFRA") def read_file(self, file): try: @@ -47,7 +49,7 @@ class TestKeysightFRA(unittest.TestCase): raise AssertionError(f"{file} did not raise {UnknownFileType}") from e def test_missing_data(self): - file = Path(r"test_io/test_importers/test_keysight_fra/missing_data.csv") + file = Path(fr"{filedir}/missing_data.csv") try: with self.assertRaises(ImportFailed): self.read_file(file) diff --git a/src/tests/test_io/test_importers/test_rhode_schwarz/test_rhode_schwarz.py b/src/tests/test_io/test_importers/test_rhode_schwarz/test_rhode_schwarz.py index a972376..15275ea 100644 --- a/src/tests/test_io/test_importers/test_rhode_schwarz/test_rhode_schwarz.py +++ b/src/tests/test_io/test_importers/test_rhode_schwarz/test_rhode_schwarz.py @@ -6,11 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.rhode_schwarz import RhodeSchwarz from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestRhodeSchwarz(unittest.TestCase): importer = RhodeSchwarz - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/RhodeSchwarz") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/RhodeSchwarz") def read_file(self, file): try: @@ -49,7 +51,7 @@ class TestRhodeSchwarz(unittest.TestCase): raise AssertionError(f"{file} did not raise {UnknownFileType}") from e def test_missing_data(self): - file = Path(r"test_io/test_importers/test_rhode_schwarz/missing_data.csv") + file = Path(fr"{filedir}/missing_data.csv") try: with self.assertRaises(ImportFailed): self.read_file(file) @@ -57,7 +59,7 @@ class TestRhodeSchwarz(unittest.TestCase): raise AssertionError(f"{file} did not raise {ImportFailed}") from e def test_missing_xUnit(self): - file = Path(r"test_io/test_importers/test_rhode_schwarz/missing_xUnit") + file = Path(fr"{filedir}/missing_xUnit") data = self.read_file(file) self.assertEqual("x", data.index.name) diff --git a/src/tests/test_io/test_importers/test_tektronix/test_tektronix.py b/src/tests/test_io/test_importers/test_tektronix/test_tektronix.py index e9b9b64..13574cf 100644 --- a/src/tests/test_io/test_importers/test_tektronix/test_tektronix.py +++ b/src/tests/test_io/test_importers/test_tektronix/test_tektronix.py @@ -6,11 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.tektronix import Tektronix from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestTektronix(unittest.TestCase): importer = Tektronix - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/Tektronix") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/Tektronix") def read_file(self, file): try: @@ -47,18 +49,18 @@ class TestTektronix(unittest.TestCase): raise AssertionError(f"{file} did not raise {UnknownFileType}") from e def test_missing_data(self): - file = r"test_io/test_importers/test_tektronix/missing_data.csv" + file = fr"{filedir}/missing_data.csv" with self.assertRaises(ImportFailed): data = self.importer(file).read() def test_wrong_number_points(self): - file = r"test_io/test_importers/test_tektronix/wrong_number_points.csv" + file = fr"{filedir}/wrong_number_points.csv" error_message = f"'Tektronix' importer: Number of points does not fit number of values in file '{file}'." with self.assertRaisesRegex(ImportFailed, error_message): self.importer(file).read() def test_different_frequency_axis(self): - file = r"test_io/test_importers/test_tektronix/different_frequency_axis.csv" + file = fr"{filedir}/different_frequency_axis.csv" error_message = f"'Tektronix' importer: Traces in file '{file}' do not have equal frequency axis." with self.assertRaisesRegex(ImportFailed, error_message): self.importer(file).read() diff --git a/src/tests/test_io/test_importers/test_tektronix_dpx/test_tektronix_dpx.py b/src/tests/test_io/test_importers/test_tektronix_dpx/test_tektronix_dpx.py index a7320d1..fe3ae47 100644 --- a/src/tests/test_io/test_importers/test_tektronix_dpx/test_tektronix_dpx.py +++ b/src/tests/test_io/test_importers/test_tektronix_dpx/test_tektronix_dpx.py @@ -6,11 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.tektronix_dpx import TektronixDPX from openqlab.io.importers.utils import ImportFailed, UnknownFileType +filedir = Path(__file__).parent + class TestTektronix(unittest.TestCase): importer = TektronixDPX - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path(r"test_io/test_importers/data_files/TektronixDPX") + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/TektronixDPX") def read_file(self, file): try: diff --git a/src/tests/test_io/test_importers/test_tektronix_spectrogram/test_tektronix_spectrogram.py b/src/tests/test_io/test_importers/test_tektronix_spectrogram/test_tektronix_spectrogram.py index 75552db..4f92982 100644 --- a/src/tests/test_io/test_importers/test_tektronix_spectrogram/test_tektronix_spectrogram.py +++ b/src/tests/test_io/test_importers/test_tektronix_spectrogram/test_tektronix_spectrogram.py @@ -6,13 +6,13 @@ from openqlab.io.data_container import DataContainer from openqlab.io.importers.tektronix_spectrogram import TektronixSpectrogram from openqlab.io.importers.utils import UnknownFileType +filedir = Path(__file__).parent + class TestTekSpectrogram(unittest.TestCase): importer = TektronixSpectrogram - files_path = Path(r"test_io/test_importers/data_files") - supported_files_path = Path( - r"test_io/test_importers/data_files/TektronixSpectrogram" - ) + files_path = Path(fr"{filedir}/../data_files") + supported_files_path = Path(fr"{files_path}/TektronixSpectrogram") def read_file(self, file): try: diff --git a/src/tests/test_plots/test_plots.py b/src/tests/test_plots/test_plots.py index 501adcd..e36188f 100644 --- a/src/tests/test_plots/test_plots.py +++ b/src/tests/test_plots/test_plots.py @@ -1,4 +1,5 @@ import unittest +from pathlib import Path import matplotlib import matplotlib.pyplot as plt @@ -7,12 +8,15 @@ from numpy import array_equal from openqlab import io, plots from openqlab.plots.frequency_domain import amplitude_phase, relative_input_noise +filedir = Path(__file__).parent +datadir = Path(f"{filedir}/../ContainerTestData/SuccedingImport") + class TestRIN(unittest.TestCase): def setUp(self): files = [] for i in range(1, 4): - files.append(f"./ContainerTestData/SuccedingImport/ASCII/flipper{i}.TXT") + files.append(f"{datadir}/ASCII/flipper{i}.TXT") self.data = io.read(files) def test_default_values(self): @@ -45,9 +49,7 @@ class TestRIN(unittest.TestCase): class TestAmplitudePhase(unittest.TestCase): def setUp(self): - self.data = io.read( - r"./ContainerTestData/SuccedingImport/Oscilloscope/KeysightFra/scope_0.csv" - ) + self.data = io.read(fr"{datadir}/Oscilloscope/KeysightFra/scope_0.csv") def test_default_plot(self): plot = amplitude_phase(self.data["Gain (dB)"], self.data["Phase (deg)"]) @@ -60,9 +62,7 @@ class TestAmplitudePhase(unittest.TestCase): class TestTimeDomain(unittest.TestCase): def setUp(self): - self.data = io.read( - r"./ContainerTestData/SuccedingImport/Oscilloscope/Keysight/20180606_001.csv" - ) + self.data = io.read(fr"{datadir}/Oscilloscope/Keysight/20180606_001.csv") def test_default_plot(self): fig = plots.scope(self.data) -- GitLab