Skip to content

Commit

Permalink
Cleanup tests, make helpers accessible as strax.testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleAalbers committed Nov 12, 2019
1 parent 3b249da commit 3f39915
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 46 deletions.
32 changes: 6 additions & 26 deletions tests/helpers.py → strax/testutils.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
import tempfile # noqa
"""Utilities to help write strax tests.
Not needed during strax operation, so this file is not imported in __init__.py
"""

from itertools import accumulate
from functools import partial

import numpy as np
from boltons import iterutils
from hypothesis import strategies
import pytest # noqa

##
# Hack to disable numba.jit
# For the mini-examples run during testing numba actually causes a massive
# performance drop. Moreover, if you make a buffer overrun bug, jitted fs
# respond "slightly" less nice (giving you junk data or segfaulting)
# Once in a while you should test without this...
##
def mock_numba():
from unittest.mock import MagicMock

class FakeNumba:

def jit(self, *args, **kwargs):
return lambda x: x

FakeNumba.caching = MagicMock()

import sys # noqa
sys.modules['numba'] = FakeNumba()


# Mock numba before importing strax
mock_numba()
import strax # noqa
import strax


# Since we use np.cumsum to get disjoint intervals, we don't want stuff
Expand Down
3 changes: 2 additions & 1 deletion tests/test_chunk_arrays.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import itertools
from .helpers import *
from strax.testutils import *


@pytest.fixture
Expand Down
8 changes: 5 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .helpers import *

import glob
import shutil
import tempfile
import os
import os.path as osp
import glob

import pytest

from strax.testutils import *

def test_core():
for allow_multiprocess in (False, True):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_data_reduction.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from hypothesis import given
from hypothesis import given, settings

from .helpers import *
from strax.testutils import *


# TODO: test with multiple fake pulses and dt != 1
@settings(deadline=None)
@given(single_fake_pulse)
def test_cut_outside_hits(records):
hits = strax.find_hits(records, threshold=0)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_general_processing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hypothesis import given, example
from hypothesis.strategies import integers
from .helpers import sorted_intervals, disjoint_sorted_intervals
from .helpers import several_fake_records
from strax.testutils import sorted_intervals, disjoint_sorted_intervals
from strax.testutils import several_fake_records

import numpy as np
import strax
Expand Down
8 changes: 4 additions & 4 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from . import helpers
from strax import testutils
from hypothesis import given
import strax


@given(helpers.sorted_bounds())
@given(testutils.sorted_bounds())
def test_sorted_bounds(bs):
assert is_sorted(bs)


@given(helpers.sorted_bounds(disjoint=True))
@given(testutils.sorted_bounds(disjoint=True))
def test_disjoint_bounds(bs):
assert is_sorted(bs)
assert is_disjoint(bs)


@given(helpers.disjoint_sorted_intervals)
@given(testutils.disjoint_sorted_intervals)
def test_dsi(intvs):
bs = list(zip(intvs['time'].tolist(), strax.endtime(intvs).tolist()))
assert is_sorted(bs)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_multi_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .helpers import *
import tempfile

from strax.testutils import *


class EvenOddSplit(strax.Plugin):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_overlap_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import helpers # Mocks numba # noqa
from strax import testutils

import numpy as np

Expand All @@ -7,7 +7,7 @@
import strax


@given(helpers.disjoint_sorted_intervals.filter(lambda x: len(x) > 0),
@given(testutils.disjoint_sorted_intervals.filter(lambda x: len(x) > 0),
strategies.integers(min_value=0, max_value=3))
# Examples that trigger issue #49
@example(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_peak_processing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .helpers import fake_hits, several_fake_records
from strax.testutils import fake_hits, several_fake_records

import numpy as np
from hypothesis import given
from hypothesis import given, settings
import hypothesis.strategies as st

import strax
Expand All @@ -10,6 +10,7 @@
@given(fake_hits,
st.one_of(st.just(1), st.just(3)),
st.one_of(st.just(0), st.just(3)))
@settings(deadline=None)
def test_find_peaks(hits, min_channels, min_area):
hits['area'] = 1
gap_threshold = 10
Expand Down Expand Up @@ -46,6 +47,7 @@ def test_find_peaks(hits, min_channels, min_area):
# TODO: add more tests, preferably test against a second algorithm


@settings(deadline=None)
@given(several_fake_records,
st.integers(min_value=0, max_value=100),
st.integers(min_value=1, max_value=100)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_pulse_processing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .helpers import single_fake_pulse
from strax.testutils import single_fake_pulse

import numpy as np
from hypothesis import given
from hypothesis import given, settings
from scipy.ndimage import convolve1d

import strax
Expand Down Expand Up @@ -46,6 +46,8 @@ def test_find_hits():
assert results == should_find_intervals



@settings(deadline=None)
@given(single_fake_pulse)
def test_find_hits_randomize(records):
"""Tests the hitfinder with whatever hypothesis can throw at it
Expand Down
Empty file removed tests/test_shm.py
Empty file.
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import helpers # noqa
import numpy as np

import strax


Expand Down

0 comments on commit 3f39915

Please sign in to comment.