Skip to content

Commit

Permalink
move to test utils. remove debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt committed Feb 17, 2023
1 parent a847272 commit 62d57c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
3 changes: 0 additions & 3 deletions cognite/client/data_classes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,12 @@ def _load(
return cls._load(json.loads(resource_list), cognite_client=cognite_client)

elif isinstance(resource_list, list):
global ASDF
ASDF.append(resource_list)
resources = [cls._RESOURCE._load(resource, cognite_client=cognite_client) for resource in resource_list]
return cls(resources, cognite_client=cognite_client)

raise TypeError(f"Resource list must be json str or list of dicts, not {type(resource_list)}")


ASDF = []
T_CogniteResourceList = TypeVar("T_CogniteResourceList", bound=CogniteResourceList)


Expand Down
13 changes: 1 addition & 12 deletions tests/tests_unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
CogniteUpdate,
)
from cognite.client.exceptions import CogniteMissingClientError
from tests.utils import all_subclasses


class MyResource(CogniteResource):
Expand Down Expand Up @@ -118,18 +119,6 @@ def _load(cls, api_response):
return cls(data["varA"])


def all_subclasses(cls: type) -> list[type]:
return sorted(
filter(
# Get all subclasses, but not from any test directory:
lambda cls: str(cls).startswith("<class 'cognite.client"),
set(cls.__subclasses__()).union(s for c in cls.__subclasses__() for s in all_subclasses(c)),
),
# Return subclasses in sorted 'import path' order
key=str,
)


class TestVerifyAllCogniteSubclasses:
@pytest.mark.parametrize("subclass", all_subclasses(CogniteResource))
def test_all_cognite_resource_subclasses(self, subclass):
Expand Down
14 changes: 14 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
from cognite.client.utils._auxiliary import random_string


def all_subclasses(cls: type) -> list[type]:
"""Returns a list (without duplicates) of all subclasses of a given class, sorted on import-path-name.
Ignores classes not part of the main library, e.g. subclasses part of tests.
"""
return sorted(
filter(
lambda cls: str(cls).startswith("<class 'cognite.client"),
set(cls.__subclasses__()).union(s for c in cls.__subclasses__() for s in all_subclasses(c)),
),
key=str,
)


@contextmanager
def rng_context(seed: int):
"""Temporarily override internal random state for deterministic behaviour without side-effects
Expand Down

0 comments on commit 62d57c1

Please sign in to comment.