def test_wls_object_delete():
    fake_wls = MagicMock()
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    wls_obj.delete(hei="sann")
    fake_wls.delete.assert_called_once_with("https://url", False, hei="sann")
    wls_obj.delete(prefer_async=True)
    fake_wls.delete.assert_called_with("https://url", True)
def test_wls_object_create():
    fake_wls = MagicMock()
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    wls_obj.create(hei="sann")
    fake_wls.post.assert_called_once_with("https://url", False, hei="sann")
    wls_obj.create(prefer_async=True, important="true")
    fake_wls.post.assert_called_with("https://url", True, important="true")
def test_wls_object_dir():
    collection = {
        "items": [{
            "links": [{
                "rel": "self",
                "href": "https://self-link"
            }],
            "attr1": True,
            "attr2": 2,
            "attr3": 3,
            "name": "item_1",
        }],
        "name":
        "navn",
        "property":
        "yesyes",
        "links": [
            {
                "rel": "action",
                "title": "superAction",
                "href": "https://action-link"
            },
            {
                "rel": "underCollection",
                "href": "https://undercollection-link"
            },
        ],
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    for attr in ["property", "item_1", "superAction"]:
        assert attr in dir(wls_obj)
def test_wls_object_with_zero_len():
    # If there is an empty item array in the response,
    # it means it has length, but it is zero
    collection = {"items": []}
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    assert len(wls_obj) == 0
def test_wls_object_update():
    fake_wls = MagicMock()
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    wls_obj.update(hei="sann")
    fake_wls.post.assert_called_once_with("https://url",
                                          False,
                                          json={"hei": "sann"})
    wls_obj.update(prefer_async=True, ssl=True)
    fake_wls.post.assert_called_with("https://url", True, json={"ssl": True})
def test_wls_object_with_len():
    collection = {
        "items": [
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 1",
            },
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link2"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 2",
            },
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link3"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 3",
            },
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link4"
                }],
                "attr1": False,
                "attr2": 8,
                "attr3": 3,
                "name": "item 4",
            },
        ]
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    assert len(wls_obj) == 4
def test_wls_object_repr():
    collection = {
        "links": [{
            "rel": "self",
            "href": "https://self-link"
        }],
        "attr1": False,
        "attr2": 9,
        "attr3": -7,
        "name": "what",
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    assert repr(wls_obj) == "<WLSObject name='name' url='https://url'>"
def test_wls_object_without_len():
    collection = {
        "links": [{
            "rel": "self",
            "href": "https://self-link"
        }],
        "attr1": False,
        "attr2": 9,
        "attr3": -7,
        "name": "what",
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    with pytest.raises(TypeError):
        len(wls_obj)
def test_wls_object_non_iter():
    collection = {
        "links": [{
            "rel": "self",
            "href": "https://self-link"
        }],
        "attr1": True,
        "attr2": 2,
        "attr3": 3,
        "name": "item 1",
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    with pytest.raises(TypeError):
        for item in wls_obj:
            pass
def test_wls_object_iter():
    collection = {
        "items": [
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 1",
            },
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link2"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 2",
            },
            {
                "links": [{
                    "rel": "self",
                    "href": "https://self-link3"
                }],
                "attr1": True,
                "attr2": 2,
                "attr3": 3,
                "name": "item 3",
            },
        ]
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    counter = 0
    for item in wls_obj:
        counter += 1
        coll = [x for x in collection["items"] if x["name"] == item._name][0]
        assert item._url == coll["links"][0]["href"]
    assert counter == 3
def test_wls_object_empty_items():
    # If there is an empty item array in the response,
    # it means it is iterable, only withouth objects
    # this should not throw an Exception
    collection = {
        "links": [{
            "rel": "self",
            "href": "https://self-link"
        }],
        "attr1": True,
        "attr2": 2,
        "attr3": 3,
        "name": "item 1",
        "items": [],
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    counter = 0
    for item in wls_obj:
        counter += 1
    assert counter == 0
def test_wls_object_getitem():
    collection = {
        "items": [{
            "links": [{
                "rel": "self",
                "href": "https://self-link"
            }],
            "attr1": True,
            "attr2": 2,
            "attr3": 3,
            "name": "item_1",
        }],
        "name":
        "navn",
        "property":
        "yesyes",
        "links": [
            {
                "rel": "action",
                "title": "superAction",
                "href": "https://action-link"
            },
            {
                "rel": "underCollection",
                "href": "https://undercollection-link"
            },
        ],
    }
    fake_wls = MagicMock()
    fake_wls.get = MagicMock(return_value=collection)
    wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
    assert wls_obj["property"] == collection["property"]
    assert wls_obj["superAction"]._url == "https://action-link"
    assert wls_obj["underCollection"]._url == "https://undercollection-link"
    assert isinstance(wls_obj.item_1, wls_rest_python.WLSObject)
    with pytest.raises(KeyError):
        wls_obj["what"]