Skip to content

Commit b3c7bdb

Browse files
committed
Adding test suite and rewrite of client
1 parent 7cc55c6 commit b3c7bdb

File tree

5 files changed

+91
-37
lines changed

5 files changed

+91
-37
lines changed

databox test/test_push.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
import unittest
22
from databox import *
3+
from pprint import pprint as pp
4+
5+
6+
def mock_push_json(data={}):
7+
return True
8+
39

410
class TestPush(unittest.TestCase):
511
def setUp(self):
612
self.databox_push_token = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
713
self.client = Client(self.databox_push_token)
14+
self.client._push_json = mock_push_json
15+
16+
def test_push(self):
17+
assert self.client.push("templj", 10.0) is True
18+
assert self.client.push("templj", 12.0, date="2015-01-01 09:00:00") is True
19+
20+
def test_push_validation(self):
21+
self.assertRaises(
22+
Client.KPIValidationException,
23+
lambda: self.client.push(None, None)
24+
)
25+
26+
def test_insert_all(self):
27+
assert self.client.insert_all([
28+
{'key': 'templj', 'value': 83.3},
29+
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
30+
{'key': 'templj', 'value': 12.3},
31+
]) is True
32+
33+
self.assertRaises(
34+
Client.KPIValidationException,
35+
lambda: self.client.insert_all([
36+
{'value': 83.3},
37+
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
38+
{'key': 'templj', 'value': 12.3},
39+
])
40+
)
41+
42+
def test_short(self):
43+
assert push("templj", 22, token=self.databox_push_token) is True
844

9-
def test_setup(self):
10-
self.client.push("oto", 10.0)
11-
push("aaa", 22)
45+
assert insert_all([
46+
{'key': 'templj', 'value': 83.3},
47+
], token=self.databox_push_token) is True

databox/__init__.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,70 @@
11
import requests
2+
from requests.auth import HTTPBasicAuth
23
from os import getenv
4+
from json import dumps as json_dumps
35

46

5-
class SingleClient(object):
6-
_token = None
7+
class Client(object):
8+
push_token = None
9+
push_host = 'https://push2new.databox.com'
710

811
class MissingToken(Exception):
912
pass
1013

11-
@property
12-
def token(self):
13-
if self._token is None:
14-
self._token = getenv("DATABOX_PUSH_TOKEN", None)
14+
class KPIValidationException(Exception):
15+
pass
1516

16-
if self._token is None:
17-
raise SingleClient.MissingToken("Missing Databox push token")
17+
def __init__(self, token=None):
18+
self.push_token = token
19+
if self.push_token is None:
20+
self.push_token = getenv('DATABOX_PUSH_TOKEN', None)
1821

19-
return self._token
22+
if self.push_token is None:
23+
raise self.MissingToken('Push token is missing!')
2024

21-
def __init__(self, token=None):
22-
if self._token is None and token is not None:
23-
self._token = token
25+
self.push_host = getenv('DATABOX_PUSH_HOST', self.push_host)
2426

25-
def push(self, key, value, date=None):
26-
print "--- push --- ", self.token
27-
pass
27+
def process_kpi(self, **args):
28+
key = args.get('key', None)
29+
if key is None:
30+
raise self.KPIValidationException('Missing "key"')
2831

32+
value = args.get('value', None)
33+
if value is None:
34+
raise self.KPIValidationException('Missing "value"')
2935

36+
item = {("$%s" % args['key']): args['value']}
3037

31-
class Client(object):
32-
_instance = None
33-
client_instance = None
38+
date = args.get('date', None)
39+
if date is not None:
40+
item['date'] = date
3441

35-
def __init__(self, token=None):
36-
self.client_instance = SingleClient(token)
42+
return item
3743

38-
def __new__(cls, *args, **kwargs):
39-
if not cls._instance:
40-
cls._instance = super(Client, cls). \
41-
__new__(cls, *args, **kwargs)
42-
return cls._instance
44+
def _push_json(self, data={}):
45+
response = requests.post(
46+
self.push_host,
47+
auth=HTTPBasicAuth(self.push_token, ''),
48+
headers={'Content-Type': 'application/json'},
49+
data=json_dumps(data)
50+
)
4351

44-
@property
45-
def client(self):
46-
return self.client_instance
52+
return response.json()['status'] == 'ok'
4753

4854
def push(self, key, value, date=None):
49-
return self.client.push(key, value, date)
55+
return self._push_json({
56+
'data': [self.process_kpi(key=key, value=value, date=date)]
57+
})
58+
59+
def insert_all(self, rows):
60+
return self._push_json({
61+
'data': [self.process_kpi(**row) for row in rows]
62+
})
5063

5164

5265
def push(key, value, date=None, token=None):
53-
return Client().push(key, value, date)
66+
return Client(token).push(key, value, date)
67+
68+
69+
def insert_all(rows=[], token=None):
70+
return Client(token).insert_all(rows)

example.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from databox import Client, push
44

5-
Client("adxg1kq5a4g04k0wk0s4wkssow8osw84")
5+
Client("adxg1kq5a4g04k0wk0s4wkssow8osw84")\
6+
.push("templj", 92.2)
67

7-
push("sales", 10)
8+
push("templj", 102.3, token="adxg1kq5a4g04k0wk0s4wkssow8osw84")

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
requests >= 2.7
1+
requests[security] >= 2.7

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
url="https://github.com/databox/databox-python",
1111
license='MIT',
1212

13-
packages=find_packages(exclude=('tests',)),
13+
packages=find_packages(exclude=('databox test',)),
1414
install_requires=[
1515
'requests >= 2.7'
1616
]

0 commit comments

Comments
 (0)