Skip to content

Commit

Permalink
Overwrite value to string for keccak256field (#399)
Browse files Browse the repository at this point in the history
* Ignore error in place order test if there is no liquidity
  • Loading branch information
moisses89 authored Nov 17, 2022
1 parent d7c76e0 commit 7b239fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gnosis/eth/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def get_prep_value(self, value: Union[bytes, str]) -> Optional[bytes]:
if value:
return self._to_bytes(value)

def value_to_string(self, obj):
return str(self.value_from_object(obj))

def to_python(self, value) -> Optional[str]:
if value is not None:
try:
Expand Down
38 changes: 38 additions & 0 deletions gnosis/eth/django/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.exceptions import ValidationError
from django.core.serializers import serialize
from django.db import DataError, transaction
from django.test import TestCase

Expand Down Expand Up @@ -146,3 +147,40 @@ def test_keccak256_field(self):
):
with transaction.atomic():
Keccak256Hash.objects.create(value=value_hex_invalid)

def test_serialize_keccak256_field_to_json(self):
hexvalue: str = (
"0xdb5b7c6d3b0cc538a5859afc4674a785d9d111c3835390295f3d3173d41ca8ea"
)
Keccak256Hash.objects.create(value=hexvalue)
serialized = serialize("json", Keccak256Hash.objects.all())
# 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)
serialized = serialize("json", EthereumAddressV2.objects.all())
# address should be in serialized data
self.assertIn(address, serialized)

def test_serialize_uint256_field_to_json(self):
value = 2**260
Uint256.objects.create(value=value)
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 = Web3.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)
6 changes: 6 additions & 0 deletions gnosis/protocol/tests/test_gnosis_protocol_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.test import TestCase

import pytest
from eth_account import Account
from web3 import Web3

Expand Down Expand Up @@ -132,5 +133,10 @@ def test_place_order(self):
order_id = self.goerli_gnosis_protocol_api.place_order(
order, Account().create().key
)

if type(order_id) is dict:
if order_id["errorType"] == "NoLiquidity":
pytest.xfail("NoLiquidity Error")

self.assertEqual(order_id[:2], "0x")
self.assertEqual(len(order_id), 114)

0 comments on commit 7b239fc

Please sign in to comment.