-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a transaction context manager to backend (#2387)
This allows to group operations that will be rolled back if the context is exited with an exception. This is laying the groundwork for implementing `Node` as part of the new backend system as links, caches, etc will have to be done in a transaction.
- Loading branch information
Showing
7 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
"""Unit tests for the ORM Backend class.""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import absolute_import | ||
from aiida.backends.testbase import AiidaTestCase | ||
from aiida import orm | ||
from aiida.common import exceptions | ||
|
||
|
||
class TestBackend(AiidaTestCase): | ||
"""Test backend.""" | ||
|
||
def test_transaction_nesting(self): | ||
"""Test that transaction nesting works.""" | ||
user = orm.User('initial@email.com').store() | ||
with self.backend.transaction(): | ||
user.email = 'pre-failure@email.com' | ||
try: | ||
with self.backend.transaction(): | ||
user.email = 'failure@email.com' | ||
self.assertEqual(user.email, 'failure@email.com') | ||
raise RuntimeError | ||
except RuntimeError: | ||
pass | ||
self.assertEqual(user.email, 'pre-failure@email.com') | ||
self.assertEqual(user.email, 'pre-failure@email.com') | ||
|
||
def test_transaction(self): | ||
"""Test that transaction nesting works.""" | ||
user1 = orm.User('user1@email.com').store() | ||
user2 = orm.User('user2@email.com').store() | ||
|
||
try: | ||
with self.backend.transaction(): | ||
user1.email = 'broken1@email.com' | ||
user2.email = 'broken2@email.com' | ||
raise RuntimeError | ||
except RuntimeError: | ||
pass | ||
self.assertEqual(user1.email, 'user1@email.com') | ||
self.assertEqual(user2.email, 'user2@email.com') | ||
|
||
def test_store_in_transaction(self): | ||
"""Test that storing inside a transaction is correctly dealt with.""" | ||
user1 = orm.User('user_store@email.com') | ||
with self.backend.transaction(): | ||
user1.store() | ||
# the following shouldn't raise | ||
orm.User.objects.get(email='user_store@email.com') | ||
|
||
user2 = orm.User('user_store_fail@email.com') | ||
try: | ||
with self.backend.transaction(): | ||
user2.store() | ||
raise RuntimeError | ||
except RuntimeError: | ||
pass | ||
|
||
with self.assertRaises(exceptions.NotExistent): | ||
orm.User.objects.get(email='user_store_fail@email.com') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters