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

Feature Create Table #25

Open
wants to merge 17 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
27 changes: 11 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@

language: python
python:
- '3.7.3'
- "3.7"
- "nightly"
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

script:
install:
- "pip install pytest"
- "pip install pytest-cov"
- "pip install coveralls"
- "pip install -r requirements.txt"

script:
- "python -m pytest tests/"
- "python -m pytest --cov tests/"

after_success:
- coveralls

cache: pip
- coveralls
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[![Build Status](https://travis-ci.com/icodeai/nbox.svg?branch=databases_postgres)](https://travis-ci.com/icodeai/nbox) [![Coverage Status](https://coveralls.io/repos/github/icodeai/nbox/badge.svg?branch=databases_postgres)](https://coveralls.io/github/icodeai/nbox?branch=databases_postgres)

[![Build Status](https://travis-ci.org/Joseorina/nbox.svg?branch=Ft-Connect_Db)](https://travis-ci.org/Joseorina/nbox)
# Nbox
ML Toolkit
Empty file added __init__.py
Empty file.
Binary file added __pycache__/config.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/database.cpython-37.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from configparser import ConfigParser

def config(filename='db.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('section {} not found in the {} file'.format(section, filename))
return db
5 changes: 5 additions & 0 deletions db.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[postgresql]
host=localhost
database=suppliers
user=postgres
password=toor
52 changes: 0 additions & 52 deletions db_config.py

This file was deleted.

113 changes: 113 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from database import Postgres
import psycopg2
from config import config

class ConnectToDb():
pass

class PostgresDb(Postgres):
"""
COnnect to the postgresql database

Arguments:
Postgres {[type]} -- [description]

Returns:
[type] -- [description]
"""
def connect():
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.close
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return 'success'

def session():
pass

def create_database():
pass

def status():
pass

def cursor():
"""
Create a cursor area in memory where SQL statement
are executed

Arguments:
query {[type]} -- [description]
"""
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
return 'success'

def select_table(query):
"""
Fetch and display records from database

Arguments:
query {[type]} -- [description]
"""
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute(query)
rows = cur.fetchall()
conn.close()
return 'success'

def create_table(query):
"""
Create a table in a database

Arguments:pi
query {[type]} -- [description]

Returns:
[type] -- [description]
"""
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute(query)
conn.commit()
conn.close()
return 'success'

def insert_rows(query):
"""
Funtion to insert records into a database

Arguments:
query {[type]} -- [description]
"""


def show_table(query):
pass

def drop_table(query):
pass

def close():
pass

if __name__ == '__main__':
a = PostgresDb
a.connect()
a.select_table("SELECT id, name, address, salary from COMPANY")
a.create_table('''CREATE TABLE TEST (ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
44 changes: 44 additions & 0 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
from main import PostgresDb
from config import config

a = PostgresDb
class TestPostgres(unittest.TestCase):
"""
Test database connectivity

Arguments:
unittest {[type]} -- [description]
"""
def test_connection(self):
"""
Test that the database connection is succesful
"""
self.assertEqual('success', a.connect())

def test_cursor(self):
"""
Test cursor function
"""
self.assertEqual('success', a.cursor())

def test_select_table(self):
"""
Test fetch and display of records
"""
b = "SELECT id, name, address, salary from COMPANY"
self.assertEqual('success', a.select_table(b))

def test_create_table(self):
"""
Test function to create a table
"""
b = ('''CREATE TABLE TEST (ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
self.assertEqual('success', a.create_table(b))

if __name__ == '__main__':
unittest.main()
20 changes: 0 additions & 20 deletions tests/integration/test_postgres.py

This file was deleted.

Empty file added tests/system/test_sys.py
Empty file.
Empty file added tests/unit/test_unit_main.py
Empty file.