c93f8ccb37c3707ada12b1f55b0f362ef2c27efe
[EVA-2020-02-2.git] / library / tests / test_noise.py
1 import sys
2 import mock
3 import pytest
4
5
6 def force_reimport(module):
7 """Force the module under test to be re-imported.
8
9 Because pytest runs all tests within the same scope (this makes me cry)
10 we have to do some manual housekeeping to avoid tests polluting each other.
11
12 Since conftest.py already does some sys.modules mangling I see no reason not to
13 do the same thing here.
14 """
15 if "." in module:
16 steps = module.split(".")
17 else:
18 steps = [module]
19
20 for i in range(len(steps)):
21 module = ".".join(steps[0:i + 1])
22 try:
23 del sys.modules[module]
24 except KeyError:
25 pass
26
27
28 def test_noise_setup(sounddevice, numpy):
29 force_reimport('enviroplus.noise')
30 from enviroplus.noise import Noise
31
32 noise = Noise(sample_rate=16000, duration=0.1)
33 del noise
34
35
36 def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy):
37 # Ippity zippidy what is this farce
38 # a curious function that makes my tests pass?
39 force_reimport('enviroplus.noise')
40 from enviroplus.noise import Noise
41
42 noise = Noise(sample_rate=16000, duration=0.1)
43 noise.get_amplitudes_at_frequency_ranges([
44 (100, 500),
45 (501, 1000)
46 ])
47
48 sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64')
49
50
51 def test_noise_get_noise_profile(sounddevice, numpy):
52 # Ippity zippidy what is this farce
53 # a curious function that makes my tests pass?
54 force_reimport('enviroplus.noise')
55 from enviroplus.noise import Noise
56
57 noise = Noise(sample_rate=16000, duration=0.1)
58 amp_low, amp_mid, amp_high, amp_total = noise.get_noise_profile(
59 noise_floor=100,
60 low=0.12,
61 mid=0.36,
62 high=None)
63
64 sounddevice.rec.assert_called_with(0.1 * 16000, samplerate=16000, blocking=True, channels=1, dtype='float64')
65
66
67 def test_get_amplitude_at_frequency_range(sounddevice, numpy):
68 # Ippity zippidy what is this farce
69 # a curious function that makes my tests pass?
70 force_reimport('enviroplus.noise')
71 from enviroplus.noise import Noise
72
73 noise = Noise(sample_rate=16000, duration=0.1)
74
75 noise.get_amplitude_at_frequency_range(0, 8000)
76
77 with pytest.raises(ValueError):
78 noise.get_amplitude_at_frequency_range(0, 16000)