Skip to content

Commit 863a5d4

Browse files
author
aviau
committed
Fixed TestResultSet + Added TestPoint
1 parent a93b217 commit 863a5d4

File tree

4 files changed

+138
-110
lines changed

4 files changed

+138
-110
lines changed

influxdb/point.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
# could it be a namedtuple .. ??
5+
class NamedValues(object):
6+
7+
def __init__(self, point):
8+
self._point = point
9+
10+
def __getattr__(self, item):
11+
try:
12+
index = self._point.columns.index(item)
13+
except ValueError:
14+
raise AttributeError('Point have no such attribute (%r)' % item)
15+
return self._point._point_values[index]
16+
17+
def __repr__(self):
18+
return 'Values(%s)' % ', '.join(
19+
'%s=%r' % (k, self._point._point_values[k])
20+
for k in self._point.columns)
21+
22+
23+
class Point(object):
24+
25+
def __init__(self, serie, columns, values, tags=None):
26+
"""
27+
28+
:param serie: The name of the serie in which this point resides.
29+
If None then it's a "system" point/result.
30+
:param columns: The ordered list of the columns.
31+
:param values: The actualy list of values of this point. Same order than columns.
32+
:param tags: The eventual tags (dict) associated with the point.
33+
:return:
34+
"""
35+
assert len(columns) == len(values)
36+
self.columns = columns
37+
self._point_values = values
38+
if tags is None:
39+
tags = {}
40+
self.serie = serie
41+
self.tags = tags
42+
self.values = NamedValues(self)
43+
44+
def __getitem__(self, tag_name):
45+
"""Indexing a Point return the tag value associated with
46+
the given tag name, if it exists"""
47+
return self._tags[tag_name]
48+
49+
def __iter__(self):
50+
"""Iterating over a Point will return its eventual tag names one per one"""
51+
return iter(self._tags)
52+
53+
def __len__(self):
54+
"""The len of a Point is its number of columns/values"""
55+
return len(self.columns)
56+
57+
def __repr__(self):
58+
return 'Point(values=(%s), tags=%s)' % (
59+
', '.join('%s=%r' % (
60+
k, getattr(self.values, k)) for k in self.columns),
61+
self.tags)
62+
63+
def __eq__(self, other):
64+
return (isinstance(other, self.__class__)
65+
and self.tags == other.tags
66+
and self._point_values == other._point_values
67+
and self.serie == other.serie
68+
and self.columns == other.columns)
69+
70+
def __ne__(self, other):
71+
return not self.__eq__(other)

influxdb/resultset.py

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,18 @@
11
# -*- coding: utf-8 -*-
22

3-
import collections
4-
from collections import namedtuple
5-
3+
from influxdb.point import Point
64

75
_sentinel = object()
86

97

10-
# could it be a namedtuple .. ??
11-
class NamedValues(object):
12-
13-
def __init__(self, point):
14-
self._point = point
15-
16-
def __getattr__(self, item):
17-
try:
18-
index = self._point.columns.index(item)
19-
except ValueError:
20-
raise AttributeError('Point have no such attribute (%r)' % item)
21-
return self._point._point_values[index]
22-
23-
def __repr__(self):
24-
return 'Values(%s)' % ', '.join(
25-
'%s=%r' % (k, self._point._point_values[k])
26-
for k in self._point.columns)
27-
28-
29-
class Point(object):
30-
31-
def __init__(self, serie, columns, values, tags=None):
32-
'''
33-
34-
:param serie: The name of the serie in which this point resides.
35-
If None then it's a "system" point/result.
36-
:param columns: The ordered list of the columns.
37-
:param values: The actualy list of values of this point. Same order than columns.
38-
:param tags: The eventual tags (dict) associated with the point.
39-
:return:
40-
'''
41-
assert len(columns) == len(values)
42-
self.columns = columns
43-
self._point_values = values
44-
if tags is None:
45-
tags = {}
46-
self.serie = serie
47-
self.tags = tags
48-
self.values = NamedValues(self)
49-
50-
def __getitem__(self, tag_name):
51-
"""Indexing a Point return the tag value associated with
52-
the given tag name, if it exists"""
53-
return self._tags[tag_name]
54-
55-
def __iter__(self):
56-
"""Iterating over a Point will return its eventual tag names one per one"""
57-
return iter(self._tags)
58-
59-
def __len__(self):
60-
"""The len of a Point is its number of columns/values"""
61-
return len(self.columns)
62-
63-
def __repr__(self):
64-
return 'Point(values=(%s), tags=%s)' % (
65-
', '.join('%s=%r' % (
66-
k, getattr(self.values, k)) for k in self.columns),
67-
self.tags)
68-
69-
70-
718
class ResultSet(object):
729
"""A wrapper around series results """
7310

7411
def __init__(self, series):
7512
self.raw = series
7613

