summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBob Halley <halley@dnspython.org>2020-08-13 07:31:57 -0700
committerBob Halley <halley@dnspython.org>2020-08-13 07:31:57 -0700
commit677c3c03c91d5d758e2f7a225a7455d9b2b5da24 (patch)
tree7c99f2cab49a467ba78bb912d5a7c5f148407885 /tests
parent060ad0db51fbb26a6f809548d884ef468ad06beb (diff)
downloaddnspython-677c3c03c91d5d758e2f7a225a7455d9b2b5da24.tar.gz
Allow explicit commit/rollback. Prevent multiple txn end. Add txn.changed().
Diffstat (limited to 'tests')
-rw-r--r--tests/test_transaction.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/test_transaction.py b/tests/test_transaction.py
index 93705f2..c9b6f5c 100644
--- a/tests/test_transaction.py
+++ b/tests/test_transaction.py
@@ -56,6 +56,12 @@ class Transaction(dns.transaction.Transaction):
return True
return False
+ def _changed(self):
+ if self.read_only:
+ return False
+ else:
+ return len(self.rdatasets) > 0
+
def _end_transaction(self, commit):
if commit:
self.db.rdatasets = self.rdatasets
@@ -244,6 +250,93 @@ def test_zone_basic(zone):
output = zone.to_text()
assert output == example_text_output
+def test_explicit_rollback_and_commit(zone):
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.delete(dns.name.from_text('bar.foo', None))
+ txn.rollback()
+ assert zone.get_node('bar.foo') is not None
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.delete(dns.name.from_text('bar.foo', None))
+ txn.commit()
+ assert zone.get_node('bar.foo') is None
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.delete(dns.name.from_text('bar.foo', None))
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.add('bar.foo', 300, dns.rdata.from_text('in', 'txt', 'hi'))
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.replace('bar.foo', 300, dns.rdata.from_text('in', 'txt', 'hi'))
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.reader() as txn:
+ txn.rollback()
+ txn.get('bar.foo', 'in', 'mx')
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.delete_exact('bar.foo')
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.name_exists('bar.foo')
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.update_serial()
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.changed()
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.rollback()
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ txn.commit()
+ with pytest.raises(dns.transaction.AlreadyEnded):
+ with zone.writer() as txn:
+ txn.rollback()
+ for rdataset in txn:
+ print(rdataset)
+
+def test_zone_changed(zone):
+ # Read-only is not changed!
+ with zone.reader() as txn:
+ assert not txn.changed()
+ # delete an existing name
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.delete(dns.name.from_text('bar.foo', None))
+ assert txn.changed()
+ # delete a nonexistent name
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.delete(dns.name.from_text('unknown.bar.foo', None))
+ assert not txn.changed()
+ # delete a nonexistent rdataset from an extant node
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.delete(dns.name.from_text('bar.foo', None), 'in', 'txt')
+ assert not txn.changed()
+ # add an rdataset to an extant Node
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.add('bar.foo', 300, dns.rdata.from_text('in', 'txt', 'hi'))
+ assert txn.changed()
+ # add an rdataset to a nonexistent Node
+ with zone.writer() as txn:
+ assert not txn.changed()
+ txn.add('foo.foo', 300, dns.rdata.from_text('in', 'txt', 'hi'))
+ assert txn.changed()
+
def test_zone_base_layer(zone):
with zone.writer() as txn:
# Get a set from the zone layer