<commit_before>""" Test for go.billing.django_utils. """

import json

from go.base.tests.helpers import GoDjangoTestCase, DjangoVumiApiHelper

from go.billing.models import Account
from go.billing.tests.helpers import mk_transaction, get_storage_credits

from go.billing.django_utils import TransactionSerializer


class TestTransactionSerializer(GoDjangoTestCase):
    def setUp(self):
        self.vumi_helper = self.add_helper(DjangoVumiApiHelper())
        self.user_helper = self.vumi_helper.make_django_user()
        self.account = Account.objects.get(
            user=self.user_helper.get_django_user())

    def test_to_json(self):
        serializer = TransactionSerializer()
        transaction = mk_transaction(self.account)
        [datum] = serializer.to_json([transaction])
        self.assertEqual(json.loads(datum), {
            u"pk": transaction.pk,
            u"model": u"billing.transaction",
            u"fields": {
                u"account_number": self.account.account_number,
                u"created": unicode(transaction.created.isoformat()),
                u"last_modified": unicode(
                    transaction.last_modified.isoformat()),
                u"credit_amount": 28.0,
                u"credit_factor": 0.25,
                u"markup_percent": 10.0,
                u"message_cost": 100.0,
                u"storage_cost": 50.0,
                u"storage_credits": float(get_storage_credits(50.0, 10.0)),
                u"message_direction": u"Inbound",
                u"message_id": None,
                u"session_cost": 0.0,
                u"session_created": None,
                u"status": u"Completed",
                u"tag_pool_name": u"pool1",
                u"tag_name": u"tag1",
            },
        })
<commit_msg>Fix transaction serializer test after adding message and session credits<commit_after>""" Test for go.billing.django_utils. """

import json

from go.base.tests.helpers import GoDjangoTestCase, DjangoVumiApiHelper

from go.billing.models import Account
from go.billing.tests.helpers import (
    mk_transaction, get_message_credits, get_storage_credits,
    get_session_credits)

from go.billing.django_utils import TransactionSerializer


class TestTransactionSerializer(GoDjangoTestCase):
    def setUp(self):
        self.vumi_helper = self.add_helper(DjangoVumiApiHelper())
        self.user_helper = self.vumi_helper.make_django_user()
        self.account = Account.objects.get(
            user=self.user_helper.get_django_user())

    def test_to_json(self):
        serializer = TransactionSerializer()
        transaction = mk_transaction(self.account)
        [datum] = serializer.to_json([transaction])
        self.assertEqual(json.loads(datum), {
            u"pk": transaction.pk,
            u"model": u"billing.transaction",
            u"fields": {
                u"account_number": self.account.account_number,
                u"created": unicode(transaction.created.isoformat()),
                u"last_modified": unicode(
                    transaction.last_modified.isoformat()),
                u"credit_amount": 28.0,
                u"credit_factor": 0.25,
                u"markup_percent": 10.0,
                u"message_cost": 100.0,
                u"storage_cost": 50.0,
                u"message_credits": float(get_message_credits(100.0, 10.0)),
                u"storage_credits": float(get_storage_credits(50.0, 10.0)),
                u"session_credits": float(get_session_credits(10.0, 10.0)),
                u"message_direction": u"Inbound",
                u"message_id": None,
                u"session_cost": 10.0,
                u"session_created": None,
                u"status": u"Completed",
                u"tag_pool_name": u"pool1",
                u"tag_name": u"tag1",
            },
        })

{'fraction_non_alphanumeric': 0.11006781429316641, 'fraction_numerical': 0.017214397496087636, 'mean_word_length': 2.286203941730934, 'pattern_counts': {'":': 40, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '508919', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1094, 'n_words': 183}