def test_append(self): element = {'test': 5, 'a':[1,2,3,5]} lista = ObservableList() lista.observer = MagicMock() lista.append(element) lista.observer.assert_called_with(UpdateType.CREATED, element, 0)
def test_insert(self): lista = ObservableList() lista.append(0) lista.append(2) lista.observer = MagicMock() lista.insert(1, 'a') lista.observer.assert_called_with(UpdateType.CREATED, 'a', 1)
def test__setitem__(self): lista = ObservableList() lista.append(1) lista.append(2) lista.append(3) lista.observer = MagicMock() lista[1] = 4 lista.observer.assert_called_with(UpdateType.UPDATED, 4, 1)
def test__setitem_equal__(self): lista = ObservableList() lista.append(1) lista.append(2) lista.append(3) lista.observer = MagicMock() lista[1] = 2 lista.observer.assert_not_called()
def test_contains(self): lista = ObservableList() lista.append(123) lista.append(456) lista.append(789) self.assertTrue(123 in lista) self.assertTrue(456 in lista) self.assertTrue(789 in lista) self.assertFalse(987 in lista) self.assertTrue(987 not in lista)
def test__delitem__(self): lista = ObservableList() lista.append(123) lista.append(456) lista.append(789) lista.observer = MagicMock() index = 1 del lista[index] lista.observer.assert_called_with(UpdateType.DELETED, 456, index)
def test_swap(self): a = {'key': 'value'} b = {'key2': 'value2'} lista = ObservableList() lista.append(a) lista.append(b) lista.observer = MagicMock() lista[0], lista[1] = lista[1], lista[0] self.assertEqual(b, lista[0]) self.assertEqual(a, lista[1]) lista.observer.assert_any_call(UpdateType.UPDATED, lista[0], 0) lista.observer.assert_any_call(UpdateType.UPDATED, lista[1], 1)
def test_pop_empty_parameter(self): lista = ObservableList() a = 'a' b = 'b' c = 'c' d = 'd' lista.append(a) lista.append(b) lista.append(c) lista.append(d) lista.observer = MagicMock() self.assertEqual(d, lista.pop()) self.assertEqual(3, len(lista)) lista.observer.assert_any_call(UpdateType.DELETED, d, len(lista))
def test_pop_with_parameter(self): lista = ObservableList() a = 'a' b = 'b' c = 'c' d = 'd' lista.append(a) lista.append(b) lista.append(c) lista.append(d) lista.observer = MagicMock() b_index = 1 self.assertEqual(b, lista.pop(b_index)) self.assertEqual(3, len(lista)) lista.observer.assert_any_call(UpdateType.DELETED, b, b_index)
def test_swap_2(self): a = {'key': 'value'} b = {'key2': 'value2'} lista = ObservableList() lista.append(a) listb = ObservableList() listb.append(b) lista.observer = MagicMock() listb.observer = MagicMock() lista[0], listb[0] = listb[0], lista[0] self.assertEqual(b, lista[0]) self.assertEqual(a, listb[0]) lista.observer.assert_called_with(UpdateType.UPDATED, lista[0], 0) listb.observer.assert_called_with(UpdateType.UPDATED, listb[0], 0)
def test_remove(self): lista = ObservableList() lista.append(1) lista.append('2') lista.append(3) lista.observer = MagicMock() lista.remove('2') lista.observer.assert_called_with(UpdateType.DELETED, '2', 1)
class Bank(object): """ Bank is a data structure that contains :class:`Pedalboard`. It's useful for group common pedalboards, like "Pedalboards will be used in the Sunday show" A fast bank overview:: >>> bank = Bank('RHCP') >>> californication = Pedalboard('Californication') >>> # Add pedalboard in bank - mode A >>> bank.append(californication) >>> californication.bank == bank True >>> bank.pedalboards[0] == californication True >>> # Add pedalboard in bank - mode B >>> bank.pedalboards.append(Pedalboard('Dark Necessities')) >>> bank.pedalboards[1].bank == bank True >>> # If you needs change pedalboards order (swap), use pythonic mode >>> bank.pedalboards[1], bank.pedalboards[0] = bank.pedalboards[0], bank.pedalboards[1] >>> bank.pedalboards[1] == californication True >>> # Set pedalboard >>> bank.pedalboards[0] = Pedalboard("Can't Stop") >>> bank.pedalboards[0].bank == bank True >>> del bank.pedalboards[0] >>> bank.pedalboards[0] == californication # Pedalboard Can't stop rermoved, first is now the californication True You can also toggle pedalboards into different banks:: >>> bank1.pedalboards[0], bank2.pedalboards[2] = bank2.pedalboards[0], bank1.pedalboards[2] :param string name: Bank name """ def __init__(self, name): self.name = name self.pedalboards = ObservableList() self.pedalboards.observer = self._pedalboards_observer self.index = -1 self._uuid = str(uuid.uuid4()) self.manager = None self._observer = MagicMock() @property def observer(self): return self._observer @observer.setter def observer(self, observer): self._observer = observer for pedalboard in self.pedalboards: pedalboard.observer = observer def _pedalboards_observer(self, update_type, pedalboard, index): kwargs = { 'index': index, 'origin': self } if update_type == UpdateType.CREATED \ or update_type == UpdateType.UPDATED: pedalboard.bank = self pedalboard.observer = self.observer elif update_type == UpdateType.DELETED: pedalboard.bank = None pedalboard.observer = MagicMock() self.observer.on_pedalboard_updated(pedalboard, update_type, **kwargs) @property def json(self): """ Get a json decodable representation of this bank :return dict: json representation """ return self.__dict__ @property def __dict__(self): return { 'index': self.index, 'name': self.name, 'pedalboards': [pedalboard.json for pedalboard in self.pedalboards] } def append(self, pedalboard): """ Add a :class:`Pedalboard` in this bank This works same as:: >>> bank.pedalboards.append(pedalboard) or:: >>> bank.pedalboards.insert(len(bank.pedalboards), pedalboard) :param Pedalboard pedalboard: Pedalboard that will be added """ self.pedalboards.append(pedalboard)
class BanksManager(object): """ BanksManager manager the banks. In these is possible add banks, obtains the banks and register observers for will be notified when occurs changes (like added new pedalboard, rename bank, set effect param value or state) For use details, view Readme.rst example documentation. :param list[Bank] banks: Banks that will be added in this. Useful for loads banks previously loaded, like banks persisted and recovered. """ def __init__(self, banks=None): self.banks = ObservableList() self.banks.observer = self._banks_observer banks = [] if banks is None else banks self.observer_manager = ObserverManager() for bank in banks: self.append(bank) def register(self, observer): """ Register an observer for it be notified when occurs changes. For more details, see :class:`UpdatesObserver` and :class:`ModHost`. :param UpdatesObserver observer: Observer that will be notified then occurs changes """ self.observer_manager.append(observer) observer.manager = self def append(self, bank): """ Append the bank in banks manager. It will be monitored, changes in this will be notified for the notifiers. :param Bank bank: Bank that will be added in this """ self.banks.append(bank) def _banks_observer(self, update_type, bank, index): if update_type == UpdateType.CREATED \ or update_type == UpdateType.UPDATED: bank.manager = self bank.observer = self.observer_manager elif update_type == UpdateType.DELETED: bank.manager = None bank.observer_manager = MagicMock() self.observer_manager.on_bank_updated(bank, update_type, index=index, origin=self) def enter_scope(self, observer): """ Informs that changes occurs by the ``observer`` and isn't necessary informs the changes for observer :param UpdatesObserver observer: Observer that causes changes """ self.observer_manager.enter_scope(observer) def exit_scope(self): """ Closes the last observer scope added """ self.observer_manager.exit_scope()