Example #1
0
    def test_TimeSlot(self):
        start_timePoint = TimePoint(dt=dt(2015,2,27,13,0,0, tzinfo='UTC'))
        end_timePoint = TimePoint(dt=dt(2015,2,27,14,0,0, tzinfo='UTC'))
        
        # Test basic behaviour with no span
        timeSlot = TimeSlot(start=start_timePoint, end=end_timePoint)
        self.assertEqual(timeSlot.start, start_timePoint)
        self.assertEqual(timeSlot.end, end_timePoint)
 
        # Test behaviour with start-end-span  
        timeSlotSpan = TimeSlotSpan("1h")
        _ = TimeSlot(start=start_timePoint, end=end_timePoint, span=timeSlotSpan)
        
        # Test behaviour with inconsistent start-end-span  
        timeSlotSpan = TimeSlotSpan("15m")        
        with self.assertRaises(InputException):
            _ = TimeSlot(start=start_timePoint, end=end_timePoint, span=timeSlotSpan)

        # Test behaviour with start-span, no end  
        timeSlotSpan = TimeSlotSpan("1h")
        timeSlot = TimeSlot(start=start_timePoint, span=timeSlotSpan)
        self.assertEqual(timeSlot.end, end_timePoint)


        # Test that a 'floating' timeslot is not allowed:
        timeSlotSpan = TimeSlotSpan("1h")
        with self.assertRaises(InputException):
            timeSlot = TimeSlot(span=timeSlotSpan)
Example #2
0
    def test_correct_dt_dst(self):
        
        # 2015-03-29 01:15:00+01:00
        dateTime =  dt(2015,3,29,1,15,0, tzinfo='Europe/Rome')
        
        # 2015-03-29 02:15:00+01:00, Wrong, does not exists and cannot be corrected
        dateTime = dateTime + datetime.timedelta(hours=1)
        with self.assertRaises(InputException):
            correct_dt_dst(dateTime)

        # 2015-03-29 03:15:00+01:00, Wrong and can be corrected
        dateTime = dateTime + datetime.timedelta(hours=1)
        self.assertEqual(correct_dt_dst(dateTime), dt(2015,3,29,3,15,0, tzinfo='Europe/Rome'))
Example #3
0
    def test_TimePoint(self):
        
        # Init with seconds
        timePoint = TimePoint(t=73637738)
        self.assertEqual(timePoint.t, 73637738)
        self.assertEqual(str(timePoint.dt), '1972-05-02 06:55:38+00:00')
        self.assertEqual(str(timePoint.tz), 'UTC')

        # Init with seconds and microseconds
        timePoint = TimePoint(t=73637738.987654)
        self.assertEqual(timePoint.t, 73637738.987654)
        self.assertEqual(str(timePoint.dt), '1972-05-02 06:55:38.987654+00:00')
        self.assertEqual(str(timePoint.tz), 'UTC')

        # Init with seconds and Timezone
        timePoint = TimePoint(t=73637738, tz="Europe/Rome")
        self.assertEqual(timePoint.t, 73637738)
        self.assertEqual(str(timePoint.dt), '1972-05-02 07:55:38+01:00')
        self.assertEqual(str(timePoint.tz), 'Europe/Rome')

        # Init with Timezone-naive datetime not allowed (not UTC forced)
        timestamp_dt = datetime(2015,2,27,13,54,32)
        with self.assertRaises(InputException):
            _ = TimePoint(dt=timestamp_dt)
    
        # Init with datetime on UTC
        timestamp_dt = dt(2015,2,27,13,54,32, tzinfo='UTC')
        timePoint = TimePoint(dt=timestamp_dt)
        self.assertEqual(timePoint.t, 1425045272)
        self.assertEqual(timePoint.dt, timestamp_dt)
        self.assertEqual(str(timePoint.tz), 'UTC')

        # Init with datetime on Europe/Rome
        timestamp_dt = dt(2015,2,27,13,54,32, tz='Europe/Rome')
        timePoint = TimePoint(dt=timestamp_dt)
        self.assertEqual(timePoint.t, 1425041672)
        self.assertEqual(timePoint.dt, timestamp_dt)
        self.assertEqual(str(timePoint.tz), 'Europe/Rome')

        # Init with datetime on another timezone not allowed (not forcing another timezone)
        with self.assertRaises(InputException):
            _ = TimePoint(dt=dt(2015,2,27,13,54,32, tz='Europe/London'), tz='Europe/Rome')

        # Init with datetime with also microseconds
        timestamp_dt = dt(2015,2,27,13,54,32,566879, tzinfo='UTC')
        timePoint = TimePoint(dt=timestamp_dt)
        self.assertEqual(timePoint.t, 1425045272.566879)
        self.assertEqual(timePoint.dt, timestamp_dt)
        self.assertEqual(str(timePoint.tz), 'UTC')
