1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
import requests
import json
from testing import gen_points
import asyncio
import functools
import random
import time
import math
import re
import qpack
from testing import Client
from testing import default_test_setup
from testing import gen_data
from testing import gen_points
from testing import gen_series
from testing import InsertError
from testing import PoolError
from testing import QueryError
from testing import run_test
from testing import Series
from testing import Server
from testing import ServerError
from testing import SiriDB
from testing import TestBase
from testing import UserAuthError
from testing import parse_args
TIME_PRECISION = 's'
class TestHTTPAPI(TestBase):
title = 'Test HTTP API requests'
@default_test_setup(3, time_precision=TIME_PRECISION)
async def run(self):
await self.client0.connect()
x = requests.get(
'http://localhost:9020/get-version', auth=('sa', 'siri'))
self.assertEqual(x.status_code, 200)
v = x.json()
self.assertTrue(isinstance(v, list))
self.assertTrue(isinstance(v[0], str))
x = requests.post(
'http://localhost:9020/insert/dbtest',
auth=('iris', 'siri'),
headers={'Content-Type': 'application/json'})
self.assertEqual(x.status_code, 400)
series_float = gen_points(
tp=float, n=10000, time_precision=TIME_PRECISION, ts_gap='5m')
series_int = gen_points(
tp=int, n=10000, time_precision=TIME_PRECISION, ts_gap='5m')
data = {
'my_float': series_float,
'my_int': series_int
}
x = requests.post(
'http://localhost:9020/insert/dbtest',
data=json.dumps(data),
auth=('iris', 'siri'),
headers={'Content-Type': 'application/json'}
)
self.assertEqual(x.status_code, 200)
self.assertDictEqual(x.json(), {
'success_msg': 'Successfully inserted 20000 point(s).'})
data = {
'dbname': 'dbtest',
'host': 'localhost',
'port': 9000,
'username': 'iris',
'password': 'siri'
}
x = requests.post(
'http://localhost:9021/new-pool',
data=json.dumps(data),
auth=('sa', 'siri'),
headers={'Content-Type': 'application/json'})
self.assertEqual(x.status_code, 200)
self.assertEqual(x.json(), 'OK')
self.db.servers.append(self.server1)
await self.assertIsRunning(self.db, self.client0, timeout=30)
data = {'data': [[1579521271, 10], [1579521573, 20]]}
x = requests.post(
'http://localhost:9020/insert/dbtest',
json=data,
auth=('iris', 'siri'))
self.assertEqual(x.status_code, 200)
self.assertDictEqual(x.json(), {
'success_msg': 'Successfully inserted 2 point(s).'})
x = requests.post(
'http://localhost:9020/query/dbtest',
json={'q': 'select * from "data"'},
auth=('iris', 'siri'))
self.assertEqual(x.status_code, 200)
self.assertEqual(x.json(), data)
x = requests.post(
'http://localhost:9020/query/dbtest',
json={'q': 'select * from "data"', 't': 'ms'},
auth=('iris', 'siri'))
data = {
'data': [[p[0] * 1000, p[1]] for p in data['data']]
}
self.assertEqual(x.status_code, 200)
self.assertEqual(x.json(), data)
x = requests.post(
'http://localhost:9020/query/dbtest',
data=qpack.packb({
'q': 'select sum(1579600000) from "data"',
't': 'ms'}),
headers={'Content-Type': 'application/qpack'},
auth=('iris', 'siri'))
self.assertEqual(x.status_code, 200)
self.assertEqual(
qpack.unpackb(x.content, decode='utf8'),
{'data': [[1579600000000, 30]]})
x = requests.post(
'http://localhost:9021/new-account',
json={'account': 't', 'password': ''},
auth=('sa', 'siri'))
self.assertEqual(x.status_code, 400)
self.assertEqual(x.json(), {
'error_msg':
'service account name should have at least 2 characters'})
x = requests.post(
'http://localhost:9021/new-account',
json={'account': 'tt', 'password': 'pass'},
auth=('sa', 'siri'))
self.assertEqual(x.status_code, 200)
data = {
'dbname': 'dbtest',
'host': 'localhost',
'port': 1234,
'pool': 0,
'username': 'iris',
'password': 'siri'
}
auth = ('tt', 'pass')
x = requests.post(
'http://localhost:9021/new-replica', json=data, auth=auth)
self.assertEqual(x.status_code, 400)
self.assertEqual(x.json(), {
'error_msg': "database name already exists: 'dbtest'"})
x = requests.post(
'http://localhost:9022/new-replica', json=data, auth=auth)
self.assertEqual(x.status_code, 401)
auth = ('sa', 'siri')
x = requests.post(
'http://localhost:9022/new-replica', json=data, auth=auth)
self.assertEqual(x.status_code, 400)
self.assertEqual(x.json(), {
'error_msg':
"connecting to server 'localhost:1234' failed with error: "
"connection refused"})
data['port'] = 9000
x = requests.post(
'http://localhost:9022/new-replica', json=data, auth=auth)
self.assertEqual(x.status_code, 200)
self.assertEqual(x.json(), 'OK')
self.db.servers.append(self.server2)
await self.assertIsRunning(self.db, self.client0, timeout=50)
x = requests.get(
'http://localhost:9022/get-databases', auth=auth)
self.assertEqual(x.status_code, 200)
self.assertEqual(x.json(), ['dbtest'])
self.client0.close()
if __name__ == '__main__':
parse_args()
run_test(TestHTTPAPI())
|