From e242fc3dc398d74d9d779c36665355405cedf865 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 11:11:37 +0300 Subject: [PATCH 01/11] modifying --- .travis.yml | 13 ++++---- database.py | 6 ++-- db_config.py | 48 +++++++++++++----------------- tests/integration/test_postgres.py | 20 ------------- tests/integration/test_session.py | 11 +++++++ 5 files changed, 40 insertions(+), 58 deletions(-) delete mode 100644 tests/integration/test_postgres.py create mode 100644 tests/integration/test_session.py diff --git a/.travis.yml b/.travis.yml index 564a56f3..cb8d7040 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: python python: - '3.7.3' - - "nightly" +env: + dbParameters='user=testuser password=password123 host=localhost port=5432 dbname=testdb' install: - pip install -r requirements.txt - pip install coveralls @@ -9,15 +10,13 @@ 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 + - psql -c "CREATE DATABASE testdb;" -U postgres + - psql -c "CREATE USER testuser WITH PASSWORD 'password123';" -U postgres + - psql -c "GRANT ALL PRIVILEGES ON DATABASE testdb to testuser;" -U postgres 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 diff --git a/database.py b/database.py index 1606cce6..474a55c6 100644 --- a/database.py +++ b/database.py @@ -7,7 +7,7 @@ class Postgres(ABC): def connect(self,database_url): pass - # @abstractmethod + @abstractmethod def session(self,): autocommit = True pass @@ -16,11 +16,11 @@ def session(self,): def create_database(self): pass - # @abstractmethod + # @abstractmethod def status(self): pass - @abstractmethod + # @abstractmethod def cursor(self): pass diff --git a/db_config.py b/db_config.py index 41972a8f..5560c188 100644 --- a/db_config.py +++ b/db_config.py @@ -1,15 +1,19 @@ import os -import psycopg2 as p +import psycopg2 from database import Postgres -DATABASE_URL = os.getenv('DATABASE_URL') +dbParameters = os.getenv('dbParameters') class PostgresConfig(Postgres): - - def connect(self,database_url): + def __init__(self): + #initializes the Postgresdb class + self.connection = None + self.cursordb = None + + def connect(self): '''Create a connection to a PostgreSQL database instance. Args: @@ -24,29 +28,17 @@ def connect(self,database_url): ''' try: + self.connection = psycopg2.connect(dbParameters) + self.cursordb = self.connection.cursor() + return 'connection successful' - connection = p.connect(database_url) - return connection - - except: - - return 'failed to connect to database.' + except (Exception, psycopg2.Error) as error : + return error - 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)) + def session(self): + try: + self.connection.set_session(autocommit = True) + return 'autocommit enabled' + except (Exception, psycopg2.Error) as error : + return error + \ No newline at end of file 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.') diff --git a/tests/integration/test_session.py b/tests/integration/test_session.py new file mode 100644 index 00000000..4d4b77df --- /dev/null +++ b/tests/integration/test_session.py @@ -0,0 +1,11 @@ +import os +from unittest import TestCase +from db_config import PostgresConfig + + +class PostgresTestCase(TestCase): + + def test_session(self): + db = PostgresConfig() + self.assertNotEqual(db.session(), + 'autocommit enabled') From 6ca65109e37e33bda70fc84c589f52d6ba7e5034 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 11:32:04 +0300 Subject: [PATCH 02/11] modifying --- database.py | 4 ++-- db_config.py | 5 ++--- tests/integration/{test_session.py => test_status.py} | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) rename tests/integration/{test_session.py => test_status.py} (66%) diff --git a/database.py b/database.py index 474a55c6..d3cbb8c6 100644 --- a/database.py +++ b/database.py @@ -7,7 +7,7 @@ class Postgres(ABC): def connect(self,database_url): pass - @abstractmethod + # @abstractmethod def session(self,): autocommit = True pass @@ -16,7 +16,7 @@ def session(self,): def create_database(self): pass - # @abstractmethod + @abstractmethod def status(self): pass diff --git a/db_config.py b/db_config.py index 5560c188..8ddaa31d 100644 --- a/db_config.py +++ b/db_config.py @@ -35,10 +35,9 @@ def connect(self): except (Exception, psycopg2.Error) as error : return error - def session(self): + def status(self): try: - self.connection.set_session(autocommit = True) - return 'autocommit enabled' + return self.connection.status except (Exception, psycopg2.Error) as error : return error \ No newline at end of file diff --git a/tests/integration/test_session.py b/tests/integration/test_status.py similarity index 66% rename from tests/integration/test_session.py rename to tests/integration/test_status.py index 4d4b77df..1d7485e1 100644 --- a/tests/integration/test_session.py +++ b/tests/integration/test_status.py @@ -7,5 +7,5 @@ class PostgresTestCase(TestCase): def test_session(self): db = PostgresConfig() - self.assertNotEqual(db.session(), - 'autocommit enabled') + self.assertNotEqual(db.status(), + 'STATUS_READY') From 54302ee2ecc94c4481b7cbe01684055df106f71e Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 11:36:00 +0300 Subject: [PATCH 03/11] modifying --- database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database.py b/database.py index d3cbb8c6..d318beb4 100644 --- a/database.py +++ b/database.py @@ -16,7 +16,7 @@ def session(self,): def create_database(self): pass - @abstractmethod + @abstractmethod def status(self): pass From 6f21b9b90256adb99aba98216b97f45450bd2ccd Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 12:04:22 +0300 Subject: [PATCH 04/11] modifying --- database.py | 4 ++-- db_config.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/database.py b/database.py index d318beb4..314b5e48 100644 --- a/database.py +++ b/database.py @@ -12,11 +12,11 @@ def session(self,): autocommit = True pass - # @abstractmethod + @abstractmethod def create_database(self): pass - @abstractmethod + # @abstractmethod def status(self): pass diff --git a/db_config.py b/db_config.py index 8ddaa31d..67a1e507 100644 --- a/db_config.py +++ b/db_config.py @@ -35,9 +35,18 @@ def connect(self): except (Exception, psycopg2.Error) as error : return error - def status(self): + def create_database(self,query): + '''creates a database + Args: + query(str):sql query to be executed + Returns: + db created if successful + an error if unsuccessful + ''' try: - return self.connection.status + self.cursordb.execute(query) + self.connection.commit() + return 'database created' except (Exception, psycopg2.Error) as error : return error \ No newline at end of file From bb0a9b3de0bbb1a7fa4749597705afe41163a0a2 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 12:17:58 +0300 Subject: [PATCH 05/11] modifying --- database.py | 4 ++-- db_config.py | 6 +++--- tests/integration/test_status.py | 11 ----------- tests/integration/test_table.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 16 deletions(-) delete mode 100644 tests/integration/test_status.py create mode 100644 tests/integration/test_table.py diff --git a/database.py b/database.py index 314b5e48..e6aaaf61 100644 --- a/database.py +++ b/database.py @@ -12,7 +12,7 @@ def session(self,): autocommit = True pass - @abstractmethod + #@abstractmethod def create_database(self): pass @@ -28,7 +28,7 @@ def cursor(self): def select_table(self,query): pass - # @abstractmethod + @abstractmethod def create_table(self,query): pass diff --git a/db_config.py b/db_config.py index 67a1e507..dc1b99b4 100644 --- a/db_config.py +++ b/db_config.py @@ -35,8 +35,8 @@ def connect(self): except (Exception, psycopg2.Error) as error : return error - def create_database(self,query): - '''creates a database + def create_table(self,query): + '''creates a table Args: query(str):sql query to be executed Returns: @@ -46,7 +46,7 @@ def create_database(self,query): try: self.cursordb.execute(query) self.connection.commit() - return 'database created' + return 'table created' except (Exception, psycopg2.Error) as error : return error \ No newline at end of file diff --git a/tests/integration/test_status.py b/tests/integration/test_status.py deleted file mode 100644 index 1d7485e1..00000000 --- a/tests/integration/test_status.py +++ /dev/null @@ -1,11 +0,0 @@ -import os -from unittest import TestCase -from db_config import PostgresConfig - - -class PostgresTestCase(TestCase): - - def test_session(self): - db = PostgresConfig() - self.assertNotEqual(db.status(), - 'STATUS_READY') diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py new file mode 100644 index 00000000..2555bc4d --- /dev/null +++ b/tests/integration/test_table.py @@ -0,0 +1,16 @@ +import os +from unittest import TestCase +from db_config import PostgresConfig + + +class PostgresTestCase(TestCase): + + def test_table(self): + query ='''CREATE TABLE TESTTABLE + (ID INT PRIMARY KEY NOT NULL, + VALUE1 TEXT NOT NULL,) + ''' + db = PostgresConfig() + db.connect() + self.assertEqual(db.create_table(query), + 'table created') From 29f83c27ca925e59abf34df8134878808bc651e7 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Thu, 22 Aug 2019 15:00:12 +0300 Subject: [PATCH 06/11] modifying --- tests/integration/test_table.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index 2555bc4d..b0edd11f 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -12,5 +12,4 @@ def test_table(self): ''' db = PostgresConfig() db.connect() - self.assertEqual(db.create_table(query), - 'table created') + self.assertEqual(db.create_table(query),'table created') From dac0384ed02e970ca3c44afdeac1893b84b4c13f Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Mon, 26 Aug 2019 09:17:05 +0300 Subject: [PATCH 07/11] modifying --- tests/integration/test_table.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index b0edd11f..1348e080 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -12,4 +12,5 @@ def test_table(self): ''' db = PostgresConfig() db.connect() - self.assertEqual(db.create_table(query),'table created') + a = db.create_table(query) + self.assertEqual(a ,'table created') From 25a27ea35fdcb45811fe4a8590a435831b175d07 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Mon, 26 Aug 2019 09:22:45 +0300 Subject: [PATCH 08/11] modifying --- tests/integration/test_table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index 1348e080..ee7d59d2 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -13,4 +13,4 @@ def test_table(self): db = PostgresConfig() db.connect() a = db.create_table(query) - self.assertEqual(a ,'table created') + self.assertEqual(a,"table created") From 5dc2c5e8bd01c49e3c81965c4695f3e7734321f3 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Mon, 26 Aug 2019 12:34:06 +0300 Subject: [PATCH 09/11] mpdifying --- tests/integration/test_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index ee7d59d2..c5f6c399 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -7,8 +7,8 @@ class PostgresTestCase(TestCase): def test_table(self): query ='''CREATE TABLE TESTTABLE - (ID INT PRIMARY KEY NOT NULL, - VALUE1 TEXT NOT NULL,) + (testvalue int, + anothervalue int) ''' db = PostgresConfig() db.connect() From 2972b4e748becf96ad18badc43bf32e4587bcbd3 Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Mon, 26 Aug 2019 12:55:58 +0300 Subject: [PATCH 10/11] modifying --- tests/integration/test_table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index c5f6c399..68739b3a 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -6,7 +6,7 @@ class PostgresTestCase(TestCase): def test_table(self): - query ='''CREATE TABLE TESTTABLE + query ='''CREATE TABLE TESTTABLE IF NOT EXISTS (testvalue int, anothervalue int) ''' From fcfd1f256222fba8eedb0149eaecf66b2fbfa3fc Mon Sep 17 00:00:00 2001 From: Mukuzi Date: Mon, 26 Aug 2019 12:57:27 +0300 Subject: [PATCH 11/11] modifying --- tests/integration/test_table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_table.py b/tests/integration/test_table.py index 68739b3a..65d145a0 100644 --- a/tests/integration/test_table.py +++ b/tests/integration/test_table.py @@ -6,7 +6,7 @@ class PostgresTestCase(TestCase): def test_table(self): - query ='''CREATE TABLE TESTTABLE IF NOT EXISTS + query ='''CREATE TABLE IF NOT EXISTS TESTTABLE (testvalue int, anothervalue int) '''