Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor web3 tests code #10293

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

filev94
Copy link
Contributor

@filev94 filev94 commented Feb 4, 2025

Description:

Removed similar code from several tests and put it in parent class so that if a change is needed it should be done in one place only. This is the first PR, more might be needed if i find more code that needs to be extracted in a similar way.

Related issue(s):

Fixes #10255

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: filev94 <todor.filev@limechain.tech>
Signed-off-by: filev94 <todor.filev@limechain.tech>
# Conflicts:
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/ContractCallDynamicCallsTest.java
#	hedera-mirror-web3/src/test/java/com/hedera/mirror/web3/service/OpcodeServiceTest.java
Signed-off-by: filev94 <todor.filev@limechain.tech>
Signed-off-by: filev94 <todor.filev@limechain.tech>
@filev94 filev94 requested a review from a team as a code owner February 4, 2025 14:20
.customize(e -> e.evmAddress(null))
.persist()
.toEntityId();
return accountEntityPersistCustomizable(e -> e.evmAddress(null)).toEntityId();
Copy link
Contributor

@bilyana-gospodinova bilyana-gospodinova Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is an example why we needed this refactoring. Usually we want both alias and evmAddress to be null or non-null or with the reusable services integration this will probably fail. We can use directly accountEntityPersist method from the parent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from customizable method to accountEntityPersist

@@ -84,7 +84,7 @@ class ContractCallNestedCallsTest extends AbstractContractCallServiceOpcodeTrace
void updateTokenKeysAndGetUpdatedTokenKeyForFungibleToken(final KeyValueType keyValueType, final KeyType keyType)
throws Exception {
// Given
final var tokenEntityId = fungibleTokenPersist();
final var tokenEntityId = fungibleTokenPersistWithTreasuryAccount(domainBuilder.entity().persist());
Copy link
Contributor

@bilyana-gospodinova bilyana-gospodinova Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the treasury account is generated, can't we use the base method where we don't pass any treasury account?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct. Fixing it

Copy link

codecov bot commented Feb 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.17%. Comparing base (756b4cf) to head (28816fc).

Additional details and impacted files
@@            Coverage Diff            @@
##               main   #10293   +/-   ##
=========================================
  Coverage     92.17%   92.17%           
  Complexity     7966     7966           
=========================================
  Files           975      975           
  Lines         33278    33278           
  Branches       4201     4201           
=========================================
+ Hits          30673    30674    +1     
  Misses         1606     1606           
+ Partials        999      998    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: filev94 <todor.filev@limechain.tech>
Signed-off-by: filev94 <todor.filev@limechain.tech>
@steven-sheehy steven-sheehy added enhancement Type: New feature test Test infrastructure, automated tests required, etc web3 Area: Web3 API labels Feb 4, 2025
@steven-sheehy steven-sheehy added this to the 0.124.0 milestone Feb 4, 2025
* @param treasuryEntity - the treasuryEntity that has to be set in the token
* @return Token object that is persisted in db
*/
protected Token fungibleTokenPersistWithTreasuryAccount(final Entity treasuryEntity) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need both fungibleTokenPersistWithTreasuryAccount(final Entity treasuryEntity) and fungibleTokenPersistWithTreasuryAccount(final EntityId treasuryEntityId)? Can't we change the code to use only 1 of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can change the code. My idea was to use the Entity as a parameter for the method so that you can extend the functionality in the future, if needed. However, there are tests that use the EntityId and to refactor it back to Entity would be a big change, so that's why i left it with both for now. In the end, i can remove the Entity parameter and just leave the EntityId, that way i should only refactor tests in one class (TokenModificationFunctionsTest)

e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(100_000_000_000_000_000L))
.persist();
return accountEntityPersistCustomizable(
e -> e.type(EntityType.ACCOUNT).evmAddress(null).alias(null).balance(100_000_000_000_000_000L));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason the balance in this method and in the other one (accountEntityWithEvmAddressPersist()) to differ? If not, we can choose one of the values and extract it into a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I tried using one balance for both, the smaller value - the tests passed so I will extract it in a constant.

final var treasury = accountPersistWithAlias(SENDER_ALIAS, SENDER_PUBLIC_KEY);
final var token = fungibleTokenPersist(treasury);
final var token = fungibleTokenPersistWithTreasuryAccount(treasury);
Copy link
Contributor

@bilyana-gospodinova bilyana-gospodinova Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to create and set a specific treasury account everywhere? If we don't use it in the test later, we can just leave it to the generated one.

Copy link
Contributor Author

@filev94 filev94 Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. When i removed the treasuryAccount and used the fungibleTokenPersist(), the tests passed. And thus the need for the fungibleTokenPersistWithTreasuryAccount(Entity ...) method was removed. Two birds with one stone i guess.

@@ -887,7 +870,7 @@ private Token nftPersist(final Entity treasuryEntity) {
}

private Entity accountPersist() {
return domainBuilder.entity().customize(a -> a.evmAddress(null)).persist();
return accountEntityPersistCustomizable(e -> e.evmAddress(null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same is valid as in #10293 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Type: New feature test Test infrastructure, automated tests required, etc web3 Area: Web3 API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor code in web3 tests
3 participants