Skip to content

Commit 259daf4

Browse files
committed
- test fixed
1 parent e64896c commit 259daf4

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ With this knowledge you can revert any changes made on selected model. Althrough
1313

1414
Example: how to use django-logging-middleware
1515
-------------------------------------------------------------
16-
simplest usage of logging is put something like this into your api
16+
simplest usage of logging middleware is put something like this into your api
1717
```python
1818
myobj = MyModel.objects.all()[0]
19-
with logging.Log(request, {"myobj": myobj}):
19+
with mlogging.Log(request, {"myobj": myobj}):
2020
myobj.someattribute = "another value"
2121
```
2222

@@ -27,7 +27,7 @@ or manually:
2727
old_obj = {"someattribute": myobj.someattribute, "id": myobj.someattribute}
2828
myobj.someattribute = "another value"
2929
new_obj = {"someattribute": myobj.someattribute, "id": myobj.someattribute}
30-
logging.add_changeset_entry(request, "my_action,
30+
mlogging.add_changeset_entry(request, "my_action,
3131
[ContentType.objects.get_for_model(MyModel)],
3232
{"myobj": old_obj},
3333
{"my_model": new_obj})
@@ -56,6 +56,6 @@ objects to prevent store excluded fields into db. By default context manager Log
5656
store all fields of objects
5757

5858
```python
59-
with logging.Log(request, {"myobj": myobj},
59+
with mlogging.Log(request, {"myobj": myobj},
6060
exclude_fields={"myobj": ["not_this field"]}):
6161
```
File renamed without changes.

mlogging/tests.py

+45-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import sys
22

33
from common.models import Arch
4-
import middle
5-
from middle import add_changeset_entry
4+
import middleware
5+
from middleware import add_changeset_entry
66
from models import Logging, ChangeSet, ChangeSetEntry
77

8+
from django.core import management
9+
from django.core.management import sql, color
10+
from django.db import connection, models
811
from django.test import TestCase
912
from django.test.client import RequestFactory
1013
from django.core.handlers.base import BaseHandler
@@ -35,23 +38,48 @@ def user_login(test_case, name, pswd, **request):
3538

3639

3740
class LoggingTest(TestCase):
41+
def make_model(self):
42+
self.model = type('DummyModel', (models.Model,), {
43+
'dummy_attr': models.CharField(max_length=255),
44+
"__module__": "mlogging.tests"})
45+
46+
self._style = color.no_style()
47+
48+
self._cursor = connection.cursor()
49+
statements, pending = connection.creation.sql_create_model(self.model,
50+
self._style)
51+
for statement in statements:
52+
self._cursor.execute(statement)
53+
ContentType.objects.get_for_model(self.model).save()
54+
3855
def setUp(self):
3956
test_user = User.objects.create_user("test_user", "[email protected]")
4057
test_user.set_password("test_pass")
4158
test_user.save()
4259
self.user = authenticate(username="test_user", password="test_pass")
4360
self.rf = RequestMock()
44-
arch = Arch.objects.create(name="dummy arch")
45-
arch.save()
46-
self.l_instance = middle.LoggingMiddleware()
61+
self.make_model()
62+
#self.model.save(self.model)
63+
dm = self.model.objects.create(dummy_attr="some value")
64+
65+
dm.save()
66+
67+
self.l_instance = middleware.LoggingMiddleware()
68+
69+
def tearDown(self):
70+
statements = connection.creation.sql_destroy_model(self.model, (),
71+
self._style)
72+
for statement in statements:
73+
self._cursor.execute(statement)
4774

48-
def test_middle(self):
75+
def test_middle_x(self):
4976
request = self.rf.request(REQUEST_METHOD='POST')
5077
login(request, self.user)
5178
self.l_instance.process_request(request)
52-
arch = Arch.objects.all()[0]
53-
with middle.Log(request, {"arch": arch}):
54-
arch.name = "silly arch"
79+
dummy_o = self.model.objects.all()[0]
80+
#print >> sys.stderr, dummy_o
81+
with middleware.Log(request, {"dummy_o": dummy_o}):
82+
dummy_o.dummy_attr = "silly value"
5583

5684
response = lambda x: x
5785
response.status_code = 200
@@ -69,20 +97,20 @@ def test_middle(self):
6997
self.assertEqual(entries[0].changeset, changesets[0])
7098
self.assertEqual(changesets[0].user, self.user)
7199
self.assertEqual(entries[0].model,
72-
ContentType.objects.get_for_model(Arch))
100+
ContentType.objects.get_for_model(self.model))
73101

74102
def test_middle_manual(self):
75103
request = self.rf.request(REQUEST_METHOD='POST')
76104
login(request, self.user)
77105
self.l_instance.process_request(request)
78-
arch = Arch.objects.all()[0]
106+
dummy_o = self.model.objects.all()[0]
79107

80-
old_arch = {"id": arch.id, "name": arch.name}
81-
arch.name = "silly arch"
82-
new_arch = {"id": arch.id, "name": arch.name}
108+
old_dummy = {"id": dummy_o.id, "dummy_attr": dummy_o.dummy_attr}
109+
dummy_o.dummy_attr = "new dummy value"
110+
new_dummy = {"id": dummy_o.id, "dummy_attr": dummy_o.dummy_attr}
83111
add_changeset_entry(request, "test_middle_manual",
84-
[ContentType.objects.get_for_model(Arch)],
85-
{"arch": old_arch}, {"arch": new_arch})
112+
[ContentType.objects.get_for_model(dummy_o._meta.model)],
113+
{"dummy_o": old_dummy}, {"dummy_o": new_dummy})
86114

87115
response = lambda x: x
88116
response.status_code = 200
@@ -100,4 +128,4 @@ def test_middle_manual(self):
100128
self.assertEqual(entries[0].changeset, changesets[0])
101129
self.assertEqual(changesets[0].user, self.user)
102130
self.assertEqual(entries[0].model,
103-
ContentType.objects.get_for_model(Arch))
131+
ContentType.objects.get_for_model(self.model))

0 commit comments

Comments
 (0)