# # Aura Engine (https://gitlab.servus.at/aura/engine) # # Copyright (C) 2017-now() - The Aura Engine Team. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import threading import time import unittest from aura_engine.base.lang import DotDict, private, synchronized class TestSynchronizedAnnotation(unittest.TestCase): """ Testing the @synchronized annotation. """ value = 0 @synchronized def increment(self): value = self.value value += 1 time.sleep(0.00001) self.value = value def worker(self): for _ in range(1000): self.increment() def test_synchronized(self): threads = [] for _ in range(10): thread = threading.Thread(target=self.worker) threads.append(thread) thread.start() for thread in threads: thread.join() self.assertEqual(self.value, 10000) class TestPrivateAnnotation(unittest.TestCase): """ Testing the @private annotation. """ def test_private(self): class Container: @private def priv(self, flag: bool) -> bool: return flag def pub(self, flag: bool) -> bool: return self.priv(flag) cont = Container() self.assertTrue(cont.pub(True)) self.assertFalse(cont.pub(False)) with self.assertRaises(Exception): cont.priv(True) class TestDotDict(unittest.TestCase): """ Testing the DotDict dictionary wrapper. """ def test_dotdict_key_val(self): print(self._testMethodName) str_val = "Hello" int_val = 8 flt_val = 1.0 / 12.0 bln_val = True lst_val = ["VCO", "LFO", "ADSR", "VCF", "VCA"] dct_val = {10: "Waveform", 11: "Frequency", 20: "Cutoff", 21: "Res"} dotdict = DotDict() dotdict.str_key = str_val dotdict.int_key = int_val dotdict.flt_key = flt_val dotdict.bln_key = bln_val dotdict.lst_key = lst_val dotdict.dct_key = dct_val self.assertEqual(dotdict.str_key, str_val) self.assertEqual(dotdict.int_key, int_val) self.assertEqual(dotdict.flt_key, flt_val) self.assertEqual(dotdict.bln_key, bln_val) self.assertEqual(dotdict.lst_key, lst_val) self.assertEqual(dotdict.dct_key, dct_val) self.assertEqual(dotdict["str_key"], str_val) self.assertEqual(dotdict["int_key"], int_val) self.assertEqual(dotdict["flt_key"], flt_val) self.assertEqual(dotdict["bln_key"], bln_val) self.assertEqual(dotdict["lst_key"], lst_val) self.assertEqual(dotdict["dct_key"], dct_val) dotdict["str_key"] = None self.assertIsNone(dotdict.str_key) self.assertIsNone(dotdict.nil) def test_dotdict_init(self): print(self._testMethodName) str_val = "Hello" int_val = 8 flt_val = 1.0 / 12.0 bln_val = True lst_val = ["VCO", "LFO", "ADSR", "VCF", "VCA"] dct_val = {10: "Waveform", 11: "Frequency", 20: "Cutoff", 21: "Res"} rawdict = { "str_key": str_val, "int_key": int_val, "flt_key": flt_val, "bln_key": bln_val, "lst_key": lst_val, "dct_key": dct_val, } dotdict = DotDict(rawdict) self.assertEqual(dotdict.str_key, str_val) self.assertEqual(dotdict.int_key, int_val) self.assertEqual(dotdict.flt_key, flt_val) self.assertEqual(dotdict.bln_key, bln_val) self.assertEqual(dotdict.lst_key, lst_val) self.assertEqual(dotdict.dct_key, dct_val) if __name__ == "__main__": unittest.main()