def construct(*, schema: types.Schema) -> TableArgs: """ Construct any table args from the object schema. Look for x-composite-unique and x-composite-index keys in the schema and construct any unique constraints and indexes based on their value. Args: schema: The schema for the object. Returns: A tuple with any unique constraints and indexes. """ # Keep track of any table arguments table_args: typing.List[typing.Iterable[TableArg]] = [] # Handle x-composite-unique unique_spec = ext_prop.get(source=schema, name=types.ExtensionProperties.COMPOSITE_UNIQUE) if unique_spec is not None: table_args.append(factory.unique_factory(spec=unique_spec)) # Handle x-composite-index index_spec = ext_prop.get(source=schema, name=types.ExtensionProperties.COMPOSITE_INDEX) if index_spec is not None: table_args.append(factory.index_factory(spec=index_spec)) return tuple(itertools.chain.from_iterable(table_args))
def test_invalid(name, value): """ GIVEN property and invalid value WHEN get is called with a source made of the property and value THEN MalformedExtensionPropertyError is raised. """ source = {name: value} with pytest.raises(exceptions.MalformedExtensionPropertyError): ext_prop.get(source=source, name=name)
def test_mixins_invalid(value): """ GIVEN value for x-mixins that has an invalid format WHEN get with x-mixins and the value THEN MalformedExtensionPropertyError is raised. """ name = "x-mixins" source = {name: value} with pytest.raises(exceptions.MalformedExtensionPropertyError): ext_prop.get(source=source, name=name)
def test_relationship_backrefs_invalid(value): """ GIVEN value for x-backrefs with an invalid format WHEN get is called with x-backrefs and the value THEN MalformedExtensionPropertyError is raised. """ name = "x-backrefs" source = {name: value} with pytest.raises(exceptions.MalformedExtensionPropertyError): ext_prop.get(source=source, name=name)
def test_unique_constraint_invalid(value): """ GIVEN value for x-composite-unique that has an invalid format WHEN get with x-composite-unique and the value THEN MalformedExtensionPropertyError is raised. """ name = "x-composite-unique" source = {name: value} with pytest.raises(exceptions.MalformedExtensionPropertyError): ext_prop.get(source=source, name=name)
def test_miss(): """ GIVEN empty source WHEN get is called with the source THEN None is returned. """ assert ext_prop.get(source={}, name="missing") is None
def test_miss_default(): """ GIVEN empty source and default value WHEN get is called with the source and default value THEN default value is returned. """ default = "value 1" value = ext_prop.get(source={}, name="missing", default=default) assert value == default
def test_relationship_backrefs_valid(prefix, value): """ GIVEN prefix and value for x-backrefs with a valid format WHEN get is called with x-backrefs and the value THEN value is returned. """ name = "backrefs" source = {f"{prefix}{name}": value} return_value = ext_prop.get(source=source, name=f"{types.KeyPrefixes.SHORT}{name}") assert return_value == value
def test_valid(prefix, name, value): """ GIVEN prefix, property and valid value WHEN get is called with a source made of the property and value THEN the value is returned. """ source = {f"{prefix}{name}": value} returned_value = ext_prop.get(source=source, name=f"{types.KeyPrefixes.SHORT}{name}") assert returned_value == value
def test_mixins_valid(prefix, value): """ GIVEN prefix and value for x-mixins that has a valid format WHEN get with x-mixins and the value THEN the value is returned. """ name = "mixins" source = {f"{prefix}{name}": value} returned_value = ext_prop.get(source=source, name=f"{types.KeyPrefixes.SHORT}{name}") assert returned_value == value
def test_composite_index_valid(prefix, value): """ GIVEN prefix and value for x-composite-index that has a valid format WHEN get is called with x-composite-index and the value THEN the value is returned. """ name = "composite-index" source = {f"{prefix}{name}": value} returned_value = ext_prop.get(source=source, name=f"{types.KeyPrefixes.SHORT}{name}") assert returned_value == value
def test_unique_constraint_valid(prefix, value): """ GIVEN prefix and value for x-composite-unique that has a valid format WHEN get with x-composite-unique and the value THEN the value is returned. """ name = "composite-unique" source = {f"{prefix}{name}": value} returned_value = ext_prop.get(source=source, name=f"{types.KeyPrefixes.SHORT}{name}") assert returned_value == value
def test_pop(prefix): """ GIVEN prefix, property and valid value WHEN get is called with the name, value and pop set THEN the key is removed from the dictionary. """ name = "dict-ignore" value = True source = {f"{prefix}{name}": value} returned_value = ext_prop.get( source=source, name=f"{types.KeyPrefixes.SHORT}{name}", pop=True ) assert returned_value == value assert source == {}
def _calculate_kwargs(*, schema: types.Schema) -> Kwargs: """ Calculate the kwargs for the table. Args: schema: The model schema. Returns: The kwargs for the table. """ kwargs: Kwargs = {} schema_name = ext_prop.get( source=schema, name=types.ExtensionProperties.SCHEMA_NAME ) if schema_name is not None: kwargs["schema"] = schema_name return kwargs