Skip to content

Commit a3d7992

Browse files
committed
Improve some tests, Remove extra line of code
1 parent 037ce43 commit a3d7992

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

qsessions/backends/common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class QSessionStoreMixin(object):
1010
- User Agent
1111
"""
1212
def __init__(self, session_key=None, user_agent=None, ip=None):
13-
self.modified = False
1413
self.user_agent = user_agent[:300] if user_agent else user_agent
1514
self.ip = ip
1615
super(QSessionStoreMixin, self).__init__(session_key)

tests/test_clearsessions.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
@pytest.mark.django_db
1010
def test_can_call():
1111
Session.objects.create(
12-
expire_date=datetime.now() - timedelta(days=1),
12+
session_key='s1',
13+
expire_date=datetime.now() + timedelta(hours=1),
1314
ip='127.0.0.1',
1415
)
15-
assert Session.objects.count() == 1
16+
Session.objects.create(
17+
session_key='s2',
18+
expire_date=datetime.now() - timedelta(hours=1),
19+
ip='127.0.0.1',
20+
)
21+
assert Session.objects.count() == 2
1622
call_command('clearsessions')
17-
assert Session.objects.count() == 0
23+
assert Session.objects.count() == 1

tests/test_sessionstore.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.sessions.backends.base import CreateError
77
from django.utils.timezone import now
88

9+
from qsessions import IP_SESSION_KEY, USER_AGENT_SESSION_KEY
910
from qsessions.models import Session
1011

1112
SessionStore = Session.get_session_store_class()
@@ -19,6 +20,17 @@ def setup_store():
1920
def test_untouched_init(store):
2021
assert store.modified is False
2122
assert store.accessed is False
23+
assert store.get('cat') is None
24+
25+
26+
def test_store(store):
27+
store['cat'] = 'dog'
28+
assert store.accessed is True
29+
assert store.modified is True
30+
assert 'cat' in store
31+
assert store.pop('cat') == 'dog'
32+
assert 'cat' not in store
33+
assert store.get('cat') is None
2234

2335

2436
def test_auth_session_key(store):
@@ -56,8 +68,8 @@ def test_load_unmodified(store, django_user_model):
5668
store2 = SessionStore(session_key=store.session_key,
5769
user_agent='TestUA/1.1', ip='127.0.0.1')
5870
store2.load()
59-
assert store2.user_agent == 'TestUA/1.1'
60-
assert store2.ip == '127.0.0.1'
71+
assert store2.get(USER_AGENT_SESSION_KEY) == 'TestUA/1.1'
72+
assert store2.get(IP_SESSION_KEY) == '127.0.0.1'
6173
assert store2.get(auth.SESSION_KEY) == 1
6274
assert store2.modified is False
6375

@@ -69,13 +81,18 @@ def test_load_modified(store, django_user_model):
6981
store[auth.SESSION_KEY] = 1
7082
store.save()
7183
store2 = SessionStore(session_key=store.session_key,
72-
user_agent='TestUA/1.1', ip='8.8.8.8')
84+
user_agent='TestUA/1.1-changed', ip='8.8.8.8')
7385
store2.load()
74-
assert store2.user_agent == 'TestUA/1.1'
75-
assert store2.ip == '8.8.8.8'
86+
assert store2.get(USER_AGENT_SESSION_KEY) == 'TestUA/1.1'
87+
assert store2.get(IP_SESSION_KEY) == '127.0.0.1'
7688
assert store2.get(auth.SESSION_KEY) == 1
7789
assert store2.modified is True
7890

91+
store2.save()
92+
93+
assert store2.get(USER_AGENT_SESSION_KEY) == 'TestUA/1.1-changed'
94+
assert store2.get(IP_SESSION_KEY) == '8.8.8.8'
95+
7996

8097
@pytest.mark.django_db
8198
def test_duplicate_create():

0 commit comments

Comments
 (0)