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..e6aaaf61 100644 --- a/database.py +++ b/database.py @@ -7,12 +7,12 @@ class Postgres(ABC): def connect(self,database_url): pass - # @abstractmethod + # @abstractmethod def session(self,): autocommit = True pass - # @abstractmethod + #@abstractmethod def create_database(self): pass @@ -20,7 +20,7 @@ def create_database(self): def status(self): pass - @abstractmethod + # @abstractmethod def cursor(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 41972a8f..dc1b99b4 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,25 @@ 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. - + def create_table(self,query): + '''creates a table + Args: + query(str):sql query to be executed Returns: - Object:cursor object. + db created if successful + an error if unsuccessful ''' - - connection = self.connect(DATABASE_URL) - cursor = connection.cursor() - return cursor - - -if __name__ == "__main__": - db = PostgresConfig() - print(db.connect(DATABASE_URL)) + try: + self.cursordb.execute(query) + self.connection.commit() + return 'table created' + 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_table.py b/tests/integration/test_table.py new file mode 100644 index 00000000..65d145a0 --- /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 IF NOT EXISTS TESTTABLE + (testvalue int, + anothervalue int) + ''' + db = PostgresConfig() + db.connect() + a = db.create_table(query) + self.assertEqual(a,"table created")