Skip to content

Commit

Permalink
tests: test_data fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
bcliang committed Feb 29, 2024
1 parent f0d4c2a commit 6ab27a2
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 30 deletions.
8 changes: 7 additions & 1 deletion tests/test_chronoamperometry.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import os
import pandas as pd
import gamry_parser as parser
import unittest


FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)

class TestChronoAmperometry(unittest.TestCase):
def setUp(self):
pass

def test_getters(self):
gp = parser.ChronoAmperometry(filename="tests/chronoa_data.dta")
gp = parser.ChronoAmperometry(filename=os.path.join(FIXTURE_PATH, "chronoa_data.dta"))
self.assertIsNone(gp.sample_time)
self.assertEqual(gp.sample_count, 0)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_cyclicvoltammetry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import gamry_parser as parser
import unittest

FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)

class TestCyclicVoltammetry(unittest.TestCase):
def setUp(self):
Expand All @@ -16,7 +21,7 @@ def test_is_loaded(self):
self.assertIsNone(gp.experiment_type)

def test_getters(self):
gp = parser.CyclicVoltammetry(filename="tests/cv_data.dta")
gp = parser.CyclicVoltammetry(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
gp.load()
vrange = gp.v_range
self.assertEqual(vrange[0], 0.1)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 23 additions & 16 deletions tests/test_gamryparser.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import os
import numpy as np
import pandas as pd
import gamry_parser as parser
import numpy as np
import unittest
import locale


FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)


class TestGamryParser(unittest.TestCase):
def setUp(self):
pass
Expand All @@ -18,7 +25,7 @@ def test_is_loaded(self):
self.assertIsNone(gp.experiment_type)

def test_read_header(self):
gp = parser.GamryParser(filename="tests/cv_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
blob, count = gp.read_header()
self.assertEqual(count, 789)
self.assertEqual(gp.header, blob)
Expand All @@ -34,7 +41,7 @@ def test_read_header(self):
self.assertEqual(gp.header["CHECK2PARAM"].get("start"), 300)
self.assertEqual(gp.header["CHECK2PARAM"].get("finish"), 0.5)

gp = parser.GamryParser(filename="tests/cv_data_incompleteheader.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data_incompleteheader.dta"))
_, count = gp.read_header()
self.assertEqual(gp.header["DELAY"], dict(enable=False, start=300, finish=0.1))

Expand All @@ -47,10 +54,10 @@ def test_load(self):

# file defined, make sure data is loaded properly
gp = parser.GamryParser()
gp.load(filename="tests/cv_data.dta")
self.assertEqual(gp.fname, "tests/cv_data.dta")
gp.load(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
self.assertEqual(gp.fname, os.path.join(FIXTURE_PATH, "cv_data.dta"))

gp = parser.GamryParser(filename="tests/cv_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
gp.load()
self.assertEqual(gp.curve_count, 5)
curve1 = gp._curves[0]
Expand All @@ -68,29 +75,29 @@ def test_load(self):
self.assertEqual(curve5["IERange"].iloc[-1], 5)

def test_use_datetime(self):
gp = parser.GamryParser(filename="tests/chronoa_data.dta", to_timestamp=False)
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "chronoa_data.dta"), to_timestamp=False)
gp.load()
curve = gp.curve()
# 'T' should return elapsed time in seconds
self.assertEqual(curve["T"][0], 0)
self.assertEqual(curve["T"].iloc[-1], 270)

gp = parser.GamryParser(filename="tests/chronoa_data.dta", to_timestamp=True)
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "chronoa_data.dta"), to_timestamp=True)
gp.load()
curve = gp.curve()
# 'T' should return datetime objects
self.assertEqual(curve["T"][0], pd.to_datetime("3/10/2019 12:00:00"))
self.assertEqual(curve["T"].iloc[-1], pd.to_datetime("3/10/2019 12:04:30"))

