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