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 database obed #37

Open
wants to merge 5 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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
2 changes: 1 addition & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def session(self,):
autocommit = True
pass

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

Expand Down
34 changes: 31 additions & 3 deletions db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def connect(self,database_url):
try:

connection = p.connect(database_url)
connection.autocommit = True
return connection

except:
Expand All @@ -41,12 +42,39 @@ 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):
"""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:

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'))
6 changes: 6 additions & 0 deletions tests/integration/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)