Skip to content

Commit

Permalink
Merge pull request #6 from openedx/pwnage101/ENT-6836
Browse files Browse the repository at this point in the history
feat: Add test factories
  • Loading branch information
pwnage101 authored Mar 6, 2023
2 parents 0fb21d2 + 7d16dc6 commit fa2b11c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openedx_ledger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
A library that records transactions against a ledger, denominated in units of value.
"""
__version__ = "0.1.3"
__version__ = "0.1.4"

default_app_config = "openedx_ledger.apps.EdxLedgerConfig"
11 changes: 8 additions & 3 deletions openedx_ledger/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from openedx_ledger import models


def create_transaction(ledger, quantity, idempotency_key, **metadata):
def create_transaction(
ledger, quantity, idempotency_key, lms_user_id=None, content_key=None, subsidy_access_policy_uuid=None, **metadata
):
"""
Create a transaction.
Expand All @@ -26,8 +28,11 @@ def create_transaction(ledger, quantity, idempotency_key, **metadata):
ledger=ledger,
idempotency_key=idempotency_key,
defaults={
'quantity': quantity,
'metadata': metadata,
"quantity": quantity,
"content_key": content_key,
"lms_user_id": lms_user_id,
"subsidy_access_policy_uuid": subsidy_access_policy_uuid,
"metadata": metadata,
},
)
return transaction
Expand Down
38 changes: 38 additions & 0 deletions openedx_ledger/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Test factories for openedx-ledger models.
"""
from uuid import uuid4

import factory

from openedx_ledger.models import Ledger, Transaction, UnitChoices


class LedgerFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `Ledger` model.
By default, no transactions are created for this test Ledger.
"""
class Meta:
model = Ledger

uuid = factory.LazyFunction(uuid4)
idempotency_key = factory.Faker("lexify", text="subsidy-????")
unit = UnitChoices.USD_CENTS


class TransactionFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `Transaction` model.
"""
class Meta:
model = Transaction

uuid = factory.LazyFunction(uuid4)
idempotency_key = factory.Faker(
"lexify",
text=factory.LazyAttribute(lambda tx: f"{tx.ledger.idempotency_key}-{tx.quantity}-????"),
)
quantity = factory.Faker("random_int", min=-100000, max=-100)
ledger = factory.Iterator(Ledger.objects.all())
2 changes: 1 addition & 1 deletion openedx_ledger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hashlib
from uuid import uuid4

TRANSACTION_METADATA_KEYS = ['opportunity_id', 'request_user', 'request_timestamp', 'etc...']
TRANSACTION_METADATA_KEYS = ['opportunity_id', 'request_user', 'request_timestamp', 'initial', 'etc...']


def create_idempotency_key_for_subsidy(subsidy_record):
Expand Down

0 comments on commit fa2b11c

Please sign in to comment.