db-sdk 4.0.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
db_sdk-4.0.12/PKG-INFO ADDED
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.4
2
+ Name: db_sdk
3
+ Version: 4.0.12
4
+ Summary: A simple database connection SDK
5
+ Author: Neha
6
+ Requires-Python: >=3.8
7
+ Requires-Dist: pymongo>=4.0.0
8
+ Requires-Dist: mysql-connector-python>=8.0
9
+ Requires-Dist: psycopg2-binary>=2.9.0
10
+ Requires-Dist: redis>=4.0.0
11
+ Requires-Dist: pytest>=7.0.0
12
+ Requires-Dist: pytest-cov>=4.0.0
13
+ Requires-Dist: mongomock>=4.1.2
14
+ Dynamic: author
15
+ Dynamic: requires-dist
16
+ Dynamic: requires-python
17
+ Dynamic: summary
File without changes
@@ -0,0 +1,19 @@
1
+ import os
2
+ from db_sdk.core.exceptions import ConfigurationError
3
+
4
+
5
+ def load_config_from_env():
6
+ db_type = os.getenv("DB_TYPE")
7
+
8
+ if not db_type:
9
+ raise ConfigurationError("DB_TYPE environment variable not set.")
10
+
11
+ return {
12
+ "db_type": db_type,
13
+ "host": os.getenv("DB_HOST"),
14
+ "user": os.getenv("DB_USER"),
15
+ "password": os.getenv("DB_PASSWORD"),
16
+ "database": os.getenv("DB_NAME"),
17
+ "port": os.getenv("DB_PORT"),
18
+ "uri": os.getenv("DB_URI"),
19
+ }
File without changes
File without changes
File without changes
@@ -0,0 +1,15 @@
1
+ from pymongo import MongoClient
2
+ from ..core.base_connector import BaseConnector
3
+ from ..core.exceptions import DatabaseConnectionError
4
+
5
+
6
+ class MongoConnector(BaseConnector):
7
+
8
+ def connect(self):
9
+ try:
10
+ uri = self.config.get("uri")
11
+ client = MongoClient(uri)
12
+ return client
13
+
14
+ except Exception as e:
15
+ raise DatabaseConnectionError(f"MongoDB connection failed: {e}")
@@ -0,0 +1,20 @@
1
+ import mysql.connector
2
+ from ..core.base_connector import BaseConnector
3
+ from ..core.exceptions import DatabaseConnectionError
4
+
5
+
6
+ class MySQLConnector(BaseConnector):
7
+
8
+ def connect(self):
9
+ try:
10
+ connection = mysql.connector.connect(
11
+ host=self.config.get("host"),
12
+ user=self.config.get("user"),
13
+ password=self.config.get("password"),
14
+ database=self.config.get("database"),
15
+ port=self.config.get("port", 3306),
16
+ )
17
+ return connection
18
+
19
+ except mysql.connector.Error as e:
20
+ raise DatabaseConnectionError(f"MySQL connection failed: {e}")
@@ -0,0 +1,20 @@
1
+ import psycopg2
2
+ from ..core.base_connector import BaseConnector
3
+ from ..core.exceptions import DatabaseConnectionError
4
+
5
+
6
+ class PostgresConnector(BaseConnector):
7
+
8
+ def connect(self):
9
+ try:
10
+ connection = psycopg2.connect(
11
+ host=self.config.get("host"),
12
+ user=self.config.get("user"),
13
+ password=self.config.get("password"),
14
+ dbname=self.config.get("database"),
15
+ port=self.config.get("port", 5432),
16
+ )
17
+ return connection
18
+
19
+ except psycopg2.Error as e:
20
+ raise DatabaseConnectionError(f"PostgreSQL connection failed: {e}")
@@ -0,0 +1,20 @@
1
+ import redis
2
+ from ..core.base_connector import BaseConnector
3
+ from ..core.exceptions import DatabaseConnectionError
4
+
5
+
6
+ class RedisConnector(BaseConnector):
7
+
8
+ def connect(self):
9
+ try:
10
+ connection = redis.Redis(
11
+ host=self.config.get("host"),
12
+ port=self.config.get("port", 6379),
13
+ password=self.config.get("password"),
14
+ decode_responses=True,
15
+ )
16
+ connection.ping() # Test connection
17
+ return connection
18
+
19
+ except redis.RedisError as e:
20
+ raise DatabaseConnectionError(f"Redis connection failed: {e}")
File without changes
@@ -0,0 +1,11 @@
1
+ class BaseConnector:
2
+ """
3
+ Base connector class.
4
+ All database connectors must inherit from this.
5
+ """
6
+
7
+ def __init__(self, config: dict):
8
+ self.config = config
9
+
10
+ def connect(self):
11
+ raise NotImplementedError("Connect method must be implemented by subclass.")
@@ -0,0 +1,18 @@
1
+ class UniversalDBException(Exception):
2
+ """Base exception for Universal DB SDK."""
3
+ pass
4
+
5
+
6
+ class UnsupportedDatabaseError(UniversalDBException):
7
+ """Raised when unsupported database type is provided."""
8
+ pass
9
+
10
+
11
+ class DatabaseConnectionError(UniversalDBException):
12
+ """Raised when connection fails."""
13
+ pass
14
+
15
+
16
+ class ConfigurationError(UniversalDBException):
17
+ """Raised when configuration is invalid."""
18
+ pass
@@ -0,0 +1,30 @@
1
+ from db_sdk.connectors.mysql import MySQLConnector
2
+ from db_sdk.connectors.postgres import PostgresConnector
3
+ from db_sdk.connectors.mongodb import MongoConnector
4
+ from db_sdk.connectors.redis import RedisConnector
5
+ # from db_sdk.core.exceptions import UnsupportedDatabaseError
6
+ from .exceptions import UnsupportedDatabaseError
7
+
8
+
9
+ class ConnectionFactory:
10
+
11
+ CONNECTOR_MAPPING = {
12
+ "mysql": MySQLConnector,
13
+ "postgres": PostgresConnector,
14
+ "mongodb": MongoConnector,
15
+ "redis": RedisConnector,
16
+ }
17
+
18
+ @staticmethod
19
+ def create_connection(db_type: str, config: dict):
20
+ db_type = db_type.lower()
21
+
22
+ if db_type not in ConnectionFactory.CONNECTOR_MAPPING:
23
+ raise UnsupportedDatabaseError(
24
+ f"Unsupported database type: {db_type}"
25
+ )
26
+
27
+ connector_class = ConnectionFactory.CONNECTOR_MAPPING[db_type]
28
+ connector = connector_class(config)
29
+
30
+ return connector.connect()
@@ -0,0 +1,15 @@
1
+ from db_sdk.core.factory import ConnectionFactory
2
+
3
+ config = {
4
+ "host": "192.168.137.9",
5
+ "user": "pwf",
6
+ "password": "JqYVmy5uPTzG",
7
+ "database": "pwf",
8
+ "port": 3306
9
+ }
10
+
11
+ try:
12
+ connection = ConnectionFactory.create_connection("mysql", config)
13
+ print("✅ Database connected successfully!")
14
+ except Exception as e:
15
+ print("❌ Connection failed:", e)
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.4
2
+ Name: db_sdk
3
+ Version: 4.0.12
4
+ Summary: A simple database connection SDK
5
+ Author: Neha
6
+ Requires-Python: >=3.8
7
+ Requires-Dist: pymongo>=4.0.0
8
+ Requires-Dist: mysql-connector-python>=8.0
9
+ Requires-Dist: psycopg2-binary>=2.9.0
10
+ Requires-Dist: redis>=4.0.0
11
+ Requires-Dist: pytest>=7.0.0
12
+ Requires-Dist: pytest-cov>=4.0.0
13
+ Requires-Dist: mongomock>=4.1.2
14
+ Dynamic: author
15
+ Dynamic: requires-dist
16
+ Dynamic: requires-python
17
+ Dynamic: summary
@@ -0,0 +1,22 @@
1
+ README.md
2
+ setup.py
3
+ db_sdk/__init__.py
4
+ db_sdk/test_connection.py
5
+ db_sdk.egg-info/PKG-INFO
6
+ db_sdk.egg-info/SOURCES.txt
7
+ db_sdk.egg-info/dependency_links.txt
8
+ db_sdk.egg-info/requires.txt
9
+ db_sdk.egg-info/top_level.txt
10
+ db_sdk/config/__init__.py
11
+ db_sdk/config/settings.py
12
+ db_sdk/connectors/__init__.py
13
+ db_sdk/connectors/mongodb.py
14
+ db_sdk/connectors/mysql.py
15
+ db_sdk/connectors/postgres.py
16
+ db_sdk/connectors/redis.py
17
+ db_sdk/core/__init__.py
18
+ db_sdk/core/base_connector.py
19
+ db_sdk/core/exceptions.py
20
+ db_sdk/core/factory.py
21
+ tests/test_connection.py
22
+ tests/test_connection_1.py
@@ -0,0 +1,7 @@
1
+ pymongo>=4.0.0
2
+ mysql-connector-python>=8.0
3
+ psycopg2-binary>=2.9.0
4
+ redis>=4.0.0
5
+ pytest>=7.0.0
6
+ pytest-cov>=4.0.0
7
+ mongomock>=4.1.2
@@ -0,0 +1 @@
1
+ db_sdk
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
db_sdk-4.0.12/setup.py ADDED
@@ -0,0 +1,21 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="db_sdk",
5
+ version="4.0.12",
6
+ packages=find_packages(),
7
+ install_requires=[
8
+ "pymongo>=4.0.0",
9
+ "mysql-connector-python>=8.0",
10
+ "psycopg2-binary>=2.9.0",
11
+ "redis>=4.0.0",
12
+ "pytest>=7.0.0",
13
+ "pytest-cov>=4.0.0",
14
+ "mongomock>=4.1.2",
15
+ ],
16
+ author="Neha",
17
+ description="A simple database connection SDK",
18
+ python_requires=">=3.8",
19
+ )
20
+
21
+ # Comment: This setup.py file defines the package metadata and dependencies for the auth-sdk. It specifies that the package requires Python 3.8 or higher and depends on bcrypt, PyJWT, and python-dotenv. The package is named "auth-sdk" and is currently at version 0.1.0.
@@ -0,0 +1,15 @@
1
+ from db_sdk.core.factory import ConnectionFactory
2
+
3
+ config = {
4
+ "host": "192.168.137.9",
5
+ "user": "pwf",
6
+ "password": "JqYVmy5uPTzG",
7
+ "database": "pwf",
8
+ "port": 3306
9
+ }
10
+
11
+ try:
12
+ connection = ConnectionFactory.create_connection("mysql", config)
13
+ print("✅ Database connected successfully!")
14
+ except Exception as e:
15
+ print("❌ Connection failed:", e)
@@ -0,0 +1,37 @@
1
+ from unittest.mock import patch, MagicMock
2
+ from db_sdk.connectors.mongodb import MongoConnector
3
+
4
+ def test_factory_creates_mongo_connection():
5
+ print("\n--- Starting test ---")
6
+
7
+ # Step 1: Create a fake MongoDB client
8
+ mock_client = MagicMock()
9
+ print(f"Mock client created: {mock_client}")
10
+
11
+ # Step 2: Patch MongoClient so no real connection is made
12
+ with patch("db_sdk.connectors.mongodb.MongoClient", return_value=mock_client) as mock_mongo:
13
+ print(f"MongoClient is now patched: {mock_mongo}")
14
+
15
+ # Step 3: Create the connector with a fake URI
16
+ config = {"uri": "mongodb://fake-host:9999"}
17
+ connector = MongoConnector(config)
18
+ print(f"Connector created: {connector}")
19
+ print(f"Connector config: {connector.config}")
20
+
21
+ # Step 4: Call connect() - this triggers MongoClient internally
22
+ client = connector.connect()
23
+ print(f"connect() was called, returned: {client}")
24
+
25
+ # Step 5: Verify MongoClient was called with the fake URI
26
+ mock_mongo.assert_called_once_with("mongodb://fake-host:9999")
27
+ print(f"MongoClient was called with URI: {mock_mongo.call_args}")
28
+
29
+ # Step 6: Verify the returned client is our mock
30
+ assert client is mock_client
31
+ print(f"Returned client matches mock_client: {client is mock_client}")
32
+
33
+ print("--- Test passed ---")
34
+
35
+
36
+ if __name__ == "__main__":
37
+ test_factory_creates_mongo_connection()