Skip to content

Commit

Permalink
Merge pull request #133 from arcondello/testing
Browse files Browse the repository at this point in the history
Remove test mixins and replace with custom asserts
  • Loading branch information
arcondello authored Mar 8, 2018
2 parents f274b34 + a2f03d7 commit 9b75ce8
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 307 deletions.
2 changes: 1 addition & 1 deletion dimod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dimod.reference import *
import dimod.reference

import dimod.test
import dimod.testing

from dimod.binary_quadratic_model import *
import dimod.binary_quadratic_model
Expand Down
2 changes: 0 additions & 2 deletions dimod/test/__init__.py

This file was deleted.

64 changes: 0 additions & 64 deletions dimod/test/composite_api.py

This file was deleted.

67 changes: 0 additions & 67 deletions dimod/test/sampler_api.py

This file was deleted.

1 change: 1 addition & 0 deletions dimod/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from dimod.testing.asserts import *
65 changes: 65 additions & 0 deletions dimod/testing/asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""todo"""
from collections import Mapping

import dimod


# def assert_response_correct_energy(response, bqm):
# """todo"""
# for sample in response:
# assert isinstance(sample, Mapping), "'for sample in response', each sample should be a Mapping"

# for v, value in sample.items():
# assert v in bqm.linear, 'sample contains a variable not in the given bqm'
# assert value in bqm.vartype.value, 'sample contains a value not of the correct type'

# for v in bqm.linear:
# assert v in sample, "bqm contains a variable not in sample"


def assert_sampler_api(sampler):
"""Assert that an instantiated sampler has the correct properties and methods exposed.
"""

# abstract base class

assert isinstance(sampler, dimod.Sampler), "must be a dimod Sampler."

# sample methods

assert hasattr(sampler, 'sample'), "instantiated sampler must have a 'sample' method"
assert callable(sampler.sample), "instantiated sampler must have a 'sample' method"

assert hasattr(sampler, 'sample_ising'), "instantiated sampler must have a 'sample_ising' method"
assert callable(sampler.sample_ising), "instantiated sampler must have a 'sample_ising' method"

assert hasattr(sampler, 'sample_qubo'), "instantiated sampler must have a 'sample_qubo' method"
assert callable(sampler.sample_qubo), "instantiated sampler must have a 'sample_qubo' method"

# properties

msg = "instantiated sampler must have a 'parameters' property, set to a Mapping"
assert hasattr(sampler, 'parameters'), msg
assert not callable(sampler.parameters), msg
assert isinstance(sampler.parameters, Mapping), msg

msg = "instantiated sampler must have a 'properties' property, set to a Mapping"
assert hasattr(sampler, 'properties'), msg
assert not callable(sampler.properties), msg
assert isinstance(sampler.properties, Mapping), msg


def assert_composite_api(composed_sampler):
"""todo"""

assert isinstance(composed_sampler, dimod.Composite)

# todo: check properties (including correctness of mixins)


def assert_structured_api(sampler):
"""todo"""

assert isinstance(sampler, dimod.Structured), "must be a Structured sampler"

# todo: check properties (including correctness of mixins)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
'dimod.reference',
'dimod.reference.composites',
'dimod.reference.samplers',
'dimod.test']
'dimod.testing']

setup(
name='dimod',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_exact_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dimod


class TestExactSolver(unittest.TestCase, dimod.test.SamplerAPITestCaseMixin):
class TestExactSolver(unittest.TestCase):
def setUp(self):
self.sampler = dimod.ExactSolver()
self.sampler_factory = dimod.ExactSolver
Expand Down
2 changes: 1 addition & 1 deletion tests/test_random_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dimod


class TestRandomSampler(unittest.TestCase, dimod.test.SamplerAPITestCaseMixin):
class TestRandomSampler(unittest.TestCase):
def setUp(self):
self.sampler = dimod.RandomSampler()
self.sampler_factory = dimod.RandomSampler
2 changes: 1 addition & 1 deletion tests/test_simulated_annealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dimod.reference.samplers.simulated_annealing import ising_simulated_annealing, greedy_coloring


class TestSASampler(unittest.TestCase, dimod.test.SamplerAPITestCaseMixin):
class TestSASampler(unittest.TestCase):
def setUp(self):
self.sampler = dimod.SimulatedAnnealingSampler()
self.sampler_factory = dimod.SimulatedAnnealingSampler
Expand Down
Loading

0 comments on commit 9b75ce8

Please sign in to comment.