Exemple #1
0
def insDataRead(dataRead):
    """Inserimento dei dati a Database
    vengono anche insiriti sensori e dispositivi
    nel caso non esistessero"""
    sensorType = session.query(SensorType).filter_by(
        code=dataRead['sensorType']).first()
    if (sensorType is None):
        sensorType = SensorType(code=dataRead['sensorType'])
        session.add(sensorType)
    deviceType = session.query(DeviceType).filter_by(
        code=dataRead['deviceType']).first()
    if (deviceType is None):
        deviceType = DeviceType(code=dataRead['deviceType'])
        session.add(deviceType)
    sensor = session.query(Sensor).filter_by(code=dataRead['sensor']).first()
    if (sensor is None):
        sensor = Sensor(code=dataRead['sensor'], sensorType=sensorType)
        session.add(sensor)
    device = session.query(Device).filter_by(code=dataRead['device']).first()
    if (device is None):
        device = Device(code=dataRead['device'], deviceType=deviceType)
        device.sensors.append(sensor)
    else:
        if (not sensor in device.sensors):
            device.sensors.append(sensor)
    session.add(device)
    deviceData = DeviceData(device=device,\
                            sensor=sensor,\
                            value=dataRead['value'],\
                            datetimeRead=datetime.datetime.utcnow()\
                            )
    session.add(deviceData)
    # Commit data
    session.commit()
    def testCRUD(self):
        # Insert device type
        dt1 = DeviceType(code='dt1', description='DESCRIPTION1')
        self.session.add(dt1)
        self.session.commit()

        # Check if inserted
        dt = self.session.query(DeviceType).filter_by(code='dt1').first()
        self.assertEquals(dt.code, dt1.code)

        # Check for non insertion
        dt = self.session.query(DeviceType).filter_by(code='dtFake').first()
        self.assertTrue(dt is None)

        # Check Update
        dt = self.session.query(DeviceType).filter_by(code='dt1').first()
        dt.description = 'DESCRIPTIONChg'
        self.session.commit()
        dtTst = self.session.query(DeviceType).filter_by(code='dt1').first()
        self.assertEquals(dtTst.description, 'DESCRIPTIONChg')

        # Check printout (to see this you have to run nosetest --nocapture
        dt = self.session.query(DeviceType).filter_by(code='dt1').first()
        print('DeviceType = %s' % dt)

        # Insert a second record and check insertion
        dt2 = DeviceType(code='dt2', description='DESCRIPTION2')
        self.session.add(dt2)
        self.session.commit()
        dt = self.session.query(DeviceType).filter_by(code='dt2').first()
        self.assertEquals(dt.code, dt2.code)

        # Rollback test
        dt3 = DeviceType(code='dt3', description='DESCRIPTION3')
        self.session.add(dt3)
        self.session.rollback()
        dt = self.session.query(DeviceType).filter_by(code='dt3').first()
        self.assertTrue(dt is None)

        # Delete record
        dt = self.session.query(DeviceType).filter_by(code='dt2').first()
        self.session.delete(dt)
        self.session.commit()
        self.assertTrue(
            self.session.query(DeviceType).filter_by(code='dt2').count() == 0)
    def testCRUD(self):
        # Insert device type
        dd1 = DeviceData(value=10.0, datetimeRead=datetime.datetime.now())
        self.session.add(dd1)
        self.session.commit()

        # Check if inserted
        dd = self.session.query(DeviceData).filter_by(value=10.0).first()
        self.assertEquals(dd.value, dd1.value)

        # Check for non insertion
        dd = self.session.query(DeviceData).filter_by(value=99.0).first()
        self.assertTrue(dd is None)

        # Check Update
        dd = self.session.query(DeviceData).filter_by(value=10.0).first()
        dd.value = 20.0
        self.session.commit()
        ddTst = self.session.query(DeviceData).filter_by(value=20.0).first()
        self.assertEquals(ddTst.value, 20.0)

        # Check printout (to see this you have to run nosetest --nocapture
        dd = self.session.query(DeviceData).filter_by(value=20.0).first()
        print('DeviceData = %s' % dd)

        # Insert a second record and check insertion
        dd2 = DeviceData(value=100.0)
        self.session.add(dd2)
        self.session.commit()
        dd = self.session.query(DeviceData).filter_by(value=100.0).first()
        self.assertEquals(dd.value, dd2.value)

        # Rollback test
        dd3 = DeviceData(value=1000.0)
        self.session.add(dd3)
        self.session.rollback()
        dd = self.session.query(DeviceData).filter_by(value=1000.0).first()
        self.assertTrue(dd is None)

        # Delete record
        dd = self.session.query(DeviceData).filter_by(value=100.0).first()
        self.session.delete(dd)
        self.session.commit()
        self.assertTrue(self.session.query(DeviceData).filter_by(value=100.0).count()==0)

        # Add to device data data read from a device
        sensorType1 = SensorType(code='sensorType1', description='SENSORTYPEDESCR1')
        sensorType2 = SensorType(code='sensorType2', description='SENSORTYPEDESCR2')
        deviceType1 = DeviceType(code='deviceType1', description='DEVICETYPEDESCR1')
        deviceType2 = DeviceType(code='deviceType2', description='DEVICETYPEDESCR2')
        sensor11 = Sensor(code='sensor11', description='SENSORDESCR11', sensorType=sensorType1)
        sensor12 = Sensor(code='sensor12', description='SENSORDESCR12', sensorType=sensorType2)
        sensor21 = Sensor(code='sensor21', description='SENSORDESCR21', sensorType=sensorType1)
        sensor22 = Sensor(code='sensor22', description='SENSORDESCR22', sensorType=sensorType2)
        device1 = Device(code='device1', description='DEVICEDESCR1',\
                         deviceType=deviceType1)
        device1.sensors.append(sensor11)
        device1.sensors.append(sensor12)
        device2 = Device(code='device2', description='DEVICEDESCR2',\
                         deviceType=deviceType2)
        device2.sensors.append(sensor21)
        device2.sensors.append(sensor22)
        deviceData11=DeviceData(datetimeRead=datetime.datetime.now(),\
                               value=11.0,\
                               device=device1)
        deviceData12=DeviceData(datetimeRead=datetime.datetime.now(),\
                               value=12.0,\
                               device=device1)
        deviceData21=DeviceData(datetimeRead=datetime.datetime.now(),\
                               value=21.0,\
                               device=device2,)
        deviceData22=DeviceData(datetimeRead=datetime.datetime.now(),\
                               value=22.0,\
                               device=device2)
        self.session.add(deviceData11)
        self.session.add(deviceData12)
        self.session.add(deviceData21)
        self.session.add(deviceData22)
        self.session.commit()
        # Print data inserted
        devicedatas = self.session.query(DeviceData)
        for dd in devicedatas:
            print('Device Data : %s'%dd)
            print('\tDevice : %s'%dd.device)
            if dd.device is not None and dd.device.sensors is not None:
                for sens in dd.device.sensors:
                    print('\t\tSensor : %s'%sens)
