Best Python code snippet using pandera_python
test_online_sync_creation_statement.py
Source:test_online_sync_creation_statement.py  
...29            'user_type_id': self.env.ref('account.data_account_type_fixed_assets').id30        })31    # This method return a list of transactions with the given dates32    # amount for each transactions is 1033    def create_transactions(self, dates):34        transactions = []35        for date in dates:36            transactions.append({37                'online_transaction_identifier': self.transaction_id,38                'date': fields.Date.from_string(date),39                'payment_ref': 'transaction_' + str(self.transaction_id),40                'amount': 10,41            })42            self.transaction_id += 143        return transactions44    def create_transaction_partner(self, date=False, partner_id=False, partner_info=False):45        tr = {46            'online_transaction_identifier': self.transaction_id,47            'date': fields.Date.from_string(date),48            'payment_ref': 'transaction_p',49            'amount': 50,50        }51        if partner_id:52            tr['partner_id'] = partner_id53        if partner_info:54            tr['online_partner_information'] = partner_info55        self.transaction_id += 156        return [tr]57    def assertDate(self, date1, date2):58        if isinstance(date1, str):59            date1 = fields.Date.from_string(date1)60        if isinstance(date2, str):61            date2 = fields.Date.from_string(date2)62        self.assertEqual(date1, date2)63    def assertBankStatementValues(self, bank_stmnt_record, bank_stmnt_dict):64        for index in range(len(bank_stmnt_record)):65            bank_stmnt_line_dict = bank_stmnt_dict[index].pop('line_ids')66            debug = [bank_stmnt_dict[index]]67            self.assertRecordValues(bank_stmnt_record[index], debug)68            self.assertRecordValues(bank_stmnt_record[index].line_ids, bank_stmnt_line_dict)69    def confirm_bank_statement(self, statement):70        for line in statement.line_ids:71            liquidity_lines, suspense_lines, other_lines = line._seek_for_lines()72            line.reconcile([{73                'name': 'toto',74                'account_id': self.account.id,75                'balance': sum(suspense_lines.mapped('balance')),76                'currency_id': False,77            }])78        if statement.state == 'open':79            statement.button_post()80        statement.button_validate()81        return statement82    # Tests83    def test_creation_initial_sync_statement(self):84        transactions = self.create_transactions(['2016-01-01', '2016-01-03'])85        self.online_account.balance = 100086        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)87        # Since ending balance is 1000$ and we only have 20$ of transactions and that it is the first statement88        # it should create a statement before this one with the initial statement line89        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')90        self.assertEqual(len(created_bnk_stmt), 2, 'Should have created an initial bank statement and one for the synchronization')91        # Since a statement already exists, next transactions should not create an initial statement even if ending_balance92        # is greater than the sum of transactions93        transactions = self.create_transactions(['2016-01-05'])94        self.online_account.balance = 200095        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)96        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')97        self.assertBankStatementValues(98            created_bnk_stmt,99            [100                {101                    'balance_start': 0.0,102                    'balance_end_real': 980.0,103                    'date': fields.Date.from_string('2015-12-31'),104                    'line_ids': [{'amount': 980.0}]105                },106                {107                    'balance_start': 980.0,108                    'balance_end_real': 1000.0,109                    'date': fields.Date.from_string('2016-01-03'),110                    'line_ids': [111                        {'amount': 10.0},112                        {'amount': 10.0}113                    ]114                },115                {116                    'balance_start': 1000.0,117                    'balance_end_real': 2000.0,118                    'date': fields.Date.from_string('2016-01-05'),119                    'line_ids': [{'amount': 10.0}]120                },121            ]122        )123    def test_creation_initial_sync_statement_bis(self):124        transactions = self.create_transactions(['2016-01-01', '2016-01-03'])125        self.online_account.balance = 20126        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)127        # Since ending balance is 20$ and we only have 20$ of transactions and that it is the first statement128        # it should NOT create a initial statement before this one129        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')130        self.assertBankStatementValues(131            created_bnk_stmt,132            [{133                'balance_start': 0.0,134                'balance_end_real': 20.0,135                'date': fields.Date.from_string('2016-01-03'),136                'line_ids': [137                    {'amount': 10.0},138                    {'amount': 10.0}139                ]140            }]141        )142        self.assertEqual(len(created_bnk_stmt[0].line_ids), 2, 'Should have two lines')143    def test_creation_every_sync(self):144        self.bank_journal.write({'bank_statement_creation_groupby': 'none'})145        # Create one statement with 2 lines146        transactions = self.create_transactions(['2016-01-01', '2016-01-03'])147        self.online_account.balance = 20148        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)149        # Create another statement with 2 lines150        transactions = self.create_transactions(['2016-01-02', '2016-01-05'])151        self.online_account.balance = 40152        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)153        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')154        self.assertBankStatementValues(155            created_bnk_stmt,156            [157                {158                    'name': 'BNKon Statement 2016/01/00001',159                    'balance_start': 0.0,160                    'balance_end_real': 20.0,161                    'date': fields.Date.from_string('2016-01-03'),162                    'line_ids': [163                        {'amount': 10.0},164                        {'amount': 10.0}165                    ]166                },167                {168                    'name': 'BNKon Statement 2016/01/00002',169                    'balance_start': 20.0,170                    'balance_end_real': 40.0,171                    'date': fields.Date.from_string('2016-01-05'),172                    'line_ids': [173                        {'amount': 10.0},174                        {'amount': 10.0}175                    ]176                }177            ]178        )179        # If we create a statement with a transactions max date in the past, it will be created in the past180        # Also the account balance will be set on the last statement181        transactions = self.create_transactions(['2016-01-04'])182        self.online_account.balance = 70183        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)184        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')185        self.assertBankStatementValues(186            created_bnk_stmt,187            [188                {189                    'balance_start': 0.0,190                    'balance_end_real': 20.0,191                    'date': fields.Date.from_string('2016-01-03'),192                    'line_ids': [193                        {'amount': 10.0},194                        {'amount': 10.0}195                    ]196                },197                {198                    'balance_start': 20.0,199                    'balance_end_real': 30.0,200                    'date': fields.Date.from_string('2016-01-04'),201                    'line_ids': [{'amount': 10.0}]202                },203                {204                    'balance_start': 30.0,205                    'balance_end_real': 70.0,206                    'date': fields.Date.from_string('2016-01-05'),207                    'line_ids': [208                        {'amount': 10.0},209                        {'amount': 10.0}210                    ]211                }212            ]213        )214        # If we create a statement with a transactions max date in the past, and that a statement at that date215        # already exists, it will be added to that statement216        transactions = self.create_transactions(['2016-01-04', '2016-01-04'])217        self.online_account.balance = 70218        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)219        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')220        self.assertBankStatementValues(221            created_bnk_stmt,222            [223                {224                    'balance_start': 0.0,225                    'balance_end_real': 20.0,226                    'date': fields.Date.from_string('2016-01-03'),227                    'line_ids': [228                        {'amount': 10.0},229                        {'amount': 10.0}230                    ]231                },232                {233                    'balance_start': 20.0,234                    'balance_end_real': 50.0,235                    'date': fields.Date.from_string('2016-01-04'),236                    'line_ids': [237                        {'amount': 10.0},238                        {'amount': 10.0},239                        {'amount': 10.0}240                    ]241                },242                {243                    'balance_start': 50.0,244                    'balance_end_real': 70.0,245                    'date': fields.Date.from_string('2016-01-05'),246                    'line_ids': [247                        {'amount': 10.0},248                        {'amount': 10.0}249                    ]250                }251            ]252        )253    def test_creation_every_day(self):254        self.bank_journal.write({'bank_statement_creation_groupby': 'day'})255        transactions = self.create_transactions(['2016-01-10', '2016-01-15'])256        # first synchronization, no previous bank statement257        self.online_account.balance = 20258        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)259        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')260        self.assertBankStatementValues(261            created_bnk_stmt,262            [263                {264                    'balance_start': 0.0,265                    'balance_end_real': 10.0,266                    'date': fields.Date.from_string('2016-01-10'),267                    'line_ids': [{'amount': 10.0}]268                },269                {270                    'balance_start': 10.0,271                    'balance_end_real': 20.0,272                    'date': fields.Date.from_string('2016-01-15'),273                    'line_ids': [{'amount': 10.0}]274                }275            ]276        )277        # Fetch new transactions, two will be added to already existing statement, two will create new statements in between278        # and one will create new statements afterwards279        transactions = self.create_transactions(['2016-01-10', '2016-01-10', '2016-01-12', '2016-01-13', '2016-01-16'])280        self.online_account.balance = 70281        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)282        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')283        self.assertBankStatementValues(284            created_bnk_stmt,285            [286                {287                    'balance_start': 0.0,288                    'balance_end_real': 30.0,289                    'date': fields.Date.from_string('2016-01-10'),290                    'line_ids': [291                        {'amount': 10.0},292                        {'amount': 10.0},293                        {'amount': 10.0}294                    ]295                },296                {297                    'balance_start': 30.0,298                    'balance_end_real': 40.0,299                    'date': fields.Date.from_string('2016-01-12'),300                    'line_ids': [{'amount': 10.0}]301                },302                {303                    'balance_start': 40.0,304                    'balance_end_real': 50.0,305                    'date': fields.Date.from_string('2016-01-13'),306                    'line_ids': [{'amount': 10.0}]307                },308                {309                    'balance_start': 50.0,310                    'balance_end_real': 60.0,311                    'date': fields.Date.from_string('2016-01-15'),312                    'line_ids': [{'amount': 10.0}]313                },314                {315                    'balance_start': 60.0,316                    'balance_end_real': 70.0,317                    'date': fields.Date.from_string('2016-01-16'),318                    'line_ids': [{'amount': 10.0}]319                }320            ]321        )322        # Post first statement and then try adding new transaction to it, new transactions should be posted inside previous statement323        self.confirm_bank_statement(created_bnk_stmt[0])324        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc', limit=1)325        self.assertEqual(created_bnk_stmt.state, 'confirm', 'Statement should be posted')326        transactions = self.create_transactions(['2016-01-10'])327        self.online_account.balance = 80328        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)329        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')330        self.assertBankStatementValues(331            created_bnk_stmt,332            [333                {334                    'balance_start': 0.0,335                    'balance_end_real': 40.0,336                    'date': fields.Date.from_string('2016-01-10'),337                    'state': 'posted',338                    'line_ids': [339                        {'amount': 10.0},340                        {'amount': 10.0},341                        {'amount': 10.0},342                        {'amount': 10.0}343                    ]344                },345                {346                    'balance_start': 40.0,347                    'balance_end_real': 50.0,348                    'date': fields.Date.from_string('2016-01-12'),349                    'line_ids': [{'amount': 10.0}]350                },351                {352                    'balance_start': 50.0,353                    'balance_end_real': 60.0,354                    'date': fields.Date.from_string('2016-01-13'),355                    'line_ids': [{'amount': 10.0}]356                },357                {358                    'balance_start': 60.0,359                    'balance_end_real': 70.0,360                    'date': fields.Date.from_string('2016-01-15'),361                    'line_ids': [{'amount': 10.0}]362                },363                {364                    'balance_start': 70.0,365                    'balance_end_real': 80.0,366                    'date': fields.Date.from_string('2016-01-16'),367                    'line_ids': [{'amount': 10.0}]368                }369            ]370        )371    def test_creation_every_week(self):372        self.bank_journal.write({'bank_statement_creation_groupby': 'week'})373        transactions = self.create_transactions(['2016-01-10', '2016-01-15'])374        # first synchronization, no previous bank statement375        self.online_account.balance = 20376        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)377        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')378        self.assertBankStatementValues(379            created_bnk_stmt,380            [381                {382                    'balance_start': 0.0,383                    'balance_end_real': 10.0,384                    'date': fields.Date.from_string('2016-01-04'),385                    'line_ids': [{'amount': 10.0}]386                },387                {388                    'balance_start': 10.0,389                    'balance_end_real': 20.0,390                    'date': fields.Date.from_string('2016-01-11'),391                    'line_ids': [{'amount': 10.0}]392                }393            ]394        )395        # Add new transactions, 2 should be in first statement, one in second statement and one newly created396        transactions = self.create_transactions(['2016-01-08', '2016-01-04', '2016-01-13', '2016-01-18'])397        self.online_account.balance = 60398        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)399        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')400        self.assertBankStatementValues(401            created_bnk_stmt,402            [403                {404                    'balance_start': 0.0,405                    'balance_end_real': 30.0,406                    'date': fields.Date.from_string('2016-01-04'),407                    'line_ids': [408                        {'amount': 10.0},409                        {'amount': 10.0},410                        {'amount': 10.0}411                    ]412                },413                {414                    'balance_start': 30.0,415                    'balance_end_real': 50.0,416                    'date': fields.Date.from_string('2016-01-11'),417                    'line_ids': [418                        {'amount': 10.0},419                        {'amount': 10.0}420                    ]421                },422                {423                    'balance_start': 50.0,424                    'balance_end_real': 60.0,425                    'date': fields.Date.from_string('2016-01-18'),426                    'line_ids': [{'amount': 10.0}]427                }428            ]429        )430    def test_creation_every_2weeks(self):431        self.bank_journal.write({'bank_statement_creation_groupby': 'bimonthly'})432        transactions = self.create_transactions(['2016-01-10', '2016-01-15'])433        # first synchronization, no previous bank statement434        self.online_account.balance = 20435        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)436        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')437        self.assertBankStatementValues(438            created_bnk_stmt,439            [440                {441                    'balance_start': 0.0,442                    'balance_end_real': 10.0,443                    'date': fields.Date.from_string('2016-01-01'),444                    'line_ids': [{'amount': 10.0}]445                },446                {447                    'balance_start': 10.0,448                    'balance_end_real': 20.0,449                    'date': fields.Date.from_string('2016-01-15'),450                    'line_ids': [{'amount': 10.0}]451                }452            ]453        )454        # Add new transactions, 2 should be in first statement, one in second statement and one newly created455        transactions = self.create_transactions(['2016-01-08', '2016-01-04', '2016-01-18', '2016-02-01'])456        self.online_account.balance = 60457        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)458        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')459        self.assertBankStatementValues(460            created_bnk_stmt,461            [462                {463                    'balance_start': 0.0,464                    'balance_end_real': 30.0,465                    'date': fields.Date.from_string('2016-01-01'),466                    'line_ids': [467                        {'amount': 10.0},468                        {'amount': 10.0},469                        {'amount': 10.0}470                    ]471                },472                {473                    'balance_start': 30.0,474                    'balance_end_real': 50.0,475                    'date': fields.Date.from_string('2016-01-15'),476                    'line_ids': [477                        {'amount': 10.0},478                        {'amount': 10.0}479                    ]480                },481                {482                    'balance_start': 50.0,483                    'balance_end_real': 60.0,484                    'date': fields.Date.from_string('2016-02-01'),485                    'line_ids': [{'amount': 10.0}]486                }487            ]488        )489    def test_creation_every_month(self):490        self.bank_journal.write({'bank_statement_creation_groupby': 'month'})491        transactions = self.create_transactions(['2016-01-10', '2016-02-15'])492        # first synchronization, no previous bank statement493        self.online_account.balance = 20494        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)495        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')496        self.assertBankStatementValues(497            created_bnk_stmt,498            [499                {500                    'balance_start': 0.0,501                    'balance_end_real': 10.0,502                    'date': fields.Date.from_string('2016-01-01'),503                    'line_ids': [{'amount': 10.0}]504                },505                {506                    'balance_start': 10.0,507                    'balance_end_real': 20.0,508                    'date': fields.Date.from_string('2016-02-01'),509                    'line_ids': [{'amount': 10.0}]510                }511            ]512        )513        # Add new transactions, 2 should be in first statement, one in second statement and one newly created514        transactions = self.create_transactions(['2016-01-08', '2016-01-04', '2016-02-01', '2016-03-18'])515        self.online_account.balance = 60516        self.bnk_stmt._online_sync_bank_statement(transactions, self.online_account)517        created_bnk_stmt = self.bnk_stmt.search([('journal_id', '=', self.bank_journal.id)], order='date asc')518        self.assertBankStatementValues(519            created_bnk_stmt,520            [521                {522                    'balance_start': 0.0,523                    'balance_end_real': 30.0,524                    'date': fields.Date.from_string('2016-01-01'),525                    'line_ids': [526                        {'amount': 10.0},527                        {'amount': 10.0},528                        {'amount': 10.0}...test_karma_transactions.py
Source:test_karma_transactions.py  
1from karma.parser import create_transactions, KarmaTransaction, RawKarma2def test_empty():3    assert create_transactions("", "", []) is None4def test_simple_positive():5    assert create_transactions(6        "Baz", "Baz", [RawKarma(name="Foobar", op="++", reason=None)]7    ) == [KarmaTransaction(name="Foobar", self_karma=False, net_karma=1, reasons=[])]8def test_simple_negative():9    assert create_transactions(10        "Baz", "Baz", [RawKarma(name="Foobar", op="--", reason=None)]11    ) == [KarmaTransaction(name="Foobar", self_karma=False, net_karma=-1, reasons=[])]12def test_simple_neutral():13    assert create_transactions(14        "Baz", "Baz", [RawKarma(name="Foobar", op="+-", reason=None)]15    ) == [KarmaTransaction(name="Foobar", self_karma=False, net_karma=0, reasons=[])]16def test_self_karma_single():17    assert create_transactions(18        "Baz", "Baz", [RawKarma(name="Baz", op="++", reason=None)]19    ) == [KarmaTransaction(name="Baz", self_karma=True, net_karma=1, reasons=[])]20def test_self_karma_multiple():21    assert create_transactions(22        "Baz",23        "Baz",24        [25            RawKarma(name="Baz", op="++", reason=None),26            RawKarma(name="Baz", op="++", reason=None),27        ],28    ) == [KarmaTransaction(name="Baz", self_karma=True, net_karma=1, reasons=[])]29def test_self_karma_single_with_others():30    assert create_transactions(31        "Baz",32        "Baz",33        [34            RawKarma(name="Baz", op="++", reason=None),35            RawKarma(name="Foobar", op="++", reason=None),36        ],37    ) == [38        KarmaTransaction(name="Baz", self_karma=True, net_karma=1, reasons=[]),39        KarmaTransaction(name="Foobar", self_karma=False, net_karma=1, reasons=[]),40    ]41def test_karma_double_positive():42    assert create_transactions(43        "Bar",44        "Bar",45        [46            RawKarma(name="Baz", op="++", reason=None),47            RawKarma(name="Baz", op="++", reason=None),48        ],49    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=1, reasons=[])]50def test_karma_double_negative():51    assert create_transactions(52        "Bar",53        "Bar",54        [55            RawKarma(name="Baz", op="--", reason=None),56            RawKarma(name="Baz", op="--", reason=None),57        ],58    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=-1, reasons=[])]59def test_karma_double_neutral():60    assert create_transactions(61        "Bar",62        "Bar",63        [64            RawKarma(name="Baz", op="+-", reason=None),65            RawKarma(name="Baz", op="-+", reason=None),66        ],67    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=0, reasons=[])]68def test_karma_positive_neutral():69    assert create_transactions(70        "Bar",71        "Bar",72        [73            RawKarma(name="Baz", op="++", reason=None),74            RawKarma(name="Baz", op="+-", reason=None),75        ],76    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=1, reasons=[])]77def test_karma_negative_neutral():78    assert create_transactions(79        "Bar",80        "Bar",81        [82            RawKarma(name="Baz", op="++", reason=None),83            RawKarma(name="Baz", op="+-", reason=None),84        ],85    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=1, reasons=[])]86def test_karma_positive_negative():87    assert create_transactions(88        "Bar",89        "Bar",90        [91            RawKarma(name="Baz", op="++", reason=None),92            RawKarma(name="Baz", op="--", reason=None),93        ],94    ) == [KarmaTransaction(name="Baz", self_karma=False, net_karma=0, reasons=[])]95def test_simple_positive_reason():96    assert create_transactions(97        "Bar", "Bar", [RawKarma(name="Baz", op="++", reason="Foobar is baz")]98    ) == [99        KarmaTransaction(100            name="Baz", self_karma=False, net_karma=1, reasons=["Foobar is baz"]101        )102    ]103def test_simple_negative_reason():104    assert create_transactions(105        "Bar", "Bar", [RawKarma(name="Baz", op="--", reason="Foobar is baz")]106    ) == [107        KarmaTransaction(108            name="Baz", self_karma=False, net_karma=-1, reasons=["Foobar is baz"]109        )110    ]111def test_simple_neutral_reason():112    assert create_transactions(113        "Bar", "Bar", [RawKarma(name="Baz", op="+-", reason="Foobar is baz")]114    ) == [115        KarmaTransaction(116            name="Baz", self_karma=False, net_karma=0, reasons=["Foobar is baz"]117        )118    ]119def test_self_karma_single_reason():120    assert create_transactions(121        "Bar", "Bar", [RawKarma(name="Bar", op="++", reason="Is awesome")]122    ) == [123        KarmaTransaction(124            name="Bar", self_karma=True, net_karma=1, reasons=["Is awesome"]125        )126    ]127def test_self_karma_single_reason_with_comma():128    assert create_transactions(129        "Bar", "Bar", [RawKarma(name="Bar", op="++", reason="Is, awesome")]130    ) == [131        KarmaTransaction(132            name="Bar", self_karma=True, net_karma=1, reasons=["Is, awesome"]133        )134    ]135def test_self_karma_multiple_reason():136    assert create_transactions(137        "Bar",138        "Bar",139        [140            RawKarma(name="Bar", op="++", reason="Is awesome"),141            RawKarma(name="Bar", op="++", reason="Is awesome"),142        ],143    ) == [144        KarmaTransaction(145            name="Bar",146            self_karma=True,147            net_karma=1,148            reasons=["Is awesome", "Is awesome"],149        )150    ]151def test_self_karma_single_with_others_and_reasons():152    assert create_transactions(153        "Bar",154        "Bar",155        [156            RawKarma(name="Bar", op="++", reason="Is awesome"),157            RawKarma(name="Foo", op="++", reason="Is awesome too"),158        ],159    ) == [160        KarmaTransaction(161            name="Bar", self_karma=True, net_karma=1, reasons=["Is awesome"]162        ),163        KarmaTransaction(164            name="Foo", self_karma=False, net_karma=1, reasons=["Is awesome too"]165        ),166    ]167def test_karma_double_positive_reasons():168    assert create_transactions(169        "Bar",170        "Bar",171        [172            RawKarma(name="Baz", op="++", reason="Foobar baz 1"),173            RawKarma(name="Baz", op="++", reason="Foobar baz 2"),174        ],175    ) == [176        KarmaTransaction(177            name="Baz",178            self_karma=False,179            net_karma=1,180            reasons=["Foobar baz 1", "Foobar baz 2"],181        )182    ]183def test_karma_double_negative_reasons():184    assert create_transactions(185        "Bar",186        "Bar",187        [188            RawKarma(name="Baz", op="--", reason="Foobar baz 1"),189            RawKarma(name="Baz", op="--", reason="Foobar baz 2"),190        ],191    ) == [192        KarmaTransaction(193            name="Baz",194            self_karma=False,195            net_karma=-1,196            reasons=["Foobar baz 1", "Foobar baz 2"],197        )198    ]199def test_karma_double_neutral_reasons():200    assert create_transactions(201        "Bar",202        "Bar",203        [204            RawKarma(name="Baz", op="+-", reason="Foobar baz 1"),205            RawKarma(name="Baz", op="-+", reason="Foobar baz 2"),206        ],207    ) == [208        KarmaTransaction(209            name="Baz",210            self_karma=False,211            net_karma=0,212            reasons=["Foobar baz 1", "Foobar baz 2"],213        )214    ]215def test_karma_double_neutral_reasons_and_commas():216    assert create_transactions(217        "Bar",218        "Bar",219        [220            RawKarma(name="Baz", op="+-", reason="Foobar, baz 1"),221            RawKarma(name="Baz", op="-+", reason="Foobar, baz 2"),222        ],223    ) == [224        KarmaTransaction(225            name="Baz",226            self_karma=False,227            net_karma=0,228            reasons=["Foobar, baz 1", "Foobar, baz 2"],229        )230    ]231def test_karma_positive_neutral_reasons():232    assert create_transactions(233        "Bar",234        "Bar",235        [236            RawKarma(name="Baz", op="++", reason="Foobar baz 1"),237            RawKarma(name="Baz", op="+-", reason="Foobar baz 2"),238        ],239    ) == [240        KarmaTransaction(241            name="Baz",242            self_karma=False,243            net_karma=1,244            reasons=["Foobar baz 1", "Foobar baz 2"],245        )246    ]247def test_karma_negative_neutral_reasons():248    assert create_transactions(249        "Bar",250        "Bar",251        [252            RawKarma(name="Baz", op="--", reason="Foobar baz 1"),253            RawKarma(name="Baz", op="+-", reason="Foobar baz 2"),254        ],255    ) == [256        KarmaTransaction(257            name="Baz",258            self_karma=False,259            net_karma=-1,260            reasons=["Foobar baz 1", "Foobar baz 2"],261        )262    ]263def test_karma_positive_negative_reasons():264    assert create_transactions(265        "Bar",266        "Bar",267        [268            RawKarma(name="Baz", op="++", reason="Foobar baz 1"),269            RawKarma(name="Baz", op="--", reason="Foobar baz 2"),270        ],271    ) == [272        KarmaTransaction(273            name="Baz",274            self_karma=False,275            net_karma=0,276            reasons=["Foobar baz 1", "Foobar baz 2"],277        )278    ]wallet_balance.py
Source:wallet_balance.py  
...9    assert_equal,10    assert_raises_rpc_error,11)12RANDOM_COINBASE_ADDRESS = 'mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ'13def create_transactions(node, address, amt, fees):14    # Create and sign raw transactions from node to address for amt.15    # Creates a transaction for each fee and returns an array16    # of the raw transactions.17    utxos = node.listunspent(0)18    # Create transactions19    inputs = []20    ins_total = 021    for utxo in utxos:22        inputs.append({"txid": utxo["txid"], "vout": utxo["vout"]})23        ins_total += utxo['amount']24        if ins_total > amt:25            break26    txs = []27    for fee in fees:28        outputs = {address: amt, node.getrawchangeaddress(): ins_total - amt - fee}29        raw_tx = node.createrawtransaction(inputs, outputs, 0, True)30        raw_tx = node.signrawtransactionwithwallet(raw_tx)31        txs.append(raw_tx)32    return txs33class WalletTest(BitcoinTestFramework):34    def set_test_params(self):35        self.num_nodes = 236        self.setup_clean_chain = True37    def skip_test_if_missing_module(self):38        self.skip_if_no_wallet()39    def run_test(self):40        # Check that nodes don't own any UTXOs41        assert_equal(len(self.nodes[0].listunspent()), 0)42        assert_equal(len(self.nodes[1].listunspent()), 0)43        self.log.info("Mining one block for each node")44        self.nodes[0].generate(1)45        self.sync_all()46        self.nodes[1].generate(1)47        self.nodes[1].generatetoaddress(100, RANDOM_COINBASE_ADDRESS)48        self.sync_all()49        assert_equal(self.nodes[0].getbalance(), 50)50        assert_equal(self.nodes[1].getbalance(), 50)51        self.log.info("Test getbalance with different arguments")52        assert_equal(self.nodes[0].getbalance("*"), 50)53        assert_equal(self.nodes[0].getbalance("*", 1), 50)54        assert_equal(self.nodes[0].getbalance("*", 1, True), 50)55        assert_equal(self.nodes[0].getbalance(minconf=1), 50)56        # Send 40 BTC from 0 to 1 and 60 BTC from 1 to 0.57        txs = create_transactions(self.nodes[0], self.nodes[1].getnewaddress(), 40, [Decimal('0.01')])58        self.nodes[0].sendrawtransaction(txs[0]['hex'])59        self.nodes[1].sendrawtransaction(txs[0]['hex'])  # sending on both nodes is faster than waiting for propagation60        self.sync_all()61        txs = create_transactions(self.nodes[1], self.nodes[0].getnewaddress(), 60, [Decimal('0.01'), Decimal('0.02')])62        self.nodes[1].sendrawtransaction(txs[0]['hex'])63        self.nodes[0].sendrawtransaction(txs[0]['hex'])  # sending on both nodes is faster than waiting for propagation64        self.sync_all()65        # First argument of getbalance must be set to "*"66        assert_raises_rpc_error(-32, "dummy first argument must be excluded or set to \"*\"", self.nodes[1].getbalance, "")67        self.log.info("Test getbalance and getunconfirmedbalance with unconfirmed inputs")68        # getbalance without any arguments includes unconfirmed transactions, but not untrusted transactions69        assert_equal(self.nodes[0].getbalance(), Decimal('9.99'))  # change from node 0's send70        assert_equal(self.nodes[1].getbalance(), Decimal('29.99'))  # change from node 1's send71        # Same with minconf=072        assert_equal(self.nodes[0].getbalance(minconf=0), Decimal('9.99'))73        assert_equal(self.nodes[1].getbalance(minconf=0), Decimal('29.99'))74        # getbalance with a minconf incorrectly excludes coins that have been spent more recently than the minconf blocks ago75        # TODO: fix getbalance tracking of coin spentness depth76        assert_equal(self.nodes[0].getbalance(minconf=1), Decimal('0'))77        assert_equal(self.nodes[1].getbalance(minconf=1), Decimal('0'))78        # getunconfirmedbalance79        assert_equal(self.nodes[0].getunconfirmedbalance(), Decimal('60'))  # output of node 1's spend80        assert_equal(self.nodes[1].getunconfirmedbalance(), Decimal('0'))  # Doesn't include output of node 0's send since it was spent81        # Node 1 bumps the transaction fee and resends82        self.nodes[1].sendrawtransaction(txs[1]['hex'])83        self.sync_all()84        self.log.info("Test getbalance and getunconfirmedbalance with conflicted unconfirmed inputs")85        assert_equal(self.nodes[0].getwalletinfo()["unconfirmed_balance"], Decimal('60'))  # output of node 1's send86        assert_equal(self.nodes[0].getunconfirmedbalance(), Decimal('60'))87        assert_equal(self.nodes[1].getwalletinfo()["unconfirmed_balance"], Decimal('0'))  # Doesn't include output of node 0's send since it was spent88        assert_equal(self.nodes[1].getunconfirmedbalance(), Decimal('0'))89        self.nodes[1].generatetoaddress(1, RANDOM_COINBASE_ADDRESS)90        self.sync_all()91        # balances are correct after the transactions are confirmed92        assert_equal(self.nodes[0].getbalance(), Decimal('69.99'))  # node 1's send plus change from node 0's send93        assert_equal(self.nodes[1].getbalance(), Decimal('29.98'))  # change from node 0's send94        # Send total balance away from node 195        txs = create_transactions(self.nodes[1], self.nodes[0].getnewaddress(), Decimal('29.97'), [Decimal('0.01')])96        self.nodes[1].sendrawtransaction(txs[0]['hex'])97        self.nodes[1].generatetoaddress(2, RANDOM_COINBASE_ADDRESS)98        self.sync_all()99        # getbalance with a minconf incorrectly excludes coins that have been spent more recently than the minconf blocks ago100        # TODO: fix getbalance tracking of coin spentness depth101        # getbalance with minconf=3 should still show the old balance102        assert_equal(self.nodes[1].getbalance(minconf=3), Decimal('0'))103        # getbalance with minconf=2 will show the new balance.104        assert_equal(self.nodes[1].getbalance(minconf=2), Decimal('0'))105if __name__ == '__main__':...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!!