Example #4
0
    def test_Simple_Aggregation(self):   

        sensor = SimpleSensor('084EB18E44FFA/7-MB-1')
        from_dt = dt(2016,3,25,9,58,0, tzinfo=sensor.timezone) # last was at 2016-03-25T14:09:45+00:00
        to_dt   = dt(2016,3,25,10,32,0, tzinfo=sensor.timezone)

        dataTimeSeries = DataTimeSeries()
        slider_dt = from_dt
        while slider_dt < to_dt:
            
            data = PhysicalData( labels = ['temp_C'], values = [25.5] ) 
            physicalDataTimePoint = PhysicalDataTimePoint(dt   = slider_dt,
                                                          data = data,
                                                          validity_region_span = sensor.Points_validity_region_span)
            dataTimeSeries.append(physicalDataTimePoint)
            slider_dt = slider_dt + TimeSlotSpan('1m')
            
        dataTimeSeriesAggregatorProcess = DataTimeSeriesAggregatorProcess(timeSlotSpan      = TimeSlotSpan('15m'),
                                                                          Sensor            = sensor,
                                                                          data_to_aggregate = PhysicalDataTimePoint)


        # Aggregate
        dataTimeSeriesAggregatorProcess.start(dataTimeSeries = dataTimeSeries,
                                              start_dt       = from_dt,
                                              end_dt         = to_dt,
                                              rounded        = True,
                                              threaded       = False)
        
        # Get results
        aggregated_dataTimeSeries = dataTimeSeriesAggregatorProcess.get_results(until=None)

        # Quick check results using string representations
        i = 0
        for slot in aggregated_dataTimeSeries:

            if i == 0:
                self.assertEqual(str(slot), '''PhysicalDataTimeSlot: from 2016-03-25 10:00:00+01:00 to 2016-03-25 10:15:00+01:00 with span of 15m and coverage of 1.0''')
                self.assertEqual(slot.data.content, {'temp_C_MAX': 25.5, 'temp_C_AVG': 25.5, 'temp_C_MIN': 25.5}) 
            elif i == 1:
                self.assertEqual(str(slot), '''PhysicalDataTimeSlot: from 2016-03-25 10:15:00+01:00 to 2016-03-25 10:30:00+01:00 with span of 15m and coverage of 1.0''')
                self.assertEqual(slot.data.content, {'temp_C_MAX': 25.5, 'temp_C_AVG': 25.5, 'temp_C_MIN': 25.5})
            else:
                raise Exception('Test failed')
            i +=1
                
        if i != 2:
            raise Exception('Test failed')