7714
def __getitem__(self, key):
78-
'''
15+
"""
7916
:param key: Either a serie name or a 2-tuple(serie_name, tags_dict)
8017
If the given serie name is None then any serie (matching
8118
the eventual given tags) will be given its points one
@@ -84,7 +21,7 @@ def __getitem__(self, key):
8421
NB:
8522
The order in which the points are yielded is actually undefined but
8623
it might change..
87-
'''
24+
"""
8825
if isinstance(key, tuple):
8926
if 2 != len(key):
9027
raise TypeError('only 2-tuples allowed')
@@ -137,8 +74,8 @@ def __repr__(self):
13774
return str(self.raw)
13875

13976
def __iter__(self):
140-
''' Iterating a ResultSet will yield one dict instance per serie result.
141-
'''
77+
""" Iterating a ResultSet will yield one dict instance per serie result.
78+
"""
14279
for results in self.raw['results']:
14380
for serie in results['series']:
14481
yield serie

tests/influxdb/point_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import unittest
4+
5+
from influxdb.point import Point
6+
7+
8+
class TestPoint(unittest.TestCase):
9+
10+
def test_point(self):
11+
point = Point(
12+
"serie_name",
13+
['col1', 'col2'],
14+
[1, '2'],
15+
tags={
16+
"SWAG": True,
17+
"ALLO": "BYE"
18+
}
19+
)
20+
21+
self.assertEqual(point.columns, ['col1', 'col2'])
22+
self.assertEqual(point.tags, {"SWAG": True, "ALLO": "BYE"})
23+
self.assertEqual(point.values.col1, 1)
24+
self.assertEqual(point.values.col2, '2')
25+
self.assertEqual(
26+
str(point),
27+
"Point(values=(col1=1, col2='2'),"
28+
" tags={'ALLO': 'BYE', 'SWAG': True})"
29+
)
30+
31+
def test_point_eq(self):
32+
point1 = Point("serie_name", ['col1', 'col2'], [1, '2'],
33+
tags={"SWAG": True, "ALLO": "BYE"})
34+
35+
point2 = Point("serie_name", ['col1', 'col2'], [1, '2'],
36+
tags={"SWAG": True, "ALLO": "BYE"})
37+
38+
self.assertEqual(point1, point2)

tests/influxdb/resultset_test.py

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44

55
from influxdb.resultset import ResultSet
6+
from influxdb.point import Point
67

78

89
class TestResultSet(unittest.TestCase):
@@ -36,57 +37,38 @@ def setUp(self):
3637
self.rs = ResultSet(self.query_response)
3738

3839
def test_filter_by_name(self):
39-
self.assertItemsEqual(
40-
self.rs['cpu_load_short'],
40+
self.assertEqual(
41+
list(self.rs['cpu_load_short']),
4142
[
42-
{
43-
"tags": {"host": "server01", "region": "us-west"},
44-
"points": [
45-
{"time": "2015-01-29T21:51:28.968422294Z",
46-
"value": 0.64}
47-
]
48-
},
49-
{
50-
"tags": {"host": "server02", "region": "us-west"},
51-
"points": [
52-
{"time": "2015-01-29T21:51:28.968422294Z",
53-
"value": 0.64}
54-
]
55-
}
43+
Point("cpu_load_short", ["time", "value"],
44+
["2015-01-29T21:51:28.968422294Z", 0.64],
45+
tags={"host": "server01", "region": "us-west"}),
46+
Point("cpu_load_short", ["time", "value"],
47+
["2015-01-29T21:51:28.968422294Z", 0.64],
48+
tags={"host": "server02", "region": "us-west"})
5649
]
5750
)
5851

5952
def test_filter_by_tags(self):
60-
self.assertItemsEqual(
61-
self.rs[('cpu_load_short', {"host": "server01"})],
53+
self.assertEqual(
54+
list(self.rs[('cpu_load_short', {"host": "server01"})]),
6255
[
63-
{
64-
"tags": {"host": "server01", "region": "us-west"},
65-
"points": [
66-
{"time": "2015-01-29T21:51:28.968422294Z",
67-
"value": 0.64}
68-
]
69-
}
56+
Point(
57+
"cpu_load_short", ["time", "value"],
58+
["2015-01-29T21:51:28.968422294Z", 0.64],
59+
tags={"host": "server01", "region": "us-west"}
60+
)
7061
]
7162
)
7263

73-
self.assertItemsEqual(
74-
self.rs[('cpu_load_short', {"region": "us-west"})],
64+
self.assertEqual(
65+
list(self.rs[('cpu_load_short', {"region": "us-west"})]),
7566
[
76-
{
77-
"tags": {"host": "server01", "region": "us-west"},
78-
"points": [
79-
{"time": "2015-01-29T21:51:28.968422294Z",
80-
"value": 0.64}
81-
]
82-
},
83-
{
84-
"tags": {"host": "server02", "region": "us-west"},
85-
"points": [
86-
{"time": "2015-01-29T21:51:28.968422294Z",
87-
"value": 0.64}
88-
]
89-
}
67+
Point("cpu_load_short", ["time", "value"],
68+
["2015-01-29T21:51:28.968422294Z", 0.64],
69+
tags={"host": "server01", "region": "us-west"}),
70+
Point("cpu_load_short", ["time", "value"],
71+
["2015-01-29T21:51:28.968422294Z", 0.64],
72+
tags={"host": "server02", "region": "us-west"}),
9073
]
9174
)
92-

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy