Skip to content

Commit

Permalink
Use transaction context manager and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Nov 22, 2013
1 parent 6463f6e commit 4123338
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
37 changes: 35 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,47 @@ def test_load_modified(self):
self.assertEqual(store2.user_id, 1)
self.assertEqual(store2.modified, True)

def test_duplicate_create(self):
s1 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
s1.create()
s2 = SessionStore('Python/2.7', '127.0.0.1', 'DUPLICATE')
s2.create()
self.assertNotEqual(s1.session_key, s2.session_key)

s3 = SessionStore('Python/2.7', '127.0.0.1', s1.session_key)
with self.assertRaises(CreateError):
s3.save(must_create=True)

def test_integrity(self):
self.store.user_agent = None
with self.assertRaisesMessage(IntegrityError,
'user_sessions_session.user_agent may '
'not be NULL'):
self.store.save()

def test_delete(self):
# not persisted, should just return
self.store.delete()

# create, then delete
self.store.create()
session_key = self.store.session_key
self.store.delete()

# non-existing sessions, should not raise
self.store.delete()
self.store.delete(session_key)


@skipUnless(geoip, geoip_msg)
class LocationTemplateFilterTest(TestCase):
@override_settings(GEOIP_PATH=None)
def test_no_location(self):
self.assertEqual(location('127.0.0.1'), '<i>unknown</i>')

def test_google(self):
@skipUnless(geoip, geoip_msg)
def test_locations(self):
self.assertEqual(location('8.8.8.8'), 'United States')
self.assertEqual(location('44.55.66.77'), 'San Diego, United States')


class DeviceTemplateFilterTest(TestCase):
Expand Down
5 changes: 2 additions & 3 deletions user_sessions/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ def save(self, must_create=False):
ip=self.ip,
)
using = router.db_for_write(Session, instance=obj)
sid = transaction.savepoint(using=using)
try:
obj.save(force_insert=must_create, using=using)
with transaction.commit_on_success(using):
obj.save(force_insert=must_create, using=using)
except IntegrityError as e:
if must_create and 'session_key' in str(e):
transaction.savepoint_rollback(sid, using=using)
raise CreateError
raise

Expand Down

0 comments on commit 4123338

Please sign in to comment.