Skip to content

Commit

Permalink
Add function to initialize a test workspace (#215)
Browse files Browse the repository at this point in the history
* Add function to initialize a test workspace

* Update changelog

* Add unit test for testing.init_jobs
  • Loading branch information
tcmoore3 authored and csadorf committed Aug 9, 2019
1 parent add19c3 commit 0345c15
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
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.
"""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
15 changes: 15 additions & 0 deletions 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 Down Expand Up @@ -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()

0 comments on commit 0345c15

Please sign in to comment.