def test_from_data_dict(self):
        """ Test from_data_dict. """
        cdt = CompositeDataType([('one', EepromByte((1, 2))), ('two', EepromByte((1, 3)))])

        addr1 = EepromAddress(1, 2, 1)
        addr2 = EepromAddress(1, 3, 1)

        data_dict = {addr1 : EepromData(addr1, str(chr(12))),
                     addr2 : EepromData(addr2, str(chr(34)))}
        self.assertEquals([12, 34], cdt.from_data_dict(data_dict))
    def test_from_eeprom_wrong_data(self):
        """ Test from_eeprom_data with wrong data. """

        try:
            model3_data = [EepromData(EepromAddress(3, 4, 10), "hello worljkfdsjklsadfjklsadf"),
                           EepromData(EepromAddress(3, 14, 1), str(chr(123))),
                           EepromData(EepromAddress(3, 15, 2), str(chr(1) + chr(200)))]

            Model3.from_eeprom_data(model3_data)
            self.fail("Should have receive TypeError.")
        except TypeError as type_error:
            self.assertTrue("Length" in str(type_error))
    def test_from_eeprom_data(self):
        """ Test from_eeprom_data. """
        model1_data = [EepromData(EepromAddress(1, 3, 100), "test" + "\xff" * 96)]
        model1 = Model1.from_eeprom_data(model1_data, 1)
        self.assertEquals(1, model1['id'])
        self.assertEquals("test", model1['name'])

        model2_data = [EepromData(EepromAddress(3, 4, 100), "hello world" + "\xff" * 89)]
        model2 = Model2.from_eeprom_data(model2_data)
        self.assertEquals("hello world", model2['name'])

        model3_data = [EepromData(EepromAddress(3, 4, 10), "hello worl"),
                       EepromData(EepromAddress(3, 14, 1), str(chr(123))),
                       EepromData(EepromAddress(3, 15, 2), str(chr(200) + chr(1)))]
        model3 = Model3.from_eeprom_data(model3_data)
        self.assertEquals("hello worl", model3['name'])
        self.assertEquals(123, model3['link'])
        self.assertEquals(456, model3['out'])

        model6_data = [EepromData(EepromAddress(3, 4, 10), "test" + "\xff" * 6),
                       EepromData(EepromAddress(3, 14, 1), str(chr(1))),
                       EepromData(EepromAddress(3, 15, 2), str(chr(2) + chr(0)))]
        model6 = Model6.from_eeprom_data(model6_data)

        self.assertEquals("test", model6['name'])
        self.assertEquals([1, 2], model6['status'])
