generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
507 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# -*- coding: utf-8 -*- | ||
# snapshottest: v1 - https://goo.gl/zC4yUc | ||
from __future__ import unicode_literals | ||
|
||
from snapshottest import GenericRepr, Snapshot | ||
|
||
|
||
snapshots = Snapshot() | ||
|
||
snapshots["test_currency_changes_extractor currency changes"] = [ | ||
( | ||
GenericRepr("<CALL@000000000000000000000000000000000000aaaa:1#0>"), | ||
{ | ||
"change": -10, | ||
"owner": "0x000000000000000000000000000000000000aaaa", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
( | ||
GenericRepr("<CALL@000000000000000000000000000000000000aaaa:1#0>"), | ||
{ | ||
"change": 10, | ||
"owner": "0x000000000000000000000000000000000000bbbb", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
( | ||
GenericRepr("<CALLCODE@000000000000000000000000000000000000aaaa:1#0>"), | ||
{ | ||
"change": -20, | ||
"owner": "0x000000000000000000000000000000000000aaaa", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
( | ||
GenericRepr("<CALLCODE@000000000000000000000000000000000000aaaa:1#0>"), | ||
{ | ||
"change": 20, | ||
"owner": "0x000000000000000000000000000000000000cccc", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
( | ||
GenericRepr("<CALL@000000000000000000000000000000000000bbbb:1#0>"), | ||
{ | ||
"change": -10, | ||
"owner": "0x000000000000000000000000000000000000bbbb", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
( | ||
GenericRepr("<CALL@000000000000000000000000000000000000bbbb:1#0>"), | ||
{ | ||
"change": 10, | ||
"owner": "0x000000000000000000000000000000000000cccc", | ||
"token_address": None, | ||
"type": "ETHER", | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from tests.test_utils.test_utils import ( | ||
_test_callcode, | ||
) | ||
from traces_analyzer.features.extractors.currency_changes import ( | ||
CurrencyChangesFeatureExtractor, | ||
) | ||
from tests.test_utils.test_utils import _test_call | ||
|
||
from snapshottest.pytest import PyTestSnapshotTest | ||
|
||
|
||
def test_currency_changes_extractor(snapshot: PyTestSnapshotTest): | ||
instructions = [ | ||
# A -= 10 B += 10 | ||
_test_call("0xaaaa", 1, "0xa", "0xbbbb"), | ||
# reverted | ||
_test_call("0xaaaa", 1, "0xaa", "0xbbbb", reverted=True), | ||
# A -= 20 C += 20 | ||
_test_callcode("0xaaaa", 1, "0x14", "0xcccc"), | ||
# B -= 10 C += 10 | ||
_test_call("0xbbbb", 1, "0xa", "0xcccc"), | ||
] | ||
|
||
extractor = CurrencyChangesFeatureExtractor() | ||
for instruction in instructions: | ||
extractor.on_instruction(instruction) | ||
changes = extractor.currency_changes | ||
|
||
snapshot.assert_match(changes, "currency changes") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/evaluation/snapshots/snap_test_financial_gain_loss.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
# snapshottest: v1 - https://goo.gl/zC4yUc | ||
from __future__ import unicode_literals | ||
|
||
from snapshottest import Snapshot | ||
|
||
|
||
snapshots = Snapshot() | ||
|
||
snapshots["test_financial_gain_loss_evaluation evaluation_dict"] = { | ||
"evaluation_type": "financial_gain_loss", | ||
"report": { | ||
"gains": { | ||
"0x000000000000000000000000000000000000cccc": { | ||
"ETHER": { | ||
"change": 10, | ||
"owner": "0x000000000000000000000000000000000000cccc", | ||
"token_address": None, | ||
"type": "ETHER", | ||
} | ||
} | ||
}, | ||
"losses": { | ||
"0x000000000000000000000000000000000000aaaa": { | ||
"ETHER": { | ||
"change": -8, | ||
"owner": "0x000000000000000000000000000000000000aaaa", | ||
"token_address": None, | ||
"type": "ETHER", | ||
} | ||
}, | ||
"0x000000000000000000000000000000000000bbbb": { | ||
"ETHER": { | ||
"change": -2, | ||
"owner": "0x000000000000000000000000000000000000bbbb", | ||
"token_address": None, | ||
"type": "ETHER", | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
snapshots[ | ||
"test_financial_gain_loss_evaluation evaluation_str" | ||
] = """=== Evaluation: Financial gains and losses === | ||
Losses in normal compared to reverse scenario: | ||
> 0x000000000000000000000000000000000000cccc lost 10 ETHER (in Wei) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
from typing import Sequence | ||
from tests.test_utils.test_utils import ( | ||
_test_callcode, | ||
) | ||
from traces_analyzer.evaluation.financial_gain_loss_evaluation import ( | ||
FinancialGainLossEvaluation, | ||
) | ||
from traces_analyzer.features.extractors.currency_changes import ( | ||
CurrencyChangesFeatureExtractor, | ||
) | ||
from tests.test_utils.test_utils import _test_call | ||
from traces_parser.parser.instructions.instruction import Instruction | ||
|
||
from snapshottest.pytest import PyTestSnapshotTest | ||
|
||
|
||
def get_changes(instructions: Sequence[Instruction]): | ||
extractor = CurrencyChangesFeatureExtractor() | ||
for instruction in instructions: | ||
extractor.on_instruction(instruction) | ||
return extractor.currency_changes | ||
|
||
|
||
def test_financial_gain_loss_evaluation(snapshot: PyTestSnapshotTest): | ||
changes_normal = get_changes( | ||
[ | ||
# A -= 10 B += 10 | ||
_test_call("0xaaaa", 1, "0xa", "0xbbbb"), | ||
# reverted | ||
_test_call("0xaaaa", 1, "0xaa", "0xbbbb", reverted=True), | ||
# A -= 20 C += 20 | ||
_test_callcode("0xaaaa", 1, "0x14", "0xcccc"), | ||
# B -= 10 C += 10 | ||
_test_call("0xbbbb", 1, "0xa", "0xcccc"), | ||
] | ||
) | ||
changes_reverse = get_changes( | ||
[ | ||
# A -= 2 B += 2 | ||
_test_call("0xaaaa", 1, "0x2", "0xbbbb"), | ||
# reverted | ||
_test_call("0xaaaa", 1, "0xaa", "0xbbbb", reverted=True), | ||
# A -= 20 C += 20 | ||
_test_callcode("0xaaaa", 1, "0x14", "0xcccc"), | ||
] | ||
) | ||
|
||
evaluation = FinancialGainLossEvaluation( | ||
changes_normal, | ||
changes_reverse, | ||
) | ||
|
||
evaluation_dict = evaluation.dict_report() | ||
snapshot.assert_match(evaluation_dict, "evaluation_dict") | ||
|
||
evaluation_str = evaluation.cli_report() | ||
snapshot.assert_match(evaluation_str, "evaluation_str") | ||
|
||
# check if it's serializable | ||
json.dumps(evaluation_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.