Example #5
0
    def test_Extended_Aggregation(self):    

        # Define sensor and boundaries
        sensor = EnergyElectricExtendedTriphase('084EB18E44FFA/7-MB-1')
        from_dt = dt(2016,3,25,9,58,0, tzinfo=sensor.timezone) # last was at 2016-03-25T14:09:45+00:00
        to_dt   = dt(2016,3,25,10,32,0, tzinfo=sensor.timezone)
        
        # Load dataset
        dataTimeSeriesSQLiteStorage = sqlite.DataTimeSeriesSQLiteStorage(in_memory=False, can_initialize=True, db_file=datasets_path + 'dataset1.sqlite')
        out_streamingDataTimeSeries = dataTimeSeriesSQLiteStorage.get(sensor=sensor)

        dataTimeSeriesAggregatorProcess = DataTimeSeriesAggregatorProcess(timeSlotSpan      = TimeSlotSpan('15m'),
                                                                          Sensor            = sensor,
                                                                          data_to_aggregate = PhysicalDataTimePoint)


        # Aggregate
        dataTimeSeriesAggregatorProcess.start(dataTimeSeries = out_streamingDataTimeSeries,
                                              start_dt       = from_dt,
                                              end_dt         = to_dt,
                                              rounded        = True,
                                              threaded       = False)
        
        # Get results
        aggregated_dataTimeSeries = dataTimeSeriesAggregatorProcess.get_results(until=None)
        
        # Quick check of results using string representations
        i = 0
        for slot in aggregated_dataTimeSeries:

            if i == 0:
                self.assertEqual(str(slot), '''PhysicalDataTimeSlot: from 2016-03-25 10:00:00+01:00 to 2016-03-25 10:15:00+01:00 with span of 15m and coverage of 1.0''')
                self.assertEqual(slot.data.content, {'current-l1_A_MIN': 4.73419189453, 'power_W_MAX': 4692.578125, 'current-l2_A_AVG': 4.0533920211715415, 'power-l1_W_AVG': 540.6015902259898, 'power-l3_W_AVG': 470.55743272105127, 'voltage-l1_V_MIN': 225.48248291, 'current-l1_A_MAX': 7.78015136719, 'rpower-l3_VAr_AVG': -157.16330377842402, 'power_W_MIN': 1462.734375, 'power-l2_W_AVG': 819.5026008426315, 'rpower-l1_VAr_MIN': -984.765625, 'voltage-l2_V_AVG': 210.6090310856981, 'current_A_AVG': 11.355183299930056, 'voltage-l1_V_MAX': 228.475524902, 'current_A_MIN': 9.98733520507, 'voltage_V_AVG': 633.851065817629, 'rpower-l1_VAr_MAX': -832.109375, 'power-l3_W_MAX': 1508.359375, 'power-l1_W_MIN': 459.453125, 'current-l2_A_MAX': 8.64349365234, 'power-l1_W_MAX': 1397.734375, 'current-l1_A_AVG': 4.792555512310997, 'current_A_MAX': 27.026062011729998, 'current-l3_A_AVG': 2.509235766447517, 'power-l2_W_MAX': 1786.484375, 'voltage_V_MIN': 671.593444824, 'current-l2_A_MIN': 3.82629394531, 'voltage-l2_V_MAX': 227.123535156, 'voltage-l3_V_MAX': 226.735778809, 'rpower_VAr_AVG': -1278.5843967083001, 'voltage-l2_V_MIN': 223.11126709, 'rpower_VAr_MIN': -1558.984375, 'rpower-l2_VAr_MIN': -271.875, 'power-l3_W_MIN': 239.921875, 'rpower_VAr_MAX': -936.796875, 'rpower-l1_VAr_AVG': -882.3312162472556, 'voltage-l3_V_AVG': 211.01158331126413, 'rpower-l3_VAr_MIN': -302.34375, 'current-l3_A_MAX': 10.6024169922, 'power_W_AVG': 1830.6616237896726, 'rpower-l2_VAr_MAX': -138.125, 'voltage-l3_V_MIN': 222.999694824, 'power-l2_W_MIN': 763.359375, 'rpower-l2_VAr_AVG': -239.08987668262057, 'voltage_V_MAX': 682.3348388669999, 'voltage-l1_V_AVG': 212.23045142066678, 'current-l3_A_MIN': 1.42684936523, 'rpower-l3_VAr_MAX': 33.4375}) 
            elif i == 1:
                self.assertEqual(str(slot), '''PhysicalDataTimeSlot: from 2016-03-25 10:15:00+01:00 to 2016-03-25 10:30:00+01:00 with span of 15m and coverage of 1.0''')
                self.assertEqual(slot.data.content, {'current-l1_A_MIN': 4.96215820312, 'power_W_MAX': 8872.65625, 'current-l2_A_AVG': 5.699640351268045, 'power-l1_W_AVG': 964.0598380394166, 'power-l3_W_AVG': 953.2915566854815, 'voltage-l1_V_MIN': 224.033081055, 'current-l1_A_MAX': 12.7659606934, 'rpower-l3_VAr_AVG': -118.72267023945415, 'power_W_MIN': 1514.609375, 'power-l2_W_AVG': 1173.7198022850064, 'rpower-l1_VAr_MIN': -998.515625, 'voltage-l2_V_AVG': 210.49519544251942, 'current_A_AVG': 16.774708245197594, 'voltage-l1_V_MAX': 228.413879395, 'current_A_MIN': 10.259704589840002, 'voltage_V_AVG': 632.8933605279044, 'rpower-l1_VAr_MAX': -835.15625, 'power-l3_W_MAX': 3423.203125, 'power-l1_W_MIN': 508.90625, 'current-l2_A_MAX': 13.1994628906, 'power-l1_W_MAX': 2622.1875, 'current-l1_A_AVG': 6.397570106683291, 'current_A_MAX': 39.8182678223, 'current-l3_A_AVG': 4.677497787246258, 'power-l2_W_MAX': 2827.265625, 'voltage_V_MIN': 667.559875488, 'current-l2_A_MIN': 3.86245727539, 'voltage-l2_V_MAX': 226.949768066, 'voltage-l3_V_MAX': 227.482421875, 'rpower_VAr_AVG': -1255.8022510368125, 'voltage-l2_V_MIN': 221.975952148, 'rpower_VAr_MIN': -1618.28125, 'rpower-l2_VAr_MIN': -316.953125, 'power-l3_W_MIN': 242.65625, 'rpower_VAr_MAX': -830.15625, 'rpower-l1_VAr_AVG': -891.1710790467758, 'voltage-l3_V_AVG': 210.47687837363117, 'rpower-l3_VAr_MIN': -302.8125, 'current-l3_A_MAX': 13.8528442383, 'power_W_AVG': 3091.0711970099046, 'rpower-l2_VAr_MAX': -81.875, 'voltage-l3_V_MIN': 221.550842285, 'power-l2_W_MIN': 763.046875, 'rpower-l2_VAr_AVG': -245.90850175058262, 'voltage_V_MAX': 682.846069336, 'voltage-l1_V_AVG': 211.92128671175377, 'current-l3_A_MIN': 1.43508911133, 'rpower-l3_VAr_MAX': 86.875})
            else:
                raise Exception('Test failed')
            i +=1
                
        if i != 2:
            raise Exception('Test failed')
Example #6
0
    def test_TimeSlotSpan(self):
        
        # TODO: I had to comment out this test, find out why..
        # Complex time intervals are not supported
        #with self.assertRaises(InputException):
        #   _ = TimeSlotSpan('15m', '20s')
        
        # TODO: test with spans
        #print TimeSlotSpan('1m').value
        #print TimeSlot(span=TimeSlotSpan('1m')).span.value

        # Not valid 'q' type
        with self.assertRaises(InputException):
            _ = TimeSlotSpan('15q')

        # String init
        timeSlotSpan1 = TimeSlotSpan('15m')
        self.assertEqual(timeSlotSpan1.string, '15m')

        timeSlotSpan2 = TimeSlotSpan('15m_30s_3u')
        self.assertEqual(timeSlotSpan2.string, '15m_30s_3u')
        
        timeSlotSpan3 = TimeSlotSpan(days=1)
        
        # Sum with other TimeSlotSpan objects
        self.assertEqual((timeSlotSpan1+timeSlotSpan2+timeSlotSpan3).string, '1D_30m_30s_3u')

        # Sum with datetime (also on DST change)
        timeSlotSpan = TimeSlotSpan('1h')
        dateTime1 = dt(2015,10,25,0,15,0, tzinfo='Europe/Rome')
        dateTime2 = dateTime1 + timeSlotSpan
        dateTime3 = dateTime2 + timeSlotSpan
        dateTime4 = dateTime3 + timeSlotSpan
        dateTime5 = dateTime4 + timeSlotSpan

        self.assertEqual(str(dateTime1), '2015-10-25 00:15:00+02:00')
        self.assertEqual(str(dateTime2), '2015-10-25 01:15:00+02:00')
        self.assertEqual(str(dateTime3), '2015-10-25 02:15:00+02:00')
        self.assertEqual(str(dateTime4), '2015-10-25 02:15:00+01:00')
        self.assertEqual(str(dateTime5), '2015-10-25 03:15:00+01:00')
