diff --git a/changelog.txt b/changelog.txt index 4a67a2c7f..1df51ac16 100644 --- a/changelog.txt +++ b/changelog.txt @@ -17,6 +17,15 @@ Highlights - Support for compressed Collection files. +Next +---- + +Added ++++++ + + - Add function to initialize a sample data space for testing purposes + + [1.2.0] -- 2019-07-22 --------------------- diff --git a/signac/__init__.py b/signac/__init__.py index a406f0cd1..ae65a6fb7 100644 --- a/signac/__init__.py +++ b/signac/__init__.py @@ -17,6 +17,7 @@ from . import errors from . import warnings from . import sync +from . import testing from .contrib import Project from .contrib import TemporaryProject from .contrib import get_project @@ -61,4 +62,5 @@ 'buffered', 'is_buffered', 'flush', 'get_buffer_size', 'get_buffer_load', 'JSONDict', 'H5Store', 'H5StoreManager', + 'testing', ] diff --git a/signac/testing.py b/signac/testing.py new file mode 100644 index 000000000..ae313bc05 --- /dev/null +++ b/signac/testing.py @@ -0,0 +1,44 @@ +# Copyright (c) 2019 The Regents of the University of Michigan +# All rights reserved. +# This software is licensed under the BSD 3-Clause License. +"""Functions for initializing workspaces for testing purposes.""" +from itertools import cycle + + +def init_jobs(project, nested=False, listed=False, heterogeneous=False): + """Initialize a dataspace for testing purposes + + :param project: + The project to add the jobs to + :type project: + :py:class:`~.project.Project` + :param nested: + If True, included nested state points + :type nested: + bool + :param listed: + If True, include lists as values of state point parameters + :type listed: + bool + :param heterogeneous: + If True, include heterogeneous state point parameters + :type heterogeneous: + bool + :returns: + A list containing the initialized jobs + :rtype: + list of :py:class:`~.Job` + """ + jobs_init = [] + vals = [1, 1.0, '1', False, True, None] + if nested: + vals += [{'b': v, 'c': 0} if heterogeneous else {'b': v} for v in vals] + if listed: + vals += [[v, 0] if heterogeneous else [v] for v in vals] + if heterogeneous: + for k, v in zip(cycle('ab'), vals): + jobs_init.append(project.open_job({k: v}).init()) + else: + for v in vals: + jobs_init.append(project.open_job(dict(a=v)).init()) + return jobs_init diff --git a/tests/test_project.py b/tests/test_project.py index 21f89c655..0b51b7d53 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -6,6 +6,7 @@ import uuid import warnings import logging +import itertools import json import pickle from tarfile import TarFile @@ -1982,5 +1983,19 @@ def test_pickle_jobs_directly(self): self.assertEqual(pickle.loads(pickle.dumps(job)), job) +class TestTestingProjectInitialization(BaseProjectTest): + + # Sanity check on all different combinations of inputs + def test_input_args(self): + for nested, listed, het in itertools.product([True, False], repeat=3): + with self.project.temporary_project() as tmp_project: + jobs = signac.testing.init_jobs( + tmp_project, nested=nested, listed=listed, heterogeneous=het) + self.assertGreater(len(tmp_project), 0) + self.assertEqual(len(tmp_project), len(jobs)) + # check that call does not fail: + tmp_project.detect_schema() + + if __name__ == '__main__': unittest.main()