From 0bdbbd5f1f1cb82a72d58a7a5e1fc6a4c80e233a Mon Sep 17 00:00:00 2001 From: Frederick Robinson Date: Sat, 31 Aug 2024 15:03:40 -0700 Subject: [PATCH 1/3] refactor "test" to a test --- pulp/sparse.py | 10 ---------- pulp/tests/test_sparse.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 pulp/tests/test_sparse.py diff --git a/pulp/sparse.py b/pulp/sparse.py index 93e0ae81..09d7231f 100644 --- a/pulp/sparse.py +++ b/pulp/sparse.py @@ -76,13 +76,3 @@ def col_based_arrays(self): lenBase.append(len(elemBase) - startsBase[-1]) startsBase.append(len(elemBase)) return numEls, startsBase, lenBase, indBase, elemBase - - -if __name__ == "__main__": - """unit test""" - rows = list(range(10)) - cols = list(range(50, 60)) - mat = Matrix(rows, cols) - mat.add(1, 52, "item") - mat.add(2, 54, "stuff") - print(mat.col_based_arrays()) diff --git a/pulp/tests/test_sparse.py b/pulp/tests/test_sparse.py new file mode 100644 index 00000000..45fefe5f --- /dev/null +++ b/pulp/tests/test_sparse.py @@ -0,0 +1,16 @@ +from pulp.sparse import Matrix + + +def test_sparse(): + rows = list(range(10)) + cols = list(range(50, 60)) + mat = Matrix(rows, cols) + mat.add(1, 52, "item") + mat.add(2, 54, "stuff") + assert mat.col_based_arrays() == ( + 2, + [0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2], + [0, 0, 1, 0, 1, 0, 0, 0, 0, 0], + [1, 2], + ["item", "stuff"], + ) From 900001b98153b1198c5b99455ad4634d9c02d8c2 Mon Sep 17 00:00:00 2001 From: Frederick Robinson Date: Sun, 1 Sep 2024 20:41:02 -0700 Subject: [PATCH 2/3] register test_sparse in `run_tests.py` --- pulp/tests/run_tests.py | 22 +++++++++++----------- pulp/tests/test_sparse.py | 29 ++++++++++++++++------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pulp/tests/run_tests.py b/pulp/tests/run_tests.py index 0a6abd2a..151cb05e 100644 --- a/pulp/tests/run_tests.py +++ b/pulp/tests/run_tests.py @@ -1,6 +1,6 @@ import unittest import pulp -from pulp.tests import test_pulp, test_examples, test_gurobipy_env +from pulp.tests import test_pulp, test_examples, test_gurobipy_env, test_sparse def pulpTestAll(test_docs=False): @@ -16,20 +16,20 @@ def pulpTestAll(test_docs=False): raise pulp.PulpError("Tests Failed") -def get_test_suite(test_docs=False): - # Tests +def get_test_suite(test_docs: bool = False) -> unittest.TestSuite: loader = unittest.TestLoader() suite_all = unittest.TestSuite() - # we get suite with all PuLP tests - pulp_solver_tests = loader.loadTestsFromModule(test_pulp) - suite_all.addTests(pulp_solver_tests) - # Add tests for gurobipy env - gurobipy_env = loader.loadTestsFromModule(test_gurobipy_env) - suite_all.addTests(gurobipy_env) + + suite_all.addTests(loader.loadTestsFromModule(test_pulp)) + suite_all.addTests(loader.loadTestsFromModule(test_sparse)) + suite_all.addTests(loader.loadTestsFromModule(test_gurobipy_env)) + # We add examples and docs tests if test_docs: - docs_examples = loader.loadTestsFromTestCase(test_examples.Examples_DocsTests) - suite_all.addTests(docs_examples) + suite_all.addTests( + loader.loadTestsFromTestCase(test_examples.Examples_DocsTests) + ) + return suite_all diff --git a/pulp/tests/test_sparse.py b/pulp/tests/test_sparse.py index 45fefe5f..4ea921aa 100644 --- a/pulp/tests/test_sparse.py +++ b/pulp/tests/test_sparse.py @@ -1,16 +1,19 @@ +import unittest + from pulp.sparse import Matrix -def test_sparse(): - rows = list(range(10)) - cols = list(range(50, 60)) - mat = Matrix(rows, cols) - mat.add(1, 52, "item") - mat.add(2, 54, "stuff") - assert mat.col_based_arrays() == ( - 2, - [0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2], - [0, 0, 1, 0, 1, 0, 0, 0, 0, 0], - [1, 2], - ["item", "stuff"], - ) +class SparseTest(unittest.TestCase): + def test_sparse(self): + rows = list(range(10)) + cols = list(range(50, 60)) + mat = Matrix(rows, cols) + mat.add(1, 52, "item") + mat.add(2, 54, "stuff") + assert mat.col_based_arrays() == ( + 2, + [0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2], + [0, 0, 1, 0, 1, 0, 0, 0, 0, 0], + [1, 2], + ["item", "stuff"], + ) From 4e4dea02484b486124b3b8b4ab7dcb3c8ed0051a Mon Sep 17 00:00:00 2001 From: Frederick Robinson Date: Sun, 1 Sep 2024 20:51:09 -0700 Subject: [PATCH 3/3] isort --- pulp/tests/run_tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pulp/tests/run_tests.py b/pulp/tests/run_tests.py index 151cb05e..7b5ce9a4 100644 --- a/pulp/tests/run_tests.py +++ b/pulp/tests/run_tests.py @@ -1,6 +1,7 @@ import unittest + import pulp -from pulp.tests import test_pulp, test_examples, test_gurobipy_env, test_sparse +from pulp.tests import test_examples, test_gurobipy_env, test_pulp, test_sparse def pulpTestAll(test_docs=False):