Best Python code snippet using tempest_python
test_models.py
Source:test_models.py  
1# -*- coding: utf-8 -*-2import dateparser3from django.core.exceptions import ObjectDoesNotExist4from django.contrib.auth.models import User5from django.test import TestCase, TransactionTestCase6from entities.models import Entity7from ledgers import utils8from ledgers.models import Account, Transaction, Line9from subledgers import settings10from subledgers.utils import convert_import_to_objects11from subledgers.creditors.models import Creditor, CreditorInvoice12from subledgers.expenses.models import Expense13from subledgers.sales.models import Sale14from subledgers.journals.models import JournalEntry15class TestModelEntrySaveTransaction(TestCase):16    def setUp(self):17        self.user = User.objects.create_user(18            'test_staff_user', 'test@example.com', '1234')19        self.user.is_staff = True20        self.user.save()21        self.code = "ABC"22        self.entity = Entity.objects.create(name=self.code.lower())23        self.creditor = Creditor.objects.create(entity=self.entity)24        self.c = Account.objects.create(25            element='03', number='0300', name='ACP')26        settings.GST_CR_ACCOUNT = Account.objects.create(27            element='03', number='0733', name='GST account')28        settings.GST_DR_ACCOUNT = Account.objects.create(29            element='03', number='0713', name='GST account')30        self.account_CR_GST = settings.GST_CR_ACCOUNT31        self.account_DR_GST = settings.GST_DR_ACCOUNT32        self.a1 = Account.objects.create(33            element='01', number='0450', name='Account 1')34        self.a2 = Account.objects.create(35            element='01', number='0709', name='Account 2')36        self.kwargs = {37            'date': '5-May-2020',38            'user': self.user,39            'account_DR': '01-0450',40            'account_CR': '01-0709',41            'value': 1.00,42        }43    def test_journalentry_save_transaction_account_code_passes(self):44        new_journalentry = JournalEntry()45        new_journalentry.save_transaction(self.kwargs)46        test_kwargs = {47            'transaction__date': utils.make_date('5-May-2020'),48            'transaction__user': self.user,49            'transaction__value': 1.00,50            'transaction__source': utils.get_source(JournalEntry)51        }52        test_object = JournalEntry.objects.get(**test_kwargs)53        self.assertEqual(new_journalentry, test_object)54    def test_journalentry_save_transaction_account_obj_passes(self):55        self.kwargs = {56            'date': '5-May-2020',57            'user': self.user,58            'account_DR': self.a1,59            'account_CR': self.a2,60            'value': 1.00,61        }62        new_journalentry = JournalEntry()63        new_journalentry.save_transaction(self.kwargs)64        test_kwargs = {65            'transaction__date': utils.make_date('5-May-2020'),66            'transaction__user': self.user,67            'transaction__value': 1.00,68            'transaction__source': utils.get_source(JournalEntry)69        }70        test_object = JournalEntry.objects.get(**test_kwargs)71        self.assertEqual(new_journalentry, test_object)72    def test_creditorinvoice_save_transaction_passes(self):73        self.kwargs = {74            'date': '5-May-2020',75            'user': self.user,76            'account': self.a1,77            'value': 1.00,78        }79        self.kwargs['invoice_number'] = 'abc123'80        self.kwargs['relation'] = self.creditor81        self.kwargs['gst_total'] = 082        new_creditorinvoice = CreditorInvoice()83        new_creditorinvoice.save_transaction(self.kwargs)84        test_kwargs = {85            'transaction__date': utils.make_date('5-May-2020'),86            'transaction__user': self.user,87            'transaction__value': 1.00,88            'transaction__source': utils.get_source(CreditorInvoice)89        }90        test_object = CreditorInvoice.objects.get(**test_kwargs)91        self.assertEqual(new_creditorinvoice, test_object)92    def test_creditorinvoice_save_transaction_accounts_passes(self):93        self.kwargs = {94            'date': '5-May-2020',95            'user': self.user,96            'accounts': [(self.a1, 1), (self.a2, 2)],97        }98        self.kwargs['invoice_number'] = 'abc123'99        self.kwargs['relation'] = self.creditor100        self.kwargs['gst_total'] = 0101        new_creditorinvoice = CreditorInvoice()102        new_creditorinvoice.save_transaction(self.kwargs)103        test_kwargs = {104            'transaction__date': utils.make_date('5-May-2020'),105            'transaction__user': self.user,106            'transaction__value': 3.00,107            'transaction__source': utils.get_source(CreditorInvoice)108        }109        test_object = CreditorInvoice.objects.get(**test_kwargs)110        self.assertEqual(new_creditorinvoice, test_object)111    def test_creditorinvoice_save_transaction_lines_passes(self):112        self.kwargs = {113            'date': '5-May-2020',114            'user': self.user,115            'lines': [116                (self.a1, 1),117                (self.a2, 2),118                (self.c, -3)119            ],120        }121        self.kwargs['invoice_number'] = 'abc123'122        self.kwargs['relation'] = self.creditor123        self.kwargs['gst_total'] = 0124        new_creditorinvoice = CreditorInvoice()125        new_creditorinvoice.save_transaction(self.kwargs)126        test_kwargs = {127            'transaction__date': utils.make_date('5-May-2020'),128            'transaction__user': self.user,129            'transaction__value': 3.00,130            'transaction__source': utils.get_source(CreditorInvoice)131        }132        test_object = CreditorInvoice.objects.get(**test_kwargs)133        self.assertEqual(new_creditorinvoice, test_object)134class TestModelEntryDRCR(TestCase):135    def test_crdr_journalentry(self):136        a = JournalEntry().is_cr_or_dr_in_tb()137        self.assertEqual(a, None)138    def test_crdr_creditorinvoice(self):139        a = CreditorInvoice().is_cr_or_dr_in_tb()140        self.assertEqual(a, 'CR')141    def test_crdr_creditorinvoice_credit(self):142        a = CreditorInvoice(is_credit=True).is_cr_or_dr_in_tb()143        self.assertEqual(a, 'DR')144class TestModelEntryGetRelation(TestCase):145    def setUp(self):146        self.code = "ABC"147        self.entity = Entity.objects.create(name=self.code.lower())148        self.creditor = Creditor.objects.create(entity=self.entity)149    def test_relation_code_Entity(self):150        test_rel = JournalEntry().get_relation(self.entity.code)151        self.assertEqual(self.entity, test_rel)152    def test_relation_obj_Entity(self):153        test_rel = JournalEntry().get_relation(self.entity)154        self.assertEqual(self.entity, test_rel)155    def test_relation_code_Creditor(self):156        test_rel = CreditorInvoice().get_relation(self.creditor.entity.code)157        self.assertEqual(self.creditor, test_rel)158    def test_relation_obj_Creditor(self):159        test_rel = CreditorInvoice().get_relation(self.creditor)160        self.assertEqual(self.creditor, test_rel)161class TestModelEntryCreateObjectJournal(TestCase):162    def setUp(self):163        self.user = User.objects.create_user(164            'test_staff_user', 'test@example.com', '1234')165        self.user.is_staff = True166        self.user.save()167        self.ac = Account.objects.create(168            element='03', number='0450', name='Clearing - Payroll')169        self.ap = Account.objects.create(170            element='03', number='0709', name='PAYG Withholding')171        self.ab = Account.objects.create(172            element='15', number='1905', name='Bar')173        self.af = Account.objects.create(174            element='15', number='1903', name='FOH')175        self.ag = Account.objects.create(176            element='15', number='1910', name='Games')177        self.ak = Account.objects.create(178            element='15', number='1901', name='Kitchen')179        self.ast = Account.objects.create(180            element='15', number='1900', name='Staff')181        self.asl = Account.objects.create(182            element='03', number='0600', name='Superannuation Liability')183        self.asu = Account.objects.create(184            element='15', number='1950', name='Superannuation')185    def test_create_object_single_journalentry_passes(self):186        test_dump = 'value\tdate\ttype\t[03-0450]\t[03-0709]\t[15-1905]\t[15-1903]\t[15-1910]\t[15-1901]\t[15-1900]\t[03-0600]\t[15-1950]\r\n23,017.03\tJan. 31, 2016\tJournalEntry\t-17,817.96\t-3,447.00\t2,385.02\t6,991.98\t730.77\t8,255.34\t2,901.85\t-1,752.07\t1,752.07'  # noqa187        test_create_object = convert_import_to_objects(188            test_dump, user=self.user, object_name='JournalEntry')189        test_transaction = Transaction.objects.get(190            value=utils.make_decimal('23017.03'),191            date=dateparser.parse('31-Jan-2016'),192            source='subledgers.journals.models.JournalEntry',193            user=self.user)194        test_lines = [195            Line.objects.get(transaction=test_transaction,196                             account=self.ac,197                             value=utils.make_decimal(-17817.96)),198            Line.objects.get(transaction=test_transaction,199                             account=self.ap,200                             value=utils.make_decimal(-3447)),201            Line.objects.get(transaction=test_transaction,202                             account=self.ab,203                             value=utils.make_decimal(2385.02)),204            Line.objects.get(transaction=test_transaction,205                             account=self.af,206                             value=utils.make_decimal(6991.98)),207            Line.objects.get(transaction=test_transaction,208                             account=self.ag,209                             value=utils.make_decimal(730.77)),210            Line.objects.get(transaction=test_transaction,211                             account=self.ak,212                             value=utils.make_decimal(8255.34)),213            Line.objects.get(transaction=test_transaction,214                             account=self.ast,215                             value=utils.make_decimal(2901.85)),216            Line.objects.get(transaction=test_transaction,217                             account=self.asl,218                             value=utils.make_decimal(-1752.07)),219            Line.objects.get(transaction=test_transaction,220                             account=self.asu,221                             value=utils.make_decimal(1752.07)),222        ]223        test_result = [JournalEntry.objects.get(transaction=test_transaction,)]224        self.assertEqual(test_create_object, test_result)225        self.assertEqual(set(list(test_result[0].transaction.lines.all())),226                         set(test_lines))227class TestModelEntryGetCls(TestCase):228    # Successes!229    def test_get_cls_valid_model_JournalEntry_passes(self):230        self.assertEqual(utils.get_cls(JournalEntry), JournalEntry)231    def test_get_cls_valid_model_JournalEntry_str_passes(self):232        self.assertEqual(utils.get_cls('JournalEntry'), JournalEntry)233    def test_get_cls_valid_model_JournalEntry_source_passes(self):234        source = utils.get_source(JournalEntry)235        self.assertEqual(utils.get_cls(source), JournalEntry)236    def test_get_cls_valid_model_CreditorInvoice_passes(self):237        self.assertEqual(utils.get_cls(CreditorInvoice), CreditorInvoice)238    def test_get_cls_valid_model_CreditorInvoice_str_passes(self):239        self.assertEqual(utils.get_cls('CreditorInvoice'), CreditorInvoice)240    def test_get_cls_valid_model_CreditorInvoice_source_passes(self):241        source = utils.get_source(CreditorInvoice)242        self.assertEqual(utils.get_cls(source), CreditorInvoice)243    # Failures!244    def test_get_cls_not_valid_model_Account_failure(self):245        self.assertRaises(Exception, utils.get_cls, Account)246    def test_get_cls_not_valid_model_Creditor_failure(self):247        self.assertRaises(Exception, utils.get_cls, Creditor)248    def test_get_cls_valid_random_str_failure(self):249        self.assertRaises(Exception, utils.get_cls, 'asdf')250    def test_get_cls_valid_model_str_failure(self):251        self.assertRaises(Exception, utils.get_cls, 'Creditor')252    def test_get_cls_valid_model_source_failure(self):253        source = utils.get_source(Creditor)254        self.assertRaises(Exception, utils.get_cls, source)255class TestModelEntryCreateObjectFailure(TestCase):256    def setUp(self):257        self.user = User.objects.create_user(258            'test_staff_user', 'test@example.com', '1234')259        self.user.is_staff = True260        self.user.save()261        self.entity0 = Entity.objects.create(name="Bidvest")262        self.creditor0 = Creditor.objects.create(263            entity=self.entity0)264        self.entity1 = Entity.objects.create(name="efg456")265        self.creditor1 = Creditor.objects.create(266            entity=self.entity1)267        ACCOUNTS_PAYABLE_ACCOUNT = Account.objects.create(268            element='03', number='0300', name='ACP')269        settings.GST_CR_ACCOUNT = Account.objects.create(270            element='03', number='0733', name='GST account')271        settings.GST_DR_ACCOUNT = Account.objects.create(272            element='03', number='0713', name='GST account')273        self.account_CR_GST = settings.GST_CR_ACCOUNT274        self.account_DR_GST = settings.GST_DR_ACCOUNT275        self.account_creditors = ACCOUNTS_PAYABLE_ACCOUNT276        self.account_GST = settings.GST_CR_ACCOUNT277        self.a1 = Account.objects.create(278            element='15', number='0151', name='Test Account 1')279        self.a2 = Account.objects.create(280            element='15', number='0608', name='Test Account 2')281    def test_create_object_single_rubbish_list_failure(self):282        test_kwargs_list = ['asfd']283        self.assertRaises(284            Exception, convert_import_to_objects, test_kwargs_list)285    def test_create_object_single_incomplete_kwarg_failure(self):286        test_kwargs = {287            'relation': self.creditor0,288             # 'object': 'CreditorInvoice', # no object failure289                             'date': dateparser.parse('02-Jun-2017'),290             'gst_total': utils.make_decimal('$0.65'),291             'invoice_number': 'I38731476',292             'order_number': 'guild house',293             'reference': 'O37696095',294             'source': 'subledgers.creditors.models.CreditorInvoice',295             'value': utils.make_decimal('$485.27'),296             'user': self.user,297             'lines': [298                 (self.a1, utils.set_DR('478.12')),299                 (self.a2, utils.set_DR('6.5')),300                 (self.account_creditors, utils.set_CR('485.27')),  # noqa301                 (self.account_GST, utils.set_DR('0.65')),302             ]}303        self.assertRaises(Exception, convert_import_to_objects, test_kwargs)304# These are the End-to-End tests (dump >> objects)305# These are the only tests that really matter.306class TestModelEntryCreateObjectSale(TestCase):307    def setUp(self):308        self.user = User.objects.create_user(309            'test_staff_user', 'test@example.com', '1234')310        self.user.is_staff = True311        self.user.save()312        self.SALES_CLEARING_ACCOUNT = Account.objects.create(313            element='03', number='0410', name='Sales Clearing')314        settings.GST_CR_ACCOUNT = Account.objects.create(315            element='03', number='0733', name='GST account')316        settings.GST_DR_ACCOUNT = Account.objects.create(317            element='03', number='0713', name='GST account')318        self.account_CR_GST = settings.GST_CR_ACCOUNT319        self.account_DR_GST = settings.GST_DR_ACCOUNT320        self.a1 = Account.objects.create(321            element='10', number='0100', name='Sales')322    def test_create_object_single_sale_passes(self):323        test_dump = 'value\tdate\ttype\tgst_total\t[10-0100]\r\n3054.6\tJan. 23, 2016\tSale\t277.69\t2776.91'  # noqa324        test_create_object = convert_import_to_objects(325            test_dump, user=self.user, object_name='Sale')326        test_transaction = Transaction.objects.get(327            value=utils.make_decimal('3054.6'),328            date=dateparser.parse('23-Jan-2016'),329            source='subledgers.sales.models.Sale',330            user=self.user)331        test_lines = [332            Line.objects.get(transaction=test_transaction,333                             account=self.SALES_CLEARING_ACCOUNT,334                             value=utils.set_DR('3054.6')),335            Line.objects.get(transaction=test_transaction,336                             account=self.a1,337                             value=utils.set_CR('2776.91')),338            Line.objects.get(transaction=test_transaction,339                             account=self.account_DR_GST,340                             value=utils.set_CR('277.69')),341        ]342        test_result = [Sale.objects.get(transaction=test_transaction,)]343        self.assertEqual(test_create_object, test_result)344        self.assertEqual(set(list(test_result[0].transaction.lines.all())),345                         set(test_lines))346    def test_create_object_double_sale_passes(self):347        test_dump = 'value\tdate\ttype\tgst_total\t[10-0100]\r\n3054.6\tJan. 23, 2016\tSale\t277.69\t2776.91\r\n3877.7\tJan. 24, 2016\tSale\t352.52\t3525.18'  # noqa348        test_create_objects = convert_import_to_objects(349            test_dump, user=self.user, object_name='Sale')350        test_transaction1 = Transaction.objects.get(351            value=utils.make_decimal('3054.6'),352            date=dateparser.parse('23-Jan-2016'),353            source='subledgers.sales.models.Sale',354            user=self.user)355        test_transaction2 = Transaction.objects.get(356            value=utils.make_decimal('3877.7'),357            date=dateparser.parse('24-Jan-2016'),358            source='subledgers.sales.models.Sale',359            user=self.user)360        test_lines = [361            Line.objects.get(transaction=test_transaction1,362                             account=self.SALES_CLEARING_ACCOUNT,363                             value=utils.set_DR('3054.6')),364            Line.objects.get(transaction=test_transaction1,365                             account=self.a1,366                             value=utils.set_CR('2776.91')),367            Line.objects.get(transaction=test_transaction1,368                             account=self.account_DR_GST,369                             value=utils.set_CR('277.69')),370        ]371        test_result = [372            Sale.objects.get(transaction=test_transaction1),373            Sale.objects.get(transaction=test_transaction2)374        ]375        self.assertEqual(test_create_objects, test_result)376        self.assertEqual(set(list(test_result[0].transaction.lines.all())),377                         set(test_lines))378class TestModelEntryCreateObjectExpense(TestCase):379    def setUp(self):380        self.user = User.objects.create_user(381            'test_staff_user', 'test@example.com', '1234')382        self.user.is_staff = True383        self.user.save()384        self.entity0 = Entity.objects.create(name="7Eleven")385        self.creditor0 = Creditor.objects.create(386            entity=self.entity0)387        self.entity1 = Entity.objects.create(name="efg456")388        self.creditor1 = Creditor.objects.create(389            entity=self.entity1)390        self.EXPENSE_CLEARING_ACCOUNT = Account.objects.create(391            element='03', number='0430', name='Expense Clearing')392        settings.GST_CR_ACCOUNT = Account.objects.create(393            element='03', number='0733', name='GST account')394        settings.GST_DR_ACCOUNT = Account.objects.create(395            element='03', number='0713', name='GST account')396        self.account_CR_GST = settings.GST_CR_ACCOUNT397        self.account_DR_GST = settings.GST_DR_ACCOUNT398        self.a1 = Account.objects.create(399            element='15', number='0150', name='Test Account 1')400        self.a2 = Account.objects.create(401            element='15', number='0608', name='Test Account 2')402    def test_create_object_single_expense_no_entity_passes(self):403        test_dump = 'value\tdate\tnote\ttype\trelation\tgst_total\t[15-1420]\t[15-0715]\t[15-0605]\t[15-0150]\t[15-0500]\t[15-0650]\t[15-0705]\t[15-0710]\t[15-1010]\t[15-1400]\t[15-1430]\t[15-0620]\t[15-1470]\r\n53.47\t11-Dec-2015\t7-ELEVEN 2296 ERINDALE CENT\tExpense\t\t4.86\t\t\t\t48.61\t\t\t\t\t\t\t\t\t'  # noqa404        test_create_object = convert_import_to_objects(405            test_dump, user=self.user, object_name='Expense')406        # `.get(..` MUST BE BELOW `test_create_object`407        # get the objects that look exactly correct408        test_transaction = Transaction.objects.get(409            value=utils.make_decimal('53.47'),410            date=dateparser.parse('11-Dec-2015'),411            source='subledgers.expenses.models.Expense',412            note='7-ELEVEN 2296 ERINDALE CENT',413            user=self.user)414        test_lines = [Line.objects.get(transaction=test_transaction,415                                       account=self.a1,416                                       value=utils.set_DR('48.61')),417                      Line.objects.get(transaction=test_transaction,418                                       account=self.EXPENSE_CLEARING_ACCOUNT,419                                       value=utils.set_CR('53.47')),420                      Line.objects.get(transaction=test_transaction,421                                       account=self.account_CR_GST,422                                       value=utils.set_DR('4.86')), ]423        test_result = [Expense.objects.get(424            transaction=test_transaction,425        )]426        self.assertEqual(test_create_object, test_result)427        self.assertEqual(set(list(test_result[0].transaction.lines.all())),428                         set(test_lines))429    def test_create_object_single_expense_using_entity_passes(self):430        test_dump = 'value\tdate\tnote\ttype\trelation\tgst_total\t[15-1420]\t[15-0715]\t[15-0605]\t[15-0150]\t[15-0500]\t[15-0650]\t[15-0705]\t[15-0710]\t[15-1010]\t[15-1400]\t[15-1430]\t[15-0620]\t[15-1470]\r\n53.47\t11-Dec-2015\t7-ELEVEN 2296 ERINDALE CENT\tExpense\t7ELEVE\t4.86\t\t\t\t48.61\t\t\t\t\t\t\t\t\t'  # noqa431        test_create_object = convert_import_to_objects(432            test_dump, user=self.user, object_name='Expense')433        # `.get(..` MUST BE BELOW `test_create_object`434        # get the objects that look exactly correct435        test_transaction = Transaction.objects.get(436            value=utils.make_decimal('53.47'),437            date=dateparser.parse('11-Dec-2015'),438            source='subledgers.expenses.models.Expense',439            note='7-ELEVEN 2296 ERINDALE CENT',440            user=self.user)441        test_lines = [Line.objects.get(transaction=test_transaction,442                                       account=self.a1,443                                       value=utils.set_DR('48.61')),444                      Line.objects.get(transaction=test_transaction,445                                       account=self.EXPENSE_CLEARING_ACCOUNT,446                                       value=utils.set_CR('53.47')),447                      Line.objects.get(transaction=test_transaction,448                                       account=self.account_CR_GST,449                                       value=utils.set_DR('4.86')), ]450        test_result = [Expense.objects.get(451            transaction=test_transaction,452            relation=self.entity0,453        )]454        self.assertEqual(test_create_object, test_result)455        self.assertEqual(set(list(test_result[0].transaction.lines.all())),456                         set(test_lines))457    def test_create_object_single_expense_using_entity_GST_variation_passes(self):  # noqa458        # GST allocated, not using `gst_total` field.459        test_dump = 'value\tdate\tnote\ttype\trelation\t[03-0733]\t[15-1420]\t[15-0715]\t[15-0605]\t[15-0150]\t[15-0500]\t[15-0650]\t[15-0705]\t[15-0710]\t[15-1010]\t[15-1400]\t[15-1430]\t[15-0620]\t[15-1470]\r\n53.47\t11-Dec-2015\t7-ELEVEN 2296 ERINDALE CENT\tExpense\t7ELEVE\t4.86\t\t\t\t48.61\t\t\t\t\t\t\t\t\t'  # noqa460        test_create_object = convert_import_to_objects(461            test_dump, user=self.user, object_name='Expense')462        test_transaction = Transaction.objects.get(463            value=utils.make_decimal('53.47'),464            date=dateparser.parse('11-Dec-2015'),465            source='subledgers.expenses.models.Expense',466            note='7-ELEVEN 2296 ERINDALE CENT',467            user=self.user)468        test_lines = [Line.objects.get(transaction=test_transaction,469                                       account=self.a1,470                                       value=utils.set_DR('48.61')),471                      Line.objects.get(transaction=test_transaction,472                                       account=self.EXPENSE_CLEARING_ACCOUNT,473                                       value=utils.set_CR('53.47')),474                      Line.objects.get(transaction=test_transaction,475                                       account=self.account_CR_GST,476                                       value=utils.set_DR('4.86')), ]477        test_result = [Expense.objects.get(478            transaction=test_transaction,479            relation=self.entity0,480        )]481        self.assertEqual(test_create_object, test_result)482        self.assertEqual(set(list(test_result[0].transaction.lines.all())),483                         set(test_lines))484    def test_create_object_single_expense_in_credit_passes(self):  # noqa485        # Is a credit (ie negative value expense)486        test_dump = 'value\tdate\tnote\ttype\trelation\t[03-0733]\t[15-1420]\t[15-0715]\t[15-0605]\t[15-0150]\t[15-0500]\t[15-0650]\t[15-0705]\t[15-0710]\t[15-1010]\t[15-1400]\t[15-1430]\t[15-0620]\t[15-1470]\r\n53.47\t11-Dec-2015\t7-ELEVEN 2296 ERINDALE CENT\tExpense\t7ELEVE\t-4.86\t\t\t\t-48.61\t\t\t\t\t\t\t\t\t'  # noqa487        test_create_object = convert_import_to_objects(488            test_dump, user=self.user, object_name='Expense')489        test_transaction = Transaction.objects.get(490            value=utils.make_decimal('53.47'),491            date=dateparser.parse('11-Dec-2015'),492            source='subledgers.expenses.models.Expense',493            note='7-ELEVEN 2296 ERINDALE CENT',494            user=self.user)495        test_lines = [Line.objects.get(transaction=test_transaction,496                                       account=self.a1,497                                       value=utils.set_CR('48.61')),498                      Line.objects.get(transaction=test_transaction,499                                       account=self.EXPENSE_CLEARING_ACCOUNT,500                                       value=utils.set_DR('53.47')),501                      Line.objects.get(transaction=test_transaction,502                                       account=self.account_CR_GST,503                                       value=utils.set_CR('4.86')), ]504        test_result = [Expense.objects.get(505            transaction=test_transaction,506            relation=self.entity0,507        )]508        self.assertEqual(test_create_object, test_result)509        self.assertEqual(set(list(test_result[0].transaction.lines.all())),510                         set(test_lines))511class TestModelEntryCreateObjectTransactionDelete(TransactionTestCase):512    """ This setUp is to test that superfluous `Transaction` objects are not513    created if there is a validation problem with the `Entry`, as `Transaction`514    must be created before `Entry`.515    This is important for system integrity, until there is some generic516    relation from `Transaction` that we can ensure exists.517    # @@ TODO: This important integrity check needs to be done better.518    """519    def setUp(self):520        self.user = User.objects.create_user(521            'test_staff_user', 'test@example.com', '1234')522        self.user.is_staff = True523        self.user.save()524    def test_create_object_failure_transaction_object_delete(self):  # noqa525        # has blank entity, should fail to create CreditorInvoice526        test_dump = "creditor\tdate\tinvoice_number\treference\tvalue\tgst_total\torder_number\t[15-0608]\t[15-0151]\t[15-0155]\t[15-0301]\t[15-0305]\r\n\t02-Jun-2017\tI38731476\tO37696095\t$485.27\t$0.65\tguild house\t6.5\t478.12\t\t\t"  # NOQA527        self.assertRaises(Exception, convert_import_to_objects,528                          test_dump, user=self.user,529                          object_name='CreditorInvoice')530        test_transaction_kwargs = {531            'value': utils.make_decimal('$485.27'),532            'date': dateparser.parse('02-Jun-2017'),533            'source': 'subledgers.creditors.models.CreditorInvoice',534            'user': self.user}535        self.assertRaises(ObjectDoesNotExist, Transaction.objects.get,536                          **test_transaction_kwargs)537        # This test saved me one time, when Entry was finding a way to be538        # created anyway, because wasn't validating `value` = bal.539        test_transaction_kwargs = {540            'source': 'subledgers.creditors.models.CreditorInvoice',541            'user': self.user}542        self.assertEqual(Transaction.objects.filter(543            **test_transaction_kwargs).count(), 0)544class TestModelEntryCreateObjectCreditorInvoice(TestCase):545    def setUp(self):546        self.user = User.objects.create_user(547            'test_staff_user', 'test@example.com', '1234')548        self.user.is_staff = True549        self.user.save()550        self.entity0 = Entity.objects.create(name="Bidvest")551        self.creditor0 = Creditor.objects.create(552            entity=self.entity0)553        self.entity1 = Entity.objects.create(name="efg456")554        self.creditor1 = Creditor.objects.create(555            entity=self.entity1)556        ACCOUNTS_PAYABLE_ACCOUNT = Account.objects.create(557            element='03', number='0300', name='ACP')558        settings.GST_CR_ACCOUNT = Account.objects.create(559            element='03', number='0733', name='GST account')560        settings.GST_DR_ACCOUNT = Account.objects.create(561            element='03', number='0713', name='GST account')562        self.account_CR_GST = settings.GST_CR_ACCOUNT563        self.account_DR_GST = settings.GST_DR_ACCOUNT564        self.account_creditors = ACCOUNTS_PAYABLE_ACCOUNT565        self.a1 = Account.objects.create(566            element='15', number='0151', name='Test Account 1')567        self.a2 = Account.objects.create(568            element='15', number='0608', name='Test Account 2')569    def test_create_object_single_creditor_invoice_using_entity_passes(self):570        test_dump = "creditor\tdate\tinvoice_number\treference\tvalue\tgst_total\torder_number\t[15-0608]\t[15-0151]\t[15-0155]\t[15-0301]\t[15-0305]\r\nBIDVES\t02-Jun-2017\tI38731476\tO37696095\t485.27\t0.65\tguild house\t6.5\t478.12\t\t\t"  # NOQA571        test_create_object = convert_import_to_objects(572            test_dump, user=self.user, object_name='CreditorInvoice')573        # `.get(..` MUST BE BELOW `test_create_object`574        # get the objects that look exactly correct575        test_transaction = Transaction.objects.get(576            value=utils.make_decimal('485.27'),577            date=dateparser.parse('02-Jun-2017'),578            source='subledgers.creditors.models.CreditorInvoice',579            user=self.user)580        test_lines = [Line.objects.get(transaction=test_transaction,581                                       account=self.a1,582                                       value=utils.set_DR('478.12')),583                      Line.objects.get(transaction=test_transaction,584                                       account=self.a2,585                                       value=utils.set_DR('6.5')),586                      Line.objects.get(transaction=test_transaction,587                                       account=self.account_creditors,588                                       value=utils.set_CR('485.27')),589                      Line.objects.get(transaction=test_transaction,590                                       account=self.account_CR_GST,591                                       value=utils.set_DR('0.65')), ]592        test_result = [CreditorInvoice.objects.get(593            transaction=test_transaction,594            relation=self.creditor0,595            gst_total=utils.make_decimal('0.65'),596            invoice_number='I38731476',597            order_number='guild house',598            reference='O37696095',)]599        self.assertEqual(test_create_object, test_result)600        self.assertEqual(set(list(test_result[0].transaction.lines.all())),601                         set(test_lines))602    def test_create_object_single_creditor_invoice_passes(self):603        test_dump = "creditor\tdate\tinvoice_number\treference\tvalue\tgst_total\torder_number\t[15-0608]\t[15-0151]\t[15-0155]\t[15-0301]\t[15-0305]\r\nBIDVES\t02-Jun-2017\tI38731476\tO37696095\t1485.27\t0.65\tguild house\t6.5\t1,478.12\t\t\t"  # NOQA604        test_create_object = convert_import_to_objects(605            test_dump, user=self.user, object_name='CreditorInvoice')606        # `.get(..` MUST BE BELOW `test_create_object`607        # get the objects that look exactly correct608        test_transaction = Transaction.objects.get(609            value=utils.make_decimal('1485.27'),610            date=dateparser.parse('02-Jun-2017'),611            source='subledgers.creditors.models.CreditorInvoice',612            user=self.user)613        test_lines = [Line.objects.get(transaction=test_transaction,614                                       account=self.a1,615                                       value=utils.set_DR('1478.12')),616                      Line.objects.get(transaction=test_transaction,617                                       account=self.a2,618                                       value=utils.set_DR('6.5')),619                      Line.objects.get(transaction=test_transaction,620                                       account=self.account_creditors,621                                       value=utils.set_CR('1485.27')),622                      Line.objects.get(transaction=test_transaction,623                                       account=self.account_CR_GST,624                                       value=utils.set_DR('0.65')), ]625        test_result = [CreditorInvoice.objects.get(626            transaction=test_transaction,627            relation=self.creditor0,628            gst_total=utils.make_decimal('0.65'),629            invoice_number='I38731476',630            order_number='guild house',631            reference='O37696095',)]632        self.assertEqual(test_create_object, test_result)633        self.assertEqual(set(list(test_result[0].transaction.lines.all())),634                         set(test_lines))635    def test_create_object_single_creditor_invoice_using_creditor_invoice(self):  # noqa636        test_dump = "creditor\tdate\tinvoice_number\treference\tvalue\tgst_total\torder_number\t[15-0608]\t[15-0151]\t[15-0155]\t[15-0301]\t[15-0305]\r\nBIDVES\t02-Jun-2017\tI38731476\tO37696095\t485.27\t0.65\tguild house\t6.5\t478.12\t\t\t"  # NOQA637        test_create_object = convert_import_to_objects(638            test_dump, user=self.user, object_name='CreditorInvoice')639        # `.get(..` MUST BE BELOW `test_create_object`640        # get the objects that look exactly correct641        test_transaction = Transaction.objects.get(642            value=utils.make_decimal('485.27'),643            date=dateparser.parse('02-Jun-2017'),644            source='subledgers.creditors.models.CreditorInvoice',645            user=self.user)646        test_lines = [Line.objects.get(transaction=test_transaction,647                                       account=self.a1,648                                       value=utils.set_DR('478.12')),649                      Line.objects.get(transaction=test_transaction,650                                       account=self.a2,651                                       value=utils.set_DR('6.5')),652                      Line.objects.get(transaction=test_transaction,653                                       account=self.account_creditors,654                                       value=utils.set_CR('485.27')),655                      Line.objects.get(transaction=test_transaction,656                                       account=self.account_CR_GST,657                                       value=utils.set_DR('0.65')), ]658        test_result = [CreditorInvoice.objects.get(659            transaction=test_transaction,660            relation=self.creditor0,661            gst_total=utils.make_decimal('0.65'),662            invoice_number='I38731476',663            order_number='guild house',664            reference='O37696095',)]665        self.assertEqual(test_create_object, test_result)666        self.assertEqual(set(list(test_result[0].transaction.lines.all())),667                         set(test_lines))668    def test_create_object_double_creditor_invoice_using_creditor_invoice(self):  # noqa669        test_dump = "creditor\tdate\tinvoice_number\treference\tvalue\tgst_total\torder_number\t[15-0608]\t[15-0151]\t[15-0155]\t[15-0301]\t[15-0305]\r\nBIDVES\t02-Jun-2017\tI38731476\tO37696095\t485.27\t0.65\tguild house\t6.5\t478.12\t\t\t\r\nEFG456\t02-Jun-2017\tI38728128\tO37688231\t217.86\t0.29\tguild house\t2.92\t214.65\t\t\t"  # noqa670        test_create_object = convert_import_to_objects(671            test_dump, user=self.user, object_name='CreditorInvoice')672        # `.get(..` MUST BE BELOW `test_create_object`673        # get the objects that look exactly correct674        test_transaction0 = Transaction.objects.get(675            value=utils.make_decimal('485.27'),676            date=dateparser.parse('02-Jun-2017'),677            source='subledgers.creditors.models.CreditorInvoice',678            user=self.user)679        test_transaction1 = Transaction.objects.get(680            value=utils.make_decimal('217.86'),681            date=dateparser.parse('02-Jun-2017'),682            source='subledgers.creditors.models.CreditorInvoice',683            user=self.user)684        test_lines0 = [Line.objects.get(transaction=test_transaction0,685                                        account=self.a1,686                                        value=utils.set_DR('478.12')),687                       Line.objects.get(transaction=test_transaction0,688                                        account=self.a2,689                                        value=utils.set_DR('6.5')),690                       Line.objects.get(transaction=test_transaction0,691                                        account=self.account_creditors,692                                        value=utils.set_CR('485.27')),693                       Line.objects.get(transaction=test_transaction0,694                                        account=self.account_CR_GST,695                                        value=utils.set_DR('0.65')), ]696        test_lines1 = [Line.objects.get(transaction=test_transaction1,697                                        account=self.a1,698                                        value=utils.set_DR('214.65')),699                       Line.objects.get(transaction=test_transaction1,700                                        account=self.a2,701                                        value=utils.set_DR('2.92')),702                       Line.objects.get(transaction=test_transaction1,703                                        account=self.account_creditors,704                                        value=utils.set_CR('217.86')),705                       Line.objects.get(transaction=test_transaction1,706                                        account=self.account_CR_GST,707                                        value=utils.set_DR('0.29')), ]708        test_result = [709            CreditorInvoice.objects.get(710                transaction=test_transaction0,711                relation=self.creditor0,712                gst_total=utils.make_decimal('0.65'),713                invoice_number='I38731476',714                order_number='guild house',715                reference='O37696095',),716            CreditorInvoice.objects.get(717                transaction=test_transaction1,718                relation=self.creditor1,719                gst_total=utils.make_decimal('0.29'),720                invoice_number='I38728128',721                order_number='guild house',722                reference='O37688231',),723        ]724        self.assertEqual(test_create_object, test_result)725        self.assertEqual(set(list(test_result[0].transaction.lines.all())),726                         set(test_lines0))727        self.assertEqual(set(list(test_result[1].transaction.lines.all())),...test_api.py
Source:test_api.py  
...79        pass80    @skip("on_delete set to PROTECT")81    def test_bulk_delete_objects(self):82        pass83    def test_create_object(self):84        self.validation_excluded_fields = ["address_objects"]85        return super().test_create_object()86    def test_update_object(self):87        self.validation_excluded_fields = ["address_objects"]88        return super().test_update_object()89    def test_bulk_create_objects(self):90        self.validation_excluded_fields = ["address_objects"]91        return super().test_bulk_create_objects()92class ServiceObjectAPIViewTest(APIViewTestCases.APIViewTestCase):93    """Test the ServiceObject viewsets."""94    model = models.ServiceObject95    bulk_update_data = {"description": "test update description"}96    choices_fields = ["ip_protocol"]97    @classmethod98    def setUpTestData(cls):99        """Create test data for API calls."""100        cls.create_data = [101            {"name": "HTTP", "port": "8088", "ip_protocol": "TCP"},102            {"name": "HTTP", "port": "8080-8088", "ip_protocol": "TCP"},103        ]104        create_env()105    @skip("Not implemented")106    def test_list_objects_brief(self):107        pass108    @skip("on_delete set to PROTECT")109    def test_delete_object(self):110        pass111    @skip("on_delete set to PROTECT")112    def test_bulk_delete_objects(self):113        pass114class ServiceGroupAPIViewTest(APIViewTestCases.APIViewTestCase):115    """Test the ServiceGroup viewsets."""116    model = models.ServiceObjectGroup117    bulk_update_data = {"description": "test update description"}118    @classmethod119    def setUpTestData(cls):120        """Create test data for API calls."""121        create_env()122        svc_obj = models.ServiceObject.objects.first()123        cls.create_data = [124            {"name": "test1", "service_objects": [svc_obj.id]},125            {"name": "test2", "service_objects": [svc_obj.id]},126        ]127    @skip("Not implemented")128    def test_list_objects_brief(self):129        pass130    @skip("on_delete set to PROTECT")131    def test_delete_object(self):132        pass133    @skip("on_delete set to PROTECT")134    def test_bulk_delete_objects(self):135        pass136    def test_create_object(self):137        self.validation_excluded_fields = ["service_objects"]138        return super().test_create_object()139    def test_update_object(self):140        self.validation_excluded_fields = ["service_objects"]141        return super().test_update_object()142    def test_bulk_create_objects(self):143        self.validation_excluded_fields = ["service_objects"]144        return super().test_bulk_create_objects()145class UserObjectAPIViewTest(APIViewTestCases.APIViewTestCase):146    """Test the User viewsets."""147    model = models.UserObject148    bulk_update_data = {"name": "User Name 123"}149    @classmethod150    def setUpTestData(cls):151        """Create test data for API calls."""152        cls.create_data = [153            {"username": "test1", "name": "Foo"},154            {"username": "test2", "name": "Bar"},155        ]156        create_env()157    @skip("Not implemented")158    def test_list_objects_brief(self):159        pass160    @skip("on_delete set to PROTECT")161    def test_delete_object(self):162        pass163    @skip("on_delete set to PROTECT")164    def test_bulk_delete_objects(self):165        pass166class UserObjectGroupAPIViewTest(APIViewTestCases.APIViewTestCase):167    """Test the UserGroup viewsets."""168    model = models.UserObjectGroup169    bulk_update_data = {"description": "test update description"}170    @classmethod171    def setUpTestData(cls):172        """Create test data for API calls."""173        create_env()174        user = models.UserObject.objects.first()175        cls.create_data = [176            {"name": "test1", "user_objects": [user.id]},177            {"name": "test2", "user_objects": [user.id]},178        ]179    @skip("Not implemented")180    def test_list_objects_brief(self):181        pass182    @skip("on_delete set to PROTECT")183    def test_delete_object(self):184        pass185    @skip("on_delete set to PROTECT")186    def test_bulk_delete_objects(self):187        pass188    def test_create_object(self):189        self.validation_excluded_fields = ["user_objects"]190        return super().test_create_object()191    def test_update_object(self):192        self.validation_excluded_fields = ["user_objects"]193        return super().test_update_object()194    def test_bulk_create_objects(self):195        self.validation_excluded_fields = ["user_objects"]196        return super().test_bulk_create_objects()197class ZoneAPIViewTest(APIViewTestCases.APIViewTestCase):198    """Test the Zone viewsets."""199    model = models.Zone200    bulk_update_data = {"description": "test update description"}201    @classmethod202    def setUpTestData(cls):203        """Create test data for API calls."""204        cls.create_data = [205            {"name": "trust"},206            {"name": "untrust"},207        ]208        create_env()209    @skip("Not implemented")210    def test_list_objects_brief(self):211        pass212    @skip("on_delete set to PROTECT")213    def test_delete_object(self):214        pass215    @skip("on_delete set to PROTECT")216    def test_bulk_delete_objects(self):217        pass218class PolicyRuleAPIViewTest(APIViewTestCases.APIViewTestCase):219    """Test the PolicyRule viewsets."""220    model = models.PolicyRule221    bulk_update_data = {"log": False}222    choices_fields = ["action"]223    @classmethod224    def setUpTestData(cls):225        """Create test data for API calls."""226        create_env()227        src_usr = models.UserObject.objects.first()228        src_addr = models.AddressObject.objects.first()229        dest_addr = models.AddressObject.objects.last()230        svc = models.ServiceObject.objects.first()231        cls.create_data = [232            {233                # pylint: disable=R0801234                "source_user": [src_usr.id],235                "source_address": [src_addr.id],236                "destination_address": [dest_addr.id],237                "action": "deny",238                "log": True,239                "service": [svc.id],240                "name": "test rule",241            },242            {243                "source_user": [src_usr.id],244                "source_address": [src_addr.id],245                "destination_address": [dest_addr.id],246                "action": "deny",247                "log": False,248                "service": [svc.id],249                "name": "test rule",250            },251        ]252    def test_list_objects_brief(self):253        pass254    @skip("on_delete set to PROTECT")255    def test_delete_object(self):256        pass257    @skip("on_delete set to PROTECT")258    def test_bulk_delete_objects(self):259        pass260class PolicyAPIViewTest(APIViewTestCases.APIViewTestCase):261    """Test the Policy viewsets."""262    model = models.Policy263    bulk_update_data = {"description": "test update description"}264    @classmethod265    def setUpTestData(cls):266        """Create test data for API calls."""267        create_env()268        pol_rule = models.PolicyRule.objects.first()269        cls.create_data = [270            {"name": "test 1", "policy_rules": [{"rule": pol_rule.id}]},271            {"name": "test 2", "policy_rules": [{"rule": pol_rule.id}], "description": "Test desc"},272        ]273    @skip("Not implemented")274    def test_list_objects_brief(self):275        pass276    def test_create_object(self):277        self.validation_excluded_fields = ["policy_rules"]278        return super().test_create_object()279    def test_update_object(self):280        self.validation_excluded_fields = ["policy_rules"]281        return super().test_update_object()282    def test_bulk_create_objects(self):283        self.validation_excluded_fields = ["policy_rules"]284        return super().test_bulk_create_objects()285    @skip("on_delete set to PROTECT")286    def test_delete_object(self):287        pass288    @skip("on_delete set to PROTECT")289    def test_bulk_delete_objects(self):...tests.py
Source:tests.py  
2# Create your tests here.3from refugeerouter.buiseness_logic import group_fits_in_flat4from refugeerouter.models import Refugee, Flat, Group5class RefugeeTest(TestCase):6    def test_create_object(self):7        r = Refugee.objects.create(last_name='Mustermann', first_name='Max', gender=Refugee.GENDER_MALE, age=42,8                                   contact_data='tel: +42 4242 42424242', origin='city of god', origin_checked=False)9        assert r.id != None10class FlatTest(TestCase):11    def test_create_object(self):12        f = Flat.objects.create(rooms=3, shared_kitchen=False, shared_bath=True, owner_last_name="Wurzel",13                                owner_first_name="Hans", address="Blafooweg 17, 11111 Berlin",14                                contact_data="+49 176 42424242", max_adults=5, min_kids_age=4)15        assert f.id != None16class GroupTest(TestCase):17    def test_create_object(self):18        r = Refugee.objects.create(last_name='Mustermann', first_name='Max', age=42,19                                   contact_data='tel: +42 4242 42424242', origin='city of god', origin_checked=False)20        r2 = Refugee.objects.create(last_name='Mustermann', first_name='Michael', age=42,21                                    contact_data='tel: +42 4242 42424242', origin='city of god', origin_checked=False)22        g = Group.objects.create(name='Group Foo', contact="+49 176 42424242", wish_city="Munich")23        r.group = g24        r.save()25        r2.group = g26        r2.save()27        assert g.refugee_set.count() == 228class FlatTest(TestCase):29    def test_create_object(self):30        f = Flat.objects.create(owner_last_name='Owner', owner_first_name='Owner Firstname',31                                contact_data="+49 176 42424242", address='Foobar Street; Foobar City', max_male=0,32                                max_kids=2, max_adults=1, min_kids_age=0, max_kids_age=2)33        assert f.id != None34    def test_fit_group(self):35        f = Flat.objects.create(owner_last_name='Owner', owner_first_name='Owner Firstname',36                                contact_data="+49 176 42424242", address='Foobar Street; Foobar City', max_male=0,37                                max_kids=2, max_adults=1, min_kids_age=0, max_kids_age=2)38        f2 = Flat.objects.create(owner_last_name='Owner', owner_first_name='Owner Firstname',39                                contact_data="+49 176 42424242", address='Foobar Street; Foobar City', max_male=2,40                                max_kids=2, max_adults=2, min_kids_age=0, max_kids_age=2)41        r = Refugee.objects.create(last_name='Mustermann', first_name='Max', age=42,42                                   contact_data='tel: +42 4242 42424242', origin='city of god', origin_checked=False)43        r2 = Refugee.objects.create(last_name='Mustermann', first_name='Michael', age=42,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
