forked from mongodb-labs/django-mongodb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,136 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django_mongodb", | ||
"NAME": "test-default", | ||
}, | ||
"other": { | ||
"ENGINE": "django_mongodb", | ||
"NAME": "test-other", | ||
}, | ||
} | ||
DEFAULT_AUTO_FIELD = "django_mongodb.fields.MongoAutoField" | ||
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) | ||
SECRET_KEY = "django_tests_secret_key" | ||
USE_TZ = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ jobs: | |
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.8 | ||
python-version: '3.10' | ||
cache: 'pip' | ||
cache-dependency-path: 'pyproject.toml' | ||
- name: Install Python dependencies | ||
|
@@ -32,33 +32,46 @@ jobs: | |
pre-commit run --hook-stage=manual --all-files | ||
build: | ||
# supercharge/mongodb-github-action requires containers so we don't test other platforms | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-20.04] | ||
python-version: ["3.8", "3.11", "pypy-3.9"] | ||
fail-fast: false | ||
name: CPython ${{ matrix.python-version }}-${{ matrix.os }} | ||
name: Django Test Suite | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Python | ||
uses: actions/setup-python@v5 | ||
- name: Checkout django-mongodb | ||
uses: actions/checkout@v4 | ||
- name: install the django-mongodb backend | ||
run: | | ||
pip3 install --upgrade pip | ||
pip3 install -e . | ||
- name: Checkout Django | ||
uses: actions/checkout@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: 'pip' | ||
cache-dependency-path: 'pyproject.toml' | ||
- name: Install dependencies | ||
repository: 'timgraham/django' | ||
ref: 'mongodb-5.0.x' | ||
path: 'django_repo' | ||
- name: Install system packages for Django's Python test dependencies | ||
run: | | ||
pip install -U pip | ||
pip install -e ".[test]" | ||
sudo apt-get update | ||
sudo apt-get install libmemcached-dev | ||
- name: Install Django and its Python test dependencies | ||
run: | | ||
cd django_repo/tests/ | ||
pip3 install -e .. | ||
pip3 install -r requirements/py3.txt | ||
- name: Copy the test settings file | ||
run: cp .github/workflows/mongodb_settings.py django_repo/tests/ | ||
- name: Start MongoDB | ||
uses: supercharge/[email protected] | ||
with: | ||
mongodb-version: 4.4 | ||
- name: Run tests | ||
run: | | ||
pytest | ||
run: > | ||
python3 django_repo/tests/runtests.py --settings mongodb_settings -v 2 | ||
basic | ||
from_db_value | ||
model_fields.test_datetimefield | ||
model_fields.test_decimalfield | ||
model_fields.test_charfield | ||
model_fields.test_textfield | ||
or_lookups | ||
docs: | ||
name: Docs Checks | ||
|
@@ -69,8 +82,7 @@ jobs: | |
with: | ||
cache: 'pip' | ||
cache-dependency-path: 'pyproject.toml' | ||
# Build docs on lowest supported Python for furo | ||
python-version: '3.8' | ||
python-version: '3.10' | ||
- name: Install dependencies | ||
run: | | ||
pip install -U pip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__version__ = "5.0a0" | ||
|
||
# Check Django compatibility before other imports which may fail if the | ||
# wrong version of Django is installed. | ||
from .utils import check_django_compatability | ||
|
||
check_django_compatability() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import copy | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from django.db.backends.base.base import BaseDatabaseWrapper | ||
from django.db.backends.signals import connection_created | ||
from pymongo.collection import Collection | ||
from pymongo.mongo_client import MongoClient | ||
|
||
from . import dbapi as Database | ||
from .client import DatabaseClient | ||
from .creation import DatabaseCreation | ||
from .features import DatabaseFeatures | ||
from .introspection import DatabaseIntrospection | ||
from .operations import DatabaseOperations | ||
from .schema import DatabaseSchemaEditor | ||
|
||
|
||
class Cursor: | ||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, exception_type, exception_value, exception_traceback): | ||
pass | ||
|
||
|
||
class DatabaseWrapper(BaseDatabaseWrapper): | ||
data_types = { | ||
"AutoField": "int", | ||
"BigAutoField": "long", | ||
"BinaryField": "binData", | ||
"BooleanField": "bool", | ||
"CharField": "string", | ||
"DateField": "date", | ||
"DateTimeField": "date", | ||
"DecimalField": "decimal", | ||
"DurationField": "long", | ||
"FileField": "string", | ||
"FilePathField": "string", | ||
"FloatField": "double", | ||
"IntegerField": "int", | ||
"BigIntegerField": "long", | ||
"GenericIPAddressField": "string", | ||
"NullBooleanField": "bool", | ||
"OneToOneField": "int", | ||
"PositiveIntegerField": "long", | ||
"PositiveSmallIntegerField": "int", | ||
"SlugField": "string", | ||
"SmallIntegerField": "int", | ||
"TextField": "string", | ||
"TimeField": "date", | ||
"UUIDField": "string", | ||
} | ||
|
||
vendor = "mongodb" | ||
SchemaEditorClass = DatabaseSchemaEditor | ||
Database = Database | ||
|
||
client_class = DatabaseClient | ||
creation_class = DatabaseCreation | ||
features_class = DatabaseFeatures | ||
introspection_class = DatabaseIntrospection | ||
ops_class = DatabaseOperations | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.connected = False | ||
del self.connection | ||
|
||
def get_collection(self, name, **kwargs): | ||
return Collection(self.database, name, **kwargs) | ||
|
||
def __getattr__(self, attr): | ||
if attr in ["connection", "database"]: | ||
assert not self.connected | ||
self._connect() | ||
return getattr(self, attr) | ||
raise AttributeError(attr) | ||
|
||
def _connect(self): | ||
settings = copy.deepcopy(self.settings_dict) | ||
|
||
def pop(name, default=None): | ||
return settings.pop(name) or default | ||
|
||
db_name = pop("NAME") | ||
host = pop("HOST") | ||
port = pop("PORT", 27017) | ||
user = pop("USER") | ||
password = pop("PASSWORD") | ||
options = pop("OPTIONS", {}) | ||
|
||
self.operation_flags = options.pop("OPERATIONS", {}) | ||
if not any(k in ["save", "delete", "update"] for k in self.operation_flags): | ||
# Flags apply to all operations. | ||
flags = self.operation_flags | ||
self.operation_flags = {"save": flags, "delete": flags, "update": flags} | ||
|
||
conn_options = { | ||
"host": host, | ||
"port": int(port), | ||
"document_class": dict, | ||
"tz_aware": False, | ||
} | ||
conn_options.update(options) | ||
|
||
self.connection = MongoClient(**conn_options) | ||
if db_name: | ||
self.database = self.connection[db_name] | ||
|
||
if user and password and not self.database.authenticate(user, password): | ||
raise ImproperlyConfigured("Invalid username or password.") | ||
|
||
self.connected = True | ||
connection_created.send(sender=self.__class__, connection=self) | ||
|
||
def _reconnect(self): | ||
if self.connected: | ||
del self.connection | ||
del self.database | ||
self.connected = False | ||
self._connect() | ||
|
||
def _commit(self): | ||
pass | ||
|
||
def _rollback(self): | ||
pass | ||
|
||
def close(self): | ||
pass | ||
|
||
def cursor(self): | ||
return Cursor() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import signal | ||
|
||
from django.db.backends.base.client import BaseDatabaseClient | ||
|
||
|
||
class DatabaseClient(BaseDatabaseClient): | ||
executable_name = "mongo" | ||
|
||
@classmethod | ||
def settings_to_cmd_args_env(cls, settings_dict, parameters): | ||
raise NotImplementedError | ||
|
||
def runshell(self, parameters): | ||
sigint_handler = signal.getsignal(signal.SIGINT) | ||
try: | ||
# Allow SIGINT to pass to mongo to abort queries. | ||
signal.signal(signal.SIGINT, signal.SIG_IGN) | ||
super().runshell(parameters) | ||
finally: | ||
# Restore the original SIGINT handler. | ||
signal.signal(signal.SIGINT, sigint_handler) |
Oops, something went wrong.