Skip to content

Commit 99495e7

Browse files
authored
Added support for BigInteger fields - ref #169 (#294)
1 parent 36bcf18 commit 99495e7

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Updated requirements_dev.txt
99
* Dropped support for Python 3.5
1010
* Dropped support for Django below 2.2.x LTS release
11+
* Added support for BigIntegerFields (#169)
1112

1213
## 2.5.2
1314

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Supported PGP public key fields are:
162162
- `DateTimePGPPublicKeyField`
163163
- `TimePGPPublicKeyField`
164164
- `IntegerPGPPublicKeyField`
165+
- `BigIntegerPGPPublicKeyField`
165166
- `DecimalPGPPublicKeyField`
166167
- `FloatPGPPublicKeyField`
167168

@@ -181,6 +182,7 @@ Supported PGP symmetric key fields are:
181182
- `DateTimePGPSymmetricKeyField`
182183
- `TimePGPSymmetricKeyField`
183184
- `IntegerPGPSymmetricKeyField`
185+
- `BigIntegerPGPSymerticKeyField`
184186
- `DecimalPGPSymmetricKeyField`
185187
- `FloatPGPSymmetricKeyField`
186188

@@ -198,6 +200,7 @@ Encrypt and decrypt the data with `settings.PGCRYPTO_KEY` which acts like a pass
198200
| `DateTimeField` | `DateTimePGPPublicKeyField` | `DateTimePGPSymmetricKeyField` |
199201
| `TimeField` | `TimePGPPublicKeyField` | `TimePGPSymmetricKeyField` |
200202
| `IntegerField` | `IntegerPGPPublicKeyField` | `IntegerPGPSymmetricKeyField` |
203+
| `BigIntegerField` | `BigIntegerPGPPublicKeyField` | `BigIntegerPGPSymmetricKeyField` |
201204
| `DecimalField` | `DecimalPGPPublicKeyField` | `DecimalPGPSymmetricKeyField` |
202205
| `FloatField` | `FloatPGPPublicKeyField` | `FloatPGPSymmetricKeyField` |
203206

pgcrypto/fields.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class IntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
4848
cast_type = 'INT4'
4949

5050

51+
class BigIntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
52+
"""BigInteger PGP public key encrypted field."""
53+
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
54+
cast_type = 'BIGINT'
55+
56+
5157
class TextPGPPublicKeyField(PGPPublicKeyFieldMixin, models.TextField):
5258
"""Text PGP public key encrypted field."""
5359

@@ -78,6 +84,12 @@ class IntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField
7884
cast_type = 'INT4'
7985

8086

87+
class BigIntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField):
88+
"""BigInteger PGP symmetric key encrypted field."""
89+
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
90+
cast_type = 'BIGINT'
91+
92+
8193
class TextPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TextField):
8294
"""Text PGP symmetric key encrypted field for postgres."""
8395

tests/factories.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):
2323

2424
email_pgp_pub_field = factory.Sequence('email{}@public.key'.format)
2525
integer_pgp_pub_field = 42
26+
biginteger_pgp_pub_field = 9223372036854775807
2627
pgp_pub_field = factory.Sequence('Text with public key {}'.format)
2728
char_pub_field = factory.Sequence('Text {}'.format)
2829

@@ -32,6 +33,7 @@ class EncryptedModelFactory(factory.django.DjangoModelFactory):
3233

3334
email_pgp_sym_field = factory.Sequence('email{}@symmetric.key'.format)
3435
integer_pgp_sym_field = 43
36+
biginteger_pgp_sym_field = 9223372036854775807
3537
pgp_sym_field = factory.Sequence('Text with symmetric key {}'.format)
3638
char_sym_field = factory.Sequence('Text {}'.format)
3739

tests/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class EncryptedModel(models.Model):
3131
email_pgp_pub_field = fields.EmailPGPPublicKeyField(blank=True, null=True,
3232
unique=True)
3333
integer_pgp_pub_field = fields.IntegerPGPPublicKeyField(blank=True, null=True)
34+
biginteger_pgp_pub_field = fields.BigIntegerPGPPublicKeyField(blank=True, null=True)
3435
pgp_pub_field = fields.TextPGPPublicKeyField(blank=True, null=True)
3536
char_pub_field = fields.CharPGPPublicKeyField(blank=True, null=True, max_length=15)
3637
date_pgp_pub_field = fields.DatePGPPublicKeyField(blank=True, null=True)
@@ -43,6 +44,9 @@ class EncryptedModel(models.Model):
4344

