Skip to content

Commit

Permalink
Test coverage bump - util.text, util.plot.style
Browse files Browse the repository at this point in the history
  • Loading branch information
morganjwilliams committed Feb 20, 2024
1 parent 5d481d9 commit 580d949
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 23 deletions.
78 changes: 77 additions & 1 deletion test/util/plot/util_plot_style.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import unittest

import matplotlib.lines
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from pyrolite.util.classification import USDASoilTexture
from pyrolite.util.plot.style import (
_mpl_sp_kw_split,
color_ternary_polygons_by_centroid,
linekwargs,
mappable_from_values,
marker_cycle,
patchkwargs,
scatterkwargs,
ternary_color,
)
from pyrolite.util.synthetic import normal_frame


class TestMarkerCycle(unittest.TestCase):
Expand All @@ -17,11 +25,79 @@ def test_iterable(self):
for i in range(15):
mkr = next(mkrs)

def test_makes_line(self):
def test_line_compatible(self):
mkrs = marker_cycle()
for i in range(10):
matplotlib.lines.Line2D([0], [0], marker=next(mkrs))


class TestSplitKwargs(unittest.TestCase):
"""
Test the function used to split scatter and line
specific keyword arguments (WIP).
"""

def test_default(self):
kw = dict(c="k", color="r", linewidth=0.5)
sctr_kw, line_kw = _mpl_sp_kw_split(kw)
self.assertTrue("c" in sctr_kw)
self.assertTrue("c" not in line_kw)
self.assertTrue("color" not in sctr_kw)
self.assertTrue("color" in line_kw)
self.assertTrue("linewidth" not in sctr_kw)
self.assertTrue("linewidth" in line_kw)


class TestMappableFromValues(unittest.TestCase):

def setUp(self):
self.fig, self.ax = plt.subplots(1)

def test_colorbar(self):
for src in [np.random.randn(10), pd.Series(np.random.randn(10))]:
with self.subTest(src=src):
mappable = mappable_from_values(src)
plt.colorbar(mappable, ax=self.ax)

def tearDown(self):
plt.close("all")


class TestTernaryColor(unittest.TestCase):
def setUp(self):
self.df = normal_frame(
columns=["CaO", "MgO", "FeO"],
size=100,
seed=42,
cov=np.array([[0.8, 0.3], [0.3, 0.8]]),
)

def test_ternary_color(self):
for src in [self.df, self.df.values]:
with self.subTest(src=src):
colors = ternary_color(
src,
alpha=0.9,
colors=["green", "0.5", [0.9, 0.1, 0.5, 0.9]],
coefficients=[0.5, 0.5, 2],
)
ax = self.df.pyroplot.scatter(c=colors)

def tearDown(self):
plt.close("all")


class TestTernaryPolygonCentroidColors(unittest.TestCase):
def setUp(self):
self.clf = USDASoilTexture()
self.ax = self.clf.add_to_axes()

def test_ternary_centroid_color(self):
color_ternary_polygons_by_centroid(self.ax)

def tearDown(self):
plt.close("all")


if __name__ == "__main__":
unittest.main()
52 changes: 30 additions & 22 deletions test/util/util_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@

class TestRemovePrefix(unittest.TestCase):
def test_prefix_present(self):
pass
self.assertEqual(remove_prefix("prefixword", "prefix"), "word")

def test_prefix_notpresent(self):
#
# for prefix in ['A_A_', 'B_', 'C_']:
# with

# self.assertFalse(s.startswith(prefix))
pass
self.assertEqual(remove_prefix("prefixword", "refix"), "prefixword")

def test_double_prefix(self):
"""Should just remove one prefix."""

pass
self.assertEqual(remove_prefix("prefixprefixword", "prefix"), "prefixword")


class TestNormaliseWhitespace(unittest.TestCase):
def test_whitepace_removal(self):
pass
self.assertEqual(normalise_whitespace("a\n\n\nb"), "a b")

def test_whitespace_preservation(self):
pass
self.assertEqual(normalise_whitespace("a b"), "a b")


class TestToWidth(unittest.TestCase):
Expand Down Expand Up @@ -128,26 +122,30 @@ def test_invalid_split_delimiters(self):

def test_initial_space(self):
"""Check that strings are stripped effectively."""
pass
# this is failing!
# self.assertEqual(titlecase(" a_b"), "AB")

def test_join_characters(self):
"""Check join charaters operate correctly."""
pass
self.assertEqual(titlecase("a_b", delim="-"), "A-B")

def test_capitalize_first_word(self):
"""Check capitalize_first operates correctly."""
pass
self.assertEqual(titlecase("a_b", capitalize_first=True), "AB")
self.assertEqual(titlecase("a_b", capitalize_first=False), "aB")

def test_exceptions(self):
"""Check execptions operate correctly."""
pass
self.assertEqual(titlecase("Sample_AAA", exceptions=["AAA"]), "SampleAAA")
self.assertEqual(titlecase("Sample_aaa", exceptions=["aaa"]), "Sampleaaa")

def test_abbreviations(self):
"""Check abbreviations operate correctly."""
pass
self.assertEqual(titlecase("Sample_ID", abbrv=["ID"]), "SampleID")

def test_original_camelcase(self):
"""Check whether original camelcase is preserved."""
pass


class TestParseEntry(unittest.TestCase):
Expand Down Expand Up @@ -236,14 +234,24 @@ def test_multiple(self):
class TestSplitRecords(unittest.TestCase):
"""Tests the regex parser for poorly formatted records."""

def setUp(self):
pass
def test_split_csv_rows(self):
self.assertEqual(split_records("a,'b'\r\nc,'d'"), ["a,'b'", "c,'d'"])

def test_single_entry(self):
pass

def test_delimiters(self):
pass
class TestSlugify(unittest.TestCase):

def test_default(self):
self.assertEqual(slugify("a b"), "a-b")
self.assertEqual(slugify("a@b"), "ab")
self.assertEqual(slugify(r"a%%b"), "ab")
self.assertEqual(slugify(r"a%% b"), "a-b")


class TestInt2Alpha(unittest.TestCase):

def test_default(self):
self.assertEqual(int_to_alpha(0), "a")
self.assertEqual(int_to_alpha(26), "aa")


if __name__ == "__main__":
Expand Down

0 comments on commit 580d949

Please sign in to comment.