def test_aborted_experiment(self):
gp = parser.GamryParser(filename="tests/eispot_data_curveaborted.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "eispot_data_curveaborted.dta"))
gp.load()
self.assertEqual(gp.curve_count, 1)
self.assertEqual(gp._curves[0].shape, (5, 10))

def test_getters(self):
locale.setlocale(locale.LC_ALL, "")
gp = parser.GamryParser(filename="tests/cv_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
gp.load()
self.assertEqual(gp.curve_count, 5)
self.assertTrue(isinstance(gp.header, dict))
Expand All @@ -102,18 +109,18 @@ def test_getters(self):

def test_indices_and_numbers(self):
locale.setlocale(locale.LC_ALL, "")
gp = parser.GamryParser(filename="tests/cv_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
gp.load()
self.assertEqual(gp.curve_indices, (0, 1, 2, 3, 4))
self.assertEqual(gp.curve_numbers, (1, 2, 3, 4, 5))

def test_ocvcurve_self(self):
locale.setlocale(locale.LC_ALL, "")
gp = parser.GamryParser(filename="tests/cv_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "cv_data.dta"))
gp.load()
self.assertEqual(gp.ocv_curve, None)
self.assertEqual(gp.ocv, None)
gp = parser.GamryParser(filename="tests/ocvcurve_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "ocvcurve_data.dta"))
gp.load()
self.assertEqual(gp.ocv_curve.iloc[0]["T"], 0.258333)
self.assertEqual(gp.ocv_curve.iloc[-1]["T"], 10.3333)
Expand All @@ -124,7 +131,7 @@ def test_locale(self):
# confirm that files will load properly with non-US locales
locale.setlocale(locale.LC_ALL, "de_DE.utf8")

gp = parser.GamryParser(filename="tests/chronoa_de_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "chronoa_de_data.dta"))
gp.load()
curve = gp.curve()
self.assertEqual(curve["T"].iloc[-1], 270)
Expand All @@ -135,7 +142,7 @@ def test_locale(self):

# confirm that files can load with mismatched locales..
# pandas.read_csv() will handle 1,234,567.890 notation even in the wrong locale.
gp = parser.GamryParser(filename="tests/chronoa_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "chronoa_data.dta"))
gp.load()
curve = gp.curve()
self.assertEqual(curve["T"].iloc[-1], 270)
Expand All @@ -147,7 +154,7 @@ def test_locale(self):
# If parsed with the wrong locale, we should expect data corruption
# in the resulting dataframe.
locale.setlocale(locale.LC_ALL, "en_US.utf8")
gp = parser.GamryParser(filename="tests/chronoa_de_data.dta")
gp = parser.GamryParser(filename=os.path.join(FIXTURE_PATH, "chronoa_de_data.dta"))
gp.load()
curve = gp.curve()
self.assertEqual(curve["Vf"].iloc[0], -50)
Expand Down
9 changes: 7 additions & 2 deletions tests/test_impedance.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import os
import gamry_parser as parser
import unittest

FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)

class TestImpedance(unittest.TestCase):
def setUp(self):
pass

def test_is_loaded(self):
gp = parser.Impedance(filename="tests/eispot_data.dta")
gp = parser.Impedance(filename=os.path.join(FIXTURE_PATH, "eispot_data.dta"))
gp.load()
self.assertEqual(len(gp.curves), 1)

curve = gp.curve()
self.assertTrue(isinstance(curve["Freq"].iloc[-1], float))

def test_getters(self):
gp = parser.Impedance(filename="tests/eispot_data.dta")
gp = parser.Impedance(filename=os.path.join(FIXTURE_PATH, "eispot_data.dta"))
gp.load()
curve = gp.curve()
self.assertEqual(len(curve), 10)
Expand Down
21 changes: 16 additions & 5 deletions tests/test_ocp.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import os
import pandas as pd
import gamry_parser as parser
import unittest
from pandas.testing import assert_frame_equal


FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)


class TestOpenCircuit(unittest.TestCase):
def setUp(self):
pass

def test_use_datetime(self):
gp = parser.OpenCircuitPotential(
filename="tests/ocp_data.dta", to_timestamp=False
filename=os.path.join(FIXTURE_PATH, "ocp_data.dta"),
to_timestamp=False
)
gp.load()
curve = gp.curve()
Expand All @@ -19,7 +27,8 @@ def test_use_datetime(self):
self.assertEqual(curve["T"].iloc[-1], 105.175)

gp = parser.OpenCircuitPotential(
filename="tests/ocp_data.dta", to_timestamp=True
filename=os.path.join(FIXTURE_PATH, "ocp_data.dta"),
to_timestamp=True
)
gp.load()
curve = gp.curve()
Expand All @@ -31,7 +40,8 @@ def test_use_datetime(self):

def test_is_loaded(self):
gp = parser.OpenCircuitPotential(
filename="tests/ocp_data.dta", to_timestamp=False
filename=os.path.join(FIXTURE_PATH, "ocp_data.dta"),
to_timestamp=False
)
gp.load()
self.assertEqual(len(gp.curves), 1)
Expand All @@ -45,7 +55,7 @@ def test_is_loaded(self):

# no filename at init, only provided at load
gp = parser.OpenCircuitPotential()
gp.load(filename="tests/ocp_data.dta")
gp.load(filename=os.path.join(FIXTURE_PATH, "ocp_data.dta"))
self.assertEqual(len(gp.curves), 1)

curve = gp.curve()
Expand All @@ -57,7 +67,8 @@ def test_is_loaded(self):

def test_getters(self):
gp = parser.OpenCircuitPotential(
filename="tests/ocp_data.dta", to_timestamp=False
filename=os.path.join(FIXTURE_PATH, "ocp_data.dta"),
to_timestamp=False
)
self.assertIsNone(gp.ocv_curve)
gp.load()
Expand Down
10 changes: 8 additions & 2 deletions tests/test_squarewave.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import os
import gamry_parser as parser
from pandas import Timestamp
import unittest


FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)

class TestSquareWaveVoltammetry(unittest.TestCase):
def setUp(self):
pass
Expand All @@ -15,7 +21,7 @@ def test_is_loaded(self):

# should raise exception if non-swv data is specified
self.assertRaises(
AssertionError, gp.load, filename="test/test_chronoamperometry.dta"
AssertionError, gp.load, filename=os.path.join(FIXTURE_PATH, "test_chronoamperometry.dta")
)

# should retain nulled values if data is not loaded
Expand All @@ -27,7 +33,7 @@ def test_is_loaded(self):
self.assertEqual(gp.cycles, 0)

def test_getters(self):
gp = parser.SquareWaveVoltammetry(filename="tests/squarewave_data.dta")
gp = parser.SquareWaveVoltammetry(filename=os.path.join(FIXTURE_PATH, "squarewave_data.dta"))
gp.load()

self.assertTupleEqual(gp.v_range, (0, -0.5))
Expand Down
13 changes: 10 additions & 3 deletions tests/test_vfp600.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import os
import gamry_parser as parser
import unittest


FIXTURE_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"test_data",
)


class TestVFP600(unittest.TestCase):
def setUp(self):
pass
Expand All @@ -13,8 +20,8 @@ def test_load(self):
self.assertEqual(gp.sample_count, 0)
self.assertEqual(gp.curve_count, 0)

gp.load("tests/vfp600_data.dta")
self.assertEqual(gp.fname, "tests/vfp600_data.dta")
gp.load(os.path.join(FIXTURE_PATH, "vfp600_data.dta"))
self.assertEqual(gp.fname, os.path.join(FIXTURE_PATH, "vfp600_data.dta"))
self.assertEqual(gp.experiment_type, "VFP600")

# data file acq frequency = 15hz
Expand All @@ -25,7 +32,7 @@ def test_load(self):

def test_getters(self):
gp = parser.VFP600()
gp.load("tests/vfp600_data.dta")
gp.load(os.path.join(FIXTURE_PATH, "vfp600_data.dta"))

curve = gp.curve()
self.assertTrue((curve.columns == ["T", "Voltage", "Current"]).all())
Expand Down

0 comments on commit 6ab27a2

Please sign in to comment.