<commit_before>import unittest

import sys
from mock import patch, mock

from pynYNAB.ClientFactory import nYnabClientFactory
from pynYNAB.scripts.__main__ import verify_common_args, MainCommands


def expect_exception(exception):
    """Marks test to expect the specified exception. Call assertRaises internally"""

    def test_decorator(fn):
        def test_decorated(self, *args, **kwargs):
            self.assertRaises(exception, fn, self, *args, **kwargs)

        return test_decorated

    return test_decorator


class TestScripts(unittest.TestCase):
    class Args(object):
        def __init__(self, email, password, budgetname):
            self.email = email
            self.password = password
            self.budgetname = budgetname

    @expect_exception(SystemExit)
    def test_verify_common_noemail(self):
        verify_common_args(self.Args('', None, ''))

    @expect_exception(SystemExit)
    def test_verify_common_nopassword(self):
        verify_common_args(self.Args(None, '', ''))

    @expect_exception(SystemExit)
    def test_verify_common_nobudgetname(self):
        verify_common_args(self.Args('', '', None))

    @patch.object(sys, 'argv', ["prog", 'csvimport'])
    @patch.object(MainCommands, 'csvimport')
    def test_command_selection_csvimport(self, new_m):
        MainCommands()
        self.assertTrue(new_m.called)

    @patch.object(sys, 'argv', ["prog", 'ofximport'])
    @patch.object(MainCommands, 'ofximport')
    def test_command_selection_ofximport(self, new_m):
        MainCommands()
        self.assertTrue(new_m.called)

    @patch.object(sys, 'argv',
                          ["prog", 'csvimport', '--email', 'email', '--password', 'password', '--budgetname',
                           'budgetname', 'csvfile',
                           'csvschema'])
    @patch.object(nYnabClientFactory, 'create_client')
    @mock.patch('pynYNAB.scripts.__main__.verify_csvimport')
    @mock.patch('pynYNAB.scripts.__main__.do_csvimport')
    def test_command_do_csvimport(self, new_m, new_m2, new_c):
        MainCommands()

        self.assertTrue(new_m2.called)
        call_args_verify = new_m2.call_args[0][0]

        self.assertEqual('email', call_args_verify.email)
        self.assertEqual('password', call_args_verify.password)
        self.assertEqual('budgetname', call_args_verify.budgetname)
        self.assertEqual('csvfile', call_args_verify.csvfile)
        self.assertEqual('csvschema', call_args_verify.schema)

        self.assertTrue(new_m.called)
        call_args = new_m.call_args[0]
        self.assertEqual(new_c(), call_args[2])
        call_args = call_args[0]
        self.assertEqual('email', call_args.email)
        self.assertEqual('password', call_args.password)
        self.assertEqual('budgetname', call_args.budgetname)
        self.assertEqual('csvfile', call_args.csvfile)
        self.assertEqual('csvschema', call_args.schema)

    @patch.object(sys, 'argv',
                          ["prog", 'ofximport', '--email', 'email', '--password', 'password', '--budgetname',
                           'budgetname', 'ofxfile'])
    @patch.object(nYnabClientFactory, 'create_client')
    @mock.patch('pynYNAB.scripts.__main__.verify_ofximport')
    @mock.patch('pynYNAB.scripts.__main__.do_ofximport')
    def test_command_do_ofximport(self, new_m, new_m2, new_c):
        MainCommands()

        self.assertTrue(new_m2.called)
        call_args_verify = new_m2.call_args[0][0]

        self.assertEqual('email', call_args_verify.email)
        self.assertEqual('password', call_args_verify.password)
        self.assertEqual('budgetname', call_args_verify.budgetname)
        self.assertEqual('ofxfile', call_args_verify.ofxfile)

        self.assertTrue(new_m.called)
        call_args = new_m.call_args[0]
        self.assertEqual(new_c(), call_args[2])
        call_args = call_args[0]
        self.assertEqual('email', call_args.email)
        self.assertEqual('password', call_args.password)
        self.assertEqual('budgetname', call_args.budgetname)
        self.assertEqual('ofxfile', call_args.ofxfile)<commit_msg>saner variable names in test_scripts<commit_after>import unittest

import sys
from mock import patch, mock

from pynYNAB.ClientFactory import nYnabClientFactory
from pynYNAB.scripts.__main__ import verify_common_args, MainCommands


