示例#1
0
    def delete(self, name):
        component = Component.get(name)
        component.delete()

        state.components.pop(name, None)
        self.reset_completer()

        print(f"Component {name} deleted")
示例#2
0
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
示例#3
0
def test_normalize_component():
    tomato = Component.get("Tomato")

    assert (tomato % 28).to_dict() == {
        "name": "Tomato",
        "kind": "solid",
        "units": 28.0,
        "attrs": {
            "fiber": 6.499999999999999
        }
    }
示例#4
0
def test_multiplicating_component():
    tomato = Component.get("Tomato")

    assert (tomato * 10).to_dict() == {
        "name": "Tomato",
        "kind": "solid",
        "units": 560,
        "attrs": {
            "fiber": 130
        }
    }
示例#5
0
    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()
示例#6
0
def test_deleting_component():
    brown_sugar = Component.get("brown_sugar")

    brown_sugar.delete()

    assert Component.get("brown_sugar") is None
示例#7
0
def test_get_non_existing_component():
    assert Component.get("TEST") is None
示例#8
0
def test_get_component(name):
    assert Component.get(name)
示例#9
0
def test_list_components():
    assert len(Component.list()) == 4
    assert len(Component.list(kind="liquid")) == 2
    assert len(Component.list(kind="solid")) == 2
示例#10
0
def test_create_components_exception():
    with raises(Exception, match="Kind must be set"):
        Component.create("Vodka")
示例#11
0
def test_create_components(kwargs):
    Component.create(**kwargs)
示例#12
0
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))):
示例#13
0
        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)
示例#14
0
 def load_components(self):
     self.components = {i.name: i for i in Component.list()}