Beispiel #4
0
    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": "\xff" * 256}
            elif data["bank"] == 2:
                done['read2'] = True
                return {"data": "\x00" * 256}
            else:
                raise Exception("Wrong page")

        def write(data):
            """ Write dummy. """
            if 'write1' not in done:
                done['write1'] = True
                self.assertEquals(1, data["bank"])
                self.assertEquals(2, data["address"])
                self.assertEquals("abc", data["data"])
            elif 'write2' not in done:
                done['write2'] = True
                self.assertEquals(2, data["bank"])
                self.assertEquals(123, data["address"])
                self.assertEquals("More bytes", data["data"])
            elif 'write3' not in done:
                done['write3'] = True
                self.assertEquals(2, data["bank"])
                self.assertEquals(133, data["address"])
                self.assertEquals(" than 10", data["data"])
            else:
                raise Exception("Too many writes")

        communicator = MasterCommunicator(read, write)

        eeprom_file = EepromFile(communicator)
        eeprom_file.write([
            EepromData(EepromAddress(1, 2, 3), "abc"),
            EepromData(EepromAddress(2, 123, 18), "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)
Beispiel #5
0
    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": "\xff" * 256}
            else:
                raise Exception("Wrong page")

        def write(data):
            """ Write dummy. """
            self.assertEquals(1, data["bank"])
            self.assertEquals(2, data["address"])
            self.assertEquals("abc", data["data"])
            done['write'] = True

        communicator = MasterCommunicator(read, write)

        eeprom_file = EepromFile(communicator)
        eeprom_file.write([EepromData(EepromAddress(1, 2, 3), "abc")])

        self.assertTrue('read1' in done)
        self.assertTrue('write' in done)
 def test_from_eeprom_wrong_address(self):
     """ Test from_eeprom_data with wrong address. """
     try:
         model1_data = [EepromData(EepromAddress(3, 4, 100), "test" + "\xff" * 96)]
         Model1.from_eeprom_data(model1_data, 1)
         self.fail("Should have receive KeyError.")
     except KeyError as key_error:
         self.assertTrue("(B1 A3 L100)" in str(key_error))
Beispiel #7
0
    def test_write_multiple_fields_same_batch(self):
        """ Test writing multiple fields to the eeprom file. """
        done = {}

        def read(data):
            """ Read dummy. """
            if data["bank"] == 1:
                done['read'] = True
                return {"data": "\xff" * 256}
            else:
                raise Exception("Wrong page")

        def write(data):
            """ Write dummy. """
            if 'write1' not in done:
                done['write1'] = True
                self.assertEquals(1, data["bank"])
                self.assertEquals(2, data["address"])
                self.assertEquals("abc\xff\xff\xffdefg", data["data"])
            elif 'write2' not in done:
                done['write2'] = True
                self.assertEquals(1, data["bank"])
                self.assertEquals(12, data["address"])
                self.assertEquals("hijklmn", data["data"])
            else:
                raise Exception("Too many writes")

        communicator = MasterCommunicator(read, write)

        eeprom_file = EepromFile(communicator)
        eeprom_file.write([
            EepromData(EepromAddress(1, 2, 3), "abc"),
            EepromData(EepromAddress(1, 8, 11), "defghijklmn")
        ])

        self.assertTrue('read' in done)
        self.assertTrue('write1' in done)
        self.assertTrue('write2' in done)
Beispiel #8
0
    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": "\xff" * 256}
            else:
                raise Exception("Too many reads !")

        def write(data):
            """ Write dummy. """
            if state['write'] == 0:
                self.assertEquals(1, data["bank"])
                self.assertEquals(100, data["address"])
                self.assertEquals("\x00" * 10, data["data"])
                state['write'] = 1
            elif state['write'] == 1:
                self.assertEquals(1, data["bank"])
                self.assertEquals(110, data["address"])
                self.assertEquals("\x00" * 10, data["data"])
                state['write'] = 2
            else:
                raise Exception("Too many writes !")

        communicator = MasterCommunicator(read, write)
        eeprom_file = EepromFile(communicator)

        address = EepromAddress(1, 0, 256)
        read = eeprom_file.read([address])
        self.assertEquals("\xff" * 256, read[address].bytes)

        eeprom_file.write([EepromData(EepromAddress(1, 100, 20), "\x00" * 20)])

        address = EepromAddress(1, 0, 256)
        read = eeprom_file.read([address])
        self.assertEquals("\xff" * 100 + "\x00" * 20 + "\xff" * 136,
                          read[address].bytes)

        self.assertEquals(1, state['read'])
        self.assertEquals(2, state['write'])
    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": "\x00" * 256}

        def write(data):
            """ Write dummy. """
            self.assertEquals(117, data["bank"])
            self.assertEquals(248, data["address"])
            self.assertEquals("test\xff\xff\xff\xff", data["data"])
            done['done'] = True

        SetUpTestInjections(master_communicator=MasterCommunicator(read, write))

        eeprom_file = EepromFile()
        eeprom_file.write([EepromData(EepromAddress(117, 248, 8), "test\xff\xff\xff\xff")])
        self.assertTrue(done['done'])
Beispiel #10
0
    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": "\xff" * 256}
            elif state['read'] == 1:
                state['read'] = 2
                return {"data": "\xff" * 256}
            else:
                raise Exception("Too many reads !")

        def write(_):
            """ Write dummy. """
            state['write'] += 1
            raise Exception("write fails...")

        communicator = MasterCommunicator(read, write)
        eeprom_file = EepromFile(communicator)

        address = EepromAddress(1, 0, 256)
        read = eeprom_file.read([address])
        self.assertEquals("\xff" * 256, read[address].bytes)

        try:
            eeprom_file.write(
                [EepromData(EepromAddress(1, 100, 20), "\x00" * 20)])
            self.fail("Should not get here !")
        except Exception:
            pass

        address = EepromAddress(1, 0, 256)
        read = eeprom_file.read([address])
        self.assertEquals("\xff" * 256, read[address].bytes)

        self.assertEquals(2, state['read'])
        self.assertEquals(1, state['write'])