def test_url_for_last(self): api = ThingSpeakApi( key='ABCDEFGHIJ1234567890', channel=700, base_url='https://api.example.com:6666' ) assert (api._get_url('last') == 'https://api.example.com:6666/channels/700/feed/last.json')
def test_url_for_update(self): api = ThingSpeakApi( key='ABCDEFGHIJ1234567890', channel=700, base_url='https://api.example.com:6666' ) assert (api._get_url('update') == 'https://api.example.com:6666/update.json')
def test_cannot_get_data_without_channel(self): api = ThingSpeakApi( key='ABCDEFGHIJKLMNOPQRST', base_url='https://api.example.com:6666' ) with responses.RequestsMock() as r_mock, \ pytest.raises(ConfigurationError): data = api.get_data() assert len(r_mock.calls) == 0
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_get_data_connection_error(self): api = ThingSpeakApi( channel=666, base_url='https://api.example.com:6666' ) with responses.RequestsMock() as r_mock: r_mock.add( r_mock.GET, 'https://api.example.com:6666/channels/666/feed/last.json', body=requests.exceptions.ConnectionError() ) with pytest.raises(requests.exceptions.ConnectionError): _ = api.get_data() assert len(r_mock.calls) == 1 assert r_mock.calls[0].request.url == 'https://api.example.com:6666/channels/666/feed/last.json' assert type(r_mock.calls[0].response) is requests.exceptions.ConnectionError
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
def test_can_get_data_without_key(self): api = ThingSpeakApi( channel=666, base_url='https://api.example.com:6666' ) with responses.RequestsMock() as r_mock: r_mock.add( r_mock.GET, 'https://api.example.com:6666/channels/666/feed/last.json', json=canned_responses['last.json'] ) data = api.get_data() assert len(r_mock.calls) == 1 the_request = r_mock.calls[0].request assert the_request.url == 'https://api.example.com:6666/channels/666/feed/last.json' assert 'X-THINGSPEAKAPIKEY' not in the_request.headers assert data['created_at'] == '2016-01-27T20:52:10Z' assert data['field1'] == '58.5 F'