Skip to content

Commit

Permalink
Feature/clone txn (#106)
Browse files Browse the repository at this point in the history
* add clone method to Txn

* add test for rxn.clone()
  • Loading branch information
Revolution1 authored Jul 1, 2019
1 parent 601c285 commit 700c2fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Notice: The authentication header through gRPC-JSON-Gateway only supported in [e

**Install**
```bash
$ pip install etcd3-py
$ pip install --upgrade etcd3-py
```

**Sync Client**
Expand Down
21 changes: 18 additions & 3 deletions etcd3/stateful/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def __init__(self, client, compare=None, success=None, failure=None):
:param failure: failure components of the transaction, default to []
"""
self.client = client
self._compare = compare or []
self._success = success or []
self._failure = failure or []
self._compare = list(compare or [])
self._success = list(success or [])
self._failure = list(failure or [])
self._committed = False

def clear(self): # pragma: no cover
"""
Expand Down Expand Up @@ -267,6 +268,20 @@ def delete(key=None, range_end=None, prev_kv=False, prefix=None, all=None):
"""
return kv.delete_range(key=key, range_end=range_end, prev_kv=prev_kv, prefix=prefix, all=all, txn_obj=True)

def __copy__(self):
return Txn(
client=self.client,
compare=self._compare[:],
success=self._success[:],
failure=self._failure[:]
)

def clone(self):
"""
:return: Txn
"""
return self.__copy__()


class TxnCompareOp(object):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_transaction_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ def test_transaction(client):
txn = client.Txn()
r = txn.compare(txn.key('foo').lease == ID).commit()
assert r.succeeded


def test_txn_clone(client):
txn0 = client.Txn()
txn0.If(txn0.key('foo').value < b'1')
txn1 = txn0.clone()
assert id(txn0._compare) != id(txn1._compare)
txn0.Then(txn0.range('foo'))
assert len(txn0._success) == 1
assert len(txn1._success) == 0

0 comments on commit 700c2fa

Please sign in to comment.