Example #1
0
    def test_write(self):
        header_size = 8
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)
        with gzip.open(TEST_FILE, 'r') as fi:
            for deviceapp in self.deviceapps:
                magic, device_type, message_length = unpack(
                    '<Ihh', fi.read(header_size))
                self.assertEqual(magic, MAGIC)
                self.assertEqual(device_type, DEVICE_APPS_TYPE)
                message = fi.read(message_length)

                da = dapps_pb2.DeviceApps()
                da.ParseFromString(message)

                da_orig = dapps_pb2.DeviceApps()
                da_orig.device.id = deviceapp['device']['id']
                da_orig.device.type = deviceapp['device']['type']
                da_orig.apps.extend(deviceapp['apps'])
                if 'lat' in deviceapp:
                    da_orig.lat = deviceapp['lat']
                if 'lon' in deviceapp:
                    da_orig.lon = deviceapp['lon']

                self.assertEqual(da, da_orig)
Example #2
0
    def test_write(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)

        with gzip.open(TEST_FILE) as fd:
            data = fd.read()
        self.assertEquals(bytes_written, len(data))

        offset = 0
        for el in self.deviceapps:
            magic, dev_apps_type, length = unpack('<IHH',
                                                  data[offset:offset + 8])
            self.assertEquals(magic, MAGIC)
            self.assertEquals(dev_apps_type, DEVICE_APPS_TYPE)

            msg_data = data[offset + 8:offset + 8 + length]
            da = dapps.DeviceApps()
            da.ParseFromString(msg_data)

            da_orig = dapps.DeviceApps()
            da_orig.device.id = el['device']['id']
            da_orig.device.type = el['device']['type']
            da_orig.apps.extend(el['apps'])
            if 'lat' in el:
                da_orig.lat = el['lat']
            if 'lon' in el:
                da_orig.lon = el['lon']

            self.assertEquals(da, da_orig)

            offset += 8 + length
Example #3
0
    def test_write(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)

        # Test Data
        header_size = 8
        with gzip.open(TEST_FILE, 'rb') as f:
            for deviceapp in self.deviceapps:
                # Test Header
                header = f.read(header_size)
                magic, dev_apps_type, length = struct.unpack('<IHH', header)
                self.assertEqual(magic, MAGIC)
                self.assertEqual(dev_apps_type, DEVICE_APPS_TYPE)

                # Test Body
                raw_data = f.read(length)
                data = deviceapps_pb2.DeviceApps()
                data.ParseFromString(raw_data)
                self.assertEqual(data.device.id,
                                 deviceapp.get('device', {}).get('id', ''))
                self.assertEqual(data.device.type,
                                 deviceapp.get('device', {}).get('type', ''))
                self.assertEqual(data.lat, deviceapp.get('lat', 0))
                self.assertEqual(data.lon, deviceapp.get('lon', 0))
                self.assertEqual(data.apps, deviceapp.get('apps', []))
Example #4
0
    def test_write(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)
        with gzip.open(TEST_FILE) as f:
            for deviceapp in self.deviceapps:
                # Test header
                header = f.read(8)  # uint32 + 2 * uint16 (arch depend)
                magic, dev_apps_type, length = struct.unpack('<IHH', header)
                self.assertEqual(magic, MAGIC)
                self.assertEqual(dev_apps_type, DEVICE_APPS_TYPE)

                # Test unpacked data
                packed = f.read(length)
                unpacked = deviceapps_pb2.DeviceApps()
                unpacked.ParseFromString(packed)
                self.assertEqual(unpacked.device.type,
                                 deviceapp['device']['type'])
                self.assertEqual(unpacked.device.id, deviceapp['device']['id'])
                self.assertEqual(unpacked.HasField('lat'), 'lat' in deviceapp)
                self.assertEqual(unpacked.HasField('lon'), 'lon' in deviceapp)
                if unpacked.HasField('lat'):
                    self.assertEqual(unpacked.lat, deviceapp['lat'])
                if unpacked.HasField('lon'):
                    self.assertEqual(unpacked.lon, deviceapp['lon'])
                self.assertEqual(unpacked.apps, deviceapp['apps'])
Example #5
0
    def test_write(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)
        with gzip.open(TEST_FILE) as fd:
            item_index = 0
            while True:
                header = self.read_header(fd)
                if not header:
                    break
                magic, type_, next_item_len = header
                self.assertEqual(magic, MAGIC)
                self.assertEqual(type_, DEVICE_APPS_TYPE)
                self.assertTrue(next_item_len >= 0)

                reference_item = self.deviceapps[item_index]
                device_apps = deviceapps_pb2.DeviceApps()

                data = fd.read(next_item_len)
                device_apps.ParseFromString(data)

                self.assertEqual(device_apps.device.id,
                                 reference_item['device']['id'])
                self.assertEqual(device_apps.device.type,
                                 reference_item['device']['type'])
                self.assertEqual(device_apps.apps, reference_item['apps'])
                self.assertEqual(device_apps.lat, reference_item.get('lat', 0))
                self.assertEqual(device_apps.lon, reference_item.get('lon', 0))

                item_index += 1
            self.assertEqual(item_index, len(self.deviceapps))
Example #6
0
    def test_write(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)
        dapps = deviceapps_pb2.DeviceApps()
        with gzip.open(TEST_FILE, 'rb') as fp:
            for app in self.deviceapps:
                magic, apps_type, length = struct.unpack('Ihh', fp.read(8))
                self.assertEqual(MAGIC, magic)
                self.assertEqual(DEVICE_APPS_TYPE, apps_type)

                dapps.ParseFromString(fp.read(length))
                if hasattr(app['device'], 'type'): self.assertEqual(app['device']['type'], dapps.device.type)
                if hasattr(app['device'], 'id'): self.assertEqual(app['device']['id'], dapps.device.id)
                if hasattr(app, 'lat'): self.assertEqual(app['lat'], dapps.lat)
                if hasattr(app, 'lon'): self.assertEqual(app['lon'], dapps.lon)
                for i in range(len(app['apps'])): self.assertEqual(app['apps'][i], dapps.apps[i])
Example #7
0
    def test_write_all_params(self):
        bytes_written = pb.deviceapps_xwrite_pb(self.deviceapps, TEST_FILE)
        self.assertTrue(bytes_written > 0)

        with gzip.open(TEST_FILE, 'rb') as fd:
            for apps in self.deviceapps:
                header = fd.read(8)
                magic, device_type, length = struct.unpack('IHH', header)
                self.assertEquals(magic, MAGIC)
                self.assertEquals(device_type, DEVICE_APPS_TYPE)
                data = fd.read(length)
                device_apps = deviceapps_pb2.DeviceApps()
                device_apps.ParseFromString(data)
                self.assertEqual(device_apps.device.type,
                                 apps['device']['type'])
                self.assertEqual(device_apps.device.id, apps['device']['id'])
                self.assertEqual(device_apps.apps, apps['apps'])
                if device_apps.HasField('lat'):
                    self.assertEqual(device_apps.lat, apps['lat'])
                if device_apps.HasField('lon'):
                    self.assertEqual(device_apps.lon, apps['lon'])
Example #8
0
    def test_write(self):
        i = 0  # Для итерации по нашему тестовому массиву
        magic_str = struct.pack('I', MAGIC)
        bytes_written = pb.deviceapps_xwrite_pb(iter(self.deviceapps),
                                                TEST_FILE)
        self.assertTrue(bytes_written > 0)

        unpacked = deviceapps_pb2.DeviceApps()
        with gzip.open(TEST_FILE, 'r') as f:
            while True:
                buf = f.read(4)  # Ищем магическую последовательность
                if not buf:
                    break
                if buf == magic_str:
                    pb_type, pb_len = struct.unpack('HH', f.read(4))
                    message = f.read(pb_len)
                    unpacked.ParseFromString(message)

                device = self.deviceapps[i].get("device", None)
                if device:
                    device_type = self.deviceapps[i]["device"].get(
                        "type", None)
                    device_id = self.deviceapps[i]["device"].get("id", None)
                    self.assertEqual(unpacked.device.type, device_type)
                    self.assertEqual(unpacked.device.id, device_id)

                lat = self.deviceapps[i].get("lat", None)
                lon = self.deviceapps[i].get("lon", None)

                if lat:
                    self.assertEqual(unpacked.lat, lat)
                if lon:
                    self.assertEqual(unpacked.lon, lon)
                apps = self.deviceapps[i].get("apps", None)
                if apps:
                    self.assertListEqual(list(unpacked.apps), apps)

                i += 1

        os.remove(TEST_FILE)