Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request dj-stripe#510 from jleclanche/cleanup/replace-djan…
Browse files Browse the repository at this point in the history
…go-mock

Replace django_mock with a much smaller QuerySetMock implementation
  • Loading branch information
kavdev authored May 30, 2017
2 parents 5af6ed6 + 167a2c5 commit d82a6a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 2 additions & 3 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.utils.functional import cached_property
from doc_inherit import class_doc_inherit
from mock_django.query import QuerySetMock
from stripe.error import StripeError, InvalidRequestError

import traceback as exception_traceback
Expand All @@ -43,7 +42,7 @@
StripeEvent, StripeInvoice, StripeInvoiceItem, StripePlan, StripeSource,
StripeSubscription, StripeTransfer
)
from .utils import get_friendly_currency_amount
from .utils import get_friendly_currency_amount, QuerySetMock


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -829,7 +828,7 @@ def invoiceitems(self):
will act like a normal queryset, but mutation will silently fail.
"""

return QuerySetMock(InvoiceItem, *self._invoiceitems)
return QuerySetMock.from_iterable(InvoiceItem, self._invoiceitems)

@property
def stripe_id(self):
Expand Down
24 changes: 24 additions & 0 deletions djstripe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ImproperlyConfigured
from django.db.models.query import QuerySet
from django.utils import timezone


Expand Down Expand Up @@ -135,3 +136,26 @@ def get_friendly_currency_amount(amount, currency):
currency = currency.upper()
sigil = CURRENCY_SIGILS.get(currency, "")
return "{sigil}{amount} {currency}".format(sigil=sigil, amount=amount, currency=currency)


class QuerySetMock(QuerySet):
"""
A mocked QuerySet class that does not handle updates.
Used by UpcomingInvoice.invoiceitems.
"""

@classmethod
def from_iterable(cls, model, iterable):
instance = cls(model)
instance._result_cache = list(iterable)
instance._prefetch_done = True
return instance

def _clone(self):
return self.__class__.from_iterable(self.model, self._result_cache)

def update(self):
return 0

def delete(self):
return 0
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ install_requires =
stripe >= 1.53.0
tqdm >= 4.8.4
python-doc-inherit >= 0.3.0
mock-django >= 0.6.10

[options.packages.find]
exclude =
Expand Down
4 changes: 4 additions & 0 deletions tests/test_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ def test_upcoming_invoice(self, invoice_upcoming_mock, subscription_retrieve_moc
self.assertEqual(1, len(items))
self.assertEqual(FAKE_SUBSCRIPTION["id"], items[0].stripe_id)

# delete/update should do nothing
self.assertEqual(invoice.invoiceitems.update(), 0)
self.assertEqual(invoice.invoiceitems.delete(), 0)

self.assertIsNotNone(invoice.plan)
self.assertEqual(FAKE_PLAN["id"], invoice.plan.stripe_id)

Expand Down

0 comments on commit d82a6a3

Please sign in to comment.