Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

WIP: Include MockDWaveSampler In Unit Tests#51

Open
m3ller wants to merge 17 commits into
dwavesystems:masterfrom
m3ller:feature/include-mockdwavesampler-in-unit-tests
Open

WIP: Include MockDWaveSampler In Unit Tests#51
m3ller wants to merge 17 commits into
dwavesystems:masterfrom
m3ller:feature/include-mockdwavesampler-in-unit-tests

Conversation

@m3ller
Copy link
Copy Markdown
Contributor

@m3ller m3ller commented Apr 23, 2019

No description provided.

Melody Wong added 17 commits April 23, 2019 16:19
I want to test mock and the API token issue by just isolating one demo
Double check that the code works with DWaveSampler before I replace it again with a mock sampler
Previous commit did not utilize mock
macOS CI tests have python 2.7 pre-installed. When 'python -m virtualenv' gets used in the macOS tests, python 2.7 ends up being used in the virtualenv rather than python 3 (which is also installed). Will not change the call to 'python3 -m virtualenv' because we still have python 2.7 tests that also run this template
Using 'python -m virtualenv' again because it turns out that Python 3.7 comes pre-installed in Xcode and virtualenv gets shipped with Python 3.7. Default interpreter on this virtualenv is Python 3.7. Hence, I'm adding 'python -m' back into the code and then using 'pyenv' to prevent 'python -m' from defaulting to python 2.7
The Linux tests don't need pyenv, hence I don't want to install pyenv in those tests just so that macOS tests can use the template to call pyenv. Temporarily duplicating the code just to make sure that pyenv solves the problem for macOS tests; will clean up later
Want to clear cache so that we can install a different interpreter for virtualenv
This if-statement may not be portable as double square brackets may only work with Bash, but not POSIX
$PYTHON only exists for the macOS tests, hence we will just grab the python version via bash
@m3ller m3ller force-pushed the feature/include-mockdwavesampler-in-unit-tests branch from e5274ed to 1a6a71d Compare April 29, 2019 22:01
Comment thread .circleci/config.yml
# Python 2.7 case
# Note: using 2>&1 because python 2.7 sends version info to stderr
python_version="$(python --version 2>&1)"
if [[ "$python_version" =~ ^Python\ 2\.7\.[0-9]+$ ]]; then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bash pro tip: you can have regex in a variable to avoid escaping:

regex='^Python 2.7.[0-9]+$'
[[ $python_version =~ $regex ]] && pip install mock

(Also, don't need to quote vars inside [[ ]]. And here a simpler expression would be enough: [[ $pyver =~ "Python 2.7" ]].)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, looking at a bigger picture, you would want to have this requirement (for Python 2.7) in the requirements file, to simplify the build script, e.g:

mock; python_version=="2.7"

Check an example.

Comment thread maze/test_maze.py
expected_solution = {'0,1w': 1, '1,1n': 1, '1,1w': 1, '2,0n': 1, '2,1w': 1, '2,2w': 1}
fill_with_zeros(expected_solution, n_rows, n_cols, [start, end])
self.compare(response_sample, expected_solution)
with mock.patch('dwave.system.samplers.DWaveSampler', MockDWaveSampler):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify mocking by patching the whole testcase, or at least the complete test. E.g.:

@patch(...)
def test(...):
    ...

or:

@patch(...)
class Test(TestCase):
    ...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants