def test_write_single_field(self): """ Write a single field to the eeprom file. """ done = {} def read(data): """ Read dummy. """ if data['bank'] == 1: done['read1'] = True return {'data': bytearray([255] * 256)} else: raise Exception('Wrong page') def write(data): """ Write dummy. """ self.assertEqual(1, data['bank']) self.assertEqual(2, data['address']) self.assertEqual(bytearray(b'abc'), data['data']) done['write'] = True SetUpTestInjections(master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() eeprom_file.write([EepromData(EepromAddress(1, 2, 3), bytearray(b'abc'))]) self.assertTrue('read1' in done) self.assertTrue('write' in done)
def test_write_end_of_page(self): """ Test writing an address that is close (< BATCH_SIZE) to the end of the page. """ done = {} def read(_): """ Read dummy. """ return {'data': bytearray([0] * 256)} def write(data): """ Write dummy. """ self.assertEqual(117, data['bank']) self.assertEqual(248, data['address']) self.assertEqual( bytearray(b'test') + bytearray([255] * 4), data['data']) done['done'] = True SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() eeprom_file.write([ EepromData(EepromAddress(117, 248, 8), bytearray(b'test') + bytearray([255] * 4)) ]) self.assertTrue(done['done'])
def test_write_multiple_fields(self): """ Test writing multiple fields to the eeprom file. """ done = {} def read(data): """ Read dummy. """ if data['bank'] == 1: done['read1'] = True return {'data': bytearray([255] * 256)} elif data['bank'] == 2: done['read2'] = True return {'data': bytearray([0] * 256)} else: raise Exception('Wrong page') def write(data): """ Write dummy. """ if 'write1' not in done: done['write1'] = True self.assertEqual(1, data['bank']) self.assertEqual(2, data['address']) self.assertEqual(bytearray(b'abc'), data['data']) elif 'write2' not in done: done['write2'] = True self.assertEqual(2, data['bank']) self.assertEqual(123, data['address']) self.assertEqual(bytearray(b'More bytes'), data['data']) elif 'write3' not in done: done['write3'] = True self.assertEqual(2, data['bank']) self.assertEqual(133, data['address']) self.assertEqual(bytearray(b' than 10'), data['data']) else: raise Exception('Too many writes') SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() eeprom_file.write([ EepromData(EepromAddress(1, 2, 3), bytearray(b'abc')), EepromData(EepromAddress(2, 123, 18), bytearray(b'More bytes than 10')) ]) self.assertTrue('read1' in done) self.assertTrue('read2' in done) self.assertTrue('write1' in done) self.assertTrue('write2' in done) self.assertTrue('write3' in done)
def test_cache_write(self): """ Test the eeprom cache on writing. """ state = {'read': 0, 'write': 0} def read(_): """ Read dummy. """ if state['read'] == 0: state['read'] = 1 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') def write(data): """ Write dummy. """ if state['write'] == 0: self.assertEqual(1, data['bank']) self.assertEqual(100, data['address']) self.assertEqual(bytearray([0] * 10), data['data']) state['write'] = 1 elif state['write'] == 1: self.assertEqual(1, data['bank']) self.assertEqual(110, data['address']) self.assertEqual(bytearray([0] * 10), data['data']) state['write'] = 2 else: raise Exception('Too many writes !') SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) eeprom_file.write( [EepromData(EepromAddress(1, 100, 20), bytearray([0] * 20))]) address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 100 + [0] * 20 + [255] * 136), read[address].bytes) self.assertEqual(1, state['read']) self.assertEqual(2, state['write'])
def test_cache_write_exception(self): """ The cache should be invalidated if a write fails. """ state = {'read': 0, 'write': 0} def read(_): """ Read dummy. """ if state['read'] == 0: state['read'] = 1 return {'data': bytearray([255] * 256)} elif state['read'] == 1: state['read'] = 2 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') def write(_): """ Write dummy. """ state['write'] += 1 raise Exception('write fails...') SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) try: eeprom_file.write( [EepromData(EepromAddress(1, 100, 20), bytearray([0] * 20))]) self.fail('Should not get here !') except Exception: pass address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) self.assertEqual(2, state['read']) self.assertEqual(1, state['write'])