-
Notifications
You must be signed in to change notification settings - Fork 420
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
enable use of replica database #359
Changes from 2 commits
6c70783
be4467f
80743b7
7dbe42f
240364e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||
import datetime | ||||
import json | ||||
|
||||
import django | ||||
from dateutil.tz import gettz | ||||
|
@@ -10,7 +11,9 @@ | |||
from django.http import HttpResponse | ||||
from django.test import RequestFactory, TestCase | ||||
from django.utils import dateformat, formats, timezone | ||||
from django.utils.connection import ConnectionDoesNotExist | ||||
|
||||
from auditlog.diff import model_instance_diff | ||||
from auditlog.middleware import AuditlogMiddleware | ||||
from auditlog.models import LogEntry | ||||
from auditlog.registry import auditlog | ||||
|
@@ -920,3 +923,31 @@ def test_no_delete_related(self): | |||
list(entries.values_list("action", flat=True)), | ||||
[LogEntry.Action.CREATE, LogEntry.Action.UPDATE, LogEntry.Action.DELETE], | ||||
) | ||||
|
||||
|
||||
class ModelFromDifferentDatabase(TestCase): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need a new class, you can add this test to django-auditlog/auditlog_tests/tests.py Line 36 in 665217d
|
||||
""" | ||||
Should use the default database even when object is from other database (read-only, for example) | ||||
""" | ||||
def setUp(self): | ||||
self.obj = SimpleModel.objects.create(text="42 is the answer") | ||||
|
||||
def test_create_log_to_object_from_other_database(self): | ||||
msg = "The log should not try to write to the same database as the object" | ||||
|
||||
instance = self.obj | ||||
instance._state.db = 'replica' # simulate object obtained from a different database (read only) | ||||
|
||||
changes = model_instance_diff(None, instance) | ||||
|
||||
try: | ||||
log_entry = LogEntry.objects.log_create( | ||||
instance, | ||||
action=LogEntry.Action.CREATE, | ||||
changes=json.dumps(changes), | ||||
) | ||||
self.assertEqual(log_entry._state.db, "default", msg=msg) # must be created in default database | ||||
except ConnectionDoesNotExist as e: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this except block? |
||||
self.assertTrue(False, msg=msg) | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please place in a new section above because this section belongs to the already released version 1.0.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please mention
LogEntry no longer save to same database instance is using