• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

andy-goellner / pco-python-sdk / 13220411033

09 Feb 2025 12:03AM UTC coverage: 81.798% (+1.2%) from 80.57%
13220411033

push

github

web-flow
adding relationships (#9)

59 of 68 new or added lines in 6 files covered. (86.76%)

364 of 445 relevant lines covered (81.8%)

2.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/src/planning_center_python/models/pco_object.py
1
from collections.abc import Mapping
3✔
2
from typing import Any, ClassVar, Optional, cast
3✔
3

4
from planning_center_python.errors import (
3✔
5
    InvalidParamsError,
6
    InvalidRequestError,
7
    NoAttributesDefinedError,
8
)
9
from planning_center_python.types.abstract_pco_object import AbstractPCOObject
3✔
10
from planning_center_python.types.relationships import (
3✔
11
    Relationship,
12
    RelationshipDefinition,
13
)
14

15

16
class PCOObject(AbstractPCOObject):
3✔
17
    """Base class object from which all Planning Center objects should inherit from"""
18

19
    OBJECT_TYPE: ClassVar[str] = "BaseClass"
3✔
20
    OBJECT_URL: ClassVar[str] = ""
3✔
21
    RELATIONSHIPS: ClassVar[list[RelationshipDefinition]] = []
3✔
22

23
    def __call__(self, data: Mapping[str, Any] = {}, id: Optional[str] = None) -> Any:
3✔
NEW
24
        self.__init__(data=data, id=id)
×
25

26
    def __init__(self, data: Mapping[str, Any] = {}, id: Optional[str] = None):
3✔
27
        self._data = data
3✔
28
        object_data = cast(Mapping[str, Any], data.get("data")) or {}
3✔
29
        self._id = id or object_data.get("id")
3✔
30
        self._type = object_data.get("type") or self.OBJECT_TYPE
3✔
31
        self._validate()
3✔
32
        self.attributes = object_data.get("attributes")
3✔
33
        self.relationships = self._init_relationships(
3✔
34
            cast(Mapping[str, Any], object_data.get("relationships"))
35
        )
36

37
    def get_attribute(self, name: str) -> Any:
3✔
38
        if not self.attributes:
3✔
39
            raise NoAttributesDefinedError
3✔
40
        return self.attributes.get(name)
3✔
41

42
    def _instance_url(self) -> str:
3✔
43
        if not self.id:
3✔
44
            raise InvalidRequestError(
×
45
                "Cannot determine instance url without a valid id"
46
            )
47
        return "%s/%s" % (self._object_url(), self.id)
3✔
48

49
    def _validate(self):
3✔
50
        if not self.id:
3✔
51
            raise InvalidParamsError(self, "id")
×
52
        if not self.type:
3✔
53
            raise InvalidParamsError(self, "type")
×
54
        if self.type != self.OBJECT_TYPE:
3✔
55
            raise InvalidParamsError(self, "type", "Class types do not match")
×
56

57
    def _init_relationships(self, object_data: Mapping[str, Any]) -> list[Relationship]:
3✔
58
        built_relationships: list[Relationship] = []
3✔
59
        if object_data:
3✔
60
            for definition in self.RELATIONSHIPS:
3✔
61
                relation = object_data.get(definition["key"])
3✔
62
                if relation:
3✔
63
                    relation_id = relation["data"]["id"]
3✔
64
                    klass_instance = definition["klass"](id=relation_id)
3✔
65
                    built_relationships.append(
3✔
66
                        Relationship(definition["association_type"], klass_instance)
67
                    )
68
                    self.__setattr__(definition["method"], klass_instance)
3✔
69
        return built_relationships
3✔
70

71
    @property
3✔
72
    def id(self) -> str | None:
3✔
73
        return self._id
3✔
74

75
    @property
3✔
76
    def type(self) -> str | None:
3✔
77
        return self._type
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc