def test_parse_fail(self):
     bads = [
         "123/456",
         "123",
         "123/456/789/8",
         "123/456/",
         "123/",
         "foo/bar/baz"
     ]
     for bad in bads:
         with self.assertRaises(ValueError):
             NarrativeRef.parse(bad)
    def test_equality(self):
        ref1 = NarrativeRef.parse("1/2/3")
        ref2 = NarrativeRef.parse("1/2/3")
        ref_ws = NarrativeRef.parse("4/2/3")
        ref_obj = NarrativeRef.parse("1/4/3")
        ref_ver = NarrativeRef.parse("1/2/4")

        self.assertEqual(ref1, ref1)
        self.assertEqual(ref1, ref2)
        self.assertEqual(ref2, ref1)
        self.assertNotEqual(ref1, ref_ws)
        self.assertNotEqual(ref1, ref_obj)
        self.assertNotEqual(ref1, ref_ver)
        self.assertNotEqual(ref_ws, ref1)
        self.assertNotEqual(ref_obj, ref1)
        self.assertNotEqual(ref_ver, ref1)
 def test_save_narrative_url_bad(self, rqm):
     mock_ws_bad(rqm, "Failed to alter metadata")
     ws_id = 234
     with self.assertRaises(WorkspaceError) as e:
         save_narrative_url(self.cfg["workspace-url"], self.token,
                            NarrativeRef.parse("234/1/2"), "/234/1")
     self.assertIn("Failed to alter metadata", str(e.exception))
     self.assertIn(str(ws_id), str(e.exception))
 def test_read_narrative_bad_client(self, rqm):
     ws_id = 908
     mock_ws_bad(rqm, "Can't fetch object")
     with self.assertRaises(WorkspaceError) as e:
         read_narrative(
             NarrativeRef.parse("908/1/1"),
             Workspace(url=self.cfg["workspace-url"], token=self.token))
     self.assertIn(str(ws_id), str(e.exception))
     self.assertIn("Can't fetch object", str(e.exception))
Пример #5
0
    def create_static_narrative(self, ctx, params):
        """
        Creates a static Narrative from the given Narrative ref string.
        :param params: instance of type "CreateStaticNarrativeInput"
           (narrative_ref - the reference to the Narrative object to make a
           static version of. must include version! overwrite - if true,
           overwrite any previous version of the static Narrative.) ->
           structure: parameter "narrative_ref" of type "ws_ref" (a workspace
           object reference string (of the form wsid/objid/ver)), parameter
           "overwrite" of type "boolean" (allowed 0 or 1)
        :returns: instance of type "CreateStaticNarrativeOutput" ->
           structure: parameter "static_narrative_url" of type "url"
        """
        # ctx is the context object
        # return variables are: output
        #BEGIN create_static_narrative

        # init - get NarrativeRef and Exporter set up
        ref = NarrativeRef.parse(params["narrative_ref"])
        ws_url = self.config["workspace-url"]
        self.logger.info(f"Creating Static Narrative {ref}")
        verify_admin_privilege(ws_url, ctx["user_id"], ctx["token"], ref.wsid)
        verify_public_narrative(ws_url, ref.wsid)
        exporter = NarrativeExporter(self.config, ctx["user_id"], ctx["token"])

        # set up output directories
        try:
            output_dir = os.path.join(self.config["scratch"], str(ref.wsid),
                                      str(ref.objid), str(ref.ver))
            os.makedirs(output_dir, exist_ok=True)
        except IOError as e:
            self.logger.error(
                "Error while creating Static Narrative directory", e)
            raise

        # export the narrative to a file
        try:
            output_path = exporter.export_narrative(ref, output_dir)
        except Exception as e:
            self.logger.error("Error while exporting Narrative", e)
            raise

        # upload it and save it to the Workspace metadata before returning the url path
        static_url = upload_static_narrative(ref, output_path,
                                             self.config["static-file-root"])
        save_narrative_url(self.config["workspace-url"], ctx["token"], ref,
                           static_url)
        output = {"static_narrative_url": static_url}
        self.logger.info(f"Finished creating Static Narrative {ref}")
        #END create_static_narrative

        # At some point might do deeper type checking...
        if not isinstance(output, dict):
            raise ValueError('Method create_static_narrative return value ' +
                             'output is not type dict as required.')
        # return the results
        return [output]
 def test_read_narrative_not_narrative(self, rqm):
     ref = "43666/3/1"
     ref_to_file = {ref: "data/43666/report-43666.3.1.json"}
     set_up_ok_mocks(rqm, ref_to_file=ref_to_file)
     with self.assertRaises(ValueError) as e:
         read_narrative(
             NarrativeRef.parse(ref),
             Workspace(url=self.cfg["workspace-url"], token=self.token))
     self.assertIn(
         f"Expected a Narrative object with reference {ref}, got a KBaseReport.Report-3.0",
         str(e.exception))
 def test_read_narrative_ok(self, rqm):
     ref = "43666/1/18"
     ref_to_file = {ref: "data/43666/narrative-43666.1.18.json"}
     set_up_ok_mocks(rqm, ref_to_file=ref_to_file)
     nar = read_narrative(
         NarrativeRef.parse("43666/1/18"),
         Workspace(url=self.cfg["workspace-url"], token=self.token))
     # spot check that it's loaded and formatted
     self.assertIsNotNone(nar)
     self.assertIn("cells", nar)
     self.assertEqual(len(nar["cells"]), 9)
 def test_validate_narr_type(self):
     good_types = [
         "KBaseNarrative.Narrative-1.0", "KBaseNarrative.Narrative-4.0"
     ]
     bad_types = [
         "SomeObject.Name", "kbasenarrative.Narrative-1.0"
         "KBaseNarrative.Narrative-1.0.0"
     ]
     invalid_types = [5, str, {"lol": "no"}, ["wat"], None]
     ref = NarrativeRef.parse("1/2/3")
     for t in good_types:
         self.assertIsNone(_validate_narr_type(t, ref))
     for t in bad_types:
         with self.assertRaises(ValueError) as e:
             _validate_narr_type(t, ref)
         self.assertIn(
             f"Expected a Narrative object with reference {str(ref)}, got a",
             str(e.exception))
     for t in invalid_types:
         with self.assertRaises(ValueError) as e:
             _validate_narr_type(t, ref)
         self.assertIn(f"The type string must be a string",
                       str(e.exception))
Пример #9
0
 def setUpClass(cls):
     cls.ref = NarrativeRef.parse("1/2/3")
     cls.cfg = get_test_config()
 def test_ok_from_parse(self):
     ref = NarrativeRef.parse("123/456/7")
     self.assertEqual(ref.wsid, 123)
     self.assertEqual(ref.objid, 456)
     self.assertEqual(ref.ver, 7)