From 3927e010a8783913c6c636c942b4be86a9f4e4d2 Mon Sep 17 00:00:00 2001 From: Kertich Date: Thu, 22 Aug 2019 17:57:41 +0300 Subject: [PATCH 1/5] Implement create_database fuction --- db_config.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/db_config.py b/db_config.py index 41972a8f..6a7ab58c 100644 --- a/db_config.py +++ b/db_config.py @@ -26,6 +26,7 @@ def connect(self,database_url): try: connection = p.connect(database_url) + connection.autocommit = True return connection except: @@ -41,12 +42,31 @@ def cursor(self): Returns: Object:cursor object. ''' + try: - connection = self.connect(DATABASE_URL) - cursor = connection.cursor() - return cursor + connection = self.connect(DATABASE_URL) + cursor = connection.cursor() + return cursor + except: + return 'Can not exucute PostgreSQL command' + + def create_database(self, db_name): + + try: + + query = f"""CREATE DATABASE {db_name};""" + + conn = self.connect(DATABASE_URL) + cursor = conn.cursor() + cursor.execute(query) + conn.commit() + + except : + return "Error while connecting to PostgreSQL" if __name__ == "__main__": db = PostgresConfig() + print(db.connect(DATABASE_URL)) + print(db.create_database('db_name')) From 296b51a0a8def29b01469dd2a6a73cb2f6a86531 Mon Sep 17 00:00:00 2001 From: Kertich Date: Thu, 22 Aug 2019 17:59:52 +0300 Subject: [PATCH 2/5] remove creation of user b4 script --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 564a56f3..f1332089 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ services: 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/" From 9eb52427640c5ee5dca544c6a8006b2e0a2374ac Mon Sep 17 00:00:00 2001 From: Kertich Date: Thu, 22 Aug 2019 18:02:44 +0300 Subject: [PATCH 3/5] uncomment abstract method decorator --- database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database.py b/database.py index 1606cce6..55fb531c 100644 --- a/database.py +++ b/database.py @@ -12,7 +12,7 @@ def session(self,): autocommit = True pass - # @abstractmethod + @abstractmethod def create_database(self): pass From 174f3c15942c60f715f77ea7bcc476106df9c05a Mon Sep 17 00:00:00 2001 From: Kertich Date: Thu, 22 Aug 2019 18:07:07 +0300 Subject: [PATCH 4/5] Implement test 4 create_database mthod --- tests/integration/test_postgres.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/integration/test_postgres.py b/tests/integration/test_postgres.py index 5168dec0..9e59251d 100644 --- a/tests/integration/test_postgres.py +++ b/tests/integration/test_postgres.py @@ -12,9 +12,15 @@ class PostgresTestCase(TestCase): def setUp(self): self.postgres = PostgresConfig() + self.db_name = "test_db_one" def test_make_good_connection(self): print(TEST_DATABASE_URL) self.assertNotEqual(self.postgres.connect(TEST_DATABASE_URL), 'failed to connect to database.') + + def test_create_database(self): + self.assertNotIn(self.postgres.create_database(self.db_name), 'failed to connect to database.' + ) + \ No newline at end of file From a841683a8809a43b92dde6aaaebf33631be2aefb Mon Sep 17 00:00:00 2001 From: Kertich Date: Thu, 22 Aug 2019 18:38:42 +0300 Subject: [PATCH 5/5] Add docstrings --- db_config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/db_config.py b/db_config.py index 6a7ab58c..c61a7ef2 100644 --- a/db_config.py +++ b/db_config.py @@ -52,6 +52,14 @@ def cursor(self): return 'Can not exucute PostgreSQL command' def create_database(self, db_name): + """Creates a Postgres database. + + Arguments: + db_name(str): A name of the given database. + + Returns: + None if no exception or error mssg incase there is one. + """ try: