Skip to content

Commit

Permalink
Add reset API function (ProtoThis#51)
Browse files Browse the repository at this point in the history
* Add reset API function

* pylint
  • Loading branch information
Quentame authored May 13, 2020
1 parent 674dfb3 commit 11512e8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,34 @@ def update(self, with_information=False):
data = self.get(SynoStorage.API_KEY, "load_info")
self._storage.update(data)

def reset(self, api):
"""Reset an API to avoid fetching in on update."""
if isinstance(api, str):
if api in ("information", SynoDSMInformation.API_KEY):
return False
if hasattr(self, "_" + api):
setattr(self, "_" + api, None)
return True
if api == SynoCoreSecurity.API_KEY:
self._security = None
return True
if api == SynoCoreUtilization.API_KEY:
self._utilisation = None
return True
if api == SynoStorage.API_KEY:
self._storage = None
return True
if isinstance(api, SynoCoreSecurity):
self._security = None
return True
if isinstance(api, SynoCoreUtilization):
self._utilisation = None
return True
if isinstance(api, SynoStorage):
self._storage = None
return True
return False

@property
def information(self):
"""Gets NAS informations."""
Expand Down
50 changes: 50 additions & 0 deletions tests/test_synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Synology DSM tests."""
from unittest import TestCase

from synology_dsm.api.core.security import SynoCoreSecurity
from synology_dsm.api.dsm.information import SynoDSMInformation
from synology_dsm.exceptions import (
SynologyDSMRequestException,
SynologyDSMAPINotExistsException,
Expand Down Expand Up @@ -188,6 +190,54 @@ def test_request_post_failed(self):
},
)

def test_reset_str_attr(self):
"""Test reset with string attr."""
assert not self.api._security
assert self.api.security
assert self.api._security
assert self.api.reset("security")
assert not self.api._security

def test_reset_str_key(self):
"""Test reset with string API key."""
assert not self.api._security
assert self.api.security
assert self.api._security
assert self.api.reset(SynoCoreSecurity.API_KEY)
assert not self.api._security

def test_reset_object(self):
"""Test reset with object."""
assert not self.api._security
assert self.api.security
assert self.api._security
assert self.api.reset(self.api.security)
assert not self.api._security

def test_reset_str_attr_information(self):
"""Test reset with string information attr (should not be reset)."""
assert not self.api._information
assert self.api.information
assert self.api._information
assert not self.api.reset("information")
assert self.api._information

def test_reset_str_key_information(self):
"""Test reset with string information API key (should not be reset)."""
assert not self.api._information
assert self.api.information
assert self.api._information
assert not self.api.reset(SynoDSMInformation.API_KEY)
assert self.api._information

def test_reset_object_information(self):
"""Test reset with information object (should not be reset)."""
assert not self.api._information
assert self.api.information
assert self.api._information
assert not self.api.reset(self.api.information)
assert self.api._information

def test_information(self):
"""Test information."""
assert self.api.information
Expand Down

0 comments on commit 11512e8

Please sign in to comment.