コード例 #1
0
    def test_data_fetcher_shared_types(self, mock_ws):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])

        # 1. shared data, default options, include Narratives, but only return KBaseModule.SomeType
        shared_data = df.fetch_accessible_data({
            "data_set":
            "shared",
            "ignore_narratives":
            0,
            "types": ["KBaseModule.SomeType"]
        })
        self.assertEqual(len(shared_data["objects"]), 36)
        for obj in shared_data["objects"]:
            self._validate_obj(obj, "KBaseModule.SomeType")
        self._validate_ws_display(shared_data["workspace_display"], 9)
        self.assertNotIn("type_counts", shared_data)

        # 2. shared data, default options, include Narratives, but only return KBaseNarrative.Narrative
        shared_data = df.fetch_accessible_data({
            "data_set":
            "shared",
            "ignore_narratives":
            0,
            "types": ["KBaseNarrative.Narrative"]
        })
        self.assertEqual(len(shared_data["objects"]), 4)
        for obj in shared_data["objects"]:
            self._validate_obj(obj, "KBaseNarrative.Narrative-4.0")
        self._validate_ws_display(shared_data["workspace_display"], 1)
        self.assertNotIn("type_counts", shared_data)
コード例 #2
0
    def list_all_data(self, ctx, params):
        """
        This is intended to support the Narrative front end. It returns all data a user
        owns, or is shared with them, excluding global data.
        :param params: instance of type "ListAllDataParams" (data_set -
           should be one of "mine", "shared" - other values with throw an
           error include_type_counts - (default 0) if 1, will populate the
           list of types with the count of each data type simple_types -
           (default 0) if 1, will "simplify" types to just their subtype
           (KBaseGenomes.Genome -> Genome) ignore_narratives - (default 1) if
           1, won't return any KBaseNarrative.* objects include_metadata -
           (default 0) if 1, includes object metadata ignore_workspaces -
           (optional) list of workspace ids - if present, will ignore any
           workspace ids given (useful for skipping the currently loaded
           Narrative)) -> structure: parameter "data_set" of String,
           parameter "include_type_counts" of type "boolean" (@range [0,1]),
           parameter "simple_types" of type "boolean" (@range [0,1]),
           parameter "ignore_narratives" of type "boolean" (@range [0,1]),
           parameter "include_metadata" of type "boolean" (@range [0,1]),
           parameter "ignore_workspaces" of list of Long
        :returns: instance of type "ListDataResult" (objects - list of
           objects returned by this function type_counts - mapping of type ->
           count in this function call. If simple_types was 1, these types
           are all the "simple" format (Genome vs KBaseGenomes.Genome)
           workspace_display - handy thing for quickly displaying Narrative
           info.) -> structure: parameter "objects" of list of type
           "DataObjectView" (upa - the UPA for the most recent version of the
           object (wsid/objid/ver format) name - the string name for the
           object narr_name - the name of the Narrative that the object came
           from type - the type of object this is (if simple_types was used,
           then this will be the simple type) savedate - the timestamp this
           object was saved saved_by - the user id who saved this object) ->
           structure: parameter "upa" of String, parameter "name" of String,
           parameter "narr_name" of String, parameter "type" of String,
           parameter "savedate" of Long, parameter "saved_by" of String,
           parameter "type_counts" of mapping from String to Long, parameter
           "workspace_display" of mapping from Long to type "WorkspaceStats"
           (display - the display name for the workspace (typically the
           Narrative name) count - the number of objects found in the
           workspace (excluding Narratives, if requested)) -> structure:
           parameter "display" of String, parameter "count" of Long
        """
        # ctx is the context object
        # return variables are: result
        #BEGIN list_all_data
        auth_url = self.config["auth-service-url"]
        fetcher = DataFetcher(self.workspaceURL, auth_url, ctx["token"])
        result = fetcher.fetch_accessible_data(params)
        #END list_all_data

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method list_all_data return value ' +
                             'result is not type dict as required.')
        # return the results
        return [result]
コード例 #3
0
    def test_data_fetcher_limit(self, mock_ws):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])

        # Get my data, but limit it.
        limit = 19
        data = df.fetch_accessible_data({"data_set": "mine", "limit": limit})
        self.assertEqual(data["limit_reached"], 1)
        self.assertEqual(len(data["objects"]), limit)