4445
email_pgp_sym_field = fields.EmailPGPSymmetricKeyField(blank=True, null=True)
4546
integer_pgp_sym_field = fields.IntegerPGPSymmetricKeyField(blank=True, null=True)
47+
biginteger_pgp_sym_field = fields.BigIntegerPGPSymmetricKeyField(
48+
blank=True, null=True
49+
)
4650
pgp_sym_field = fields.TextPGPSymmetricKeyField(blank=True, null=True)
4751
char_sym_field = fields.CharPGPPublicKeyField(blank=True, null=True, max_length=15)
4852
date_pgp_sym_field = fields.DatePGPSymmetricKeyField(blank=True, null=True)

tests/test_fields.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ def test_fields(self):
8383
'hmac_with_original_field',
8484
'email_pgp_pub_field',
8585
'integer_pgp_pub_field',
86+
'biginteger_pgp_pub_field',
8687
'pgp_pub_field',
8788
'char_pub_field',
8889
'decimal_pgp_pub_field',
8990
'email_pgp_sym_field',
9091
'integer_pgp_sym_field',
92+
'biginteger_pgp_sym_field',
9193
'pgp_sym_field',
9294
'char_sym_field',
9395
'date_pgp_sym_field',
@@ -113,12 +115,14 @@ def test_value_returned_is_not_bytea(self):
113115

114116
self.assertIsInstance(instance.email_pgp_pub_field, str)
115117
self.assertIsInstance(instance.integer_pgp_pub_field, int)
118+
self.assertIsInstance(instance.biginteger_pgp_pub_field, int)
116119
self.assertIsInstance(instance.pgp_pub_field, str)
117120
self.assertIsInstance(instance.date_pgp_pub_field, date)
118121
self.assertIsInstance(instance.datetime_pgp_pub_field, datetime)
119122

120123
self.assertIsInstance(instance.email_pgp_sym_field, str)
121124
self.assertIsInstance(instance.integer_pgp_sym_field, int)
125+
self.assertIsInstance(instance.biginteger_pgp_sym_field, int)
122126
self.assertIsInstance(instance.pgp_sym_field, str)
123127
self.assertIsInstance(instance.date_pgp_sym_field, date)
124128
self.assertIsInstance(instance.datetime_pgp_sym_field, datetime)
@@ -311,19 +315,39 @@ def test_update_one_attribute(self):
311315
self.assertEqual(updated_instance.first(), instance)
312316

313317
def test_pgp_public_key_negative_number(self):
314-
"""Assert negative value is saved with an `IntegerPGPPublicKeyField` field."""
315-
expected = -1
318+
"""
319+
Assert negative value is saved with Public Key integer fields.
320+
321+
* `IntegerPGPPublicKeyField`
322+
* `BigIntegerPGPSymmetricKeyField`
323+
"""
324+
expected = -2147483648
316325
instance = EncryptedModelFactory.create(integer_pgp_pub_field=expected)
317326

318327
self.assertEqual(instance.integer_pgp_pub_field, expected)
319328

329+
expected = -9223372036854775808
330+
instance = EncryptedModelFactory.create(biginteger_pgp_pub_field=expected)
331+
332+
self.assertEqual(instance.biginteger_pgp_pub_field, expected)
333+
320334
def test_pgp_symmetric_key_negative_number(self):
321-
"""Assert negative value is saved with an `IntegerPGPSymmetricKeyField` field."""
322-
expected = -1
335+
"""
336+
Assert negative value is saved with Symmetric Key fields.
337+
338+
* `IntegerPGPSymmetricKeyField`
339+
* `BigIntegerPGPSymmetricKeyField`
340+
"""
341+
expected = -2147483648
323342
instance = EncryptedModelFactory.create(integer_pgp_sym_field=expected)
324343

325344
self.assertEqual(instance.integer_pgp_sym_field, expected)
326345

346+
expected = -9223372036854775808
347+
instance = EncryptedModelFactory.create(biginteger_pgp_sym_field=expected)
348+
349+
self.assertEqual(instance.biginteger_pgp_sym_field, expected)
350+
327351
def test_pgp_symmetric_key_date(self):
328352
"""Assert date is save with an `DatePGPSymmetricKeyField` field."""
329353
expected = date.today()

0 commit comments

Comments
 (0)