Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address inconsistent _lazy_values after attribute set #45

Closed
2 changes: 1 addition & 1 deletion e2e-tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_pod_apis(obj_name):

# read pod logs
for l in client.log(obj_name, follow=True):
assert l == 'this is a test\n'
assert l == 'this is a test'
break
finally:
# delete the pod
Expand Down
6 changes: 5 additions & 1 deletion lightkube/core/dataclasses_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def __get__(self, instance, owner):
if value is not None:
value = self.convert(value, instance._lazy_kwargs)
setattr(instance, self.key, value)
del instance._lazy_values[self.key]
gtsystem marked this conversation as resolved.
Show resolved Hide resolved
return value


Expand All @@ -96,6 +95,11 @@ class DataclassDictMixIn:
_prop_to_json: typing.Dict = None
_valid_params: typing.Set = None

def __setattr__(self, name, value):
if name in getattr(self, "_lazy_values", {}):
del self._lazy_values[name]
self.__dict__[name] = value

@classmethod
def _setup(cls):
if cls._late_init_from is None:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,22 @@ async def alist(aiter):
@pytest.mark.asyncio
async def test_pod_log(client: lightkube.AsyncClient):
result = ['line1\n', 'line2\n', 'line3\n']
expected = [_.strip() for _ in result]
addyess marked this conversation as resolved.
Show resolved Hide resolved
content = "".join(result)

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log").respond(content=content)
lines = await alist(client.log('test'))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?since=30&timestamps=true").respond(
content=content)
lines = await alist(client.log('test', since=30, timestamps=True))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?container=bla").respond(
content=content)
lines = await alist(client.log('test', container="bla"))
assert lines == result
assert lines == expected
await client.close()

@respx.mock
Expand Down
11 changes: 6 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,31 +542,32 @@ def test_replace_global(client: lightkube.Client):
@respx.mock
def test_pod_log(client: lightkube.Client):
result = ['line1\n', 'line2\n', 'line3\n']
expected = [_.strip() for _ in result]
addyess marked this conversation as resolved.
Show resolved Hide resolved
content = "".join(result)

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log").respond(content=content)
lines = list(client.log('test'))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?follow=true").respond(
content=content)
lines = list(client.log('test', follow=True))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?tailLines=3").respond(
content=content)
lines = list(client.log('test', tail_lines=3))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?since=30&timestamps=true").respond(
content=content)
lines = list(client.log('test', since=30, timestamps=True))
assert lines == result
assert lines == expected

respx.get("https://localhost:9443/api/v1/namespaces/default/pods/test/log?container=bla").respond(
content=content)
lines = list(client.log('test', container="bla"))
assert lines == result
assert lines == expected


@respx.mock
Expand Down
29 changes: 29 additions & 0 deletions tests/test_dataclasses_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ class Def(DataclassDictMixIn):
d4: str = "ok"


@pytest.mark.parametrize("lazy", [True, False])
def test_lazy_default_not_hiding_added(lazy):
# Setup a C object without setting c2 (default)
inst = C.from_dict({"c1": "val"}, lazy=lazy)
assert inst.to_dict() == {"c1": "val"}
inst.c2 = [A("def")]
assert inst.to_dict() == {"c1": "val", "c2": [{"a1": "def"}]}


@pytest.mark.parametrize("lazy", [True, False])
def test_lazy_loaded_not_hiding_set(lazy):
# Setup a C object without setting c2 (default)
inst = C.from_dict({"c1": "val", "c2": [{"a1": "abc"}]}, lazy=lazy)
assert inst.to_dict() == {"c1": "val", "c2": [{"a1": "abc"}]}
# Change c2 list attribute
inst.c2 = [A("def")]
assert inst.to_dict() == {"c1": "val", "c2": [{"a1": "def"}]}


@pytest.mark.parametrize("lazy", [True, False])
def test_lazy_loaded_not_hiding_cleared(lazy):
# Setup a C object without setting c2 (default)
inst = C.from_dict({"c1": "val", "c2": [{"a1": "abc"}]}, lazy=lazy)
assert inst.to_dict() == {"c1": "val", "c2": [{"a1": "abc"}]}
# Change c2 list attribute
inst.c2 = None
assert inst.to_dict() == {"c1": "val"}


@pytest.mark.parametrize("lazy", [True, False])
def test_single(lazy):
a = A.from_dict({'a1': 'a', 'a3': True}, lazy=lazy)
Expand Down