| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9518db7 commit 63265b0
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,6 +47,8 @@ def test_ctor(self): | |||
| 47 | 47 | self.assertEqual(xact.dataset_id, _DATASET) | |
| 48 | 48 | self.assertEqual(xact.connection, connection) | |
| 49 | 49 | self.assertEqual(xact.id, None) | |
| 50 | + self.assertEqual(xact._status, None) | ||
| 51 | + self.assertTrue(xact._commit_success is False) | ||
| 50 | 52 | self.assertTrue(isinstance(xact.mutation, Mutation)) | |
| 51 | 53 | self.assertEqual(len(xact._auto_id_entities), 0) | |
| 52 | 54 | ||
@@ -64,6 +66,8 @@ def test_ctor_with_env(self): | |||
| 64 | 66 | self.assertEqual(xact.id, None) | |
| 65 | 67 | self.assertEqual(xact.dataset_id, DATASET_ID) | |
| 66 | 68 | self.assertEqual(xact.connection, CONNECTION) | |
| 69 | + self.assertEqual(xact._status, None) | ||
| 70 | + self.assertTrue(xact._commit_success is False) | ||
| 67 | 71 | ||
| 68 | 72 | def test_current(self): | |
| 69 | 73 | from gcloud.datastore.test_api import _NoCommitBatch | |
@@ -90,6 +94,47 @@ def test_current(self): | |||
| 90 | 94 | self.assertTrue(xact1.current() is None) | |
| 91 | 95 | self.assertTrue(xact2.current() is None) | |
| 92 | 96 | ||
| 97 | + def test_succeeded_fresh_transaction(self): | ||
| 98 | + _DATASET = 'DATASET' | ||
| 99 | + connection = _Connection() | ||
| 100 | + xact = self._makeOne(dataset_id=_DATASET, connection=connection) | ||
| 101 | + self.assertEqual(xact._status, None) | ||
| 102 | + | ||
| 103 | + success = marker = object() | ||
| 104 | + with self.assertRaises(ValueError): | ||
| 105 | + success = xact.succeeded | ||
| 106 | + self.assertTrue(success is marker) | ||
| 107 | + | ||
| 108 | + def test_succeeded_in_progress(self): | ||
| 109 | + _DATASET = 'DATASET' | ||
| 110 | + connection = _Connection() | ||
| 111 | + xact = self._makeOne(dataset_id=_DATASET, connection=connection) | ||
| 112 | + xact.begin() | ||
| 113 | + self.assertEqual(xact._status, self._getTargetClass()._IN_PROGRESS) | ||
| 114 | + | ||
| 115 | + success = marker = object() | ||
| 116 | + with self.assertRaises(ValueError): | ||
| 117 | + success = xact.succeeded | ||
| 118 | + self.assertTrue(success is marker) | ||
| 119 | + | ||
| 120 | + def test_succeeded_on_success(self): | ||
| 121 | + _DATASET = 'DATASET' | ||
| 122 | + connection = _Connection() | ||
| 123 | + xact = self._makeOne(dataset_id=_DATASET, connection=connection) | ||
| 124 | + xact.begin() | ||
| 125 | + xact.commit() | ||
| 126 | + self.assertEqual(xact._status, self._getTargetClass()._FINISHED) | ||
| 127 | + self.assertTrue(xact.succeeded is True) | ||
| 128 | + | ||
| 129 | + def test_succeeded_on_failure(self): | ||
| 130 | + _DATASET = 'DATASET' | ||
| 131 | + connection = _Connection() | ||
| 132 | + xact = self._makeOne(dataset_id=_DATASET, connection=connection) | ||
| 133 | + xact.begin() | ||
| 134 | + xact.rollback() | ||
| 135 | + self.assertEqual(xact._status, self._getTargetClass()._FINISHED) | ||
| 136 | + self.assertTrue(xact.succeeded is False) | ||
| 137 | + | ||
| 93 | 138 | def test_begin(self): | |
| 94 | 139 | _DATASET = 'DATASET' | |
| 95 | 140 | connection = _Connection(234) | |
@@ -98,6 +143,19 @@ def test_begin(self): | |||
| 98 | 143 | self.assertEqual(xact.id, 234) | |
| 99 | 144 | self.assertEqual(connection._begun, _DATASET) | |
| 100 | 145 | ||
| 146 | + def test_begin_tombstoned(self): | ||
| 147 | + _DATASET = 'DATASET' | ||
| 148 | + connection = _Connection(234) | ||
| 149 | + xact = self._makeOne(dataset_id=_DATASET, connection=connection) | ||
| 150 | + xact.begin() | ||
| 151 | + self.assertEqual(xact.id, 234) | ||
| 152 | + self.assertEqual(connection._begun, _DATASET) | ||
| 153 | + | ||
| 154 | + xact.rollback() | ||
| 155 | + self.assertEqual(xact.id, None) | ||
| 156 | + | ||
| 157 | + self.assertRaises(ValueError, xact.begin) | ||
| 158 | + | ||
| 101 | 159 | def test_rollback(self): | |
| 102 | 160 | _DATASET = 'DATASET' | |
| 103 | 161 | connection = _Connection(234) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,9 +32,8 @@ class Transaction(Batch): | |||
| 32 | 32 | ||
| 33 | 33 | >>> datastore.set_defaults() | |
| 34 | 34 | ||
| 35 | - >>> with Transaction() as xact: | ||
| 36 | - ... datastore.put(entity1) | ||
| 37 | - ... datastore.put(entity2) | ||
| 35 | + >>> with Transaction(): | ||
| 36 | + ... datastore.put([entity1, entity2]) | ||
| 38 | 37 | ||
| 39 | 38 | Because it derives from :class:`Batch`, :class`Transaction` also provides | |
| 40 | 39 | :meth:`put` and :meth:`delete` methods:: | |
@@ -46,7 +45,7 @@ class Transaction(Batch): | |||
| 46 | 45 | By default, the transaction is rolled back if the transaction block | |
| 47 | 46 | exits with an error:: | |
| 48 | 47 | ||
| 49 | - >>> with Transaction() as txn: | ||
| 48 | + >>> with Transaction(): | ||
| 50 | 49 | ... do_some_work() | |
| 51 | 50 | ... raise SomeException() # rolls back | |
| 52 | 51 | ||
@@ -71,16 +70,34 @@ class Transaction(Batch): | |||
| 71 | 70 | ... entity = Entity(key=Key('Thing')) | |
| 72 | 71 | ... datastore.put([entity]) | |
| 73 | 72 | ... assert entity.key.is_partial # There is no ID on this key. | |
| 73 | + ... | ||
| 74 | 74 | >>> assert not entity.key.is_partial # There *is* an ID. | |
| 75 | 75 | ||
| 76 | + After completion, you can determine if a commit succeeded or failed. | ||
| 77 | + For example, trying to delete a key that doesn't exist:: | ||
| 78 | + | ||
| 79 | + >>> with Transaction() as xact: | ||
| 80 | + ... xact.delete(key) | ||
| 81 | + ... | ||
| 82 | + >>> xact.succeeded | ||
| 83 | + False | ||
| 84 | + | ||
| 85 | + or successfully storing two entities: | ||
| 86 | + | ||
| 87 | + >>> with Transaction() as xact: | ||
| 88 | + ... datastore.put([entity1, entity2]) | ||
| 89 | + ... | ||
| 90 | + >>> xact.succeeded | ||
| 91 | + True | ||
| 92 | + | ||
| 76 | 93 | If you don't want to use the context manager you can initialize a | |
| 77 | 94 | transaction manually:: | |
| 78 | 95 | ||
| 79 | 96 | >>> transaction = Transaction() | |
| 80 | 97 | >>> transaction.begin() | |
| 81 | 98 | ||
| 82 | 99 | >>> entity = Entity(key=Key('Thing')) | |
| 83 | - >>> transaction.put([entity]) | ||
| 100 | + >>> transaction.put(entity) | ||
| 84 | 101 | ||
| 85 | 102 | >>> if error: | |
| 86 | 103 | ... transaction.rollback() | |
@@ -97,9 +114,17 @@ class Transaction(Batch): | |||
| 97 | 114 | are not set. | |
| 98 | 115 | """ | |
| 99 | 116 | ||
| 117 | + _IN_PROGRESS = 1 | ||
| 118 | + """Enum value for _IN_PROGRESS status of transaction.""" | ||
| 119 | + | ||
| 120 | + _FINISHED = 2 | ||
| 121 | + """Enum value for _FINISHED status of transaction.""" | ||
| 122 | + | ||
| 100 | 123 | def __init__(self, dataset_id=None, connection=None): | |
| 101 | 124 | super(Transaction, self).__init__(dataset_id, connection) | |
| 102 | 125 | self._id = None | |
| 126 | + self._status = None | ||
| 127 | + self._commit_success = False | ||
| 103 | 128 | ||
| 104 | 129 | @property | |
| 105 | 130 | def id(self): | |
@@ -123,13 +148,32 @@ def current(): | |||
| 123 | 148 | if isinstance(top, Transaction): | |
| 124 | 149 | return top | |
| 125 | 150 | ||
| 151 | + @property | ||
| 152 | + def succeeded(self): | ||
| 153 | + """Determines if transaction has succeeded or failed. | ||
| 154 | + | ||
| 155 | + :rtype: boolean | ||
| 156 | + :returns: Boolean indicating successful commit. | ||
| 157 | + :raises: :class:`ValueError` if the transaction is still in progress. | ||
| 158 | + """ | ||
| 159 | + if self._status != self._FINISHED: | ||
| 160 | + raise ValueError('Transaction not yet finished. ' | ||
| 161 | + 'Success not known.') | ||
| 162 | + | ||
| 163 | + return self._commit_success | ||
| 164 | + | ||
| 126 | 165 | def begin(self): | |
| 127 | 166 | """Begins a transaction. | |
| 128 | 167 | ||
| 129 | 168 | This method is called automatically when entering a with | |
| 130 | 169 | statement, however it can be called explicitly if you don't want | |
| 131 | 170 | to use a context manager. | |
| 171 | + | ||
| 172 | + :raises: :class:`ValueError` if the transaction has already begun. | ||
| 132 | 173 | """ | |
| 174 | + if self._status is not None: | ||
| 175 | + raise ValueError('Transaction already started previously.') | ||
| 176 | + self._status = self._IN_PROGRESS | ||
| 133 | 177 | self._id = self.connection.begin_transaction(self._dataset_id) | |
| 134 | 178 | ||
| 135 | 179 | def rollback(self): | |
@@ -140,8 +184,12 @@ def rollback(self): | |||
| 140 | 184 | - Sets the current connection's transaction reference to None. | |
| 141 | 185 | - Sets the current transaction's ID to None. | |
| 142 | 186 | """ | |
| 143 | - self.connection.rollback(self._dataset_id, self._id) | ||
| 144 | - self._id = None | ||
| 187 | + try: | ||
| 188 | + self.connection.rollback(self._dataset_id, self._id) | ||
| 189 | + finally: | ||
| 190 | + self._status = self._FINISHED | ||
| 191 | + # Clear our own ID in case this gets accidentally reused. | ||
| 192 | + self._id = None | ||
| 145 | 193 | ||
| 146 | 194 | def commit(self): | |
| 147 | 195 | """Commits the transaction. | |
@@ -154,7 +202,10 @@ def commit(self): | |||
| 154 | 202 | ||
| 155 | 203 | - Sets the current transaction's ID to None. | |
| 156 | 204 | """ | |
| 157 | - super(Transaction, self).commit() | ||
| 158 | - | ||
| 159 | - # Clear our own ID in case this gets accidentally reused. | ||
| 160 | - self._id = None | ||
| 205 | + try: | ||
| 206 | + super(Transaction, self).commit() | ||
| 207 | + finally: | ||
| 208 | + self._commit_success = True | ||
| 209 | + self._status = self._FINISHED | ||
| 210 | + # Clear our own ID in case this gets accidentally reused. | ||
| 211 | + self._id = None | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments