def test_from_mongo(self): # Arrange data = { "\$prop1": "value1", "prop$DOT$2": "value2", "prop$DOT$$3": "value3", "prop4": { "\$prop1": "value1", "prop$DOT$2": "value2", "prop$DOT$$3": "value3", }, "prop5": [{ "key$DOT$1": "v1", "\$key2": "v2" }] } # Act obj = ObjectDict._from_mongo(data) # Assert self.assertTrue(isinstance(obj, ObjectDict)) self.assertTrue(hasattr(obj, "$prop1")) self.assertTrue(hasattr(obj, "prop.2")) self.assertTrue(hasattr(obj, "prop.$3")) self.assertTrue(hasattr(obj, "prop4")) self.assertTrue(hasattr(obj, "prop5")) self.assertEqual(obj["$prop1"], "value1") self.assertEqual(obj["prop.2"], "value2") self.assertEqual(obj["prop.$3"], "value3") self.assertEqual(obj["prop4"], \ {"$prop1": "value1", "prop.2": "value2", "prop.$3": "value3"}) self.assertEqual(obj["prop5"], [{"key.1": "v1", "$key2": "v2"}])
def test_from_mongo(self): # Arrange data = { "\$prop1": "value1", "prop$DOT$2": "value2", "prop$DOT$$3": "value3", "prop4": { "\$prop1": "value1", "prop$DOT$2": "value2", "prop$DOT$$3": "value3", }, "prop5": [ { "key$DOT$1": "v1", "\$key2": "v2" } ] } # Act obj = ObjectDict._from_mongo(data) # Assert self.assertTrue(isinstance(obj, ObjectDict)) self.assertTrue(hasattr(obj, "$prop1")) self.assertTrue(hasattr(obj, "prop.2")) self.assertTrue(hasattr(obj, "prop.$3")) self.assertTrue(hasattr(obj, "prop4")) self.assertTrue(hasattr(obj, "prop5")) self.assertEqual(obj["$prop1"], "value1") self.assertEqual(obj["prop.2"], "value2") self.assertEqual(obj["prop.$3"], "value3") self.assertEqual(obj["prop4"], \ {"$prop1": "value1", "prop.2": "value2", "prop.$3": "value3"}) self.assertEqual(obj["prop5"], [{"key.1": "v1", "$key2": "v2"}])
def _insert(self, resources): accessPoint = resources[0]["accessPoint"] tmpExists = yield self.dblayer.find_one({ "accessPoint": accessPoint }) self.application._level = max(self.application._level, resources[0]["properties"]["depth"] + 1) if tmpExists: yield self.dblayer.update({ "accessPoint": accessPoint }, resources[0], replace = True, multi=False) else: yield self.dblayer.insert(resources) for manifest in resources[0]["properties"]["summary"]: manifest = ObjectDict._from_mongo(manifest) manifest["href"] = accessPoint manifest["ttl"] = resources[0]["ttl"] manifest.pop(self.timestamp, 0) yield self._update_manifest(manifest, accessPoint)
def _report_to_root(self): manifests = [] cursor = self.db["manifests"].find({ "\\$shard": False}, {"_id": False }) while (yield cursor.fetch_next): manifests.append(ObjectDict._from_mongo(cursor.next_object())) import time if self.options["unis_ssl"]["enable"]: http_str = "https" else: http_str = "http" callback = functools.partial(self.registered, fatal = False) service = { u"id": u"unis_" + socket.gethostname(), u"ts": int(time.time() * 1e6), u"\$schema": unicode(SCHEMAS["service"]), u"accessPoint": self.options["unis"]["url"], u"name": u"unis_" + socket.gethostname(), u"status": u"ON", u"serviceType": u"ps:tools:unis", u"ttl": int(self.options["unis"]["summary_collection_period"]), u"runningOn": { u"href": u"%s/nodes/%s" % (self.options["unis"]["url"], socket.gethostname()), u"rel": u"full" }, u"properties": { u"summary": manifests, u"depth": self._depth } } for lookup in self.options["unis"]["root_urls"]: service_url = lookup + '/register' http_client = AsyncHTTPClient() content_type = MIME['PSJSON'] + '; profile=' + SCHEMAS['service'] http_client.fetch(service_url, method="POST", body=json.dumps(service), headers = { "Content-Type": content_type, "Cache-Control": "no-cache", "Accept": MIME['PSJSON'], "Connection": "close"}, callback=callback)
def _return_resources(self, query): try: cursor = self._find(query=query) response = [] while (yield cursor.fetch_next): resource = ObjectDict._from_mongo(cursor.next_object()) response.append(resource) if len(response) == 1: location = self.request.full_url().split('?')[0] if not location.endswith(response[0][self.Id]): location = location + "/" + response[0][self.Id] self.set_header("Location", location) self.write(dumps_mongo(response[0], indent=2)) else: self.write(dumps_mongo(response, indent=2)) except Exception as exp: raise ValueError(exp)
def _return_resources(self, query): try: cursor = self._find(query = query) response = [] while (yield cursor.fetch_next): resource = ObjectDict._from_mongo(cursor.next_object()) response.append(resource) if len(response) == 1: location = self.request.full_url().split('?')[0] if not location.endswith(response[0][self.Id]): location = location + "/" + response[0][self.Id] self.set_header("Location", location) self.write(dumps_mongo(response[0], indent=2)) else: self.write(dumps_mongo(response, indent=2)) except Exception as exp: raise ValueError(exp)