Beispiel #1
0
def test_resources_from_json():
    "Resources.from_json consumes a data structure and produces the expected result"
    json = {
        "managed": ["ListThing=*", "Thing=*"],
        "resources": [
            {"kind": "ListThing", "listThingId": "lt", "things": ["1", "2"]},
            {"kind": "Thing", "thingId": "x", "value": "1"},
            {"kind": "Thing", "thingId": "y", "value": "1"},
        ],
    }
    assert Resources.from_json(json) == Resources(
        [Thing("x", "1"), ListThing("lt", ["1", "2"]), Thing("y", "1")],
        ["ListThing=*", "Thing=*"],
    )
Beispiel #2
0
def test_resources_to_json():
    "Resources.to_json produces the expected data structure"
    rsrcs = Resources(
        [Thing("x", "1"), Thing("y", "1"), ListThing("lt", ["1", "2"])],
        ["Thing=*", "ListThing=*"],
    )
    assert rsrcs.to_json() == {
        "managed": ["Thing=*", "ListThing=*"],
        "resources": [
            {"kind": "ListThing", "listThingId": "lt", "things": ["1", "2"]},
            {"kind": "Thing", "thingId": "x", "value": "1"},
            {"kind": "Thing", "thingId": "y", "value": "1"},
        ],
    }
Beispiel #3
0
def test_resources_str():
    "Resources are stringified in order"
    resources = Resources(
        [Thing("x", "1"), Thing("y", "1")], ["Thing=*", "OtherStuff=.*"]
    )
    assert str(resources) == textwrap.dedent(
        """\
      managed:
        - Thing=*
        - OtherStuff=.*

      resources:
        Thing=x:
          thingId: x
          value: 1

        Thing=y:
          thingId: y
          value: 1"""
    )
Beispiel #4
0
def test_resources_repr():
    "Resources repr is pretty JSON"
    resources = Resources([Thing("x", "1"), Thing("y", "1")], [".*"])
    assert json.loads(repr(resources)) == json.loads(
        """\
        {
            "managed": [
                ".*"
            ],
            "resources": [
                {
                    "kind": "Thing",
                    "thingId": "x",
                    "value": "1"
                },
                {
                    "kind": "Thing",
                    "thingId": "y",
                    "value": "1"
                }
            ]
        }"""
    )
Beispiel #5
0
def test_resources_verify_unmanaged_prohibited():
    "Duplicate resources are not allowed"
    with pytest.raises(RuntimeError) as exc:
        Resources([Thing("x", "1"), ListThing("y", [])], ["Thing=*"])
    assert "unmanaged resources: ListThing=y" in str(exc.value)
Beispiel #6
0
def test_resources_verify_duplicates_prohibited():
    "Duplicate resources are not allowed"
    rsrcs = Resources([Thing("x", "1")], [".*"])
    with pytest.raises(RuntimeError) as exc:
        rsrcs.add(Thing("x", "1"))
    assert "Cannot merge resources of kind Thing" in str(exc.value)
Beispiel #7
0
def test_resources_verify_duplicates_prohibited_constructor():
    "Duplicate resources are not allowed"
    with pytest.raises(RuntimeError) as exc:
        Resources([Thing("x", "1"), Thing("x", "1")], [".*"])
    assert "duplicate resources" in str(exc.value)
Beispiel #8
0
def test_resources_manages():
    "Managing a resource adds it to the list of managed resources"
    rsrcs = Resources([], [])
    rsrcs.manage("Thing=x")
    assert rsrcs.managed.matches("Thing=x")
    assert not rsrcs.managed.matches("Thing=y")
Beispiel #9
0
def test_resources_add_unmanaged_prohibited():
    "Adding an unmanaged resource is an error"
    with pytest.raises(RuntimeError) as exc:
        rsrcs = Resources([], ["OtherStuff"])
        rsrcs.add(Thing("x", "1"))
    assert "unmanaged resource: Thing=x" in str(exc.value)
Beispiel #10
0
def test_resources_merge():
    "Resources merge when added"
    coll = Resources([], [".*"])
    coll.add(MergeableThing(thingId="a", value="artichoke"))
    coll.add(MergeableThing(thingId="a", value="aardvark"))
    assert coll.resources[0] == MergeableThing(thingId="a", value="artichoke|aardvark")
Beispiel #11
0
def test_resources_filter():
    coll = Resources(
        [Thing("abc", "3"), Thing("abd", "1"), Thing("dbc", "2")], ["Thing=*"]
    ).filter("hin.=a")
    assert [r.thingId for r in coll] == ["abc", "abd"]
Beispiel #12
0
def test_resources_sorted():
    "Resources are always sorted"
    coll = Resources([Thing("z", "3"), Thing("x", "1"), Thing("y", "2")], ["Thing=*"])
    assert [r.thingId for r in coll] == ["x", "y", "z"]