def test_to_json(self): series = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) json = series.to_json() expected = { 'id': 'id', 'key': 'key', 'name': 'name', 'tags': ['tag1', 'tag2'], 'attributes': {'key1': 'value1'}, } self.assertEqual(json, expected)
def test_update_series(self): update = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1']) self.client.session.put.return_value = MockResponse(200, simplejson.dumps(update.to_json())) updated = self.client.update_series(update) self.client.session.put.assert_called_once_with( 'https://example.com/v1/series/id/id/', auth=('key', 'secret'), data=simplejson.dumps(update.to_json()), headers=self.put_headers ) self.assertEqual(update, updated)
def test_update_series(self): update = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1']) self.client.session.put.return_value = MockResponse( 200, simplejson.dumps(update.to_json())) updated = self.client.update_series(update) self.client.session.put.assert_called_once_with( 'https://example.com/v1/series/id/id/', auth=('key', 'secret'), data=simplejson.dumps(update.to_json()), headers=self.put_headers) self.assertEqual(update, updated)
def test_from_json(self): json = { "id": "id", "key": "key", "name": "name", "tags": ["tag1", "tag2"], "attributes": { "key1": "value1" } } series = Series.from_json(json) expected = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) self.assertEqual(series, expected)
def test_to_json(self): series = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) json = series.to_json() expected = { 'id': 'id', 'key': 'key', 'name': 'name', 'tags': ['tag1', 'tag2'], 'attributes': { 'key1': 'value1' }, } self.assertEqual(json, expected)
def test_read_key_escape(self): self.client.session.get.return_value = MockResponse( 200, """{ "series": { "id": "id", "key": "ke:y/1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000", "end": "2012-03-28T00:00:00.000", "data": [{"t": "2012-03-27T00:00:00.000", "v": 12.34}], "summary": {}, "rollup": {} }""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) dataset = self.client.read_key('ke:y/1', start, end) expected = DataSet(Series('id', 'ke:y/1'), start, end, [DataPoint(start, 12.34)], Summary(), Rollup()) self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/key/ke%3Ay%2F1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00', auth=('key', 'secret'), headers=self.get_headers) print dataset print expected self.assertEqual(dataset, expected)
def test_read(self): self.client.session.get.return_value = MockResponse(200, """[{ "series": { "id": "id", "key": "key1", "name": "", "tags": [], "attributes": {} }, "start": "2012-03-27T00:00:00.000Z", "end": "2012-03-28T00:00:00.000Z", "data": [{"t": "2012-03-27T00:00:00.000Z", "v": 12.34}], "summary": {} }]""") start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) datasets = self.client.read(start, end, keys=['key1']) expected = [ DataSet(Series('id', 'key1'), start, end, [DataPoint(start, 12.34)], Summary())] self.client.session.get.assert_called_once_with( 'https://example.com/v1/data/?start=2012-03-27T00%3A00%3A00&end=2012-03-28T00%3A00%3A00&key=key1', auth=('key', 'secret'), headers=self.get_headers ) self.assertEqual(datasets, expected)
def test_init(self): series = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) self.assertEqual(series.id, 'id') self.assertEqual(series.key, 'key') self.assertEqual(series.name, 'name') self.assertEqual(series.attributes, {'key1': 'value1'}) self.assertEqual(series.tags, ['tag1', 'tag2'])
def test_from_json(self): json = { "id": "id", "key": "key", "name": "name", "tags": ["tag1", "tag2"], "attributes": {"key1": "value1"} } series = Series.from_json(json) expected = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) self.assertEqual(series, expected)
def create_series(self, key=None): if key and not RE_VALID_SERIES_KEY.match(key): raise ValueError("Series key must match the following regex: %s" % (VALID_SERIES_KEY,)) params = {} if key is not None: params['key'] = key json = self.request('/series/', method='POST', params=params) series = Series.from_json(json) return series
def create_series(self, key=None): if key and not RE_VALID_SERIES_KEY.match(key): raise ValueError("Series key must match the following regex: %s" % (VALID_SERIES_KEY, )) params = {} if key is not None: params['key'] = key json = self.request('/series/', method='POST', params=params) series = Series.from_json(json) return series
def test_init(self): start = datetime.datetime(2012, 1, 1) end = datetime.datetime(2012, 1, 2) series = Series("id", "key") data = [DataPoint(start, 12.34), DataPoint(end, 23.45)] summary = Summary() dataset = DataSet(series, start, end, data, summary) self.assertEqual(dataset.series, series) self.assertEqual(dataset.start, start) self.assertEqual(dataset.end, end) self.assertEqual(dataset.data, data) self.assertEqual(dataset.summary, summary)
def get_series(self, ids=[], keys=[], tags=[], attributes={}): params = {} if ids: params['id'] = ids if keys: params['key'] = keys if tags: params['tag'] = tags if attributes: params['attr'] = attributes json = self.request('/series/', method='GET', params=params) series = [Series.from_json(s) for s in json] return series
def test_get_series(self): self.client.session.get.return_value = MockResponse(200, """[{ "id": "id", "key": "key", "name": "name", "tags": ["tag1", "tag2"], "attributes": {"key1": "value1"} }]""") series = self.client.get_series() self.client.session.get.assert_called_once_with( 'https://example.com/v1/series/', auth=('key', 'secret'), headers=self.get_headers ) expected = [ Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2'])] self.assertEqual(series, expected)
def test_create_series(self): self.client.session.post.return_value = MockResponse(200, """{ "id": "id", "key": "my-key.tag1.1", "name": "", "tags": ["my-key", "tag1"], "attributes": {} }""") series = self.client.create_series("my-key.tag1.1") self.client.session.post.assert_called_once_with( 'https://example.com/v1/series/', data="""{"key": "my-key.tag1.1"}""", auth=('key', 'secret'), headers=self.post_headers ) expected = Series('id', 'my-key.tag1.1', '', {}, ['my-key', 'tag1']) self.assertEqual(series, expected)
def test_from_json(self): json = { 'series': { 'id': 'id', 'key': 'key', 'name': 'name', 'tags': ['tag1', 'tag2'], 'attributes': {'key1': 'value1'}, }, 'start': '2012-03-27T00:00:00.000Z', 'end': '2012-03-28T00:00:00.000Z', 'data': [], 'summary': {'min': 45.5} } dataset = DataSet.from_json(json) series = Series('id', 'key', 'name', {'key1': 'value1'}, ['tag1', 'tag2']) start = datetime.datetime(2012, 3, 27) end = datetime.datetime(2012, 3, 28) data = [] summary = Summary(min=45.5) expected = DataSet(series, start, end, data, summary) self.assertEqual(dataset, expected)
def get_series(self, ids=[], keys=[], tags=[], attributes={}): params = self._normalize_params(ids, keys, tags, attributes) json = self.request('/series/', method='GET', params=params) series = [Series.from_json(s) for s in json] return series
def update_series(self, series): json = self.request('/series/id/%s/' % (series.id,), method='PUT', params=series.to_json()) series = Series.from_json(json) return series
def update_series(self, series): json = self.request('/series/id/%s/' % (series.id, ), method='PUT', params=series.to_json()) series = Series.from_json(json) return series