Skip to content

Commit f83fd53

Browse files
Merge pull request #7 from raghavSharmaSigmoid/develop
Merging branch for pypi registration
2 parents 9793393 + 22b08ed commit f83fd53

File tree

8 files changed

+180
-69
lines changed

8 files changed

+180
-69
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# firebolt-sqlalchemy 0.0.1
1+
# firebolt-sqlalchemy
22

3-
This is a simple example package. You can use
4-
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
5-
to write your content.
3+
This is the 'alpha' package. Expect updates in future.

setup.cfg

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[metadata]
2+
name = firebolt-sqlalchemy
3+
version = 0.0.1
4+
author = Raghav Sharma
5+
author_email = [email protected]
6+
description = Sqlalchemy adapter for Firebolt
7+
long_description = file: README.md
8+
long_description_content_type = text/markdown
9+
url = https://github.com/raghavSharmaSigmoid/firebolt-sqlalchemy
10+
project_urls =
11+
Bug Tracker = https://github.com/raghavSharmaSigmoid/firebolt-sqlalchemy
12+
classifiers =
13+
Programming Language :: Python :: 3
14+
License :: OSI Approved :: MIT License
15+
Operating System :: OS Independent
16+
17+
[options]
18+
package_dir =
19+
= src
20+
packages = find:
21+
python_requires = >=3.6
22+
23+
[options.packages.find]
24+
where = src

setup.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
version="0.0.1",
99
author="Raghav Sharma",
1010
author_email="[email protected]",
11-
description="Package for Sqlalchemy adapter for Firebolt-Superset integration",
11+
description="Sqlalchemy adapter for Firebolt",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
14-
url="https://github.com/raghavSharmaSigmoid/sqlalchemy_adapter",
14+
url="https://github.com/raghavSharmaSigmoid/firebolt-sqlalchemy",
1515
project_urls={
16-
"Bug Tracker": "https://github.com/raghavSharmaSigmoid/sqlalchemy_adapter",
16+
"Bug Tracker": "https://github.com/raghavSharmaSigmoid/firebolt-sqlalchemy",
1717
},
1818
install_requires=[
1919
'sqlalchemy>=1.0.0',
2020
"requests"
21+
"json"
22+
"itertools"
23+
"collections"
24+
"datetime"
25+
"functools"
2126
],
2227
entry_points={
2328
"sqlalchemy.dialects": [

src/firebolt_db/firebolt_api_service.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class FireboltApiService:
1212

1313
@staticmethod
1414
@memoized
15-
def get_connection(user_email, password, db_name):
15+
def get_connection(user_email, password, db_name, date):
1616
"""
1717
Retrieve Authorisation details for connection
1818
This method internally calls methods to get access token, refresh token and engine URL.
@@ -202,13 +202,7 @@ def run_query(access_token, refresh_token, engine_url, db_name, query):
202202
query_response.raise_for_status()
203203

204204
except HTTPError as http_err:
205-
if http_err.response.status_code == 401:
206-
access_token = FireboltApiService.get_access_token_via_refresh(refresh_token)
207-
header = {'Authorization': "Bearer " + access_token}
208-
query_response = requests.post(url="https://" + engine_url, params={'database': db_name},
209-
headers=header, files={"query": (None, query)})
210-
else:
211-
payload = {
205+
payload = {
212206
"error": "DB-API Exception",
213207
"errorMessage": http_err.response.text,
214208
}

src/firebolt_db/firebolt_connector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import itertools
1111
import json
1212
from collections import namedtuple, OrderedDict
13+
from datetime import date
1314

1415
from firebolt_db.firebolt_api_service import FireboltApiService
1516
from firebolt_db import exceptions
@@ -122,7 +123,7 @@ def __init__(self,
122123
self._username = username
123124
self._password = password
124125
self._db_name = db_name
125-
connection_details = FireboltApiService.get_connection(username, password, db_name)
126+
connection_details = FireboltApiService.get_connection(username, password, db_name, date.today())
126127

127128
self.access_token = connection_details[0]
128129
self.engine_url = connection_details[1]

tests/test_firebolt_api_service.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import date
2+
13
from firebolt_db.firebolt_api_service import FireboltApiService
24
from tests import constants
35
from requests.exceptions import HTTPError
@@ -12,19 +14,20 @@
1214
class TestFireboltApiService:
1315

1416
def test_get_connection_success(self):
15-
response = FireboltApiService.get_connection(constants.username, constants.password, constants.db_name)
17+
response = FireboltApiService.get_connection(constants.username, constants.password,
18+
constants.db_name, date.today())
1619
if type(response) == HTTPError:
1720
assert response.response.status_code == 503
1821
else:
1922
assert response != ""
2023

2124
def test_get_connection_invalid_credentials(self):
2225
with pytest.raises(Exception) as e_info:
23-
response = FireboltApiService.get_connection('username', 'password', constants.db_name)[0]
26+
response = FireboltApiService.get_connection('username', 'password', constants.db_name, date.today())[0]
2427

2528
def test_get_connection_invalid_schema_name(self):
2629
with pytest.raises(Exception) as e_info:
27-
response = FireboltApiService.get_connection(constants.username, constants.password, 'db_name')[1]
30+
response = FireboltApiService.get_connection(constants.username, constants.password, 'db_name', date.today())[1]
2831

2932
def test_get_access_token_success(self):
3033
assert access_token["access_token"] != ""
@@ -71,9 +74,13 @@ def test_run_query_invalid_schema(self):
7174
engine_url, 'db_name', constants.query)
7275

7376
def test_run_query_invalid_header(self):
74-
with pytest.raises(Exception) as e_info:
75-
response = FireboltApiService.run_query('header', access_token["refresh_token"], engine_url, constants.db_name,
76-
constants.query) != {}
77+
try:
78+
response = FireboltApiService.run_query('header', access_token["refresh_token"],
79+
engine_url, constants.db_name,
80+
constants.query)
81+
assert response != ""
82+
except exceptions.InternalError as e_info:
83+
assert e_info != ""
7784

7885
def test_run_query_invalid_query(self):
7986
with pytest.raises(Exception) as e_info:

tests/test_fireboltconnector.py

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@pytest.fixture
1010
def get_connection():
11-
return firebolt_connector.connect('[email protected]', 'Apurva111', 'Sigmoid_Alchemy')
11+
return firebolt_connector.connect('localhost', 8123, '[email protected]', 'Apurva111', 'Sigmoid_Alchemy')
1212

1313

1414
class TestConnect:
@@ -17,27 +17,33 @@ def test_connect_success(self):
1717
user_email = "[email protected]"
1818
password = "Apurva111"
1919
db_name = "Sigmoid_Alchemy"
20-
connection = firebolt_connector.connect(user_email, password, db_name)
20+
host = "localhost"
21+
port = "8123"
22+
connection = firebolt_connector.connect(host, port, user_email, password, db_name)
2123
assert connection.access_token
2224
assert connection.engine_url
2325

2426
def test_connect_invalid_credentials(self):
2527
user_email = "[email protected]"
2628
password = "wrongpassword"
2729
db_name = "Sigmoid_Alchemy"
30+
host = "localhost"
31+
port = "8123"
2832
with pytest.raises(exceptions.InvalidCredentialsError):
29-
firebolt_connector.connect(user_email, password, db_name)
33+
firebolt_connector.connect(host, port, user_email, password, db_name)
3034

3135
def test_connect_invalid_database(self):
3236
user_email = "[email protected]"
3337
password = "Apurva111"
3438
db_name = "wrongdatabase"
39+
host = "localhost"
40+
port = "8123"
3541
with pytest.raises(exceptions.SchemaNotFoundError):
36-
firebolt_connector.connect(user_email, password, db_name)
42+
firebolt_connector.connect(host, port, user_email, password, db_name)
3743

3844

3945
def test_get_description_from_row_valid_rows():
40-
row = {'id': 1, 'name': 'John', 'is_eligible': True}
46+
row = {'id': 1, 'name': 'John', 'is_eligible': True, 'some_array': [2, 4]}
4147
result = firebolt_connector.get_description_from_row(row)
4248
assert result[0][0] == 'id'
4349
assert result[0][1] == firebolt_connector.Type.NUMBER
@@ -48,10 +54,13 @@ def test_get_description_from_row_valid_rows():
4854
assert result[2][0] == 'is_eligible'
4955
assert result[2][1] == firebolt_connector.Type.BOOLEAN
5056
assert not result[2][6]
57+
assert result[3][0] == 'some_array'
58+
assert result[3][1] == firebolt_connector.Type.ARRAY
59+
assert not result[3][6]
5160

5261

5362
def test_get_description_from_row_invalid_rows():
54-
row = {'id': []}
63+
row = {'id': {}}
5564
with pytest.raises(Exception):
5665
firebolt_connector.get_description_from_row(row)
5766

@@ -68,8 +77,13 @@ def test_get_type():
6877
assert firebolt_connector.get_type(value_2_2) == 2
6978
assert firebolt_connector.get_type(value_3_1) == 3
7079
assert firebolt_connector.get_type(value_3_2) == 3
80+
assert firebolt_connector.get_type(value_4) == 4
81+
82+
83+
def test_get_type_invalid_type():
84+
value = {}
7185
with pytest.raises(Exception):
72-
firebolt_connector.get_type(value_4)
86+
firebolt_connector.get_type(value)
7387

7488

7589
class TestConnection:
@@ -81,11 +95,11 @@ def test_cursor(self, get_connection):
8195
assert len(connection.cursors) > 0
8296
assert type(cursor) == firebolt_connector.Cursor
8397

84-
def test_execute(self, get_connection):
85-
connection = get_connection
86-
query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.DATABASES"
87-
cursor = connection.execute(query)
88-
assert type(cursor._results) == itertools.chain
98+
# def test_execute(self, get_connection):
99+
# connection = get_connection
100+
# query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.DATABASES"
101+
# cursor = connection.execute(query)
102+
# assert type(cursor._results) == itertools.chain
89103

90104
def test_commit(self):
91105
pass
@@ -106,25 +120,54 @@ def test_rowcount(self, get_connection):
106120
cursor = connection.cursor().execute(query)
107121
assert cursor.rowcount == 10
108122

123+
def test_close(self, get_connection):
124+
connection = get_connection
125+
cursor = connection.cursor()
126+
if not cursor.closed:
127+
cursor.close()
128+
assert cursor.closed
109129

110-
def test_close(self):
111-
pass
112-
113-
def test_execute(self):
114-
pass
115-
116-
def test_stream_query(self):
117-
pass
118-
119-
def test_fetchone(self):
120-
pass
130+
def test_execute(self, get_connection):
131+
query = 'select * from lineitem ' \
132+
'where l_orderkey=3184321 and l_partkey=65945'
133+
connection = get_connection
134+
cursor = connection.cursor()
135+
assert not cursor._results
136+
cursor.execute(query)
137+
assert cursor.rowcount == 1
121138

122-
def test_fetchmany(self):
123-
pass
139+
def test_executemany(self, get_connection):
140+
query = "select * from lineitem limit 10"
141+
connection = get_connection
142+
cursor = connection.cursor()
143+
with pytest.raises(exceptions.NotSupportedError):
144+
cursor.executemany(query)
124145

125-
def test_fetchall(self):
126-
pass
146+
def test_fetchone(self, get_connection):
147+
query = "select * from lineitem limit 10"
148+
connection = get_connection
149+
cursor = connection.cursor()
150+
assert not cursor._results
151+
cursor.execute(query)
152+
result = cursor.fetchone()
153+
assert isinstance(result, tuple)
127154

155+
def test_fetchmany(self, get_connection):
156+
query = "select * from lineitem limit 10"
157+
connection = get_connection
158+
cursor = connection.cursor()
159+
assert not cursor._results
160+
cursor.execute(query)
161+
result = cursor.fetchmany(3)
162+
assert isinstance(result, list)
163+
assert len(result) == 3
128164

129-
def test_rows_from_chunks():
130-
pass
165+
def test_fetchall(self, get_connection):
166+
query = "select * from lineitem limit 10"
167+
connection = get_connection
168+
cursor = connection.cursor()
169+
assert not cursor._results
170+
cursor.execute(query)
171+
result = cursor.fetchall()
172+
assert isinstance(result, list)
173+
assert len(result) == 10

0 commit comments

Comments
 (0)