def testNonSaneFutureRecords(self): uri = "/%s/inbox/json/%s" % (self.e.key().id(), TEST_SENSOR_ID) lat = 1.3 lon = 36.9 MOVE_SIZE = 0.01 N_POINTS = 10 DELAY_SECS = 1 now = datetime.now() # Populate dummy data with random moves data = [] for x in range(N_POINTS): now += timedelta(seconds=DELAY_SECS) lat += (random.random() - 0.5) * MOVE_SIZE lon += (random.random() - 0.5) * MOVE_SIZE loc = "%s,%s" % (lat, lon) data.append({ 'timestamp': tools.unixtime(dt=now) + 1000 * 60 * 60 * 24 * 30, # Non-sane (1 month in future) 'location': loc }) last_loc = loc body = json.dumps(data) response = self.post(uri, body) self.assertEqual(response.status_int, 200) content = json.loads(response.normal_body) self.assertTrue(content['success']) self.assertEqual(content['data']['count'], N_POINTS) # Fetch created records from db records = Record.Fetch(self.geosensor1) self.assertEqual(len(records), 0) # No records saved, all non-sane
def testGeoJsonIn(self): uri = "/%s/inbox/json/%s" % (self.e.key().id(), TEST_SENSOR_ID) lat = 1.3 lon = 36.9 MOVE_SIZE = 0.01 MAX_ACCEL = 10 N_POINTS = 10 DELAY_SECS = 1 now = datetime.now() - timedelta(seconds=60) # Populate dummy data with random moves data = [] target_accel_mags = [] for x in range(N_POINTS): now += timedelta(seconds=DELAY_SECS) lat += (random.random() - 0.5) * MOVE_SIZE lon += (random.random() - 0.5) * MOVE_SIZE loc = "%s,%s" % (lat, lon) ax = (random.random() * MAX_ACCEL) - MAX_ACCEL / 2 ay = (random.random() * MAX_ACCEL) - MAX_ACCEL / 2 az = (random.random() * MAX_ACCEL) - MAX_ACCEL / 2 accel_mag = math.sqrt(pow(ax, 2) + pow(ay, 2) + pow(az, 2)) target_accel_mags.append(accel_mag) data.append({ 'timestamp': tools.unixtime(dt=now), # milliseconds 'location': loc, 'ax': ax, 'ay': ay, 'az': az }) last_loc = loc body = json.dumps(data) response = self.post(uri, body) self.assertEqual(response.status_int, 200) content = json.loads(response.normal_body) self.assertTrue(content['success']) self.assertEqual(content['data']['count'], N_POINTS) # Fetch created records from db records = Record.Fetch(self.geosensor1) self.assertEqual(len(records), N_POINTS) last_r = records[0] self.assertEqual(tools.unixtime(last_r.dt_recorded), tools.unixtime(now)) accel_mags = [r.columnValue('accel_mag') for r in records] self.assertListEqual(accel_mags, list(reversed(target_accel_mags))) # Confirm sensor state update self.geosensor1 = Sensor.get(self.geosensor1.key()) # Refetch from db self.assertEqual(self.geosensor1.location, db.GeoPt(last_loc))