-
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.
feat: add test and django structure for test
- Loading branch information
Hamzeh Pourmahdi
committed
Sep 3, 2024
1 parent
8f4d433
commit 0c61583
Showing
9 changed files
with
181 additions
and
17 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,3 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = tests.test_settings | ||
python_files = tests.py test_*.py *_tests.py |
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,15 @@ | ||
from mongoengine import connect | ||
|
||
db_name = "django_mongoengine_logger" | ||
username = "root" | ||
password = "rootpassword" | ||
host = "localhost" # e.g., 'localhost' or 'mongodb+srv://cluster0.mongodb.net' | ||
|
||
connect( | ||
db=db_name, | ||
username=username, | ||
password=password, | ||
host=host, | ||
# port=27017, # Uncomment and set if you need to specify a port | ||
uuidRepresentation="standard", | ||
) |
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,15 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.test_settings") | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) |
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 |
---|---|---|
@@ -1,22 +1,25 @@ | ||
import json | ||
import logging | ||
|
||
from django.test import TestCase | ||
import pytest | ||
|
||
from django_mongoengine_logger.documents import LogDocument | ||
from django_mongoengine_logger.logging import MongoDBHandler | ||
|
||
|
||
class MongoDBHandlerTest(TestCase): | ||
def setUp(self): | ||
self.handler = MongoDBHandler( | ||
log_document_path="django_mongo_logger.documents.LogDocument" | ||
) | ||
self.logger = logging.getLogger("test_logger") | ||
self.logger.addHandler(self.handler) | ||
self.logger.setLevel(logging.DEBUG) | ||
@pytest.fixture | ||
def logger(): | ||
logger = logging.getLogger("mongo_log") | ||
return logger | ||
|
||
def test_log_message(self): | ||
self.logger.info("Test log message") | ||
log_entry = LogDocument.objects.first() | ||
self.assertIsNotNone(log_entry) | ||
self.assertEqual(log_entry.message, "Test log message") | ||
|
||
@pytest.mark.django_db | ||
def test_log_message(logger): | ||
logger.info("Test log message") | ||
log_entry = LogDocument.objects.first() | ||
assert log_entry is not None | ||
assert log_entry.message == "Test log message" | ||
|
||
logger.info(json.dumps({"message": "test"})) | ||
log_entry = LogDocument.objects.order_by("-id").first() # Get the last log entry | ||
assert log_entry.message["message"] == "test" |
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,99 @@ | ||
from pathlib import Path | ||
|
||
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent | ||
|
||
SECRET_KEY = "@xb6tkxjug(!yia#6x*0dxhfbx)=xm=wzwvrgbw7k(i07vx#-q" | ||
|
||
DEBUG = True | ||
|
||
USE_TZ = False | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
INSTALLED_APPS = [ | ||
"django.contrib.admin", | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"django.contrib.sessions", | ||
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"django_mongoengine_logger", # Include your package here | ||
] | ||
|
||
MIDDLEWARE = [ | ||
"django.middleware.security.SecurityMiddleware", | ||
"django.contrib.sessions.middleware.SessionMiddleware", | ||
"django.middleware.common.CommonMiddleware", | ||
"django.middleware.csrf.CsrfViewMiddleware", | ||
"django.contrib.auth.middleware.AuthenticationMiddleware", | ||
"django.contrib.messages.middleware.MessageMiddleware", | ||
"django.middleware.clickjacking.XFrameOptionsMiddleware", | ||
] | ||
|
||
ROOT_URLCONF = "tests.urls" | ||
|
||
TEMPLATES = [ | ||
{ | ||
"BACKEND": "django.template.backends.django.DjangoTemplates", | ||
"DIRS": [BASE_DIR / "templates"], | ||
"APP_DIRS": True, | ||
"OPTIONS": { | ||
"context_processors": [ | ||
"django.template.context_processors.debug", | ||
"django.template.context_processors.request", | ||
"django.contrib.auth.context_processors.auth", | ||
"django.contrib.messages.context_processors.messages", | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = "tests.wsgi.application" | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": BASE_DIR / "db.sqlite3", | ||
} | ||
} | ||
|
||
STATIC_URL = "/static/" | ||
|
||
|
||
LOGGING = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"verbose": { | ||
"format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s", | ||
}, | ||
"simple": { | ||
"format": "%(levelname)s %(message)s", | ||
}, | ||
}, | ||
"handlers": { | ||
"console": { | ||
"level": "DEBUG", | ||
"class": "logging.StreamHandler", | ||
"formatter": "verbose", | ||
}, | ||
"mongo": { | ||
"level": "DEBUG", | ||
"class": "django_mongoengine_logger.logging.MongoDBHandler", | ||
# path to you mongo engine log Document if not provided if not provided package will use default Document | ||
"log_document_path": "django_mongoengine_logger.documents.LogDocument", | ||
}, | ||
}, | ||
"loggers": { | ||
"django": { | ||
"handlers": ["console"], | ||
"level": "INFO", | ||
"propagate": True, | ||
}, | ||
"mongo_log": { | ||
"handlers": ["mongo"], | ||
"level": "DEBUG", | ||
"propagate": 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django.contrib import admin | ||
from django.urls import path | ||
|
||
urlpatterns = [] |