Example #7
0
    def test_dt(self):
        
        # TODO: understand if it is fine to test with string representation. Can we use native datetime?
        
        # Test UTC
        dateTime = dt(2015,3,29,2,15,0, tzinfo='UTC')
        self.assertEqual(str(dateTime), '2015-03-29 02:15:00+00:00')
        
        # Test UTC forced
        dateTime = dt(2015,3,29,2,15,0)
        self.assertEqual(str(dateTime), '2015-03-29 02:15:00+00:00')
        
        # Test with  timezone
        dateTime = dt(2015,3,25,4,15,0, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '2015-03-25 04:15:00+01:00')
        dateTime = dt(2015,9,25,4,15,0, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '2015-09-25 04:15:00+02:00')

        #---------------------
        # Test border cases
        #---------------------
        
        # Not existent time raises
        with self.assertRaises(InputException):
            _ = dt(2015,3,29,2,15,0, tzinfo='Europe/Rome')
         
        # Not existent time does not raises   
        dateTime = dt(2015,3,29,2,15,0, tzinfo='Europe/Rome', trustme=True)
        self.assertEqual(dateTime.year, 2015)
        self.assertEqual(dateTime.month, 3)
        self.assertEqual(dateTime.day, 29)
        self.assertEqual(dateTime.hour, 2)
        self.assertEqual(dateTime.minute, 15)
        self.assertEqual(str(dateTime.tzinfo), 'Europe/Rome')

        # Very past years (no DST)
        dateTime = dt(1856,12,1,16,46, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '1856-12-01 16:46:00+01:00')

        # NOTE: with pytz==2015.4 instead of the above use the following (which should also be more correct...)
        #self.assertEqual(str(dateTime), '1856-12-01 16:46:00+00:50')

        dateTime = dt(1926,12,1,16,46, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '1926-12-01 16:46:00+01:00')

        dateTime = dt(1926,8,1,16,46, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '1926-08-01 16:46:00+01:00')
        
        # Very future years (no DST
        dateTime = dt(3567,12,1,16,46, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '3567-12-01 16:46:00+01:00')

        dateTime = dt(3567,8,1,16,46, tzinfo='Europe/Rome')
        self.assertEqual(str(dateTime), '3567-08-01 16:46:00+01:00')
