def test_cannot_post_update_without_key(self): api = ThingSpeakApi( channel=666, base_url='https://api.example.com:6666' ) data = {"created_at": "2016-01-27T20:52:20", "field1": "60.0 F"} with responses.RequestsMock() as r_mock, \ pytest.raises(ConfigurationError): api.post_update(data) assert len(r_mock.calls) == 0
def test_post_update_connection_error(self): api = ThingSpeakApi( key='ZYXWVUTSRQP0987654321', base_url='https://api.example.com:6666' ) data = {"created_at": "2016-01-27T20:52:20", "field1": "60.0 F"} with responses.RequestsMock() as r_mock: r_mock.add( r_mock.POST, 'https://api.example.com:6666/update.json', body=requests.exceptions.ConnectionError() ) with pytest.raises(requests.exceptions.ConnectionError): api.post_update(data) assert len(r_mock.calls) == 1 assert r_mock.calls[0].request.url == 'https://api.example.com:6666/update.json' assert type(r_mock.calls[0].response) is requests.exceptions.ConnectionError
def test_can_post_update_without_channel(self): api = ThingSpeakApi( key='ZYXWVUTSRQP0987654321', base_url='https://api.example.com:6666' ) data = {"created_at": "2016-01-27T20:52:20", "field1": "60.0 F"} with responses.RequestsMock() as r_mock: r_mock.add( r_mock.POST, 'https://api.example.com:6666/update.json', json=canned_responses['update.json'] ) api.post_update(data) assert len(r_mock.calls) == 1 the_request = r_mock.calls[0].request assert the_request.url == 'https://api.example.com:6666/update.json' assert the_request.headers['X-THINGSPEAKAPIKEY'] == 'ZYXWVUTSRQP0987654321' assert json.loads(the_request.body.decode()) == data