Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to initialize a test workspace #215

Merged
merged 12 commits into from
Aug 9, 2019
9 changes: 9 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------

Expand Down
2 changes: 2 additions & 0 deletions signac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,4 +62,5 @@
'buffered', 'is_buffered', 'flush', 'get_buffer_size', 'get_buffer_load',
'JSONDict',
'H5Store', 'H5StoreManager',
'testing',
]
44 changes: 44 additions & 0 deletions signac/testing.py
Original file line number Diff line number Diff line change
@@ -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.
tcmoore3 marked this conversation as resolved.
Show resolved Hide resolved
"""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
16 changes: 15 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uuid
import warnings
import logging
import itertools
import json
import pickle
from tarfile import TarFile
Expand All @@ -19,7 +20,6 @@
from signac.contrib.errors import JobsCorruptedError
from signac.contrib.errors import WorkspaceError
from signac.contrib.errors import StatepointParsingError
from signac.contrib.project import JobsCursor, Project
tcmoore3 marked this conversation as resolved.
Show resolved Hide resolved

from test_job import BaseJobTest

Expand Down Expand Up @@ -1982,5 +1982,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()