Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ft create table #48

Open
wants to merge 11 commits into
base: databases_postgres
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
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
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
- coveralls
8 changes: 4 additions & 4 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ class Postgres(ABC):
def connect(self,database_url):
pass

# @abstractmethod
# @abstractmethod
def session(self,):
autocommit = True
pass

# @abstractmethod
#@abstractmethod
def create_database(self):
pass

# @abstractmethod
def status(self):
pass

@abstractmethod
# @abstractmethod
def cursor(self):
pass

# @abstractmethod
def select_table(self,query):
pass

# @abstractmethod
@abstractmethod
def create_table(self,query):
pass

Expand Down
52 changes: 26 additions & 26 deletions db_config.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

20 changes: 0 additions & 20 deletions tests/integration/test_postgres.py

This file was deleted.

16 changes: 16 additions & 0 deletions tests/integration/test_table.py
Original file line number Diff line number Diff line change
@@ -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")