コード例 #4
0
    def list_workspace_data(self, ctx, params):
        """
        Also intended to support the Narrative front end. It returns data from a list of
        workspaces. If the authenticated user doesn't have access to any of workspaces, it raises
        an exception. Otherwise, it returns the same structure of results as list_all_data.
        :param params: instance of type "ListWorkspaceDataParams"
           (workspace_ids - list of workspace ids - will only return info
           from these workspaces. include_type_counts - (default 0) if 1,
           will populate the list of types with the count of each data type
           simple_types - (default 0) if 1, will "simplify" types to just
           their subtype (KBaseGenomes.Genome -> Genome) ignore_narratives -
           (default 1) if 1, won't return any KBaseNarrative.* objects
           include_metadata - (default 0) if 1, includes object metadata) ->
           structure: parameter "workspace_ids" of list of Long, parameter
           "include_type_counts" of type "boolean" (@range [0,1]), parameter
           "simple_types" of type "boolean" (@range [0,1]), parameter
           "ignore_narratives" of type "boolean" (@range [0,1]), parameter
           "include_metadata" of type "boolean" (@range [0,1])
        :returns: instance of type "ListDataResult" (objects - list of
           objects returned by this function type_counts - mapping of type ->
           count in this function call. If simple_types was 1, these types
           are all the "simple" format (Genome vs KBaseGenomes.Genome)
           workspace_display - handy thing for quickly displaying Narrative
           info.) -> structure: parameter "objects" of list of type
           "DataObjectView" (upa - the UPA for the most recent version of the
           object (wsid/objid/ver format) name - the string name for the
           object narr_name - the name of the Narrative that the object came
           from type - the type of object this is (if simple_types was used,
           then this will be the simple type) savedate - the timestamp this
           object was saved saved_by - the user id who saved this object) ->
           structure: parameter "upa" of String, parameter "name" of String,
           parameter "narr_name" of String, parameter "type" of String,
           parameter "savedate" of Long, parameter "saved_by" of String,
           parameter "type_counts" of mapping from String to Long, parameter
           "workspace_display" of mapping from Long to type "WorkspaceStats"
           (display - the display name for the workspace (typically the
           Narrative name) count - the number of objects found in the
           workspace (excluding Narratives, if requested)) -> structure:
           parameter "display" of String, parameter "count" of Long
        """
        # ctx is the context object
        # return variables are: result
        #BEGIN list_workspace_data
        auth_url = self.config["auth-service-url"]
        fetcher = DataFetcher(self.workspaceURL, auth_url, ctx["token"])
        result = fetcher.fetch_specific_workspace_data(params)
        #END list_workspace_data

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method list_workspace_data return value ' +
                             'result is not type dict as required.')
        # return the results
        return [result]
コード例 #5
0
    def test_data_fetcher_specific_types(self, mock_ws):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])

        # 1. ws 1,2,3,5, default options, include Narratives, but only return KBaseModule.SomeType
        data = df.fetch_specific_workspace_data({
            "workspace_ids": [1, 2, 3, 5],
            "ignore_narratives":
            0,
            "types": ["KBaseModule.SomeType"]
        })
        self.assertEqual(len(data["objects"]), 36)
        for obj in data["objects"]:
            self._validate_obj(obj, "KBaseModule.SomeType")
        self._validate_ws_display(data["workspace_display"], 9)
        self.assertNotIn("type_counts", data)

        # 2. ws 1,2,3,5, default options, include Narratives, but only return KBaseNarrative.Narrative
        data = df.fetch_specific_workspace_data({
            "workspace_ids": [1, 2, 3, 5],
            "ignore_narratives":
            0,
            "types": ["KBaseNarrative.Narrative"]
        })
        self.assertEqual(len(data["objects"]), 4)
        for obj in data["objects"]:
            self._validate_obj(obj, "KBaseNarrative.Narrative-4.0")
        self._validate_ws_display(data["workspace_display"], 1)
        self.assertNotIn("type_counts", data)

        # 3. ws 1,2,3,5, default options, include Narratives and SomeType, so return everything
        data = df.fetch_specific_workspace_data({
            "workspace_ids": [1, 2, 3, 5],
            "ignore_narratives":
            0,
            "types": ["KBaseNarrative.Narrative", "KBaseModule.SomeType"]
        })
        self.assertEqual(len(data["objects"]), 40)
        for obj in data["objects"]:
            if obj["obj_id"] == 1:
                self._validate_obj(obj, "KBaseNarrative.Narrative-4.0")
            else:
                self._validate_obj(obj, "KBaseModule.SomeType")
        self._validate_ws_display(data["workspace_display"], 10)
        self.assertNotIn("type_counts", data)
