def test_tag_suggest(self): a = Account.objects.get(pk=3) p = Payee.objects.get(name="Sainsbury's") tags = p.suggest_tags(a.user) self.assertEqual(tags, [u'food', u'household']) p = Payee.objects.get(name="HMV") tags = p.suggest_tags(a.user) self.assertEqual( tags, [u'cds', u'entertainment', u'birthdays', u'dvds', u'presents']) t = Transaction.objects.create(account=a, mobile=False, payee=p, amount=Decimal('-50'), date=datetime.date.today()) # By creating these tags we should move presents up the ranking, and add in books TagLink.create_relationships(t, 'books cds presents') tags = p.suggest_tags(a.user) self.assertEqual(tags, [ u'cds', u'entertainment', u'birthdays', u'presents', u'dvds', u'books' ])
def test_create_relationships(self): a = Account.objects.get(pk=4) p = Payee.objects.get(pk=5) t = Transaction(account=a, mobile=False, payee=p, amount=Decimal('-50'), date=datetime.date.today()) t.save() TagLink.create_relationships( t, u' food:26.30 toiletries:12.98\n household:51.23') expected = [{ 'name': 'food', 'split': Decimal('-26.30') }, { 'name': 'toiletries', 'split': Decimal('-12.98') }, { 'name': 'household', 'split': Decimal('-50') }] results = TagLink.objects.select_related().filter( transaction=t).order_by('id') self.assertEqual(len(results), len(expected)) for t in range(len(results)): self.assertEqual(results[t].tag.name, expected[t]['name']) self.assertEqual(results[t].split, expected[t]['split'])
def tag_transaction(request, transaction): transaction = get_object_or_404(Transaction, pk=transaction, account__user=request.user) if request.method == "POST" and 'tags' in request.POST.keys(): transaction.taglink_set.all().delete() TagLink.create_relationships(transaction, request.POST['tags']) # Get the up-to-date set of tags for this transaction and return them tags = [{'name': tag.tag.name, 'amount': abs(float(tag.split))} for tag in transaction.taglink_set.select_related().all()] return HttpResponse(json.dumps({'transaction': transaction.pk, 'tags': tags, 'total': abs(float(transaction.amount))}), content_type='application/javascript; charset=utf-8') else: return HttpResponseBadRequest()
def test_view_tag(self): """ Test the view page shows only transactions for a specific tag """ p = Payee.objects.get(name="HMV") a = Account.objects.get(pk=3) t = Transaction.objects.create(account=a, mobile=False, payee=p, amount=Decimal('-50'), date=datetime.date.today()) # By creating these tags we should move dvds up the ranking, and add in books TagLink.create_relationships(t, 'books cds dvds') r = self.client.get(reverse('tag-view', args=['books'])) self.assertEqual(r.status_code, 200) # Should just be showing the one transaction we added self.assertEqual(len(r.context['transactions'].object_list), 1) self.assertEqual([tr.id for tr in r.context['transactions'].object_list], [t.id])
def test_tag_suggest(self): a = Account.objects.get(pk=3) p = Payee.objects.get(name="Sainsbury's") tags = p.suggest_tags(a.user) self.assertEqual(tags, [u"food", u"household"]) p = Payee.objects.get(name="HMV") tags = p.suggest_tags(a.user) self.assertEqual(tags, [u"cds", u"entertainment", u"birthdays", u"dvds", u"presents"]) t = Transaction.objects.create( account=a, mobile=False, payee=p, amount=Decimal("-50"), date=datetime.date.today() ) # By creating these tags we should move presents up the ranking, and add in books TagLink.create_relationships(t, "books cds presents") tags = p.suggest_tags(a.user) self.assertEqual(tags, [u"cds", u"entertainment", u"birthdays", u"presents", u"dvds", u"books"])
def test_create_relationships(self): a = Account.objects.get(pk=4) p = Payee.objects.get(pk=5) t = Transaction(account=a, mobile=False, payee=p, amount=Decimal("-50"), date=datetime.date.today()) t.save() TagLink.create_relationships(t, u" food:26.30 toiletries:12.98\n household:51.23") expected = [ {"name": "food", "split": Decimal("-26.30")}, {"name": "toiletries", "split": Decimal("-12.98")}, {"name": "household", "split": Decimal("-50")}, ] results = TagLink.objects.select_related().filter(transaction=t).order_by("id") self.assertEqual(len(results), len(expected)) for t in range(len(results)): self.assertEqual(results[t].tag.name, expected[t]["name"]) self.assertEqual(results[t].split, expected[t]["split"])
def test_view_tag(self): """ Test the view page shows only transactions for a specific tag """ p = Payee.objects.get(name="HMV") a = Account.objects.get(pk=3) t = Transaction.objects.create(account=a, mobile=False, payee=p, amount=Decimal('-50'), date=datetime.date.today()) # By creating these tags we should move dvds up the ranking, and add in books TagLink.create_relationships(t, 'books cds dvds') r = self.client.get(reverse('tag-view', args=['books'])) self.assertEqual(r.status_code, 200) # Should just be showing the one transaction we added self.assertEqual(len(r.context['transactions'].object_list), 1) self.assertEqual( [tr.id for tr in r.context['transactions'].object_list], [t.id])