From 76af6b5aa305349c9e8076d72d604665d206f02c Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Thu, 15 Aug 2019 02:14:13 +0300 Subject: [PATCH 01/13] Connects to postgres database --- config.py | 13 +++++++++++++ db.ini | 5 +++++ main.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 config.py create mode 100644 db.ini create mode 100644 main.py diff --git a/config.py b/config.py new file mode 100644 index 00000000..44f0ca4d --- /dev/null +++ b/config.py @@ -0,0 +1,13 @@ +from configparser import ConfigParser + +def config(filename='db.ini', section='postgresql'): + parser = ConfigParser() + parser.read(filename) + db = {} + if parser.has_section(section): + params = parser.items(section) + for param in params: + db[param[0]] = param[1] + else: + raise Exception('section {} not found in the {} file'.format(section, filename)) + return db \ No newline at end of file diff --git a/db.ini b/db.ini new file mode 100644 index 00000000..277f08e3 --- /dev/null +++ b/db.ini @@ -0,0 +1,5 @@ +[postgresql] +host=localhost +database=suppliers +user=postgres +password=toor \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 00000000..837415b0 --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +from database import Postgres +import psycopg2 +from config import config + +class ConnectToDb(): + pass + +class PostgresDb(Postgres): + """ + COnnect to the postgresql database + + Arguments: + Postgres {[type]} -- [description] + + Returns: + [type] -- [description] + """ + def connect(): + conn = None + try: + params = config() + print('Connecting to Posgress..../') + conn = psycopg2.connect(**params) + cur = conn.cursor() + print('Postgresql database version: ') + cur.execute('SELECT Version()') + db_version = cur.fetchone() + print(db_version) + cur.close + except (Exception, psycopg2.DatabaseError) as error: + print(error) + finally: + if conn is not None: + conn.close() + print('Database connection closed') + + + + + +if __name__ == '__main__': + a = PostgresDb + a.connect() \ No newline at end of file From 90c050386c541317a230aee2d0588e2540328c98 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Thu, 15 Aug 2019 02:19:57 +0300 Subject: [PATCH 02/13] Travis CI markdown update --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index b34cd530..b510d427 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -[![Build Status](https://travis-ci.com/icodeai/nbox.svg?branch=databases_postgres)](https://travis-ci.com/icodeai/nbox) [![Coverage Status](https://coveralls.io/repos/github/icodeai/nbox/badge.svg?branch=master)](https://coveralls.io/github/icodeai/nbox?branch=master) - +[![Build Status](https://travis-ci.org/Joseorina/nbox.svg?branch=Ft-Connect_Db)](https://travis-ci.org/Joseorina/nbox) # Nbox ML Toolkit From ea57d95aa15eae83d466d4bb663574e3794f4e01 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Thu, 15 Aug 2019 02:47:16 +0300 Subject: [PATCH 03/13] Added class for unit testing --- __pycache__/config.cpython-37.pyc | Bin 0 -> 546 bytes __pycache__/database.cpython-37.pyc | Bin 0 -> 1737 bytes tests/integration/test_integ_main.py | 0 tests/system/test_sys.py | 0 tests/unit/test_unit_main.py | 18 ++++++++++++++++++ 5 files changed, 18 insertions(+) create mode 100644 __pycache__/config.cpython-37.pyc create mode 100644 __pycache__/database.cpython-37.pyc create mode 100644 tests/integration/test_integ_main.py create mode 100644 tests/system/test_sys.py create mode 100644 tests/unit/test_unit_main.py diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c10bc1c73627ab985482e57a5b4acb1d11c1c417 GIT binary patch literal 546 zcmYjOK}#Gl6n-x=6SwPbkx~SE8Yl>J+5H0|QZ4i(1%<+(l-W(zopvV~lL%`&bE)3F zc=Du2|5C0!`4>F-GHanPye}`w_wru8d>Rh>Kz4I`e0qlff5YORMHK9j+YbZ+1TLV$ z91+Bth(&UZITQQ{vfdM&Lxz5^uhpzP`&t+)%`+F1QK?GbueEh&M%wemlm#XkCr`-t z$?XY&gDYrR!|t(x2P#oQ8_|k2_<-gMMNx>_csXj~12}!(CQTx63z~>#wgo$IKKce~ z!^I-HPnvKGyTU)%MATq>_(X356X4fJE2pm1D*fH2O1pHXFO*12mAbhMd$V#OGj2ks zJ~pxt`TD%D-(RzQQaV{#|K{^@D(i5M=QC}p!e!j|Lar(*nXkRlb6$61kWv#L;-a#7 z(#?-x22|sDdpy^b9RJW(jt`{$>2y6-6TMu)v{Q6cUwXa@!>8DH1b`g-7?X#w$u`iu srQO4fnZPkIK^EnMRobeJ?o}PFtNkAgiS2tkl@=EZxf_JCL4g5#0e{(ttN;K2 literal 0 HcmV?d00001 diff --git a/__pycache__/database.cpython-37.pyc b/__pycache__/database.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8f9ff3e1b0daaf056dab2bba65754a67393985a9 GIT binary patch literal 1737 zcmbtVy>8nu5GM6wE4KWTI7vNp=+Gesia6+86m3$Vn^B;d1QdcS5^-9|R7eVG3Uo>y zphI7xucWn8U!ha)DC;Ldh61L+^GDz5K7J=2eL5KQ2#kZD=bt>Akl(ndh6CUYO!Wc` zBa9{_r2$Q?z@oUe5<9Tp6J|5#jxdMQTRU)=^^wfnKkx*ZS%v-f^sE?0OQmHL>y+!2 zV2c=?)d13xC5wU8`uR`7V>WJ2n~W08)Q5d>fgWBwk^S0d%}&qDF}JFdQH zko(~COud%`n}w!wKzA_GaB@w zT#Hzw>7{PABr&?^Dz4O}SodLJL=wxPExwJr=7X??uT_**-dc3YFp4FQG!H9{V`SRs znl`%b2W`REQ99DO+HcW}YMRm)W5saXtKLI3Gg`P;IhQvrZKh_OIG0MuDI}l;T=FZ&B^n zvRVOKG4<3+T$lAbY7tFpqQ<`+50fG*dGPM6PD8`f__sLGZk)iiand3f)}Wnh6?grPdiW z{f&(vv14$h4#9Yq@Axjvdmj@6_oW>zV~JgFHc@z)x`{=0@zVGoOI~=dN)@lMNzK(X Hrk?!|>J?7+ literal 0 HcmV?d00001 diff --git a/tests/integration/test_integ_main.py b/tests/integration/test_integ_main.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/system/test_sys.py b/tests/system/test_sys.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/test_unit_main.py b/tests/unit/test_unit_main.py new file mode 100644 index 00000000..de893fa2 --- /dev/null +++ b/tests/unit/test_unit_main.py @@ -0,0 +1,18 @@ +from .. import main +import unittest + +class TestPostgres(unittest.TestCase): + """ + Test database connectivity + + Arguments: + unittest {[type]} -- [description] + """ + def test_connection(self): + """ + Test that the database connection is succesful + """ + pass + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 687a9b4c562797068a31f5f66462bd57d43b0933 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 01:35:27 +0300 Subject: [PATCH 04/13] Added integration test for database connection --- .../test_integ_main.py => __init__.py | 0 main.py | 55 +++++++++++++++++-- tests/integration/test_main.py | 26 +++++++++ tests/unit/test_unit_main.py | 18 ------ 4 files changed, 75 insertions(+), 24 deletions(-) rename tests/integration/test_integ_main.py => __init__.py (100%) create mode 100644 tests/integration/test_main.py diff --git a/tests/integration/test_integ_main.py b/__init__.py similarity index 100% rename from tests/integration/test_integ_main.py rename to __init__.py diff --git a/main.py b/main.py index 837415b0..e28310a7 100644 --- a/main.py +++ b/main.py @@ -19,10 +19,10 @@ def connect(): conn = None try: params = config() - print('Connecting to Posgress..../') conn = psycopg2.connect(**params) cur = conn.cursor() - print('Postgresql database version: ') + #return 'Version' + #print('Postgresql database version: ') cur.execute('SELECT Version()') db_version = cur.fetchone() print(db_version) @@ -32,12 +32,55 @@ def connect(): finally: if conn is not None: conn.close() - print('Database connection closed') - + # print('Database connection closed') + return 'success' + def session(): + pass + + def create_database(): + pass + + def status(): + pass + + def cursor(query): + pass - + def select_table(query): + pass + + def create_table(query): + """ + Create table in a database + + Arguments:pi + query {[type]} -- [description] + + Returns: + [type] -- [description] + """ + + + def insert_rows(query): + """ + Funtion to insert records intp a database + + Arguments: + query {[type]} -- [description] + """ + pass + + def show_table(query): + pass + + def drop_table(query): + pass + + def close(): + pass if __name__ == '__main__': a = PostgresDb - a.connect() \ No newline at end of file + a.connect() + \ No newline at end of file diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py new file mode 100644 index 00000000..e4c4faa4 --- /dev/null +++ b/tests/integration/test_main.py @@ -0,0 +1,26 @@ +import unittest +from main import PostgresDb +from config import config + +a = PostgresDb +class TestPostgres(unittest.TestCase): + """ + Test database connectivity + + Arguments: + unittest {[type]} -- [description] + """ + def test_connection(self): + """ + Test that the database connection is succesful + """ + self.assertEqual('success', a.connect()) + #self.assertEqual('Version', a.connect()) + #self.assertEqual(None, a.connect()) + + + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/unit/test_unit_main.py b/tests/unit/test_unit_main.py index de893fa2..e69de29b 100644 --- a/tests/unit/test_unit_main.py +++ b/tests/unit/test_unit_main.py @@ -1,18 +0,0 @@ -from .. import main -import unittest - -class TestPostgres(unittest.TestCase): - """ - Test database connectivity - - Arguments: - unittest {[type]} -- [description] - """ - def test_connection(self): - """ - Test that the database connection is succesful - """ - pass - -if __name__ == '__main__': - unittest.main() \ No newline at end of file From 5115341819ad23f05e24914c002b7047c90a8db6 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 01:37:17 +0300 Subject: [PATCH 05/13] Removed non funtioning tests --- tests/integration/test_main.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index e4c4faa4..1b8f2694 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -15,12 +15,7 @@ def test_connection(self): Test that the database connection is succesful """ self.assertEqual('success', a.connect()) - #self.assertEqual('Version', a.connect()) - #self.assertEqual(None, a.connect()) - - - - + if __name__ == '__main__': unittest.main() \ No newline at end of file From d99fe6ea9a828d43f1023f27bb6a66186b083f64 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 01:49:58 +0300 Subject: [PATCH 06/13] Removed non functioning code from main.py --- main.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/main.py b/main.py index e28310a7..6d6914a9 100644 --- a/main.py +++ b/main.py @@ -21,18 +21,12 @@ def connect(): params = config() conn = psycopg2.connect(**params) cur = conn.cursor() - #return 'Version' - #print('Postgresql database version: ') - cur.execute('SELECT Version()') - db_version = cur.fetchone() - print(db_version) cur.close except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() - # print('Database connection closed') return 'success' def session(): From 2b9873f501a1bdb9995c6dfe7e2ff3f72ed9962b Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 01:59:58 +0300 Subject: [PATCH 07/13] Added cursor feature --- main.py | 16 +++++++++++++--- tests/integration/test_main.py | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 6d6914a9..1f0a100e 100644 --- a/main.py +++ b/main.py @@ -38,9 +38,19 @@ def create_database(): def status(): pass - def cursor(query): - pass - + def cursor(): + """ + Create a cursor area in memory where SQL statement + are executed + + Arguments: + query {[type]} -- [description] + """ + params = config() + conn = psycopg2.connect(**params) + cursor = conn.cursor() + return 'success' + def select_table(query): pass diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 1b8f2694..9644858d 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -15,6 +15,8 @@ def test_connection(self): Test that the database connection is succesful """ self.assertEqual('success', a.connect()) + + if __name__ == '__main__': From 6a446dca1860fc57da99f01b4b5702559a5618d1 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 02:01:21 +0300 Subject: [PATCH 08/13] Added tests for cursor feature --- tests/integration/test_main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 9644858d..c7c8c73a 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -16,7 +16,11 @@ def test_connection(self): """ self.assertEqual('success', a.connect()) - + def test_cursor(self): + """ + Test cursor function + """ + self.assertEqual('success', a.cursor()) if __name__ == '__main__': From 51bf76e9bd3e0deef4a0348db9a2671aebc1a1fb Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 02:24:13 +0300 Subject: [PATCH 09/13] Created a function for selecting a table --- main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 1f0a100e..221bc876 100644 --- a/main.py +++ b/main.py @@ -48,11 +48,23 @@ def cursor(): """ params = config() conn = psycopg2.connect(**params) - cursor = conn.cursor() + cur = conn.cursor() return 'success' def select_table(query): - pass + """ + Fetch and display records from database + + Arguments: + query {[type]} -- [description] + """ + params = config() + conn = psycopg2.connect(**params) + cur = conn.cursor() + cur.execute(query) + rows = cur.fetchall() + conn.close() + return 'success' def create_table(query): """ @@ -87,4 +99,5 @@ def close(): if __name__ == '__main__': a = PostgresDb a.connect() + a.select_table("SELECT id, name, address, salary from COMPANY") \ No newline at end of file From c6d6d7d102e900e53732149079d537224d432ef4 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 02:25:48 +0300 Subject: [PATCH 10/13] Added test for select table --- tests/integration/test_main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index c7c8c73a..6fc398c3 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -22,6 +22,12 @@ def test_cursor(self): """ self.assertEqual('success', a.cursor()) - + def test_select_table(self): + """ + Test fetch and display of records + """ + b = "SELECT id, name, address, salary from COMPANY" + self.assertEqual('success', a.select_table(b)) + if __name__ == '__main__': unittest.main() \ No newline at end of file From 99545b97a844eaac99d395284b57388bcd879675 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 02:32:41 +0300 Subject: [PATCH 11/13] Deleted some modules --- __pycache__/database.cpython-37.pyc | Bin 1737 -> 1699 bytes db_config.py | 52 ---------------------------- tests/integration/test_postgres.py | 20 ----------- 3 files changed, 72 deletions(-) delete mode 100644 db_config.py delete mode 100644 tests/integration/test_postgres.py diff --git a/__pycache__/database.cpython-37.pyc b/__pycache__/database.cpython-37.pyc index 8f9ff3e1b0daaf056dab2bba65754a67393985a9..05d0d644daf1906cc98d8945e51b7504347767bf 100644 GIT binary patch literal 1699 zcmb`H&2G~`5P*06leqbBll~kK;=rMiP{RWt1T7M`2no1YLYCuMXu;05yK9w3;?%wj z&%zt{$_XBU6Ek+~xFr%oVI$A1J>&Ui){{Bzc3TYBx1Vp{-M1P0PB-OpaPtf={SXB( z;4w?M&lAhHID5&!0{e!6E%>$V+u*!rWA_Jovayxf&yP>CUO3Z6g^@{wIh7ERtz5_| zV#l}<%;&&-OW44{x?$JccK{>aFy93i8mJr41P^r+eYT*L`*_fX4nEt0F7!~hpbrDo zZPRVFeviRI z6V5^{f>g!IMW0;!I-H!!L`*(PEhaC7{$%9&WImIh3sJswbbggJBRQXo$TSF&ldZ*| zOyy{4j3^CSw^H)(0q`$)NSKy2eUqcdmZDI8D@=`yWRjejRbHxzrmU%j)@O3w#+$q@ z#vNXRF3lB`;Oowz;PISym(h=u2#pBJQU}D!%j1iKRe86aHX=5(2~Da;HBi3-6}HXd zN2$_M?N{-w#VUSb`)lXazfk60q(WWYJ-80`6}CfwZ54}q6^t)-SH;vk{t2nN81E0@ z@=N8w2Wx?KkIjMYv$+<^1WI1!Go);bQB`2gVRKwdpUTVPI1g*p4J+Ep{vTkV|}O0 aixka-A`5zLUChjTQY8PBhEU!u1N%3%oIVKv literal 1737 zcmbtVy>8nu5GM6wE4KWTI7vNp=+Gesia6+86m3$Vn^B;d1QdcS5^-9|R7eVG3Uo>y zphI7xucWn8U!ha)DC;Ldh61L+^GDz5K7J=2eL5KQ2#kZD=bt>Akl(ndh6CUYO!Wc` zBa9{_r2$Q?z@oUe5<9Tp6J|5#jxdMQTRU)=^^wfnKkx*ZS%v-f^sE?0OQmHL>y+!2 zV2c=?)d13xC5wU8`uR`7V>WJ2n~W08)Q5d>fgWBwk^S0d%}&qDF}JFdQH zko(~COud%`n}w!wKzA_GaB@w zT#Hzw>7{PABr&?^Dz4O}SodLJL=wxPExwJr=7X??uT_**-dc3YFp4FQG!H9{V`SRs znl`%b2W`REQ99DO+HcW}YMRm)W5saXtKLI3Gg`P;IhQvrZKh_OIG0MuDI}l;T=FZ&B^n zvRVOKG4<3+T$lAbY7tFpqQ<`+50fG*dGPM6PD8`f__sLGZk)iiand3f)}Wnh6?grPdiW z{f&(vv14$h4#9Yq@Axjvdmj@6_oW>zV~JgFHc@z)x`{=0@zVGoOI~=dN)@lMNzK(X Hrk?!|>J?7+ diff --git a/db_config.py b/db_config.py deleted file mode 100644 index 41972a8f..00000000 --- a/db_config.py +++ /dev/null @@ -1,52 +0,0 @@ -import os - -import psycopg2 as p - -from database import Postgres - -DATABASE_URL = os.getenv('DATABASE_URL') - -class PostgresConfig(Postgres): - - - def connect(self,database_url): - '''Create a connection to a PostgreSQL database instance. - - Args: - database_url (str): A url with a given user database credentials. - - Returns: - object: A PostgreSQL Connection Object. - This connection is thread-safe and can be shared among many threads. - - str: It is returned incase there is a database error or exception that may - occur while working with PostgreSQL from Python. - ''' - - try: - - connection = p.connect(database_url) - return connection - - except: - - return 'failed to connect to database.' - - def cursor(self): - '''Create a cursor object which allows us to execute PostgreSQL command - through Python source code. - Cursors created from the same connection are not isolated, i.e., any changes - done to the database by a cursor are immediately visible by the other cursors. - - Returns: - Object:cursor object. - ''' - - connection = self.connect(DATABASE_URL) - cursor = connection.cursor() - return cursor - - -if __name__ == "__main__": - db = PostgresConfig() - print(db.connect(DATABASE_URL)) diff --git a/tests/integration/test_postgres.py b/tests/integration/test_postgres.py deleted file mode 100644 index 5168dec0..00000000 --- a/tests/integration/test_postgres.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -from unittest import TestCase - -from db_config import PostgresConfig - - -TEST_DATABASE_URL = os.getenv('TEST_DATABASE_URL') -WRONG_TEST_DATABASE_URL = os.getenv('WRONG_TEST_DATABASE_URL') - - -class PostgresTestCase(TestCase): - - def setUp(self): - self.postgres = PostgresConfig() - - - def test_make_good_connection(self): - print(TEST_DATABASE_URL) - self.assertNotEqual(self.postgres.connect(TEST_DATABASE_URL), - 'failed to connect to database.') From 7a68581f9bd7b7e0292c1038faa37bd5a59022f1 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 02:45:39 +0300 Subject: [PATCH 12/13] .travis.yml formating --- .travis.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 564a56f3..5c27c633 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,11 +8,6 @@ install: services: - postgresql -before_script: - - psql -c "CREATE DATABASE test_nbox;" -U postgres - - psql -c "CREATE USER antoo WITH PASSWORD 'antoo123';" -U postgres - - psql -c "GRANT ALL PRIVILEGES ON DATABASE test_nbox to antoo;" -U postgres - script: - "python -m pytest tests/" - "python -m pytest --cov tests/" From 4a42fb06ea9b12eb9876ef44b3fe8028b4e412a5 Mon Sep 17 00:00:00 2001 From: Joseph Orina Date: Fri, 16 Aug 2019 03:11:35 +0300 Subject: [PATCH 13/13] .travis.yml formating --- .travis.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5c27c633..e08a2c22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,18 @@ + language: python python: - - '3.7.3' + - "3.7" - "nightly" -install: - - pip install -r requirements.txt - - pip install coveralls -services: - - postgresql -script: +install: + - "pip install pytest" + - "pip install pytest-cov" + - "pip install coveralls" + - "pip install -r requirements.txt" + +script: - "python -m pytest tests/" - "python -m pytest --cov tests/" - + after_success: - - coveralls - -cache: pip \ No newline at end of file + - coveralls \ No newline at end of file