コード例 #1
0
ファイル: main.py プロジェクト: stelioslogothetis/nomis-dts
def handle_variables(connector: NomisApiConnector,
                     transformations: DatasetTransformations,
                     variables: List[str]) -> None:
    """
    Handle variable transmission/manipulation.

    :param connector: An open, initialised instance of `NomisApiConnector`.
    :param transformations: An initialised instance of `DatasetTransformations` with a valid table attribute.
    :param variables: A list of variables to be assigned to the dataset.
    """

    logger.debug("\n-----VARIABLE CREATION-----")

    # Create the variable creation and category request bodies
    variable_request_body = transformations.variable_creation()
    type_request_body = transformations.type_creation()
    type_ids = get_type_ids(type_request_body)
    category_request_body = transformations.category_creation(type_ids)

    for variable in variables:

        # Check to see variables already exist for the given dimensions; IF the variable does NOT exist then create it
        if not connector.get_variable(variable, return_bool=True):

            # Create variable
            for request in variable_request_body:
                if request["name"] == variable:
                    connector.create_variable(variable, request)

            # Create variable type
            requests = []
            for request in type_request_body:
                if request["reference"] == variable:
                    requests.append(request)
            connector.create_variable_type(variable, requests)

            # Create the categories for this new variable
            requests = []
            for category in transformations.table["dimension"][variable][
                    "category"]["index"]:
                for request in category_request_body:
                    if category == request["code"] and transformations \
                            .table["dimension"][variable]["category"]["label"][category] == request["title"]:
                        requests.append(request)

            connector.create_variable_category(variable, requests)
コード例 #2
0
class TestDatasetTransformations(unittest.TestCase):
    def setUp(self) -> None:
        """Set up a valid instance of DatasetTransformation."""
        self.valid_dataset_transformations = DatasetTransformations(
            VALID_TABLE)

    def test_validate_table(self):
        """Test that an instance of DataTransformations is blocked when the table parameter is invalid
        """
        with self.assertRaises(LookupError):
            DatasetTransformations(INVALID_TABLE)
        with self.assertRaises(TypeError):
            DatasetTransformations({"dimensions": True})

    def test_dataset_creation(self) -> None:
        """Test the dataset_creation() method of the DatasetTransformations class by asserting that expected exceptions
        are raised on invalid parameters, and that on valid calls all returned values and types are as expected. Also 
        ensure that the method works as a staticmethod, i.e. is callable without creating an instance of the class.
        """
        # Ensure expected exceptions are raised upon invalid parameters
        with self.assertRaises(TypeError):
            self.valid_dataset_transformations.dataset_creation(VALID_ID, 42)
        with self.assertRaises(ValueError):
            self.valid_dataset_transformations.dataset_creation(
                "", VALID_TITLE)

        # Retrieve a dataset using valid strings as the dataset title and id
        ds = self.valid_dataset_transformations.dataset_creation(
            VALID_ID, VALID_TITLE)

        # Ensure use as a staticmethod works in the same way
        self.assertEqual(
            ds, DatasetTransformations.dataset_creation(VALID_ID, VALID_TITLE))

        # Value & Type check
        self.assertIsInstance(ds, dict)
        self.assertEqual(ds["id"], VALID_ID)
        self.assertEqual(ds["title"], VALID_TITLE)

    def test_variable_creation(self) -> None:
        """Test the variable_creation() method of the DatasetTransformations class by assuring that on the valid
        instances all types and values are as expected
        """
        # Retrieve variables using the valid pyjstat table and assert types/values are as expected
        varis = self.valid_dataset_transformations.variable_creation()
        self.assertIsInstance(varis, list)
        for var in varis:
            self.assertIsInstance(var, dict)
            self.assertTrue(var["name"] in self.valid_dataset_transformations.
                            table["dimension"])

    def test_category_creation(self) -> None:
        """Test the category_creation() method
        """
        # Check valid attempts
        cats = self.valid_dataset_transformations.category_creation(["10000"])
        self.assertIsInstance(cats, list)
        for cat in cats:
            self.assertIsInstance(cat, dict)

    def test_assign_dimensions(self) -> None:
        """Test the assign_dimensions() method
        """
        dims = self.valid_dataset_transformations.assign_dimensions("SEX")
        self.assertIsInstance(dims, list)
        for dim in dims:
            self.assertIsInstance(dim, dict)

    def test_observations(self) -> None:
        """Test the observations() method
        """
        with self.assertRaises(TypeError):
            self.valid_dataset_transformations.observations(12345)
        with self.assertRaises(ValueError):
            self.valid_dataset_transformations.observations("")
        obs = self.valid_dataset_transformations.observations(VALID_ID)
        self.assertIsInstance(obs, dict)
        self.assertEqual(obs["dataset"], VALID_ID)

    def test_variable_metadata_request(self):
        """Test the variable_metadata_request() method
        """
        with self.assertRaises(TypeError):
            self.valid_dataset_transformations.variable_metadata_request(
                VALID_UUID_MD)
        mds = self.valid_dataset_transformations.variable_metadata_request(
            [VALID_UUID_MD])
        self.assertIsInstance(mds, list)
        for md in mds:
            self.assertIsInstance(md, dict)
            self.assertEqual(md['belongsTo'], VALID_UUID_MD.uuid)