Skip to content

Commit

Permalink
start test_notify_matchers for codesy#37
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Nov 9, 2014
1 parent 5248fef commit b8ca9a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
9 changes: 5 additions & 4 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ class Bid(models.Model):
def notify_matchers(sender, instance, **kwargs):
issue_bids = Bid.objects.filter(url=instance.url).aggregate(Sum('offer'))
met_asks = Bid.objects.filter(url=instance.url,
ask__gt=0,
ask__lt=issue_bids['offer__sum'])
for bid in met_asks:
# TODO: only email if the winning asker has not been notified:
send_mail('codesy.io match for %s' % instance.url,
'Your ask for %s for %s has been met!' % (bid.ask, bid.url),
send_mail('codesy.io match for {url}'.format(url=instance.url),
'Your ask for ${ask} for {url} has been met!'.format(
ask=bid.ask, url=bid.url),
'codesy.io',
[bid.user.email]
)
[bid.user.email])
# TODO: email all offering bidders that a new asker has been matched
42 changes: 42 additions & 0 deletions auctions/tests/model_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fudge

from django.conf import settings
from django.test import TestCase

from model_mommy import mommy


class BidSaveReceiverTest(TestCase):
def setUp(self):
self.url = 'http://github.com/codesy/codesy/issues/37'
self.bid1 = mommy.make('auctions.Bid', user__email='user1@test.com',
ask=50, offer=0, url=self.url)
self.bid2 = mommy.make('auctions.Bid', user__email='user2@test.com',
ask=100, offer=10, url=self.url)
self.bid3 = mommy.make('auctions.Bid', user__email='user3@test.com',
ask=0, offer=45, url=self.url)

def _add_url(self, string):
return string.format(url=self.url)

@fudge.patch('auctions.models.send_mail')
def test_notify_matchers_sends_mail(self, mock_send_mail):
user = mommy.make(settings.AUTH_USER_MODEL)
mock_send_mail.expects_call().with_args(
self._add_url('codesy.io match for {url}'),
(self._add_url('Your ask for $50 for {url} has been met!')),
'codesy.io',
['user1@test.com']
)

mock_send_mail.next_call().with_args(
self._add_url('codesy.io match for {url}'),
(self._add_url('Your ask for $100 for {url} has been met!')),
'codesy.io',
['user2@test.com']
)

offer_bid = mommy.prepare(
'auctions.Bid', offer=100, user=user, ask=1000,
url='http://github.com/codesy/codesy/issues/37')
offer_bid.save()

0 comments on commit b8ca9a0

Please sign in to comment.