def delete(self, name): component = Component.get(name) component.delete() state.components.pop(name, None) self.reset_completer() print(f"Component {name} deleted")
def test_updating_component(): brown_sugar = Component.create("brown_sugar", kind="solid", units=100) old_units = brown_sugar.units brown_sugar.update(units=200) new_units = brown_sugar.units assert old_units != new_units
def test_normalize_component(): tomato = Component.get("Tomato") assert (tomato % 28).to_dict() == { "name": "Tomato", "kind": "solid", "units": 28.0, "attrs": { "fiber": 6.499999999999999 } }
def test_multiplicating_component(): tomato = Component.get("Tomato") assert (tomato * 10).to_dict() == { "name": "Tomato", "kind": "solid", "units": 560, "attrs": { "fiber": 130 } }
def register(self, name): name = name.lower() component = Component.get(name) if not component: print("Registering", name) kind = self._get_kind() attrs = self._get_attrs() else: print("Updating", name) kind = self._get_kind(default=component.kind) attrs = self._get_attrs(defaults={"units": component.units, **component.attrs}) units = attrs.pop("units", None) component, _ = self.create_or_update_component( name, kind, units, attrs, component=component ) self.reset_completer()
def test_deleting_component(): brown_sugar = Component.get("brown_sugar") brown_sugar.delete() assert Component.get("brown_sugar") is None
def test_get_non_existing_component(): assert Component.get("TEST") is None
def test_get_component(name): assert Component.get(name)
def test_list_components(): assert len(Component.list()) == 4 assert len(Component.list(kind="liquid")) == 2 assert len(Component.list(kind="solid")) == 2
def test_create_components_exception(): with raises(Exception, match="Kind must be set"): Component.create("Vodka")
def test_create_components(kwargs): Component.create(**kwargs)
def _get_members(obj, cached=None): members = [] for name, units in obj.components.items(): if (cached and (member := cached.get(name)) or (member := Component.get(name))):
assert units, "Units must be set" assert kind in ("solid", "liquid"), "Kind must be either solid or liquid" if component or (component := state.components.get(name.lower())): component.update( kind=kind, units=units, attrs=attrs, components=components ) print(f"Updating {name}") else: component = Component.create( name, kind=kind, units=units, attrs=attrs, components=components ) created = True print(f"Creating {name}") state.components[name] = component return component, created def detail_compound(self, compound): display_details(compound)
def load_components(self): self.components = {i.name: i for i in Component.list()}