def test_validate_null(self):
        base_cls = NullableDnaString
        cls = extend_class(base_cls, RequireNullValue)

        column = base_cls.build("name", "ACGT")
        self.assertEqual(len(column.validate()), 0)
        column = base_cls.build("name", "")
        self.assertEqual(len(column.validate()), 0)

        column = cls.build("name", "ACGT")
        self.assertIn("was not a null value", column.validate()[0].message)
        column = cls.build("name", "")
        self.assertEqual(len(column.validate()), 0)
Exemple #2
0
def combine_columns(
    base_columns: List[_Column],
    extra_columns: Optional[List[_Column]] = None,
    filtered: Optional[List[str]] = None,
) -> List[_Column]:
    """Combines columns when building a scheme.

    The `base_columns` and `extra_columns` should be a list of columns (of
    type `_Column`).

    Any columns in `extra_columns` that are also in `base_columns` will have
    the column class from `extra_columns` mixed into `base_columns`.  New
    columns from `extra_columns` will be appended.  The description will be
    kept from `extra_columns`.

    `filtered` can be used to _remove_ any columns from either the
    `base_columns` or the `extra_columns` after they are combined.
    """

    columns = {c.name: c for c in base_columns}

    if extra_columns:
        # add any additional types to the base columns
        for extra_column in extra_columns:
            name = extra_column.name
            if name in columns:
                cls = extend_class(columns[name].cls, extra_column.cls)
                desc = extra_column.desc
                columns[name] = _Column(name=name, cls=cls,
                                        desc=desc)  # type: ignore

        # add the extra_columns
        columns.update([(c.name, c) for c in extra_columns
                        if c.name not in columns])

    if filtered is not None:
        missing_filtered = [f for f in filtered if f not in columns]
        if missing_filtered:
            raise ValueError("Filtered columns not found in the scheme it "
                             "extends: %s" % ", ".join(missing_filtered))
        filtered_columns = [
            column for name, column in columns.items() if name not in filtered
        ]
        return filtered_columns
    return list(columns.values())
Exemple #3
0
 def test_extend_class(self):
     base_cls = TestMisc.Hello
     cls = extend_class(base_cls, TestMisc.World)
     self.assertEqual(str(base_cls()), "hello")
     self.assertEqual(str(cls()), "world")