-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_sql.py
124 lines (99 loc) · 3.46 KB
/
test_sql.py
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
import unittest
from unittest.mock import call, patch
import pandas as pd
from pandas.testing import assert_frame_equal
from chainbase_sdk.sql import ChainbaseSQL
@patch("chainbase_sdk.sql.ChainbaseAPI.post")
class TestSQL(unittest.TestCase):
def setUp(self):
self.url = "https://api.chainbase.online/v1/dw/query"
self.query = "select * from test"
self.chainbaseSQL = ChainbaseSQL("api-key")
def test_query(self, post_mock):
post_mock.return_value = {"data": {"meta": "metadata", "result": ["r1"]}}
meta, results = self.chainbaseSQL.query(self.query)
post_mock.assert_called_with({"query": self.query})
self.assertEqual(meta, "metadata")
self.assertEqual(results, ["r1"])
def test_query_many_pages(self, post_mock):
post_mock.side_effect = [
{
"data": {
"meta": "metadata",
"result": ["r1"],
"next_page": 2,
"task_id": "task_id",
}
},
{"data": {"meta": "metadata", "result": ["r2"]}},
]
meta, results = self.chainbaseSQL.query(self.query)
post_mock.assert_has_calls(
[
call({"query": self.query}),
call(
{
"task_id": "task_id",
"page": 2,
}
),
]
)
self.assertEqual(meta, "metadata")
self.assertEqual(results, ["r1", "r2"])
def test_query_pandas(self, post_mock):
metadata = [
{"name": "column1", "type": "String"},
{"name": "column2", "type": "Decimal(76, 0)"},
]
result = [
{"column1": "k11", "column2": 2.1},
{"column1": "k12", "column2": 2.2},
]
post_mock.return_value = {"data": {"meta": metadata, "result": result}}
df = self.chainbaseSQL.query_pandas(self.query)
post_mock.assert_called_with({"query": self.query})
expected = pd.DataFrame(result).astype(
{"column1": "object", "column2": "Float64"}
)
assert_frame_equal(df, expected)
def test_query_pandas_many_pages(self, post_mock):
metadata = [
{"name": "column1", "type": "String"},
{"name": "column2", "type": "Decimal(76, 0)"},
]
result1 = [
{"column1": "k11", "column2": 2.1},
{"column1": "k12", "column2": 2.2},
]
result2 = [
{"column1": "k211", "column2": 22.1},
{"column1": "k212", "column2": 22.2},
]
post_mock.side_effect = [
{
"data": {
"meta": metadata,
"result": result1,
"next_page": 2,
"task_id": "task_id",
}
},
{"data": {"meta": metadata, "result": result2}},
]
df = self.chainbaseSQL.query_pandas(self.query)
post_mock.assert_has_calls(
[
call({"query": self.query}),
call(
{
"task_id": "task_id",
"page": 2,
}
),
]
)
expected = pd.DataFrame(result1 + result2).astype(
{"column1": "object", "column2": "Float64"}
)
assert_frame_equal(df, expected)