summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Willison <swillison@gmail.com>2019-11-12 08:15:24 -0800
committerNed Batchelder <ned@nedbatchelder.com>2019-11-12 21:06:57 -0500
commitb5c69bd598d31173695453cedb18211e292ec777 (patch)
treed2d5ea41ec7dd48e03b2586af62a7b6272b5efb4
parent09adf5d4b238cc1443b4ae7fcdfe34997cfcf00a (diff)
downloadpython-coveragepy-git-b5c69bd598d31173695453cedb18211e292ec777.tar.gz
Expose numbits_to_nums() SQL function, refs #868
-rw-r--r--coverage/numbits.py2
-rw-r--r--tests/test_numbits.py5
2 files changed, 7 insertions, 0 deletions
diff --git a/coverage/numbits.py b/coverage/numbits.py
index 367ec6a1..c4f0ad02 100644
--- a/coverage/numbits.py
+++ b/coverage/numbits.py
@@ -13,6 +13,7 @@ in the blobs should be considered an implementation detail that might change in
the future. Use these functions to work with those binary blobs of data.
"""
+import json
from coverage import env
from coverage.backward import byte_to_int, bytes_to_ints, binary_bytes, zip_longest
@@ -154,3 +155,4 @@ def register_sqlite_functions(connection):
connection.create_function("numbits_intersection", 2, numbits_intersection)
connection.create_function("numbits_any_intersection", 2, numbits_any_intersection)
connection.create_function("num_in_numbits", 2, num_in_numbits)
+ connection.create_function("numbits_to_nums", 1, lambda b: json.dumps(numbits_to_nums(b)))
diff --git a/tests/test_numbits.py b/tests/test_numbits.py
index eb094d2a..232d48d3 100644
--- a/tests/test_numbits.py
+++ b/tests/test_numbits.py
@@ -3,6 +3,7 @@
"""Tests for coverage.numbits"""
+import json
import sqlite3
from hypothesis import example, given, settings
@@ -156,3 +157,7 @@ class NumbitsSqliteFunctionTest(CoverageTest):
res = self.cursor.execute("select id, num_in_numbits(12, numbits) from data order by id")
answer = [is_in for (id, is_in) in res]
self.assertEqual([1, 1, 1, 1, 0, 1, 0, 0, 0, 0], answer)
+
+ def test_numbits_to_nums(self):
+ res = self.cursor.execute("select numbits_to_nums(?)", [nums_to_numbits([1, 2, 3])])
+ self.assertEqual([1, 2, 3], json.loads(res.fetchone()[0]))