Exemple #1
0
    def get_suggested_json(self, raw_actual: JsonDict = None) -> JsonDict:
        """Return the suggested JSON based on actual values."""
        actual = set(flatten(raw_actual).keys()) if raw_actual else set()
        expected = set(self.file_dict.get(KEY_CONTAINS_KEYS) or [])
        missing = expected - actual
        if not missing:
            return {}

        return SortedDict(unflatten({key: self.SOME_VALUE_PLACEHOLDER for key in missing}))
Exemple #2
0
    def compare_with_flatten(
            self,
            expected: Union[JsonDict, "BaseFormat"] = None) -> Comparison:
        """Compare two flattened dictionaries and compute missing and different items."""
        comparison = self._create_comparison(expected)
        if comparison.flat_expected.items() <= comparison.flat_actual.items():
            return comparison

        comparison.set_missing(
            unflatten({
                k: v
                for k, v in comparison.flat_expected.items()
                if k not in comparison.flat_actual
            }))
        comparison.set_diff(
            unflatten({
                k: v
                for k, v in comparison.flat_expected.items()
                if k in comparison.flat_actual
                and comparison.flat_expected[k] != comparison.flat_actual[k]
            }))
        return comparison
Exemple #3
0
    def update_pair(self, key, raw_expected):
        """Update a key on one of the comparison dicts, with its raw expected value."""
        if isinstance(key, str):
            self.diff_dict.update({key: raw_expected})
            return

        if isinstance(key, list):
            if len(key) == 4 and isinstance(key[3], int):
                _, _, new_key, index = key
                if new_key not in self.diff_dict:
                    self.diff_dict[new_key] = []
                self.diff_dict[new_key].insert(index, raw_expected)
                return
            if len(key) == 1:
                self.diff_dict.update(unflatten({key[0]: raw_expected}))
                return

        LOGGER.warning(
            "Err... this is unexpected, please open an issue: key=%s raw_expected=%s",
            key, raw_expected)