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})
Пример #2
0
def dataset_transformations(connector: NomisApiConnector, exists: bool,
                            data: Tuple[pyjstat.Dataset, List[str]]) -> None:
    """
    Function containing the dataset transformation operations.

    :param connector: An open, initialised instance of `NomisApiConnector`.
    :param exists: A bool indicating whether or not the dataset currently exists; if `True`, then the function will
        handle for updating an existing dataset. Conversely, the function will create a new dataset if exists is
        `False`.
    :param data: A tuple containing the required data. That is, a pyjstat dataset corresponding with the query made to
        cantabular, and the list of variables to be assigned to the dataset.
    """
    logger.info("Commencing dataset transformations.")

    table, variables = data

    # Check variables against known geographies. If geography then remove from list and make key.
    geography_variables = config.get_geography()

    key = None
    geography_flag = False
    table_geography = None
    for variable in variables:
        if variable in geography_variables:
            geography_flag = True
            key = variable
            table_geography = copy.deepcopy(table)
            del table["dimension"][variable]
            table["id"].remove(variable)
            variables.remove(variable)

    # If no variables are geography then make first variable key
    if geography_flag is False:
        key = variables[0]

    transformations = DatasetTransformations(table, geography_flag,
                                             table_geography)

    # Create the dataset if it doesn't exist, otherwise retrieve the non-assigned variables
    if not exists:
        non_assigned_variables = variables
        create_dataset(connector, transformations)
        handle_variables(connector, transformations, non_assigned_variables)
    else:
        are_dimensions_same = check_dataset_dimensions(connector, variables)
        if are_dimensions_same is False:
            raise KeyError(
                "ERROR: Dimensions are not the same as existing dataset.")

    handle_dimensions(connector, transformations, key)
    handle_observations(connector, transformations)
 def setUp(self) -> None:
     """Set up a valid instance of DatasetTransformation."""
     self.valid_dataset_transformations = DatasetTransformations(
         VALID_TABLE)