Skip to content

Commit

Permalink
Remove deprecated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Mar 15, 2024
1 parent 7c4aa34 commit 6115926
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 95 deletions.
10 changes: 0 additions & 10 deletions gnosis/eth/django/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
from django.db import models

from ..models import (
EthereumAddressField,
EthereumAddressV2Field,
Keccak256Field,
Sha3HashField,
Uint32Field,
Uint96Field,
Uint256Field,
)


class EthereumAddress(models.Model):
value = EthereumAddressField(null=True)


class EthereumAddressV2(models.Model):
value = EthereumAddressV2Field(null=True)

Expand All @@ -31,9 +25,5 @@ class Uint32(models.Model):
value = Uint32Field(null=True)


class Sha3Hash(models.Model):
value = Sha3HashField(null=True)


class Keccak256Hash(models.Model):
value = Keccak256Field(null=True)
117 changes: 32 additions & 85 deletions gnosis/eth/django/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
from django.core.exceptions import ValidationError
from django.core.serializers import serialize
from django.db import DataError, transaction
from django.db import transaction
from django.test import TestCase

from eth_account import Account
from faker import Faker

from ...constants import NULL_ADDRESS, SENTINEL_ADDRESS
from ...utils import fast_is_checksum_address, fast_keccak_text
from .models import (
EthereumAddress,
EthereumAddressV2,
Keccak256Hash,
Sha3Hash,
Uint32,
Uint96,
Uint256,
)
from .models import EthereumAddressV2, Keccak256Hash, Uint32, Uint96, Uint256

faker = Faker()


class TestModels(TestCase):
def test_ethereum_address_field(self):
for EthereumAddressModel in (EthereumAddress, EthereumAddressV2):
with self.subTest(EthereumAddressModel=EthereumAddressModel):
address = Account.create().address
self.assertTrue(fast_is_checksum_address(address))
ethereum_address = EthereumAddressModel.objects.create(value=address)
ethereum_address.refresh_from_db()
self.assertTrue(fast_is_checksum_address(ethereum_address.value))
self.assertEqual(address, ethereum_address.value)

# Test addresses
for addresss in (
None,
NULL_ADDRESS,
SENTINEL_ADDRESS,
Account.create().address,
):
with self.subTest(special_address=addresss):
EthereumAddressModel.objects.create(value=addresss)
self.assertEqual(
EthereumAddressModel.objects.get(value=addresss).value,
addresss,
)

with self.assertRaisesMessage(
ValidationError,
'"0x23" value must be an EIP55 checksummed address.',
):
with transaction.atomic():
EthereumAddressModel.objects.create(value="0x23")

ethereum_address = EthereumAddressModel(value=Account.create().address)
self.assertIsNone(ethereum_address.full_clean())
address = Account.create().address
self.assertTrue(fast_is_checksum_address(address))
ethereum_address = EthereumAddressV2.objects.create(value=address)
ethereum_address.refresh_from_db()
self.assertTrue(fast_is_checksum_address(ethereum_address.value))
self.assertEqual(address, ethereum_address.value)

# Test addresses
for addresss in (
None,
NULL_ADDRESS,
SENTINEL_ADDRESS,
Account.create().address,
):
with self.subTest(special_address=addresss):
EthereumAddressV2.objects.create(value=addresss)
self.assertEqual(
EthereumAddressV2.objects.get(value=addresss).value,
addresss,
)

with self.assertRaisesMessage(
ValidationError,
'"0x23" value must be an EIP55 checksummed address.',
):
with transaction.atomic():
EthereumAddressV2.objects.create(value="0x23")

ethereum_address = EthereumAddressV2(value=Account.create().address)
self.assertIsNone(ethereum_address.full_clean())

def test_uint256_field(self):
for value in [
Expand Down Expand Up @@ -115,35 +105,6 @@ def test_uint32_field(self):
with self.assertRaises(ValidationError):
Uint32.objects.create(value=-2)

def test_sha3_hash_field(self):
value_hexbytes = fast_keccak_text(faker.name())
value_hex_with_0x: str = value_hexbytes.hex()
value_hex_without_0x: str = value_hex_with_0x[2:]
value: bytes = bytes(value_hexbytes)

values = [value, value_hex_without_0x, value_hex_with_0x, value_hexbytes]

for v in values:
sha3_hash = Sha3Hash.objects.create(value=v)
sha3_hash.refresh_from_db()
self.assertEqual(sha3_hash.value, value_hex_with_0x)

for v in values:
self.assertEqual(Sha3Hash.objects.filter(value=v).count(), len(values))

# Hash null
sha3_hash = Sha3Hash.objects.create(value=None)
sha3_hash.refresh_from_db()
self.assertIsNone(sha3_hash.value)

# Hash too big
value_hex_invalid: str = "0x" + value_hex_without_0x + "a"
with self.assertRaisesMessage(
DataError, "value too long for type character varying(64)"
):
with transaction.atomic():
Sha3Hash.objects.create(value=value_hex_invalid)

def test_keccak256_field(self):
value_hexbytes = fast_keccak_text(faker.name())
value_hex_with_0x: str = value_hexbytes.hex()
Expand Down Expand Up @@ -205,13 +166,6 @@ def test_serialize_keccak256_field_to_json(self):
# hexvalue should be in serialized data
self.assertIn(hexvalue, serialized)

def test_serialize_ethereum_address_field_to_json(self):
address: str = "0x5aFE3855358E112B5647B952709E6165e1c1eEEe"
EthereumAddress.objects.create(value=address)
serialized = serialize("json", EthereumAddress.objects.all())
# address should be in serialized data
self.assertIn(address, serialized)

def test_serialize_ethereum_address_v2_field_to_json(self):
address: str = "0x5aFE3855358E112B5647B952709E6165e1c1eEEe"
EthereumAddressV2.objects.create(value=address)
Expand All @@ -225,10 +179,3 @@ def test_serialize_uint256_field_to_json(self):
serialized = serialize("json", Uint256.objects.all())
# value should be in serialized data
self.assertIn(str(value), serialized)

def test_serialize_sha3_hash_to_json(self):
hash = fast_keccak_text("testSerializer")
Sha3Hash.objects.create(value=hash)
serialized = serialize("json", Sha3Hash.objects.all())
# hash should be in serialized data
self.assertIn(hash.hex(), serialized)

0 comments on commit 6115926

Please sign in to comment.