def test_addPingExisting(self, datetime_mock): location = '30.000 20.000' current_time = datetime.datetime.now() datetime_mock.now.return_value = current_time device = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) device.buckets[0]['currentSequence'] = 1 device.buckets[0]['data'] = [{'uptime': 100, 'date': current_time}] addPing(device) expected = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) expected.buckets[0]['currentSequence'] = 2 expected.buckets[0]['data'] = [{ 'uptime': 100, 'date': current_time }, { 'uptime': 100, 'date': current_time }] self.assertEq(expected, device)
def test_calculateAverageOnOverflow(self, datetime_mock): location = '30.000 20.000' current_time = datetime.datetime.now() datetime_mock.now.return_value = current_time old_time = current_time device = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) overflow = device.buckets[0]['overflow'] data = [] for i in range(0, overflow): data.append((i * 10) % 100) for i in range(0, len(data)): addPingCustom(device, current_time + timedelta(minutes=i + 1), data[i]) expectedUptime = Average(data) expected = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) expected.buckets[0]['currentSequence'] = 0 expected.buckets[0]['data'] = device.buckets[0]['data'] expected.buckets[1]['currentSequence'] = 1 expected.buckets[1]['data'] = [{ 'uptime': expectedUptime, 'date': current_time + timedelta(minutes=len(data)) }] self.assertEq(expected, device)
def test_addMissingIntervalsPing(self, datetime_mock): location = '30.000 20.000' current_time = datetime.datetime.now() datetime_mock.now.return_value = current_time old_time = current_time - timedelta(seconds=140) device = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) data = [{'uptime': 100, 'date': old_time}] device.buckets[0]['currentSequence'] = 1 device.buckets[0]['data'] = copy.deepcopy(data) addPing(device) data.append({'uptime': 0, 'date': old_time + timedelta(seconds=60)}) data.append({'uptime': 0, 'date': old_time + timedelta(seconds=120)}) data.append({'uptime': 100, 'date': current_time}) expected = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) expected.buckets[0]['currentSequence'] = 4 expected.buckets[0]['data'] = data self.assertEq(expected, device)
def test_addPingOverflows(self, datetime_mock): location = '30.000 20.000' current_time = datetime.datetime.now() datetime_mock.now.return_value = current_time device = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) overflow = device.buckets[0]['overflow'] device.buckets[0]['currentSequence'] = overflow - 1 data = [{'uptime': 100, 'date': current_time}] * (overflow - 1) device.buckets[0]['data'] = copy.deepcopy(data) addPing(device) data.append({'uptime': 100, 'date': current_time}) expected = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) expected.buckets[0]['currentSequence'] = 0 expected.buckets[0]['data'] = data expected.buckets[1]['currentSequence'] = 1 expected.buckets[1]['data'] = [{'uptime': 100, 'date': current_time}] self.assertEq(expected, device)
def setUp(self): location = '30.000 20.000' self.current_time = datetime.datetime.now() self.cluster_id = 1 devices = [] for i in range(0, 3): device = createDevice( DevicePing(id=i, clusterId=self.cluster_id, location=location)) devices.append(device) for device in devices: overflow = device.buckets[0]['overflow'] device.buckets[0]['currentSequence'] = overflow - 1 data = [{ 'uptime': 100, 'date': self.current_time }] * (overflow - 1) device.buckets[0]['data'] = copy.deepcopy(data) device.save() self.service = getDevicesByClusterAndBucketLevel
def test_addPingRemovesIfFull(self, datetime_mock): location = '30.000 20.000' current_time = datetime.datetime.now() datetime_mock.now.return_value = current_time device = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) maxSize = device.buckets[0]['maxSize'] data = [{'uptime': 100, 'date': current_time}] * maxSize device.buckets[0]['data'] = copy.deepcopy(data) addPing(device) del data[0] data.append({'uptime': 100, 'date': current_time}) expected = createDevice( DevicePing(id="1", clusterId="cl1", location=location)) expected.buckets[0]['currentSequence'] = 1 expected.buckets[0]['data'] = data self.assertEq(expected, device)