def test_uuid_queries(self): if not should_test_uuid: raise SkipTest() c = get_connection() coll = c.pymongo_test.test coll.drop() uu = uuid.uuid4() coll.insert({'uuid': Binary(uu.bytes, 3)}) self.assertEqual(1, coll.count()) # Test UUIDLegacy queries. coll.uuid_subtype = 4 self.assertEqual(0, coll.find({'uuid': uu}).count()) cur = coll.find({'uuid': UUIDLegacy(uu)}) self.assertEqual(1, cur.count()) retrieved = cur.next()['uuid'] self.assertEqual(uu, retrieved) # Test regular UUID queries (using subtype 4). coll.insert({'uuid': uu}) self.assertEqual(2, coll.count()) cur = coll.find({'uuid': uu}) self.assertEqual(1, cur.count()) self.assertEqual(uu, cur.next()['uuid']) # Test both. cur = coll.find({'uuid': {'$in': [uu, UUIDLegacy(uu)]}}) self.assertEqual(2, cur.count()) coll.drop()
def get_all_acquisition_device_metrics(acquisitionId, deviceId): query = { '_id': UUIDLegacy(acquisitionId), 'devices._id': UUIDLegacy(deviceId) } #Thanks https://stackoverflow.com/a/51284449 projection = {'devices._id': 1, 'devices.$': 1} query_result = db.acquisitions.find_one(query, projection) result = {} if query_result is not None: if request.args.get('metric') is not None: metric_nested = request.args.get('metric').split('.') gte = request.args.get('gte', None) gt = request.args.get('gt', None) lte = request.args.get('lte', None) lt = request.args.get('lt', None) result['sensors'] = [ { 'sensor': sensor['sensorType'].title(), 'metrics': sensor['metrics'] } for sensor in query_result['devices'][0]['sensors'] if get_nested_metric(sensor['metrics'], metric_nested) is not None and is_value_within_limits( float(get_nested_metric(sensor['metrics'], metric_nested)), lt, lte, gt, gte) ] else: result['sensors'] = [{ 'sensor': sensor['sensorType'].title(), 'metrics': sensor['metrics'] } for sensor in query_result['devices'][0]['sensors']] return jsonify(result)
def test_uuid_queries(self): if not should_test_uuid: raise SkipTest() c = get_connection() coll = c.pymongo_test.test coll.drop() uu = uuid.uuid4() # Wrap uu.bytes in binary_type to work # around http://bugs.python.org/issue7380. coll.insert({'uuid': Binary(binary_type(uu.bytes), 3)}) self.assertEqual(1, coll.count()) # Test UUIDLegacy queries. coll.uuid_subtype = 4 self.assertEqual(0, coll.find({'uuid': uu}).count()) cur = coll.find({'uuid': UUIDLegacy(uu)}) self.assertEqual(1, cur.count()) retrieved = cur.next() self.assertEqual(uu, retrieved['uuid']) # Test regular UUID queries (using subtype 4). coll.insert({'uuid': uu}) self.assertEqual(2, coll.count()) cur = coll.find({'uuid': uu}) self.assertEqual(1, cur.count()) retrieved = cur.next() self.assertEqual(uu, retrieved['uuid']) # Test both. cur = coll.find({'uuid': {'$in': [uu, UUIDLegacy(uu)]}}) self.assertEqual(2, cur.count()) coll.drop()
def test_uuid_legacy(self): id = uuid.uuid4() legacy = UUIDLegacy(id) self.assertEqual(3, legacy.subtype) transformed = (BSON.encode({"uuid": legacy})).decode()["uuid"] self.assertTrue(isinstance(transformed, uuid.UUID)) self.assertEqual(id, transformed) self.assertNotEqual(UUIDLegacy(uuid.uuid4()), UUIDLegacy(transformed))
def test_uuid_legacy(self): if not should_test_uuid: raise SkipTest("No uuid module") id = uuid.uuid4() legacy = UUIDLegacy(id) self.assertEqual(3, legacy.subtype) transformed = (BSON.encode({"uuid": legacy})).decode()["uuid"] self.assertTrue(isinstance(transformed, uuid.UUID)) self.assertEqual(id, transformed) self.assertNotEqual(UUIDLegacy(uuid.uuid4()), UUIDLegacy(transformed))
def get_acquisition_metrics(acquisitionId): query = {'_id': UUIDLegacy(acquisitionId)} projection = {'devices': 1} query_result = db.acquisitions.find_one(query, projection) result = [] if query_result is not None: for device in query_result['devices']: device_entry = {'device': device['model'], 'sensors': []} for sensor in device['sensors']: if request.args.get('sensor') is not None: if sensor['sensorType'].title() == request.args.get( 'sensor').title(): device_entry['sensors'].append({ 'sensor': sensor['sensorType'].title(), 'metrics': sensor['metrics'] }) else: device_entry['sensors'].append({ 'sensor': sensor['sensorType'].title(), 'metrics': sensor['metrics'] }) if len(device_entry['sensors']) > 0: result.append(device_entry) return jsonify(result)
def get_acquisition_device_sensor_metrics(acquisitionId, deviceId, sensorType): query = { '_id': UUIDLegacy(acquisitionId), 'devices._id': UUIDLegacy(deviceId) } projection = {'devices._id': 1, 'devices.$': 1} query_result = db.acquisitions.find_one(query, projection) result = {} if query_result is not None: result['metrics'] = next( (sensor['metrics'] for sensor in query_result['devices'][0]['sensors'] if sensor['sensorType'].title() == sensorType.title()), dict()) return jsonify(result)
def get_acquisition_device_sensor_specific_metric(acquisitionId, deviceId, sensorType, metric): query = { '_id': UUIDLegacy(acquisitionId), 'devices._id': UUIDLegacy(deviceId) } projection = {'devices._id': 1, 'devices.$': 1} query_result = db.acquisitions.find_one(query, projection) result = {} if query_result is not None: metrics = next((sensor['metrics'] for sensor in query_result['devices'][0]['sensors'] if sensor['sensorType'].title() == sensorType.title()), dict()) value = get_nested_metric(metrics, metric.split('.')) result['value'] = value if value is not None else 'NOT_FOUND' return jsonify(result)
def refresh_token(): """ Issue a new set of access and refresh token after validating an old refresh token Old refresh token can only be used once and hence is removed from database Unused but expired refresh token is also deleted from database """ # get old refresh token from request header old_token = request.headers.get("refresh-token") if old_token is None: raise BadRequest("missing refresh-token") # check token exists in database and get expire time and user id try: old_token_document = RefreshTokens().find_one( {"token": UUIDLegacy(UUID(old_token))}, { "expire_time": 1, "username": 1 }) if old_token_document is None: raise Unauthorized("refresh-token invalid") except Exception: raise Unauthorized("refresh-token invalid") # check token is not expired if old_token_document["expire_time"] < getnow(): raise Unauthorized("token expired") # check user exists user = Users().find_one({"username": old_token_document["username"]}, { "username": 1, "scope": 1 }) if user is None: raise Unauthorized("user not found") # generate token access_token = AccessToken.encode(user) refresh_token = create_refresh_token(user["username"]) # delete old refresh token from database RefreshTokens().delete_one({"token": UUID(old_token)}) # send response response_json = { "access_token": access_token, "token_type": "bearer", "expires_in": AccessToken.get_expiry(access_token), "refresh_token": refresh_token, } response = jsonify(response_json) response.headers["Cache-Control"] = "no-store" response.headers["Pragma"] = "no-cache" return response
def test_uuid_representation(self): coll = self.db.uuid coll.drop() # Test property self.assertEqual(PYTHON_LEGACY, coll.codec_options.uuid_representation) # Test basic query uu = uuid.uuid4() # Insert as binary subtype 3 coll.insert_one({'uu': uu}) self.assertEqual(uu, coll.find_one({'uu': uu})['uu']) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) self.assertEqual(STANDARD, coll.codec_options.uuid_representation) self.assertEqual(None, coll.find_one({'uu': uu})) self.assertEqual(uu, coll.find_one({'uu': UUIDLegacy(uu)})['uu']) # Test Cursor.count self.assertEqual(0, coll.find({'uu': uu}).count()) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) self.assertEqual(1, coll.find({'uu': uu}).count()) # Test delete coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) coll.delete_one({'uu': uu}) self.assertEqual(1, coll.count()) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) coll.delete_one({'uu': uu}) self.assertEqual(0, coll.count()) # Test update_one coll.insert_one({'_id': uu, 'i': 1}) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) coll.update_one({'_id': uu}, {'$set': {'i': 2}}) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) self.assertEqual(1, coll.find_one({'_id': uu})['i']) coll.update_one({'_id': uu}, {'$set': {'i': 2}}) self.assertEqual(2, coll.find_one({'_id': uu})['i']) # Test Cursor.distinct self.assertEqual([2], coll.find({'_id': uu}).distinct('i')) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) self.assertEqual([], coll.find({'_id': uu}).distinct('i')) # Test findAndModify self.assertEqual( None, coll.find_one_and_update({'_id': uu}, {'$set': { 'i': 5 }})) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) self.assertEqual( 2, coll.find_one_and_update({'_id': uu}, {'$set': { 'i': 5 }})['i']) self.assertEqual(5, coll.find_one({'_id': uu})['i']) # Test command self.assertEqual( 5, self.db.command('findAndModify', 'uuid', update={'$set': { 'i': 6 }}, query={'_id': uu})['value']['i']) self.assertEqual( 6, self.db.command('findAndModify', 'uuid', update={'$set': { 'i': 7 }}, query={'_id': UUIDLegacy(uu)})['value']['i']) # Test (inline)_map_reduce coll.drop() coll.insert_one({"_id": uu, "x": 1, "tags": ["dog", "cat"]}) coll.insert_one({ "_id": uuid.uuid4(), "x": 3, "tags": ["mouse", "cat", "dog"] }) map = Code("function () {" " this.tags.forEach(function(z) {" " emit(z, 1);" " });" "}") reduce = Code("function (key, values) {" " var total = 0;" " for (var i = 0; i < values.length; i++) {" " total += values[i];" " }" " return total;" "}") coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) q = {"_id": uu} result = coll.inline_map_reduce(map, reduce, query=q) self.assertEqual([], result) result = coll.map_reduce(map, reduce, "results", query=q) self.assertEqual(0, self.db.results.count()) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) q = {"_id": uu} result = coll.inline_map_reduce(map, reduce, query=q) self.assertEqual(2, len(result)) result = coll.map_reduce(map, reduce, "results", query=q) self.assertEqual(2, self.db.results.count()) self.db.drop_collection("result") coll.drop() # Test group coll.insert_one({"_id": uu, "a": 2}) coll.insert_one({"_id": uuid.uuid4(), "a": 1}) reduce = "function (obj, prev) { prev.count++; }" coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=STANDARD)) self.assertEqual([], coll.group([], {"_id": uu}, {"count": 0}, reduce)) coll = self.db.get_collection( "uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) self.assertEqual([{ "count": 1 }], coll.group([], {"_id": uu}, {"count": 0}, reduce))
def test_uuid_subtype(self): if not have_uuid: raise SkipTest("No uuid module") self.client = MongoClient(pair) self.db = self.client.pymongo_test coll = self.client.pymongo_test.uuid coll.drop() def change_subtype(collection, subtype): collection.uuid_subtype = subtype # Test property self.assertEqual(OLD_UUID_SUBTYPE, coll.uuid_subtype) self.assertRaises(ConfigurationError, change_subtype, coll, 7) self.assertRaises(ConfigurationError, change_subtype, coll, 2) # Test basic query uu = uuid.uuid4() # Insert as binary subtype 3 coll.insert({'uu': uu}) self.assertEqual(uu, coll.find_one({'uu': uu})['uu']) coll.uuid_subtype = UUID_SUBTYPE self.assertEqual(UUID_SUBTYPE, coll.uuid_subtype) self.assertEqual(None, coll.find_one({'uu': uu})) self.assertEqual(uu, coll.find_one({'uu': UUIDLegacy(uu)})['uu']) # Test Cursor.count self.assertEqual(0, coll.find({'uu': uu}).count()) coll.uuid_subtype = OLD_UUID_SUBTYPE self.assertEqual(1, coll.find({'uu': uu}).count()) # Test remove coll.uuid_subtype = UUID_SUBTYPE coll.remove({'uu': uu}) self.assertEqual(1, coll.count()) coll.uuid_subtype = OLD_UUID_SUBTYPE coll.remove({'uu': uu}) self.assertEqual(0, coll.count()) # Test save coll.insert({'_id': uu, 'i': 0}) self.assertEqual(1, coll.count()) self.assertEqual(1, coll.find({'_id': uu}).count()) self.assertEqual(0, coll.find_one({'_id': uu})['i']) doc = coll.find_one({'_id': uu}) doc['i'] = 1 coll.save(doc) self.assertEqual(1, coll.find_one({'_id': uu})['i']) # Test update coll.uuid_subtype = UUID_SUBTYPE coll.update({'_id': uu}, {'$set': {'i': 2}}) coll.uuid_subtype = OLD_UUID_SUBTYPE self.assertEqual(1, coll.find_one({'_id': uu})['i']) coll.update({'_id': uu}, {'$set': {'i': 2}}) self.assertEqual(2, coll.find_one({'_id': uu})['i']) # Test Cursor.distinct self.assertEqual([2], coll.find({'_id': uu}).distinct('i')) coll.uuid_subtype = UUID_SUBTYPE self.assertEqual([], coll.find({'_id': uu}).distinct('i')) # Test find_and_modify self.assertEqual(None, coll.find_and_modify({'_id': uu}, {'$set': {'i': 5}})) coll.uuid_subtype = OLD_UUID_SUBTYPE self.assertEqual(2, coll.find_and_modify({'_id': uu}, {'$set': {'i': 5}})['i']) self.assertEqual(5, coll.find_one({'_id': uu})['i']) # Test command db = self.client.pymongo_test no_obj_error = "No matching object found" result = db.command('findAndModify', 'uuid', allowable_errors=[no_obj_error], uuid_subtype=UUID_SUBTYPE, query={'_id': uu}, update={'$set': {'i': 6}}) self.assertEqual(None, result.get('value')) self.assertEqual(5, db.command('findAndModify', 'uuid', update={'$set': {'i': 6}}, query={'_id': uu})['value']['i']) self.assertEqual(6, db.command('findAndModify', 'uuid', update={'$set': {'i': 7}}, query={'_id': UUIDLegacy(uu)} )['value']['i']) # Test (inline)_map_reduce coll.drop() coll.insert({"_id": uu, "x": 1, "tags": ["dog", "cat"]}) coll.insert({"_id": uuid.uuid4(), "x": 3, "tags": ["mouse", "cat", "dog"]}) map = Code("function () {" " this.tags.forEach(function(z) {" " emit(z, 1);" " });" "}") reduce = Code("function (key, values) {" " var total = 0;" " for (var i = 0; i < values.length; i++) {" " total += values[i];" " }" " return total;" "}") coll.uuid_subtype = UUID_SUBTYPE q = {"_id": uu} if version.at_least(self.db.connection, (1, 7, 4)): result = coll.inline_map_reduce(map, reduce, query=q) self.assertEqual([], result) result = coll.map_reduce(map, reduce, "results", query=q) self.assertEqual(0, db.results.count()) coll.uuid_subtype = OLD_UUID_SUBTYPE q = {"_id": uu} if version.at_least(self.db.connection, (1, 7, 4)): result = coll.inline_map_reduce(map, reduce, query=q) self.assertEqual(2, len(result)) result = coll.map_reduce(map, reduce, "results", query=q) self.assertEqual(2, db.results.count()) db.drop_collection("result") coll.drop() # Test group coll.insert({"_id": uu, "a": 2}) coll.insert({"_id": uuid.uuid4(), "a": 1}) reduce = "function (obj, prev) { prev.count++; }" coll.uuid_subtype = UUID_SUBTYPE self.assertEqual([], coll.group([], {"_id": uu}, {"count": 0}, reduce)) coll.uuid_subtype = OLD_UUID_SUBTYPE self.assertEqual([{"count": 1}], coll.group([], {"_id": uu}, {"count": 0}, reduce))