forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
63 lines (49 loc) · 2.11 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pytest
from osf.models import Node
from osf.utils.migrations import disable_auto_now_fields
from osf_tests.factories import NodeFactory
pytestmark = pytest.mark.django_db
@pytest.fixture()
def node():
return NodeFactory()
class TestDisableAutoNowContextManager:
def test_auto_now_not_updated(self, node):
# update, save, confirm date changes
original_date_modified = node.modified
node.title = 'A'
node.save()
assert node.modified != original_date_modified
# update and save within context manager, confirm date doesn't change (i.e. auto_now was set to False)
new_date_modified = node.modified
with disable_auto_now_fields(models=[Node]):
node.title = 'AB'
node.save()
assert node.modified == new_date_modified
# update, save, confirm date changes (i.e. that auto_now was set back to True)
node.title = 'ABC'
node.save()
assert node.modified != new_date_modified
def test_auto_now_all_models_not_updated(self, node):
# update, save, confirm date changes
original_date_modified = node.modified
node.title = 'A'
node.save()
assert node.modified != original_date_modified
# update and save within context manager, confirm date doesn't change (i.e. auto_now was set to False)
new_date_modified = node.modified
with disable_auto_now_fields():
node.title = 'AB'
node.save()
assert node.modified == new_date_modified
# update, save, confirm date changes (i.e. that auto_now was set back to True)
node.title = 'ABC'
node.save()
assert node.modified != new_date_modified
def test_auto_now_does_not_modify_non_auto_now_fields(self, node):
old_created = node.created
assert Node._meta.get_field('created').auto_now is False
with disable_auto_now_fields(models=[Node]):
node.description = 'new cool description!!'
node.save()
assert node.created == old_created
assert Node._meta.get_field('created').auto_now is False