-
Notifications
You must be signed in to change notification settings - Fork 111
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
DB/ Created Airdrop table in DB, Added crud methods #112
Merged
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
56e937b
new table in DB, Add crud methods
binayak9932 f520322
ran isort and black
binayak9932 6e4e237
noedit
binayak9932 1c4f552
pylint fixes
binayak9932 02d1749
added test file for airdrop
binayak9932 d59771e
fixed pylint issue
binayak9932 531f827
requested changes
binayak9932 57e5aa1
Merge branch 'djeck1432:main' into DB/Airdrop
binayak9932 fdffc68
Merge branch 'main' of github.com:binayak9932/spotnet1
binayak9932 92b96fe
Merge branch 'DB/Airdrop' of github.com:binayak9932/spotnet1 into DB/…
binayak9932 c7b5423
Merge branch 'djeck1432:main' into DB/Airdrop
binayak9932 ba5803d
Merge branch 'DB/Airdrop' of github.com:binayak9932/spotnet1 into DB/…
binayak9932 6621996
Merge branch 'djeck1432:main' into DB/Airdrop
binayak9932 d035140
Merge branch 'DB/Airdrop' of github.com:binayak9932/spotnet1 into DB/…
binayak9932 3e11a35
test cases for new method create_empty_claim
binayak9932 5d2a070
requested changes rev
binayak9932 4d3114c
requested changes rev2
binayak9932 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
web_app/alembic/versions/e69320e12cc7_add_airdrop_model.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,67 @@ | ||
"""Add AirDrop model | ||
|
||
Revision ID: e69320e12cc7 | ||
Revises: a009512f5362 | ||
Create Date: 2024-10-25 16:47:37.723379 | ||
|
||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'e69320e12cc7' | ||
down_revision = 'a009512f5362' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
""" | ||
Perform the upgrade migration to create the 'airdrop' table in the database. | ||
|
||
This migration creates a new table, 'airdrop', with the following columns: | ||
|
||
- `id`: Primary key, UUID type, non-nullable. | ||
- `user_id`: Foreign key referencing `user.id`, UUID type, non-nullable. | ||
- `created_at`: Timestamp for when the airdrop was created, DateTime type, non-nullable. | ||
- `amount`: Decimal type, nullable, representing the amount associated with the airdrop. | ||
- `is_claimed`: Boolean type, indicating if the airdrop has been claimed, nullable. | ||
- `claimed_at`: Timestamp for when the airdrop was claimed, DateTime type, nullable. | ||
|
||
Additional configuration: | ||
|
||
- Foreign key constraint on `user_id` to reference the `user` table. | ||
- Primary key constraint on `id`. | ||
- Index on `user_id` for optimized querying based on the `user_id` field. | ||
|
||
This function is part of the Alembic migration and is auto-generated. | ||
Adjustments may be made if additional configuration or constraints are needed. | ||
""" | ||
op.create_table('airdrop', | ||
binayak9932 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sa.Column('id', sa.UUID(), nullable=False), | ||
sa.Column('user_id', sa.UUID(), nullable=False), | ||
sa.Column('created_at', sa.DateTime(), nullable=False), | ||
sa.Column('amount', sa.DECIMAL(), nullable=True), | ||
sa.Column('is_claimed', sa.Boolean(), nullable=True), | ||
sa.Column('claimed_at', sa.DateTime(), nullable=True), | ||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index(op.f('ix_airdrop_user_id'), 'airdrop', ['user_id'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
""" | ||
Perform the downgrade migration to remove the 'airdrop' table from the database. | ||
|
||
This migration drops the 'airdrop' table and its associated index on `user_id`. | ||
It is intended to reverse the changes made in the `upgrade` function, allowing | ||
for a rollback of the database schema to the state before the 'airdrop' table was added. | ||
""" | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_airdrop_user_id'), table_name='airdrop') | ||
binayak9932 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
op.drop_table('airdrop') | ||
# ### end Alembic commands ### |
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
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
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,171 @@ | ||
""" | ||
Tests for the AirDropDBConnector class, covering key database operations for airdrops. | ||
|
||
Fixtures: | ||
- db_connector: Provides an AirDropDBConnector instance with test user and airdrop data. | ||
|
||
Test Cases: | ||
- test_create_empty_claim_positive: Verifies airdrop creation for an existing user. | ||
- test_create_empty_claim_non_existent_user: Checks error handling for invalid user IDs. | ||
- test_save_claim_data_positive: Ensures claim data updates correctly. | ||
- test_save_claim_data_non_existent_airdrop: Confirms logging for invalid airdrop IDs. | ||
- test_get_all_unclaimed_positive: Retrieves unclaimed airdrops. | ||
- test_get_all_unclaimed_after_claiming: Excludes claimed airdrops from unclaimed results. | ||
""" | ||
|
||
import uuid | ||
from datetime import datetime | ||
from decimal import Decimal | ||
|
||
import pytest | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from web_app.db.crud import AirDropDBConnector | ||
from web_app.db.models import AirDrop, User | ||
|
||
|
||
@pytest.fixture | ||
def db_connector(): | ||
""" | ||
Sets up an AirDropDBConnector with a test user and airdrop record, then cleans | ||
up after the test. | ||
This fixture: | ||
- Initializes an AirDropDBConnector instance. | ||
- Creates and saves a test user and associated airdrop record. | ||
- Yields the connector, user, and airdrop instances for test use. | ||
- Cleans up the database by removing the test user and airdrop after the test. | ||
|
||
Yields: | ||
tuple: (AirDropDBConnector, User, AirDrop) | ||
""" | ||
connector = AirDropDBConnector() | ||
test_user = User(wallet_id="test_wallet_id") | ||
connector.write_to_db(test_user) | ||
airdrop = AirDrop(user_id=test_user.id) | ||
connector.write_to_db(airdrop) | ||
yield connector, test_user, airdrop | ||
connector.delete_object(AirDrop, airdrop.id) | ||
connector.delete_object(User, test_user.id) | ||
|
||
|
||
def test_create_empty_claim_positive(db_connector): | ||
""" | ||
Tests that create_empty_claim successfully creates a new airdrop for an | ||
existing user. | ||
|
||
Steps: | ||
- Calls create_empty_claim with a valid user ID. | ||
- Asserts the airdrop is created with the correct user_id and | ||
is initially unclaimed. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector, test user, | ||
and test airdrop. | ||
""" | ||
connector, test_user, _ = db_connector | ||
new_airdrop = connector.create_empty_claim(test_user.id) | ||
assert new_airdrop is not None | ||
assert new_airdrop.user_id == test_user.id | ||
assert not new_airdrop.is_claimed | ||
connector.delete_object(AirDrop, new_airdrop.id) | ||
|
||
|
||
def test_create_empty_claim_non_existent_user(db_connector): | ||
""" | ||
Tests that create_empty_claim raises an error when called with | ||
a non-existent user ID. | ||
|
||
Steps: | ||
- Generates a fake user ID that does not exist in the database. | ||
- Verifies that calling create_empty_claim with this ID raises | ||
an SQLAlchemyError. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector | ||
and test setup. | ||
""" | ||
connector, _, _ = db_connector | ||
fake_user_id = uuid.uuid4() | ||
with pytest.raises(SQLAlchemyError): | ||
connector.create_empty_claim(fake_user_id) | ||
|
||
|
||
def test_save_claim_data_positive(db_connector): | ||
""" | ||
Tests that save_claim_data correctly updates an existing airdrop | ||
with claim details. | ||
|
||
Steps: | ||
- Calls save_claim_data with a valid airdrop ID and amount. | ||
- Asserts the airdrop's amount, is_claimed status, and claimed_at | ||
timestamp are updated correctly. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector, test user, | ||
and test airdrop. | ||
""" | ||
connector, _, airdrop = db_connector | ||
amount = Decimal("100.50") | ||
connector.save_claim_data(airdrop.id, amount) | ||
updated_airdrop = connector.get_object(AirDrop, airdrop.id) | ||
assert updated_airdrop.amount == amount | ||
assert updated_airdrop.is_claimed | ||
assert updated_airdrop.claimed_at is not None | ||
|
||
|
||
def test_save_claim_data_non_existent_airdrop(db_connector, caplog): | ||
""" | ||
Tests that save_claim_data logs an error when called with a non-existent | ||
airdrop ID. | ||
|
||
Steps: | ||
- Generates a fake airdrop ID that is not in the database. | ||
- Calls save_claim_data with this ID and checks that the appropriate | ||
error message is logged. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector and | ||
test setup. | ||
caplog (fixture): Captures log output for verification. | ||
""" | ||
connector, _, _ = db_connector | ||
fake_airdrop_id = uuid.uuid4() | ||
connector.save_claim_data(fake_airdrop_id, Decimal("50.00")) | ||
assert f"AirDrop with ID {fake_airdrop_id} not found" in caplog.text | ||
|
||
|
||
def test_get_all_unclaimed_positive(db_connector): | ||
""" | ||
Tests that get_all_unclaimed retrieves unclaimed airdrops correctly. | ||
|
||
Steps: | ||
- Calls get_all_unclaimed to fetch unclaimed airdrops. | ||
- Asserts that the test airdrop (unclaimed) is present in the retrieved | ||
list by matching IDs. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector, test user, | ||
and test airdrop. | ||
""" | ||
connector, _, airdrop = db_connector | ||
unclaimed_airdrops = connector.get_all_unclaimed() | ||
assert any(airdrop.id == unclaimed.id for unclaimed in unclaimed_airdrops) | ||
|
||
|
||
def test_get_all_unclaimed_after_claiming(db_connector): | ||
""" | ||
Tests that get_all_unclaimed excludes airdrops that have been claimed. | ||
|
||
Steps: | ||
- Marks the test airdrop as claimed using save_claim_data. | ||
- Calls get_all_unclaimed to fetch unclaimed airdrops. | ||
- Asserts that the claimed airdrop is not included in the | ||
returned list. | ||
|
||
Args: | ||
db_connector (fixture): Provides the AirDropDBConnector, | ||
test user, and test airdrop. | ||
""" | ||
connector, _, airdrop = db_connector | ||
connector.save_claim_data(airdrop.id, Decimal("50.00")) | ||
unclaimed_airdrops = connector.get_all_unclaimed() | ||
assert airdrop not in unclaimed_airdrops |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you change old migration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without this config its giving error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which error, please ,be more specific, if you have any error, attach it here with explanation what did you run the command to get this error and traceback of this error