def test_to_mongo(self): # Arrange obj = ObjectDict() obj["$prop1"] = "value1" obj["prop.2"] = "value2" obj["prop.$3"] = "value3" obj["prop4"] = copy.deepcopy(obj) obj["prop5"] = [{"key.1": "v1", "$key2": "v2"}] expected = { "\$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 mongo_dict = dict(obj._to_mongoiter()) # Assert self.assertTrue(hasattr(obj, "$prop1")) self.assertTrue(hasattr(obj, "prop.2")) self.assertTrue(hasattr(obj, "prop.$3")) self.assertEqual(obj["$prop1"], "value1") self.assertEqual(obj["prop.2"], "value2") self.assertEqual(obj["prop.$3"], "value3") self.assertEqual(mongo_dict, expected)
def test_to_mongo(self): # Arrange obj = ObjectDict() obj["$prop1"] = "value1" obj["prop.2"] = "value2" obj["prop.$3"] = "value3" obj["prop4"] = copy.deepcopy(obj) obj["prop5"] = [{"key.1": "v1", "$key2": "v2"}] expected = { "\$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 mongo_dict = dict(obj._to_mongoiter()) # Assert self.assertTrue(hasattr(obj, "$prop1")) self.assertTrue(hasattr(obj, "prop.2")) self.assertTrue(hasattr(obj, "prop.$3")) self.assertEqual(obj["$prop1"], "value1") self.assertEqual(obj["prop.2"], "value2") self.assertEqual(obj["prop.$3"], "value3") self.assertEqual(mongo_dict, expected)
def test_init(self): # Arrange test_data = {"key1": "value1", "key2": "value2", "$key3": "value3"} # Act obj1 = ObjectDict() obj2 = ObjectDict(test_data) # Assert self.assertEqual(obj1, {}) self.assertEqual(obj2, test_data) for key, value in test_data.items(): self.assertTrue(hasattr(obj2, key)) self.assertEqual(getattr(obj2, key), value)
def test_convert_value(self): # Arrange test_data = { "key1": "value1", "key2": { "k1": "v1" }, "key3": { "k2": { "k3": "v3" } } } obj = ObjectDict(test_data) # Act key1 = obj.key1 key2 = obj["key2"] key3 = obj.key3 k1 = key2.k1 k2 = key3["k2"] k3 = k2.k3 # Assert self.assertEqual(obj, test_data) self.assertTrue(isinstance(obj, ObjectDict)) self.assertTrue(isinstance(key1, str)) self.assertTrue(isinstance(key2, ObjectDict)) self.assertTrue(isinstance(key3, ObjectDict)) self.assertTrue(isinstance(k1, str)) self.assertTrue(isinstance(k2, ObjectDict)) self.assertTrue(isinstance(k3, str))
def test_add_property(self): # Arrange test_data = {"key1": "value1", "key2": "value2", "$key3": "value3"} obj = ObjectDict(test_data) # Act obj.key4 = "value4" obj["key5"] = "value5" # Assert self.assertTrue(hasattr(obj, "key4")) self.assertEqual(getattr(obj, "key4"), "value4") self.assertTrue(hasattr(obj, "key5")) self.assertEqual(getattr(obj, "key5"), "value5") self.assertNotEqual(obj, test_data) for key, value in test_data.items(): self.assertTrue(hasattr(obj, key)) self.assertEqual(getattr(obj, key), value)
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 _create_manifest_shard(self, resource): if "\\$collection" in resource: tmpResource = resource["properties"] else: tmpResource = resource tmpResult = {} tmpResult["properties"] = self._flatten_shard(tmpResource) tmpResult["$shard"] = True tmpResult["$collection"] = self._collection_name return dict(ObjectDict(tmpResult)._to_mongoiter())
def test_convert_value_nested(self): # Arrange test_data = { "key1": "value1", "key2": {"k1": "v1"}, "key3": {"k2": {"k3": "v3"}}, "key4": [{"k4": {"k5": "v5"}}], } expected_data = { "key1": "value1", "key2": {"k1": "v1"}, "key3": {"k2": {"k3": "v3"}}, "key4": [{"k4": {"k5": "v5"}}, {"k6": "v6"}], "key5": {"k7": [{"k8": "v8"}, {"k9": "v9"}]}, } obj = ObjectDict(test_data) # Act key1 = obj.key1 key2 = obj["key2"] key3 = obj.key3 key4 = obj.key4 k1 = key2.k1 k2 = key3["k2"] k3 = k2.k3 key4 = obj.key4 key4.append({"k6": "v6"}) obj.key5 = {"k7": [{"k8": "v8"}]} k7 = obj.key5.k7 k7.append({"k9": "v9"}) # Assert self.assertEqual(obj, expected_data) self.assertTrue(isinstance(obj, ObjectDict)) self.assertTrue(isinstance(key1, str)) self.assertTrue(isinstance(key2, ObjectDict)) self.assertTrue(isinstance(key3, ObjectDict)) self.assertTrue(isinstance(k1, str)) self.assertTrue(isinstance(k2, ObjectDict)) self.assertTrue(isinstance(k3, str))
def test_del_property(self): # Arrange test_data = {"key1": "value1", "key2": "value2", "$key3": "value3"} obj = ObjectDict(test_data) # Act del obj.key1 del obj["key2"] # Assert self.assertNotEqual(obj, test_data) self.assertFalse(hasattr(obj, "key1")) self.assertFalse(hasattr(obj, "key2")) self.assertFalse("key1" in obj) self.assertFalse("key2" in obj) self.assertTrue(hasattr(obj, "$key3")) self.assertEqual(getattr(obj, "$key3"), "value3")
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)
def test_convert_value_nested(self): # Arrange test_data = { "key1": "value1", "key2": { "k1": "v1" }, "key3": { "k2": { "k3": "v3" } }, "key4": [{ "k4": { "k5": "v5" } }] } expected_data = { "key1": "value1", "key2": { "k1": "v1" }, "key3": { "k2": { "k3": "v3" } }, "key4": [{ "k4": { "k5": "v5" } }, { "k6": "v6" }], "key5": { "k7": [{ "k8": "v8" }, { "k9": "v9" }] } } obj = ObjectDict(test_data) # Act key1 = obj.key1 key2 = obj["key2"] key3 = obj.key3 key4 = obj.key4 k1 = key2.k1 k2 = key3["k2"] k3 = k2.k3 key4 = obj.key4 key4.append({"k6": "v6"}) obj.key5 = {"k7": [{"k8": "v8"}]} k7 = obj.key5.k7 k7.append({"k9": "v9"}) # Assert self.assertEqual(obj, expected_data) self.assertTrue(isinstance(obj, ObjectDict)) self.assertTrue(isinstance(key1, str)) self.assertTrue(isinstance(key2, ObjectDict)) self.assertTrue(isinstance(key3, ObjectDict)) self.assertTrue(isinstance(k1, str)) self.assertTrue(isinstance(k2, ObjectDict)) self.assertTrue(isinstance(k3, str))