Skip to content

Commit 35dda11

Browse files
Final alpha release
Complete all abstract class for each package. Complete all tests.
2 parents 057b0dc + 1dc01d0 commit 35dda11

File tree

12 files changed

+244
-49
lines changed

12 files changed

+244
-49
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ Exception
138138

139139
`Connection` objects should respond to the following methods.
140140

141+
#### Connection attributes
142+
143+
`.connected`
144+
145+
This read-only attribute contains a boolean value.
146+
141147
#### Connection methods
142148

143149
`.close()`
@@ -164,6 +170,10 @@ Deleting of a single database with position and keyword arguments.
164170

165171
List all databases.
166172

173+
`.show_database(parameters...)`
174+
175+
Show an information of a specific database
176+
167177
### Session Objects
168178

169179
`Session` objects should respond to the following methods.

__info__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
"""Information variable used by modules on this package."""
2424

25-
__version__ = '0.0.6'
25+
__version__ = '0.0.7'
2626
__author__ = 'Matteo Guadrini'
2727
__email__ = '[email protected]'
2828
__homepage__ = 'https://github.com/MatteoGuadrini/nosqlapi'

nosqlapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""Python NOSQL Database library."""
2424

2525
from nosqlapi.common.exception import *
26+
from nosqlapi.common.core import Connection, Session, Selector, Response, Batch
2627
from nosqlapi.kvdb import KVConnection, KVSelector, KVSession, KVResponse, KVBatch
2728
from nosqlapi.columndb import ColumnConnection, ColumnSelector, ColumnSession, ColumnResponse, ColumnBatch
2829
from nosqlapi.docdb import DocConnection, DocSelector, DocSession, DocResponse, DocBatch

nosqlapi/common/core.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,35 @@
2424
from abc import ABC, abstractmethod
2525
from .exception import *
2626

27+
# endregion
28+
29+
# region global variables
30+
API_NAME = 'nosqlapi'
31+
2732

2833
# endregion
2934

3035
# region classes
3136
class Connection(ABC):
3237
"""Server connection abstract class"""
3338

39+
def __init__(self):
40+
self._connected = False
41+
42+
@property
43+
def connected(self):
44+
return bool(self._connected)
45+
3446
@abstractmethod
35-
def close(self):
47+
def close(self, *args, **kwargs):
3648
"""Delete this object
3749
3850
:return: None
3951
"""
4052
pass
4153

4254
@abstractmethod
43-
def connect(self):
55+
def connect(self, *args, **kwargs):
4456
"""Connect database server
4557
4658
:return: Session object
@@ -72,13 +84,37 @@ def delete_database(self, *args, **kwargs):
7284
pass
7385

7486
@abstractmethod
75-
def databases(self):
87+
def databases(self, *args, **kwargs):
7688
"""Get all databases
7789
7890
:return: Response
7991
"""
8092
pass
8193

94+
@abstractmethod
95+
def show_database(self, *args, **kwargs):
96+
"""Show a database information
97+
98+
:return : Response object
99+
"""
100+
pass
101+
102+
def __repr__(self):
103+
return f"<{API_NAME} {self.__class__.__name__} object>"
104+
105+
def __str__(self):
106+
return f"{repr(self)}, connected={self.connected}"
107+
108+
def __bool__(self):
109+
if self.connected:
110+
return True
111+
112+
def __enter__(self):
113+
return self
114+
115+
def __exit__(self, exc_type, exc_val, exc_tb):
116+
self.close()
117+
82118

83119
class Selector(ABC):
84120
"""Selector abstract class"""
@@ -140,13 +176,23 @@ def limit(self, value):
140176
self._limit = value
141177

142178
@abstractmethod
143-
def build(self):
179+
def build(self, *args, **kwargs):
144180
"""Build string query selector
145181
146182
:return: string
147183
"""
148184
pass
149185

186+
def __repr__(self):
187+
return f"<{API_NAME} {self.__class__.__name__} object>"
188+
189+
def __str__(self):
190+
return self.build()
191+
192+
def __bool__(self):
193+
if self.selector:
194+
return True
195+
150196

151197
class Session(ABC):
152198
"""Server session abstract class"""
@@ -222,7 +268,7 @@ def delete(self, *args, **kwargs):
222268
pass
223269