def expect_exception(exception):
    """Marks test to expect the specified exception. Call assertRaises internally"""

    def test_decorator(fn):
        def test_decorated(self, *args, **kwargs):
            self.assertRaises(exception, fn, self, *args, **kwargs)

        return test_decorated

    return test_decorator


class TestScripts(unittest.TestCase):
    class Args(object):
        def __init__(self, email, password, budgetname):
            self.email = email
            self.password = password
            self.budgetname = budgetname

    @expect_exception(SystemExit)
    def test_verify_common_noemail(self):
        verify_common_args(self.Args('', None, ''))

    @expect_exception(SystemExit)
    def test_verify_common_nopassword(self):
        verify_common_args(self.Args(None, '', ''))

    @expect_exception(SystemExit)
    def test_verify_common_nobudgetname(self):
        verify_common_args(self.Args('', '', None))

    @patch.object(sys, 'argv', ["prog", 'csvimport'])
    @patch.object(MainCommands, 'csvimport')
    def test_command_selection_csvimport(self, mock_csvimport):
        MainCommands()
        self.assertTrue(mock_csvimport.called)

    @patch.object(sys, 'argv', ["prog", 'ofximport'])
    @patch.object(MainCommands, 'ofximport')
    def test_command_selection_ofximport(self, mock_ofximport):
        MainCommands()
        self.assertTrue(mock_ofximport.called)

    @patch.object(sys, 'argv',
                          ["prog", 'csvimport', '--email', 'email', '--password', 'password', '--budgetname',
                           'budgetname', 'csvfile',
                           'csvschema'])
    @patch.object(nYnabClientFactory, 'create_client')
    @mock.patch('pynYNAB.scripts.__main__.verify_csvimport')
    @mock.patch('pynYNAB.scripts.__main__.do_csvimport')
    def test_command_do_csvimport(self, mock_do_csvimport, mock_verify_csvimport, mock_create_client):
        MainCommands()

        self.assertTrue(mock_verify_csvimport.called)
        call_args_verify = mock_verify_csvimport.call_args[0][0]

        self.assertEqual('email', call_args_verify.email)
        self.assertEqual('password', call_args_verify.password)
        self.assertEqual('budgetname', call_args_verify.budgetname)
        self.assertEqual('csvfile', call_args_verify.csvfile)
        self.assertEqual('csvschema', call_args_verify.schema)

        self.assertTrue(mock_do_csvimport.called)
        call_args_do = mock_do_csvimport.call_args[0]
        self.assertEqual(mock_create_client(), call_args_do[2])
        call_args_do = call_args_do[0]
        self.assertEqual('email', call_args_do.email)
        self.assertEqual('password', call_args_do.password)
        self.assertEqual('budgetname', call_args_do.budgetname)
        self.assertEqual('csvfile', call_args_do.csvfile)
        self.assertEqual('csvschema', call_args_do.schema)

    @patch.object(sys, 'argv',
                          ["prog", 'ofximport', '--email', 'email', '--password', 'password', '--budgetname',
                           'budgetname', 'ofxfile'])
    @patch.object(nYnabClientFactory, 'create_client')
    @mock.patch('pynYNAB.scripts.__main__.verify_ofximport')
    @mock.patch('pynYNAB.scripts.__main__.do_ofximport')
    def test_command_do_ofximport(self, mock_do_ofximport, mock_verify_ofximport, mock_create_client):
        MainCommands()

        self.assertTrue(mock_verify_ofximport.called)
        call_args_verify = mock_verify_ofximport.call_args[0][0]

        self.assertEqual('email', call_args_verify.email)
        self.assertEqual('password', call_args_verify.password)
        self.assertEqual('budgetname', call_args_verify.budgetname)
        self.assertEqual('ofxfile', call_args_verify.ofxfile)

        self.assertTrue(mock_do_ofximport.called)
        call_args_do = mock_do_ofximport.call_args[0]
        self.assertEqual(mock_create_client(), call_args_do[2])
        call_args_do = call_args_do[0]
        self.assertEqual('email', call_args_do.email)
        self.assertEqual('password', call_args_do.password)
        self.assertEqual('budgetname', call_args_do.budgetname)
        self.assertEqual('ofxfile', call_args_do.ofxfile)
{'fraction_non_alphanumeric': 0.11940997424490751, 'fraction_numerical': 0.0030437836572231327, 'mean_word_length': 3.4751178627553694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3355992', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2634, 'n_words': 393}