Skip to content

Commit

Permalink
chore: clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pik694 committed Mar 27, 2024
1 parent b8abb30 commit 5187e3f
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 392 deletions.
38 changes: 1 addition & 37 deletions backend/app/infrastructure/database/allocations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import defaultdict
from datetime import datetime
from typing import List, Optional
from typing import List

from eth_utils import to_checksum_address
from sqlalchemy.orm import Query
Expand Down Expand Up @@ -182,42 +182,6 @@ def store_allocation_request(
db.session.add_all(new_allocations)


@deprecated("Alloc rework")
def add_all(epoch: int, user_id: int, nonce: int, allocations):
new_allocations = [
Allocation(
epoch=epoch,
user_id=user_id,
nonce=nonce,
proposal_address=to_checksum_address(a.proposal_address),
amount=str(a.amount),
)
for a in allocations
]
db.session.add_all(new_allocations)


@deprecated("Alloc rework")
def add_allocation_request(
user_address: str,
epoch: int,
nonce: int,
signature: str,
is_manually_edited: Optional[bool] = None,
):
user: User = get_by_address(user_address)

allocation_request = AllocationRequest(
user=user,
epoch=epoch,
nonce=nonce,
signature=signature,
is_manually_edited=is_manually_edited,
)

db.session.add(allocation_request)


def get_allocation_request_by_user_nonce(
user_address: str, nonce: int
) -> AllocationRequest | None:
Expand Down
10 changes: 3 additions & 7 deletions backend/app/infrastructure/routes/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from flask import current_app as app
from flask_restx import Namespace, fields

from app.legacy.controllers import allocations
from app.legacy.core.allocations import AllocationRequest
from app.extensions import api
from app.infrastructure import OctantResource
from app.modules.user.allocations import controller
Expand Down Expand Up @@ -165,14 +163,12 @@ class Allocation(OctantResource):
@ns.expect(user_allocation_request)
@ns.response(201, "User allocated successfully")
def post(self):
payload, signature = ns.payload["payload"], ns.payload["signature"]
app.logger.info(f"User allocation: {ns.payload}")
is_manually_edited = (
ns.payload["isManuallyEdited"] if "isManuallyEdited" in ns.payload else None
)
app.logger.info(f"User allocation payload: {payload}, signature: {signature}")
user_address = allocations.allocate(
AllocationRequest(payload, signature, override_existing_allocations=True),
is_manually_edited,
user_address = controller.allocate(
ns.payload, is_manually_edited=is_manually_edited
)
app.logger.info(f"User: {user_address} allocated successfully")

Expand Down
14 changes: 0 additions & 14 deletions backend/app/legacy/controllers/allocations.py

This file was deleted.

117 changes: 0 additions & 117 deletions backend/app/legacy/core/allocations.py

This file was deleted.

7 changes: 4 additions & 3 deletions backend/app/modules/user/allocations/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ def revoke_previous_allocation(user_address: str):


def _deserialize_payload(payload: Dict) -> UserAllocationRequestPayload:
allocation_items = _deserialize_items(payload.payload)
nonce = int(payload.payload["nonce"])
signature = payload.signature
allocation_payload = payload["payload"]
allocation_items = _deserialize_items(allocation_payload)
nonce = int(allocation_payload["nonce"])
signature = payload["signature"]

return UserAllocationRequestPayload(
payload=UserAllocationPayload(allocation_items, nonce), signature=signature
Expand Down
74 changes: 30 additions & 44 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import gql
import pytest
from eth_account import Account
from flask import g as request_context
from flask.testing import FlaskClient
from web3 import Web3
Expand All @@ -23,8 +22,6 @@
from app.infrastructure.contracts.erc20 import ERC20
from app.infrastructure.contracts.proposals import Proposals
from app.infrastructure.contracts.vault import Vault
from app.legacy.controllers.allocations import allocate
from app.legacy.core.allocations import Allocation, AllocationRequest
from app.legacy.crypto.account import Account as CryptoAccount
from app.legacy.crypto.eip712 import build_allocations_eip712_data, sign
from app.modules.dto import AccountFundsDTO, AllocationItem
Expand Down Expand Up @@ -55,6 +52,7 @@
MATCHED_REWARDS_AFTER_OVERHAUL,
NO_PATRONS_REWARDS,
)
from tests.helpers import make_user_allocation
from tests.helpers.context import get_context
from tests.helpers.gql_client import MockGQLClient
from tests.helpers.mocked_epoch_details import EPOCH_EVENTS
Expand Down Expand Up @@ -437,7 +435,6 @@ def patch_epochs(monkeypatch):

@pytest.fixture(scope="function")
def patch_proposals(monkeypatch, proposal_accounts):
monkeypatch.setattr("app.legacy.core.allocations.proposals", MOCK_PROPOSALS)
monkeypatch.setattr("app.legacy.core.proposals.proposals", MOCK_PROPOSALS)
monkeypatch.setattr("app.context.projects.proposals", MOCK_PROPOSALS)

Expand Down Expand Up @@ -492,12 +489,6 @@ def patch_eth_get_balance(monkeypatch):

@pytest.fixture(scope="function")
def patch_has_pending_epoch_snapshot(monkeypatch):
(
monkeypatch.setattr(
"app.legacy.core.allocations.has_pending_epoch_snapshot",
MOCK_HAS_PENDING_SNAPSHOT,
)
)
(
monkeypatch.setattr(
"app.context.epoch_state._has_pending_epoch_snapshot",
Expand All @@ -520,7 +511,6 @@ def patch_last_finalized_snapshot(monkeypatch):

@pytest.fixture(scope="function")
def patch_user_budget(monkeypatch):
monkeypatch.setattr("app.legacy.core.allocations.get_budget", MOCK_GET_USER_BUDGET)
monkeypatch.setattr(
"app.modules.user.budgets.service.saved.SavedUserBudgets.get_budget",
MOCK_GET_USER_BUDGET,
Expand Down Expand Up @@ -608,45 +598,51 @@ def mock_finalized_epoch_snapshot_db(app, user_accounts):


@pytest.fixture(scope="function")
def mock_allocations_db(app, user_accounts, proposal_accounts):
user1 = database.user.get_or_add_user(user_accounts[0].address)
user2 = database.user.get_or_add_user(user_accounts[1].address)
db.session.commit()
def mock_allocations_db(app, mock_users_db, proposal_accounts):
prev_epoch_context = get_context(MOCKED_PENDING_EPOCH_NO - 1)
pending_epoch_context = get_context(MOCKED_PENDING_EPOCH_NO)
user1, user2, _ = mock_users_db

user1_allocations = [
Allocation(proposal_accounts[0].address, 10 * 10**18),
Allocation(proposal_accounts[1].address, 5 * 10**18),
Allocation(proposal_accounts[2].address, 300 * 10**18),
AllocationItem(proposal_accounts[0].address, 10 * 10**18),
AllocationItem(proposal_accounts[1].address, 5 * 10**18),
AllocationItem(proposal_accounts[2].address, 300 * 10**18),
]

user1_allocations_prev_epoch = [
Allocation(proposal_accounts[0].address, 101 * 10**18),
Allocation(proposal_accounts[1].address, 51 * 10**18),
Allocation(proposal_accounts[2].address, 3001 * 10**18),
AllocationItem(proposal_accounts[0].address, 101 * 10**18),
AllocationItem(proposal_accounts[1].address, 51 * 10**18),
AllocationItem(proposal_accounts[2].address, 3001 * 10**18),
]

user2_allocations = [
Allocation(proposal_accounts[1].address, 1050 * 10**18),
Allocation(proposal_accounts[3].address, 500 * 10**18),
AllocationItem(proposal_accounts[1].address, 1050 * 10**18),
AllocationItem(proposal_accounts[3].address, 500 * 10**18),
]

user2_allocations_prev_epoch = [
Allocation(proposal_accounts[1].address, 10501 * 10**18),
Allocation(proposal_accounts[3].address, 5001 * 10**18),
AllocationItem(proposal_accounts[1].address, 10501 * 10**18),
AllocationItem(proposal_accounts[3].address, 5001 * 10**18),
]

database.allocations.add_all(
MOCKED_PENDING_EPOCH_NO - 1, user1.id, 0, user1_allocations_prev_epoch
make_user_allocation(
prev_epoch_context,
user1,
nonce=0,
allocation_items=user1_allocations_prev_epoch,
)
database.allocations.add_all(
MOCKED_PENDING_EPOCH_NO - 1, user2.id, 0, user2_allocations_prev_epoch
make_user_allocation(
prev_epoch_context,
user2,
nonce=0,
allocation_items=user2_allocations_prev_epoch,
)

database.allocations.add_all(
MOCKED_PENDING_EPOCH_NO, user1.id, 1, user1_allocations
make_user_allocation(
pending_epoch_context, user1, nonce=1, allocation_items=user1_allocations
)
database.allocations.add_all(
MOCKED_PENDING_EPOCH_NO, user2.id, 1, user2_allocations
make_user_allocation(
pending_epoch_context, user2, nonce=1, allocation_items=user2_allocations
)

db.session.commit()
Expand Down Expand Up @@ -765,16 +761,6 @@ def mock_user_rewards(alice, bob):
return user_rewards_service_mock


def allocate_user_rewards(
user_account: Account, proposal_account, allocation_amount, nonce: int = 0
):
payload = create_payload([proposal_account], [allocation_amount], nonce)
signature = sign(user_account, build_allocations_eip712_data(payload))
request = AllocationRequest(payload, signature, override_existing_allocations=False)

allocate(request)


def create_payload(proposals, amounts: list[int] | None, nonce: int = 0):
if amounts is None:
amounts = [randint(1 * 10**18, 1000 * 10**18) for _ in proposals]
Expand All @@ -790,7 +776,7 @@ def create_payload(proposals, amounts: list[int] | None, nonce: int = 0):
return {"allocations": allocations, "nonce": nonce}


def deserialize_allocations(payload) -> List[Allocation]:
def deserialize_allocations(payload) -> List[AllocationItem]:
return [
AllocationItem(
proposal_address=allocation_data["proposalAddress"],
Expand Down
1 change: 1 addition & 0 deletions backend/tests/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .allocations import make_user_allocation # noqa
from .subgraph.events import ( # noqa
create_epoch_event,
generate_epoch_events,
Expand Down
Loading

0 comments on commit 5187e3f

Please sign in to comment.