From 22a419d561118a0bb813214114970d54b1ae7d73 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Mon, 7 Feb 2022 15:46:51 -0500
Subject: [PATCH] add VoucherStore.count_random_tokens and an invariant
 demonstrating deletion

tests are failing
---
 src/_zkapauthorizer/model.py            | 11 +++++++++++
 src/_zkapauthorizer/tests/test_model.py | 15 +++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/src/_zkapauthorizer/model.py b/src/_zkapauthorizer/model.py
index 88bad2c..b6f9a93 100644
--- a/src/_zkapauthorizer/model.py
+++ b/src/_zkapauthorizer/model.py
@@ -533,6 +533,17 @@ class VoucherStore(object):
         )
         return list(UnblindedToken(t.encode("ascii")) for (t,) in texts)
 
+    @with_cursor
+    def count_random_tokens(self, cursor) -> int:
+        """
+        :return: The number of random tokens present in the database.  This is
+        usually not interesting but it is exposed so the test suite can check
+        invariants related to it.
+        """
+        cursor.execute("SELECT count(1) FROM [tokens]")
+        (count,) = cursor.fetchone()
+        return count
+
     @with_cursor
     def count_unblinded_tokens(self, cursor):
         """
diff --git a/src/_zkapauthorizer/tests/test_model.py b/src/_zkapauthorizer/tests/test_model.py
index 494e0af..7bd9aec 100644
--- a/src/_zkapauthorizer/tests/test_model.py
+++ b/src/_zkapauthorizer/tests/test_model.py
@@ -452,6 +452,21 @@ class UnblindedTokenStateMachine(RuleBasedStateMachine):
         self.available += len(self.using)
         del self.using[:]
 
+    @invariant()
+    def random_token_count(self):
+        """
+        ``VoucherStore.random_token_count`` returns ``0``.
+
+        The state machine currently jumps over all intermediate states where
+        this function could legitimately return non-zero.  It might be nice to
+        split up the ``get_passes`` rule so that we could see other values
+        sometimes.
+        """
+        self.case.assertThat(
+            self.configless.store.count_random_tokens(),
+            Equals(0),
+        )
+
     @invariant()
     def unblinded_token_count(self):
         """
-- 
GitLab