From f5a245d1dc7e7680a979c52ec77cdc3e54ccea4a Mon Sep 17 00:00:00 2001
From: Arun Isaac
Date: Mon, 7 Jul 2025 12:17:28 +0100
Subject: Add tests.
* test_pyhegp.py: New file.
* README.md (Run tests): New section.
* guix.scm: Import python-hypothesis from (gnu packages check).
(python-pyhegp)[arguments]: Enable tests.
[native-inputs]: Add python-hypothesis.
---
README.md | 7 +++++++
guix.scm | 5 ++---
test_pyhegp.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+), 3 deletions(-)
create mode 100644 test_pyhegp.py
diff --git a/README.md b/README.md
index e78e883..27d6fd5 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,12 @@
pyhegp is a Python library and CLI utility implementing homomorphic encryption of genotypes and phenotypes as described in [Private Genomes and Public SNPs: Homomorphic Encryption of Genotypes and Phenotypes for Shared Quantitative Genetics](https://academic.oup.com/genetics/article/215/2/359/5930450).
+# Run tests
+
+Run the test suite using
+```
+python3 -m pytest
+```
+
# License
pyhegp is free software released under the terms of the [GNU General Public License](https://www.gnu.org/licenses/gpl.html), either version 3 of the License, or (at your option) any later version.
diff --git a/guix.scm b/guix.scm
index 6b9e67f..44bbf73 100644
--- a/guix.scm
+++ b/guix.scm
@@ -16,7 +16,7 @@
;;; You should have received a copy of the GNU General Public License
;;; along with pyhegp. If not, see .
-(use-modules ((gnu packages check) #:select (python-pytest))
+(use-modules ((gnu packages check) #:select (python-hypothesis python-pytest))
((gnu packages python-build) #:select (python-flit-core))
((gnu packages python-science) #:select (python-scipy))
((gnu packages python-xyz) #:select (python-click python-numpy))
@@ -36,10 +36,9 @@
#:recursive? #t
#:select? (git-predicate (current-source-directory))))
(build-system pyproject-build-system)
- (arguments
- (list #:tests? #f))
(native-inputs
(list python-flit-core
+ python-hypothesis
python-pytest))
(propagated-inputs
(list python-click
diff --git a/test_pyhegp.py b/test_pyhegp.py
new file mode 100644
index 0000000..19e58d2
--- /dev/null
+++ b/test_pyhegp.py
@@ -0,0 +1,45 @@
+### pyhegp --- Homomorphic encryption of genotypes and phenotypes
+### Copyright © 2025 Arun Isaac
+###
+### This file is part of pyhegp.
+###
+### pyhegp is free software: you can redistribute it and/or modify it
+### under the terms of the GNU General Public License as published by
+### the Free Software Foundation, either version 3 of the License, or
+### (at your option) any later version.
+###
+### pyhegp is distributed in the hope that it will be useful, but
+### WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+### General Public License for more details.
+###
+### You should have received a copy of the GNU General Public License
+### along with pyhegp. If not, see .
+
+from hypothesis import given, settings, strategies as st
+from hypothesis.extra.numpy import arrays, array_shapes
+import numpy as np
+from pytest import approx
+
+from pyhegp import hegp_encrypt, hegp_decrypt, random_key
+
+@given(st.one_of(
+ arrays("int32",
+ array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200),
+ elements=st.integers(min_value=0, max_value=2)),
+ # The array above is the only realistic input, but we test more
+ # kinds of inputs for good measure.
+ arrays("int32",
+ array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200),
+ elements=st.integers(min_value=0, max_value=100)),
+ arrays("float64",
+ array_shapes(min_dims=2, max_dims=2, min_side=2, max_side=200),
+ elements=st.floats(min_value=0, max_value=100)))
+)
+@settings(deadline=None)
+def test_hegp_encryption_decryption_are_inverses(plaintext):
+ rng = np.random.default_rng()
+ key = random_key(rng, len(plaintext))
+ # FIXME: We don't use maf at the moment.
+ maf = None
+ assert hegp_decrypt(hegp_encrypt(plaintext, maf, key), key) == approx(plaintext)
--
cgit v1.2.3