def check_write(encoding, obj, expected): doc = document.WritableDocument("application/json", encoding=encoding) yield document.write(doc, obj) data = doc.get_data() self.assertTrue(isinstance(data, str)) self.assertEqual(expected, data)
def render_inline_model(obj, context, *args, **kwargs): obj = IModel(obj) doc = NestedJson() d = document.write(doc, obj, context=context) d.addCallback(defer.override_result, doc) return d
def _validate_model(self, model, context): self.info("Validating model %r", model) # check writing it to supported mime types for mime_type in self.validated_mime_types: doc = document.WritableDocument(mime_type) d = document.write(doc, model, context=context) d.addErrback(self._writing_errback, mime_type, model) yield d self.info('Result of rendering for mimetype: %s\n%s', mime_type, doc.get_data()) # call querying actions actions = yield model.fetch_actions() for action in actions: if action.category != ActionCategories.retrieve: self.info("Not validating action name: %s, label: %s, " "as it's category is: %s", action.name, action.label, action.category) continue d = action.perform() d.addErrback(self._action_errback, action, model) result = yield d self.info("Successfully performed action %s with result %r", action.name, result)
def check(self, obj, expected, exp_type=None, encoding="UTF8", verbose=False, **kwargs): doc = document.WritableDocument("application/json", encoding=encoding) ctx = DummyContext(("ROOT", ), ("root", )) fmt = "verbose" if verbose else "compact" yield document.write(doc, obj, context=ctx, format=fmt, **kwargs) data = doc.get_data() self.assertTrue(isinstance(data, str)) struct = json.loads(data, encoding=encoding) if exp_type is not None: self.assertTrue(isinstance(struct, exp_type)) if expected != struct: path, msg = deep_compare(expected, struct) expected_str = StringIO.StringIO() result_str = StringIO.StringIO() pprint.pprint(expected, stream=expected_str) pprint.pprint(struct, stream=result_str) self.fail("ERROR in %s: %s\nEXPECTED:\n%s\nRESULT:\n%s" % (path, msg, expected_str.getvalue(), result_str.getvalue())) defer.returnValue(data)
def model_as_json(self, model): doc = document.WritableDocument("application/json", encoding='utf-8') ctx = DummyContext(("ROOT", ), ("root", )) yield document.write(doc, model, context=ctx, format="compact") defer.returnValue(json.loads(doc.get_data()))