def test_retrieve_cb_info_nbm(cbcsdk_mock): """Test _retrieve_cb_info method of NewBaseModel""" api = cbcsdk_mock.api cbcsdk_mock.mock_request("GET", "/integrationServices/v3/event/a1e12604d67b11ea920d3d9192a785d1", EVENT_GET_SPECIFIC_RESP) event = Event(api, "a1e12604d67b11ea920d3d9192a785d1") assert event._build_api_request_uri() == "/integrationServices/v3/event/a1e12604d67b11ea920d3d9192a785d1" # _retrieve_cb_info() calls cb.get_object, which makes the API call to retrieve the object assert event._retrieve_cb_info() == EVENT_GET_SPECIFIC_RESP
def test_getattr_nbm(cbcsdk_mock): """Test __getattr__ method of NewBaseModel""" api = cbcsdk_mock.api cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP) containsIdMutableBase = api.select(EndpointStandardDevice, 12345) assert containsIdMutableBase._model_unique_id == 12345 assert containsIdMutableBase.__getattr__("avMaster") is False event = Event(api, 1234) assert 'eventTime' not in event._info cbcsdk_mock.mock_request("GET", "/integrationServices/v3/event/1234", {"eventInfo": {"eventTime": 1}}) assert event.__getattr__("eventTime") == 1 assert 'eventTime' in event._info with pytest.raises(AttributeError): assert event.__getattr__("missingInfoTag") is None
def test_new_object_nbm(cbcsdk_mock): """Test new_object class method of NewBaseModel via an Event object""" api = cbcsdk_mock.api nbm_object = Event.new_object(api, { "eventId": "testEventId", "otherData": "test" }) assert nbm_object._model_unique_id == "testEventId"
def test_getattr_nbm(cbcsdk_mock): """Test __getattr__ method of NewBaseModel""" api = cbcsdk_mock.api cbcsdk_mock.mock_request("GET", "/integrationServices/v3/policy/30241", POLICY_GET_RESP) policy = Policy(api, 30241) assert policy._model_unique_id == 30241 assert policy.__getattr__("name") == 'Lyon_test' event = Event(api, 1234) assert 'eventTime' not in event._info cbcsdk_mock.mock_request("GET", "/integrationServices/v3/event/1234", {"eventInfo": { "eventTime": 1 }}) assert event.__getattr__("eventTime") == 1 assert 'eventTime' in event._info with pytest.raises(AttributeError): assert event.__getattr__("missingInfoTag") is None
def test_refresh_nbm(cbcsdk_mock): """Test refresh and _refresh methods of NewBaseModel""" api = cbcsdk_mock.api # refresh() should return False if there is no _model_unique_id object_without_model_unique_id = NewBaseModel(api) assert object_without_model_unique_id.refresh() is False # refresh() should return False if cls.primary_key is in _dirty_attributes object_with_dirty_model_unique_id = NewBaseModel(api, 1) object_with_dirty_model_unique_id._dirty_attributes["id"] = 2 assert object_with_dirty_model_unique_id.primary_key == "id" assert "id" in object_with_dirty_model_unique_id._dirty_attributes object_with_dirty_model_unique_id.refresh() is False # refresh() should return True if there's a _model_unique_id and primary_key hasn't been changed cbcsdk_mock.mock_request("GET", "/integrationServices/v3/event/a1e12604d67b11ea920d3d9192a785d1", EVENT_GET_SPECIFIC_RESP) refreshable_object = Event(api, "a1e12604d67b11ea920d3d9192a785d1") assert refreshable_object._full_init is False assert refreshable_object.refresh() is True assert refreshable_object._full_init is True
def test_setattr_nbm(cbcsdk_mock): """Test __setattr__ method of NewBaseModel""" api = cbcsdk_mock.api nbm = Event(api, 101) nbm.__setattr__("_my_attr", "attr_val") assert nbm._my_attr == "attr_val" # attribute name must start with "_", otherwise it's immutable with pytest.raises(AttributeError): nbm.__setattr__("immutable_attr", "immutable_val")
def test_build_api_request_uri_nbm(cbcsdk_mock): """Test _build_api_request_uri method of NewBaseModel""" api = cbcsdk_mock.api # if there's no _model_unique_id, _build_api_request_uri should return just cls.urlobject event = Event(api, model_unique_id=None) assert event._build_api_request_uri() == "/integrationServices/v3/event" # if there's a _model_unique_id, _build_api_request_uri should return cls.urlobect + _model_unique_id cbcsdk_mock.mock_request("GET", "/integrationServices/v3/event/a1e12604d67b11ea920d3d9192a785d1", EVENT_GET_SPECIFIC_RESP) event = Event(api, "a1e12604d67b11ea920d3d9192a785d1") assert event._build_api_request_uri() == "/integrationServices/v3/event/a1e12604d67b11ea920d3d9192a785d1"