224270
@abstractmethod
225-
def close(self):
271+
def close(self, *args, **kwargs):
226272
"""Delete session
227273
228274
:return: None
@@ -253,6 +299,22 @@ def revoke(self, *args, **kwargs):
253299
"""
254300
pass
255301

302+
def __repr__(self):
303+
return f"<{API_NAME} {self.__class__.__name__} object>"
304+
305+
def __str__(self):
306+
return f"database={self.database}, description={self.description}"
307+
308+
def __bool__(self):
309+
if self.description:
310+
return True
311+
312+
def __enter__(self):
313+
return self
314+
315+
def __exit__(self, exc_type, exc_val, exc_tb):
316+
self.close()
317+
256318

257319
class Response(ABC):
258320
"""Server response abstract class"""
@@ -291,7 +353,7 @@ def __str__(self):
291353
return str(self.data)
292354

293355
def __repr__(self):
294-
return f'<class {self.__class__.__name__}: data={type(self.data)}, code={self.code}, error={self.error}>'
356+
return f"<{API_NAME} {self.__class__.__name__} object>"
295357

296358
def __contains__(self, item):
297359
return True if item in self.data else False
@@ -330,4 +392,14 @@ def execute(self, *args, **kwargs):
330392
"""
331393
pass
332394

395+
def __repr__(self):
396+
return f"<{API_NAME} {self.__class__.__name__} object>"
397+
398+
def __str__(self):
399+
return str(self.batch)
400+
401+
def __bool__(self):
402+
if self.batch:
403+
return True
404+
333405
# endregion

nosqlapi/docdb/client.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,9 @@
3838
class DocConnection(Connection, ABC):
3939
"""Document NOSQL database Connection class"""
4040

41-
def __init__(self,
42-
host=None,
43-
port=None,
44-
username=None,
45-
password=None,
46-
ssl=None,
47-
tls=None,
48-
cert=None,
49-
ca_cert=None,
50-
ca_bundle=None
51-
):
41+
def __init__(self, host=None, port=None, username=None, password=None, ssl=None, tls=None, cert=None, ca_cert=None,
42+
ca_bundle=None):
43+
super().__init__()
5244
self.host = host
5345
self.port = port
5446
self.username = username

nosqlapi/graphdb/client.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,9 @@
3838
class GraphConnection(Connection, ABC):
3939
"""Graph NOSQL database Connection class"""
4040

41-
def __init__(self,
42-
host=None,
43-
port=None,
44-
database=None,
45-
username=None,
46-
password=None,
47-
ssl=None,
48-
tls=None,
49-
cert=None,
50-
ca_cert=None,
51-
ca_bundle=None
52-
):
41+
def __init__(self, host=None, port=None, database=None, username=None, password=None, ssl=None, tls=None, cert=None,
42+
ca_cert=None, ca_bundle=None):
43+
super().__init__()
5344
self.host = host
5445
self.port = port
5546
self.database = database

nosqlapi/kvdb/client.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,9 @@
3838
class KVConnection(Connection, ABC):
3939
"""Key-value NOSQL database Connection class"""
4040

41-
def __init__(self,
42-
host=None,
43-
port=None,
44-
database=None,
45-
username=None,
46-
password=None,
47-
ssl=None,
48-
tls=None,
49-
cert=None,
50-
ca_cert=None,
51-
ca_bundle=None
52-
):
41+
def __init__(self, host=None, port=None, database=None, username=None, password=None, ssl=None, tls=None, cert=None,
42+
ca_cert=None, ca_bundle=None):
43+
super().__init__()
5344
self.host = host
5445
self.port = port
5546
self.database = database

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from setuptools import setup
2+
import __info__
3+
4+
with open("README.md") as fh:
5+
long_description = fh.read()
6+
7+
setup(
8+
name='nosqlapi',
9+
version=__info__.__version__,
10+
packages=['nosqlapi', 'nosqlapi.kvdb', 'nosqlapi.docdb', 'nosqlapi.common', 'nosqlapi.graphdb',
11+
'nosqlapi.columndb'],
12+
url=__info__.__homepage__,
13+
license='GNU General Public License v3.0',
14+
author=__info__.__author__,
15+
author_email=__info__.__email__,
16+
maintainer=__info__.__author__,
17+
maintainer_email=__info__.__email__,
18+
description='nosqlapi is a library for building standard NOSQL python libraries.',
19+
long_description=long_description,
20+
long_description_content_type="text/markdown",
21+
classifiers=[
22+
"Programming Language :: Python :: 3",
23+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
24+
"Operating System :: OS Independent",
25+
],
26+
python_requires='>=3.6'
27+
)

tests/test_columndb.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def delete_database(self, name, exists=False):
9191

9292
def databases(self):
9393
if self.connection:
94-
self.connection.send(f"DESCRIBE keyspaces;")
94+
self.connection.send(f"DESCRIBE KEYSPACES;")
9595
# while len(self.t.recv(2048)) > 0:
9696
self.t.recv = mock.MagicMock(return_value='test_db db1 db2')
9797
self._return_data = self.t.recv(2048)
@@ -101,6 +101,18 @@ def databases(self):
101101
else:
102102
raise ConnectError(f"Server isn't connected")
103103

104+
def show_database(self, name):
105+
if self.connection:
106+
self.connection.send(f"DESCRIBE KEYSPACE {name};")
107+
# while len(self.t.recv(2048)) > 0:
108+
self.t.recv = mock.MagicMock(return_value='table1 table2 table3')
109+
self._return_data = self.t.recv(2048)
110+
if not self:
111+
raise DatabaseError(f'Request error: {self.return_data}')
112+
return MyDBResponse(self.return_data.split())
113+
else:
114+
raise ConnectError(f"Server isn't connected")
115+
104116

105117
class MyDBSession(nosqlapi.columndb.ColumnSession):
106118

@@ -386,6 +398,17 @@ def test_columndb_get_all_database(self):
386398
self.assertEqual(myconn.return_data, 'CLOSED')
387399
self.assertRaises(ConnectError, myconn.databases)
388400

401+
def test_columndb_show_database(self):
402+
myconn = MyDBConnection('mycolumndb.local', 12345, username='admin', password='pass', database='test_db')
403+
myconn.connect()
404+
self.assertEqual(myconn.return_data, 'OK_PACKET')
405+
dbs = myconn.show_database('test_db')
406+
self.assertIsInstance(dbs, MyDBResponse)
407+
self.assertEqual(dbs.data, ['table1', 'table2', 'table3'])
408+
myconn.close()
409+
self.assertEqual(myconn.return_data, 'CLOSED')
410+
self.assertRaises(ConnectError, myconn.databases)
411+
389412

390413
class ColumnSessionTest(unittest.TestCase):
391414
myconn = MyDBConnection('mycolumndb.local', 12345, username='admin', password='pass', database='test_db')

tests/test_docdb.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ def databases(self):
8888
else:
8989
raise ConnectError("server isn't connected")
9090

91+
def show_database(self, name):
92+
self.req.get = mock.MagicMock(return_value={'body': '{"result": {"name": "test_db", "size": "0.4GB"}}',
93+
'status': 200,
94+
'header': '"Content-Type": [ "application/json" ]'})
95+
if self.connection:
96+
ret = self.req.get(f"{self.connection}/databases?name={name}")
97+
dbs = json.loads(ret.get('body'))
98+
if dbs['result']:
99+
return MyDBResponse(dbs['result'],
100+
ret['status'],
101+
ret['header'])
102+
else:
103+
raise DatabaseError('no databases found on this server')
104+
else:
105+
raise ConnectError("server isn't connected")
106+
91107

92108
class MyDBSession(nosqlapi.docdb.DocSession):
93109
# Simulate http requests
@@ -337,6 +353,17 @@ def test_docdb_get_all_database(self):
337353
self.assertEqual(myconn.connection, None)
338354
self.assertRaises(ConnectError, myconn.databases)
339355

356+
def test_columndb_show_database(self):
357+
myconn = MyDBConnection('mydocdb.local', 12345, username='admin', password='test')
358+
myconn.connect()
359+
self.assertEqual(myconn.connection, 'http://admin:[email protected]:12345')
360+
dbs = myconn.show_database('test_db')
361+
self.assertIsInstance(dbs, MyDBResponse)
362+
self.assertEqual(dbs.data, {'name': 'test_db', 'size': '0.4GB'})
363+
myconn.close()
364+
self.assertEqual(myconn.connection, None)
365+
self.assertRaises(ConnectError, myconn.databases)
366+
340367

341368
class DocSessionTest(unittest.TestCase):
342369
myconn = MyDBConnection('mydocdb.local', 12345, username='admin', password='test')

0 commit comments

Comments
 (0)