コード例 #6
0
    def test_fetch_specific_bad_params(self):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])
        with self.assertRaises(ValueError) as err:
            df.fetch_specific_workspace_data({"workspace_ids": "foo"})
        self.assertIn("Parameter 'workspace_ids' must be a list of integers.",
                      str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_specific_workspace_data({"workspace_ids": []})
        self.assertIn("Parameter 'workspace_ids' must be a list of integers.",
                      str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_specific_workspace_data({"workspace_ids": ["foo"]})
        self.assertIn("Parameter 'workspace_ids' must be a list of integers.",
                      str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_specific_workspace_data(
                {"workspace_ids": [1, 2, 3, "foo", 5]})
        self.assertIn("Parameter 'workspace_ids' must be a list of integers.",
                      str(err.exception))

        optional_params = [
            "include_type_counts", "simple_types", "ignore_narratives"
        ]
        for opt in optional_params:
            with self.assertRaises(ValueError) as err:
                df.fetch_specific_workspace_data({
                    "workspace_ids": [1, 2],
                    opt: "wat"
                })
            self.assertIn(
                "Parameter '{}' must be 0 or 1, not 'wat'".format(opt),
                str(err.exception))

        bad_limits = [0, -5, "a", "foo", ["foo", "bar"], {"no": "wai"}]
        for bad in bad_limits:
            with self.assertRaises(ValueError) as err:
                df.fetch_specific_workspace_data({
                    "workspace_ids": [1],
                    "limit": bad
                })
            self.assertIn("Parameter 'limit' must be an integer > 0",
                          str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_specific_workspace_data({
                "workspace_ids": [1],
                "types": "wat"
            })
        self.assertIn("Parameter 'types' must be a list if present.",
                      str(err.exception))
コード例 #7
0
    def test_fetch_accessible_bad_params(self):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])
        with self.assertRaises(ValueError) as err:
            df.fetch_accessible_data({"data_set": "foo"})
        self.assertIn(
            "Parameter 'data_set' must be either 'mine' or 'shared', not 'foo'",
            str(err.exception))

        optional_params = [
            "include_type_counts", "simple_types", "ignore_narratives"
        ]
        for opt in optional_params:
            with self.assertRaises(ValueError) as err:
                df.fetch_accessible_data({"data_set": "mine", opt: "wat"})
            self.assertIn(
                "Parameter '{}' must be 0 or 1, not 'wat'".format(opt),
                str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_accessible_data({
                "data_set": "mine",
                "ignore_workspaces": "wat"
            })
        self.assertIn(
            "Parameter 'ignore_workspaces' must be a list if present",
            str(err.exception))

        bad_limits = [0, -5, "a", "foo", ["foo", "bar"], {"no": "wai"}]
        for bad in bad_limits:
            with self.assertRaises(ValueError) as err:
                df.fetch_accessible_data({"data_set": "mine", "limit": bad})
            self.assertIn("Parameter 'limit' must be an integer > 0",
                          str(err.exception))

        with self.assertRaises(ValueError) as err:
            df.fetch_accessible_data({"data_set": "mine", "types": "wat"})
        self.assertIn("Parameter 'types' must be a list if present.",
                      str(err.exception))
コード例 #8
0
    def test_data_fetcher_shared(self, mock_ws):
        df = DataFetcher(self.cfg["workspace-url"],
                         self.cfg["auth-service-url"],
                         self.get_context()["token"])

        # 1. shared data, default options
        shared_data = df.fetch_accessible_data({"data_set": "shared"})
        self.assertEqual(len(shared_data["objects"]), 36)
        for obj in shared_data["objects"]:
            self._validate_obj(obj, "KBaseModule.SomeType")
        self._validate_ws_display(shared_data["workspace_display"], 9)
        self.assertNotIn("type_counts", shared_data)

        # 2. shared data, with type counts
        shared_data = df.fetch_accessible_data({
            "data_set": "shared",
            "include_type_counts": 1
        })
        self.assertEqual(len(shared_data["objects"]), 36)
        for obj in shared_data["objects"]:
            self._validate_obj(obj, "KBaseModule.SomeType")
        self._validate_ws_display(shared_data["workspace_display"], 9)
        self.assertIn("type_counts", shared_data)
        self.assertEqual(len(shared_data["type_counts"]),
                         9)  # one for each version of SomeType
        self.assertIn("KBaseModule.SomeType-1.0", shared_data["type_counts"])
        self.assertEqual(
            shared_data["type_counts"]["KBaseModule.SomeType-1.0"], 4)

        # 3. shared data, with simple types, with type counts
        shared_data = df.fetch_accessible_data({
            "data_set": "shared",
            "include_type_counts": 1,
            "simple_types": 1
        })
        self.assertEqual(len(shared_data["objects"]), 36)
        for obj in shared_data["objects"]:
            self._validate_obj(obj, "SomeType")
        self._validate_ws_display(shared_data["workspace_display"], 9)
        self.assertIn("type_counts", shared_data)
        self.assertEqual(len(shared_data["type_counts"]), 1)
        self.assertIn("SomeType", shared_data["type_counts"])
        self.assertEqual(shared_data["type_counts"]["SomeType"], 36)

        # 4. shared data, with simple types, and type counts, don't ignore narratives
        shared_data = df.fetch_accessible_data({
            "data_set": "shared",
            "include_type_counts": 1,
            "simple_types": 1,
            "ignore_narratives": 0
        })
        self.assertEqual(len(shared_data["objects"]), 40)
        for obj in shared_data["objects"]:
            if obj["obj_id"] == 1:
                self._validate_obj(obj, "Narrative")
            else:
                self._validate_obj(obj, "SomeType")
        self._validate_ws_display(shared_data["workspace_display"], 10)
        self.assertIn("type_counts", shared_data)
        self.assertEqual(len(shared_data["type_counts"]), 2)
        self.assertIn("SomeType", shared_data["type_counts"])
        self.assertEqual(shared_data["type_counts"]["SomeType"], 36)
        self.assertIn("Narrative", shared_data["type_counts"])
        self.assertEqual(shared_data["type_counts"]["Narrative"], 4)