Example #8
0
    def test_dt_math(self):

        # Test that complex timeSlotSpans are not handable
        timeSlotSpan = TimeSlotSpan('1D_3h_5m')
        dateTime = dt(2015,1,1,16,37,14, tzinfo='Europe/Rome')
        
        with self.assertRaises(InputException):
            _ = timeSlotSpan.floor_dt(dateTime)


        # Test in ceil/floor/round normal conditions (hours)
        timeSlotSpan = TimeSlotSpan('1h')
        dateTime = dt(2015,1,1,16,37,14, tzinfo='Europe/Rome')
        self.assertEqual(timeSlotSpan.floor_dt(dateTime), dt(2015,1,1,16,0,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime), dt(2015,1,1,17,0,0, tzinfo='Europe/Rome'))

         
        # Test in ceil/floor/round normal conditions (minutes)
        timeSlotSpan = TimeSlotSpan('15m')
        dateTime = dt(2015,1,1,16,37,14, tzinfo='Europe/Rome')
        self.assertEqual(timeSlotSpan.floor_dt(dateTime), dt(2015,1,1,16,30,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime), dt(2015,1,1,16,45,0, tzinfo='Europe/Rome'))


        # Test ceil/floor/round in normal conditions (seconds)
        timeSlotSpan = TimeSlotSpan('30s')
        dateTime = dt(2015,1,1,16,37,14, tzinfo='Europe/Rome') 
        self.assertEqual(timeSlotSpan.floor_dt(dateTime), dt(2015,1,1,16,37,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime), dt(2015,1,1,16,37,30, tzinfo='Europe/Rome'))

   
        # Test ceil/floor/round across 1970-1-1 (minutes) 
        timeSlotSpan = TimeSlotSpan('5m')
        dateTime1 = dt(1969,12,31,23,57,29, tzinfo='UTC') # epoch = -3601
        dateTime2 = dt(1969,12,31,23,59,59, tzinfo='UTC') # epoch = -3601       
        self.assertEqual(timeSlotSpan.floor_dt(dateTime1), dt(1969,12,31,23,55,0, tzinfo='UTC'))
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime1), dt(1970,1,1,0,0, tzinfo='UTC'))
        self.assertEqual(timeSlotSpan.round_dt(dateTime1), dt(1969,12,31,23,55,0, tzinfo='UTC'))
        self.assertEqual(timeSlotSpan.round_dt(dateTime2), dt(1970,1,1,0,0, tzinfo='UTC'))


        # Test ceil/floor/round (3 hours-test)
        timeSlotSpan = TimeSlotSpan('3h')
        dateTime = dt(1969,12,31,22,0,1, tzinfo='Europe/Rome') # negative epoch
        
        # TODO: test fails!! fix me!        
        #self.assertEqual(timeSlotSpan.floor_dt(dateTime1), dt(1969,12,31,21,0,0, tzinfo='Europe/Rome'))
        #self.assertEqual(timeSlotSpan.ceil_dt(dateTime1), dt(1970,1,1,0,0, tzinfo='Europe/Rome'))


        # Test ceil/floor/round across 1970-1-1 (together with the 2 hours-test, TODO: decouple) 
        timeSlotSpan = TimeSlotSpan('2h')
        dateTime1 = dt(1969,12,31,22,59,59, tzinfo='Europe/Rome') # negative epoch
        dateTime2 = dt(1969,12,31,23,0,1, tzinfo='Europe/Rome') # negative epoch  
        self.assertEqual(timeSlotSpan.floor_dt(dateTime1), dt(1969,12,31,22,0,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime1), dt(1970,1,1,0,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.round_dt(dateTime1), dt(1969,12,31,22,0, tzinfo='Europe/Rome'))
        self.assertEqual(timeSlotSpan.round_dt(dateTime2), dt(1970,1,1,0,0, tzinfo='Europe/Rome'))

        # Test ceil/floor/round across DST change (hours)
        timeSlotSpan = TimeSlotSpan('1h')
        
        dateTime1 = dt(2015,10,25,0,15,0, tzinfo='Europe/Rome')
        dateTime2 = dateTime1 + timeSlotSpan    # 2015-10-25 01:15:00+02:00    
        dateTime3 = dateTime2 + timeSlotSpan    # 2015-10-25 02:15:00+02:00
        dateTime4 = dateTime3 + timeSlotSpan    # 2015-10-25 02:15:00+01:00

        dateTime1_rounded = dt(2015,10,25,0,0,0, tzinfo='Europe/Rome')
        dateTime2_rounded = dateTime1_rounded + timeSlotSpan        
        dateTime3_rounded = dateTime2_rounded + timeSlotSpan
        dateTime4_rounded = dateTime3_rounded + timeSlotSpan
        dateTime5_rounded = dateTime4_rounded + timeSlotSpan
               
        self.assertEqual(timeSlotSpan.floor_dt(dateTime2), dateTime2_rounded)
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime2), dateTime3_rounded)
          
        self.assertEqual(timeSlotSpan.floor_dt(dateTime3), dateTime3_rounded)
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime3), dateTime4_rounded)
        
        self.assertEqual(timeSlotSpan.floor_dt(dateTime4), dateTime4_rounded)
        self.assertEqual(timeSlotSpan.ceil_dt(dateTime4), dateTime5_rounded)
