| blc2 - Python library and frontend for running theatrical lighting and SFX. Written for the English 2041F fall 2019 theatre production of "The Cenci".
git clone https://benconnors.ca/git-repos/blc2 |
test_functions_audio.py (2973B) - raw
1 """Tests for the audio primitive.""" 2 3 import xml.etree.ElementTree as et 4 5 import pytest 6 7 from blc2.functions.audio import Audio 8 from blc2.constants import BXW 9 from blc2.exceptions import LoadError 10 11 def test_audio(aws): 12 a = Audio(aws) 13 14 a.fade_out = 1000 15 a.fade_out = 1000 16 a.fade_in = 1000 17 a.fade_in = 1000 18 a.filename = "nonexistant" 19 20 assert a.audio_scope == {"nonexistant"} 21 assert not a.scope 22 assert a.duration == 0 23 assert a.actual_duration == 0 24 25 a.filename = "tests/silence.m4a" 26 assert a.filename == "tests/silence.m4a" 27 assert a.audio_scope == {"tests/silence.m4a"} 28 assert a.duration == 2024 29 assert a.actual_duration == 3024 30 31 a.fade_out = 500 32 assert a.fade_out == 500 33 assert a.duration == 2524 34 assert a.actual_duration == 3024 35 36 lc, ac, _ = a.render(0) 37 assert not lc 38 assert len(ac) == 1 39 assert ac[0][1:] == ("tests/silence.m4a", 0, 1000, 2524, 500) 40 41 _, ac2, _ = a.render(3000) 42 assert ac2 == ac 43 44 _, ac, _ = a.render(5000) 45 assert not ac 46 47 with pytest.raises(ValueError): 48 _ = Audio(aws, id_=101, fade_in=-10) 49 50 assert 101 not in aws.functions 51 52 fout = a.fade_out 53 with pytest.raises(ValueError): 54 a.fade_out = -50 55 assert a.fade_out == fout 56 57 fin = a.fade_in 58 with pytest.raises(ValueError): 59 a.fade_in = -50 60 assert a.fade_in == fin 61 62 a._set_duration(10000) 63 assert a.copy_data(a.get_data()) is None 64 65 a.filename = None 66 assert a.duration == 0 67 assert a.actual_duration == 0 68 69 def test_audio_serialize(aws, test_xml_eq): 70 a1 = Audio(aws, id_=123, name="Test Audio 1", fade_out=100, filename="test.wav") 71 a2 = Audio(aws, id_=124, name="Test Audio 2", filename=None) 72 73 test = (a1, a2) 74 success = [ 75 """<function type="Audio" xmlns="{}" id="123" name="Test Audio 1" fade-in="0" fade-out="100" filename="test.wav"/>""", 76 """<function type="Audio" xmlns="{}" id="124" name="Test Audio 2" fade-in="0" fade-out="0"/>""" 77 ] 78 79 for case, s in zip(test, success): 80 s = s.replace('\n', "").format(BXW.strip("{}")) 81 assert test_xml_eq(case.serialize(), s) 82 83 def test_audio_deserialize(aws, test_xml_eq): 84 fail = [ 85 """<function xmlns={} type="asdf"/>""", 86 """<asdf/>""", 87 """<function xmlns={} type="Audio" id="asdf"/>""", 88 """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-out="asdf"/>""", 89 """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="asdf"/>""", 90 ] 91 92 for case in fail: 93 case = case.format('"'+BXW.strip("{}")+'"') 94 with pytest.raises(LoadError): 95 Audio.deserialize(aws, et.fromstring(case)) 96 97 case = """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="123" fade-out="321" filename="test.wav"/>""" 98 case = case.format('"'+BXW.strip("{}")+'"') 99 100 s = Audio.deserialize(aws, et.fromstring(case)) 101 102 assert test_xml_eq(s.serialize(), case)