Exemple #4
0
    def testCRUD(self):
        # Insert device type
        device1 = Device(code='device1', description='DESCRIPTION1')
        self.session.add(device1)
        self.session.commit()

        # Check if inserted
        device = self.session.query(Device).filter_by(code='device1').first()
        self.assertEquals(device.code, device1.code)

        # Check for non insertion
        device = self.session.query(Device).filter_by(
            code='deviceFake').first()
        self.assertTrue(device is None)

        # Check Update
        device = self.session.query(Device).filter_by(code='device1').first()
        device.description = 'DESCRIPTIONChg'
        self.session.commit()
        deviceTst = self.session.query(Device).filter_by(
            code='device1').first()
        self.assertEquals(deviceTst.description, 'DESCRIPTIONChg')

        # Check printout (to see this you have to run nosetest --nocapture
        device = self.session.query(Device).filter_by(code='device1').first()
        print('Device = %s' % device)

        # Insert a second record and check insertion
        device2 = Device(code='device2', description='DESCRIPTION2')
        self.session.add(device2)
        self.session.commit()
        device = self.session.query(Device).filter_by(code='device2').first()
        self.assertEquals(device.code, device2.code)

        # Rollback test
        device3 = Device(code='device3', description='DESCRIPTION3')
        self.session.add(device3)
        self.session.rollback()
        device = self.session.query(Device).filter_by(code='device3').first()
        self.assertTrue(device is None)

        # Delete record
        device = self.session.query(Device).filter_by(code='device2').first()
        self.session.delete(device)
        self.session.commit()
        self.assertTrue(
            self.session.query(Device).filter_by(code='device2').count() == 0)

        # Add a relation to a device type
        deviceType = DeviceType(code='devicetype1',
                                description='DESCRIPTIONDT1')
        device = self.session.query(Device).filter_by(code='device1').first()
        device.deviceType = deviceType
        self.session.commit()
        print('Device = %s' % device)
        self.assertEquals(self.session.query(Device).filter_by(code='device1').first()\
                        .deviceType.code, 'devicetype1')

        # Add list of sensors
        sensor1 = Sensor(code='sensor1', description='DESCRIPTIONS1', \
                         sensorType=SensorType(code='sensortype1', description='DESCRIPTIONST1'))
        sensor2 = Sensor(code='sensor2', description='DESCRIPTIONS2', \
                         sensorType=SensorType(code='sensortype2', description='DESCRIPTIONST2'))
        device.sensors.append(sensor1)
        device.sensors.append(sensor2)
        self.session.commit()
        # List sensors of a device
        device = self.session.query(Device).filter_by(code='device1').first()
        print('Device = %s' % device)
        for sensor in device.sensors:
            print('\tSensor = %s' % sensor)