Example #9
0
    def test_PutGet_DataTimePoints(self):
        
        dataTimeSeriesSQLiteStorage = sqlite.DataTimeSeriesSQLiteStorage(in_memory=True)
        
        # Generate 10 points DataTimeSeries with flowrate sensor
        dataTimeSeries = DataTimeSeries()
        for i in range(10):
            data = PhysicalData( labels = ['flowrate_m3s'], values = [20.6+i] ) 
            physicalDataTimePoint = PhysicalDataTimePoint(t = 1436022000 + (i*60), tz="Europe/Rome", data=data)
            dataTimeSeries.append(physicalDataTimePoint)

        # Generate 10 points DataTimeSeries with light sensor
        dataTimeSeries_light = DataTimeSeries()
        for i in range(10):
            data = PhysicalData( labels = ['light_pct'], values = [60.6+i] ) 
            physicalDataTimePoint = PhysicalDataTimePoint(t = 1436022000 + (i*60), tz="Europe/Rome", data=data)
            dataTimeSeries_light.append(physicalDataTimePoint)
        
        # Test put data without sensor (not implemented for now)
        with self.assertRaises(NotImplementedError):
            data_id_1 = dataTimeSeriesSQLiteStorage.put(dataTimeSeries)
        
        # Test volumetric sensor
        volumetricSensorV1_1 = VolumetricSensorV1('lu65na')
        volumetricSensorV1_2 = VolumetricSensorV1('lu34na')
        
        # Test labels inconsistency
        with self.assertRaises(InputException):
            dataTimeSeriesSQLiteStorage.put(dataTimeSeries_light, sensor=volumetricSensorV1_1)
        
        # Test put data with sensor and no right to create structure
        with self.assertRaises(StorageException):
            dataTimeSeriesSQLiteStorage.put(dataTimeSeries, sensor=volumetricSensorV1_1)

        # Test get with sensor and no structure in the storage
        with self.assertRaises(StorageException):
            _  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, cached=True)

        # Test put data with sensor and right to create structure AND get with sensor and without from_dt/to_dt
        # TODO: this is not correct unit test of the put and get. It is testing them at the same time!
        dataTimeSeriesSQLiteStorage.put(dataTimeSeries, sensor=volumetricSensorV1_1, can_initialize=True)
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, cached=True)
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries)

        # Test get of no data:
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_2, cached=True)

        # We can check the equality against a simple DataTimeSeries
        empyt_dataTimeSeries = DataTimeSeries()
        self.assertEqual(out_streamingDataTimeSeries, empyt_dataTimeSeries)

        # The following test is just for confirm of the above steps. Should not be here in a proper unittesting approach.
        self.assertNotEqual(out_streamingDataTimeSeries, dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, cached=True))

        # Now test the get with start_dt and end_dt     
        from_dt = dt(2015,7,4,17,3,0, tzinfo='Europe/Rome')
        to_dt   = dt(2015,7,4,17,6,0, tzinfo='Europe/Rome')        
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get( sensor  = volumetricSensorV1_1,
                                                                from_dt = from_dt,
                                                                to_dt   = to_dt,
                                                                cached = True)
        dataTimeSeries_filtered = dataTimeSeries.filter(from_dt = from_dt, to_dt=to_dt)
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries_filtered)
Example #10
0
    def test_PutGet_DataTimeSlots(self):
        
        dataTimeSeriesSQLiteStorage = sqlite.DataTimeSeriesSQLiteStorage(in_memory=True)
        
        # Generate 10 slots DataTimeSeries with flowrate sensor aggregated data
        dataTimeSeries = DataTimeSeries()
        for i in range(10):
            data = PhysicalData(labels = ['flowrate_m3s_AVG', 'flowrate_m3s_MIN', 'flowrate_m3s_MAX', 'volume_m3_TOT'],
                                            values = [20.6+i,20.6+i,20.6+i,20.6+i] ) 
            physicalDataTimeSlot = PhysicalDataTimeSlot(start = TimePoint(t=1436022000 + (i*60),tz="Europe/Rome"), 
                                                        end = TimePoint(t=1436022000 + ((i+1)*60), tz="Europe/Rome"),
                                                        data=data, span=TimeSlotSpan('60s'))
            dataTimeSeries.append(physicalDataTimeSlot)        

        # Generate 10 points DataTimeSeries with light sensor aggregated data
        dataTimeSeries_light = DataTimeSeries()
        for i in range(10):
            data = PhysicalData(labels = ['light_pct_AVG'], values = [20.6+i] ) 
            physicalDataTimeSlot = PhysicalDataTimeSlot(start = TimePoint(t=1436022000 + (i*60),tz="Europe/Rome"), 
                                                        end = TimePoint(t=1436022000 + ((i+1)*60), tz="Europe/Rome"),
                                                        data=data, span=TimeSlotSpan('60s'))
            dataTimeSeries_light.append(physicalDataTimeSlot)      
        
  
        # Test put data without sensor (not implemented for now)
        with self.assertRaises(NotImplementedError):
            data_id_1 = dataTimeSeriesSQLiteStorage.put(dataTimeSeries)
        
        # Test volumetric sensor
        volumetricSensorV1_1 = VolumetricSensorV1('lu65na')
        volumetricSensorV1_2 = VolumetricSensorV1('lu34na')

        # Test labels inconsistency
        with self.assertRaises(InputException):
            dataTimeSeriesSQLiteStorage.put(dataTimeSeries_light, sensor=volumetricSensorV1_1)
        
        # Test put data with sensor and no right to create structure
        with self.assertRaises(StorageException):
            dataTimeSeriesSQLiteStorage.put(dataTimeSeries, sensor=volumetricSensorV1_1)

        # Test get with sensor and no structure in the storage
        with self.assertRaises(StorageException):
            _  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, timeSlotSpan=TimeSlotSpan('60s'), cached=True)

        # Test put data with sensor and right to create structure AND get with sensor and without from_dt/to_dt
        # TODO: this is not correct unit test of the put and get. It is testing them at the same time!
        dataTimeSeriesSQLiteStorage.put(dataTimeSeries, sensor=volumetricSensorV1_1, can_initialize=True)
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, timeSlotSpan=TimeSlotSpan('60s'), cached=True)
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries)

        # Test get of no data:
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_2, timeSlotSpan=TimeSlotSpan('60s'), cached=True)

        # We can check the equality against a simple DataTimeSeries
        empyt_dataTimeSeries = DataTimeSeries()
        self.assertEqual(out_streamingDataTimeSeries, empyt_dataTimeSeries)

        # The following test is just for confirm of the above steps. Should not be here in a proper unittesting approach.
        self.assertNotEqual(out_streamingDataTimeSeries, dataTimeSeriesSQLiteStorage.get(sensor=volumetricSensorV1_1, timeSlotSpan=TimeSlotSpan('60s'), cached=True))

        # Now test the get with start_dt and end_dt     
        from_dt = dt(2015,7,4,17,3,0, tzinfo='Europe/Rome')
        to_dt   = dt(2015,7,4,17,6,0, tzinfo='Europe/Rome')        
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get( sensor  = volumetricSensorV1_1,
                                                                from_dt = from_dt,
                                                                to_dt   = to_dt,
                                                                timeSlotSpan = TimeSlotSpan('60s'),
                                                                cached = True)
        dataTimeSeries_filtered = dataTimeSeries.filter(from_dt = from_dt, to_dt=to_dt)
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries_filtered)
        
        # Also test that if we go trough the cached streaminTimeSeries again, we get the same result:
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries_filtered)
        
        
        # Now get the time series without caching:
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get( sensor  = volumetricSensorV1_1,
                                                        from_dt = from_dt,
                                                        to_dt   = to_dt,
                                                        timeSlotSpan = TimeSlotSpan('60s'))
        
        # Check that we can compare it as is even if it is not cached:
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries_filtered)

        # Check that we can compare it again:
        self.assertEqual(out_streamingDataTimeSeries, dataTimeSeries_filtered)        

        
        # Now get AGAIn the time series without caching:
        out_streamingDataTimeSeries  = dataTimeSeriesSQLiteStorage.get( sensor  = volumetricSensorV1_1,
                                                        from_dt = from_dt,
                                                        to_dt   = to_dt,
                                                        timeSlotSpan = TimeSlotSpan('60s'))
        
        # But this time do not test any comparisons (that triggers the caching of the TimeSeries),
        # instead test that going trough it twice we achieve the same result (under the hood we go twice in the DB):
        
        items_A = [item for item in out_streamingDataTimeSeries]
        items_B = [item for item in out_streamingDataTimeSeries]
        
        self.assertEqual(items_A, items_B)

        # WARNING: This is specific to SLQlite and its dataTimeStream
        self.assertEqual(out_streamingDataTimeSeries.dataTimeStream.get_statistics()['source_acceses'], 2)
        
        # Now foce load te time series:
        out_streamingDataTimeSeries.force_load()

        # After force-loading, another soruce acces is performed
        self.assertEqual(out_streamingDataTimeSeries.dataTimeStream.get_statistics()['source_acceses'], 3)
        
        items_C = [item for item in out_streamingDataTimeSeries]
        
        self.assertEqual(items_A, items_C)

        # Generating the list items_C after a force_load should not generate a new source_access
        self.assertEqual(out_streamingDataTimeSeries.dataTimeStream.get_statistics()['source_acceses'], 3)

        # Perform again the iterator check:
        items_A = [item for item in out_streamingDataTimeSeries]
        items_B = [item for item in out_streamingDataTimeSeries]
        
        self.assertEqual(items_A, items_B)
 
        # And ensure that the source accesses is still set to three
        self.assertEqual(out_streamingDataTimeSeries.dataTimeStream.get_statistics()['source_acceses'], 3)