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

GitAuto: [FEATURE] Implement Database wrapper #205

Closed
16 changes: 16 additions & 0 deletions docs/database_wrapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Database Wrapper Documentation

## Overview

The Database Wrapper provides an abstraction layer over the database operations, allowing for simplified and consistent interaction with the database.

## Configuration

The wrapper can be configured using environment variables or a configuration dictionary passed during initialization.

## Usage

1. **Initialization**: Create an instance of the `DatabaseWrapper` with the necessary configuration.
2. **Connection**: Use the `connect` method to establish a connection to the database.
3. **Operations**: Execute queries and fetch results using `execute_query` and `fetch_results` methods.
4. **Disconnection**: Use the `disconnect` method to close the connection.
47 changes: 47 additions & 0 deletions src/DatabaseWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class DatabaseWrapperInterface:
def connect(self):
raise NotImplementedError("Connect method not implemented.")

def disconnect(self):
raise NotImplementedError("Disconnect method not implemented.")

def execute_query(self, query):
raise NotImplementedError("Execute query method not implemented.")

def fetch_results(self):
raise NotImplementedError("Fetch results method not implemented.")


class DatabaseWrapper(DatabaseWrapperInterface):
def __init__(self, config):
self.config = config
self.connection = None

def connect(self):
# Implement connection logic using a database library
pass

def disconnect(self):
# Implement disconnection logic
pass

def execute_query(self, query):
# Implement query execution logic
pass

def fetch_results(self):
# Implement results fetching logic
pass

def _handle_error(self, error):
# Implement error handling logic
pass

def _manage_transaction(self):
# Implement transaction management logic
pass

@staticmethod
def from_env():
# Create a DatabaseWrapper instance using environment variables
pass
40 changes: 40 additions & 0 deletions tests/test_database_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from src.DatabaseWrapper import DatabaseWrapper

class TestDatabaseWrapper(unittest.TestCase):
def setUp(self):
self.config = {
'host': 'localhost',
'port': 5432,
'user': 'user',
'password': 'password',
'database': 'test_db'
}
self.wrapper = DatabaseWrapper(self.config)

def test_connect(self):
# Test the connect method
self.wrapper.connect()
self.assertIsNotNone(self.wrapper.connection)

def test_disconnect(self):
# Test the disconnect method
self.wrapper.connect()
self.wrapper.disconnect()
self.assertIsNone(self.wrapper.connection)

def test_execute_query(self):
# Test the execute_query method
self.wrapper.connect()
result = self.wrapper.execute_query("SELECT 1")
self.assertEqual(result, 1)

def test_fetch_results(self):
# Test the fetch_results method
self.wrapper.connect()
self.wrapper.execute_query("SELECT 1")
results = self.wrapper.fetch_results()
self.assertEqual(results, [1])

if __name__ == '__main__':
unittest.main()
Loading