Ejemplo n.º 1
0
def test_init_groc(
    mock_os_path_isdir, mock_create_setup_db,
    mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.init_groc()
    assert mock_create_setup_db.called
Ejemplo n.º 2
0
def test_add_purchase_manual(
    mock_validate_insert_row, mock_create_connection,
    mock_os_path_expanduser
):
    g = Groc()
    g.add_purchase_manual({'foo': 'bar'}, False)
    mock_validate_insert_row('some-connection', {'foo': 'bar'}, False)
Ejemplo n.º 3
0
def test_delete_purchases_verbose_dry_run(groc_connection, groc_db_url,
                                          connection_function_scope,
                                          stores_and_purchases_function_scope):

    groc_connection.return_value = connection_function_scope

    # Get a purchase id
    cursor = groc_connection().cursor()
    res = cursor.execute('SELECT id FROM purchase LIMIT 1;').fetchone()
    id = (res['id'])

    g = Groc()

    purchases = g.select_by_id((id, ))
    table = from_db_cursor(purchases)
    table.align['store'] = 'r'
    table.align['total'] = 'r'
    table.align['description'] = 'l'

    # Output this string regardless of dry-run or not
    delete_msg = f'Deleting purchases with id(s) {id}.'

    runner = CliRunner()
    result = runner.invoke(groc_cli,
                           ['delete', '--id', id, '--verbose', '--dry-run'])
    assert result.exit_code == 0
    assert result.output == f'{table}\n{delete_msg}\n'
Ejemplo n.º 4
0
def test_breakdown_default(groc_connection, groc_db_url,
                           connection_function_scope,
                           stores_and_purchases_function_scope):

    groc_connection.return_value = connection_function_scope
    g = Groc()
    today = datetime.date.today()
    month, year = [today.strftime('%m')], [today.strftime('%Y')]

    data = g.breakdown(month, year)
    table = from_db_cursor(data)
    field_names = [name for name in table.field_names]

    # No purchases exist for the current month.
    # Add a row with dashes if table empty
    temp_str = table.get_string()
    temp_table = copy.deepcopy(table)
    temp_table.clear_rows()
    if temp_str == temp_table.get_string():
        table.add_row(['--' for x in field_names])

    # Rmove columns used for db ordering
    field_names.remove('num_month')
    field_names.remove('min purchase')
    field_names.remove('max purchase')
    field_names.remove('avg purchase')
    field_names.remove('store count')
    output_msg = table.get_string(fields=field_names)

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['breakdown'])
    assert result.exit_code == 0
    assert result.output == f'{output_msg}\n'
Ejemplo n.º 5
0
def test_list_purchases_date(
    mock_list_purchases_date, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.list_purchases_date('01', '2019')
    mock_list_purchases_date.assert_called_with(
        'some-connection', '01', '2019')
Ejemplo n.º 6
0
def test_init_groc_raise_exception(
    mock_create_setup_db, mock_os_path_isfile,
    mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    with pytest.raises(exceptions.DatabaseError):
        g.init_groc()
    assert not mock_create_setup_db.called
Ejemplo n.º 7
0
def test_add_purchase_path_with_dir(
    mock_insert_csv, mock_compile_csvs, mock_os_path_is_dir, 
    mock_os_path_abspath, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.add_purchase_path('some-path', False)

    mock_insert_csv.assert_called_with(
        'some-connection', ['foo.csv', 'bar.csv'], False)
Ejemplo n.º 8
0
def test_add_purchase_path_with_file(
    mock_insert_csv, mock_os_path_isfile, mock_os_path_is_dir,
    mock_os_path_abspath, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.add_purchase_path('foo.csv', False)

    mock_insert_csv.assert_called_with(
        'some-connection', ['some-path-to-file'], False)
Ejemplo n.º 9
0
def test_add_purchase_path_exception(
    mock_insert_csv, mock_os_path_isfile, mock_os_path_is_dir,
    mock_os_path_abspath, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()

    with pytest.raises(Exception) as e:
        g.add_purchase_path('foo', False)
    assert "some-path-to-file could not be found!" in str(e.value)
Ejemplo n.º 10
0
def test_init_groc_2(
    mock_os_mkdir, mock_os_path_isdir,
    mock_create_setup_db, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.init_groc()
    assert g.groc_dir == 'my-groc-dir'
    assert mock_os_mkdir.called
    mock_os_mkdir.assert_called_with('my-groc-dir')
Ejemplo n.º 11
0
def test_add_dir_with_duplicate_purchase(groc_connection, groc_db_url,
                                         connection_function_scope,
                                         create_purchase_csvs,
                                         add_csv_file_with_duplicate,
                                         purchase_csv_dir):

    groc_connection.return_value = connection_function_scope
    _ = Groc()
    dir_path = purchase_csv_dir

    files = purchase_csv_dir.listdir()

    output = (f'Importing data from {files[0]}\n'
              f'2 purchase(s) added\n'
              f'Importing data from {files[1]}\n'
              f'2 purchase(s) added\n'
              f'Importing data from {files[2]}')
    exc_str = 'Duplicate purchase detected -- '\
              '(date: 2019-01-03, store: Store Bar, ' \
              'total: 25.00, description: bars)'

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['add', '--source', dir_path])
    assert result.exit_code == 1
    assert result.output == f'{output}\n'
    assert result.exception.__str__() == exc_str
Ejemplo n.º 12
0
def test_reset_dry_run(groc_clear, groc_purchase_count, groc_connection,
                       groc_db_url):
    _ = Groc()
    runner = CliRunner()
    result = runner.invoke(groc_cli, ['reset', '--dry-run'])
    assert result.exit_code == 0
    assert result.output == 'Database reset will delete 10 purchase entries.\n'
Ejemplo n.º 13
0
def test_list_limit_no_purchases(groc_purchase_count, groc_connection,
                                 groc_db_url):
    _ = Groc()

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['list', '--verbose'])
    assert result.exit_code == 0
    assert result.output == 'No purchase entries available. You should add some!\nSee groc add --help to add purchases.\n'
Ejemplo n.º 14
0
def test_reset(groc_clear, groc_purchase_count, groc_connection, groc_db_url):
    _ = Groc()
    runner = CliRunner()
    result = runner.invoke(groc_cli, ['reset'])
    assert result.exit_code == 0
    assert result.output == (
        'Database reset will delete 10 purchase entries.\n'
        'Database reset successful.\n')
Ejemplo n.º 15
0
def test_init(groc_init, groc_dir_exists, groc_connection, groc_db_url):
    _ = Groc()
    runner = CliRunner()

    result = runner.invoke(groc_cli, ['init', '--verbose'])
    assert result.exit_code == 0
    assert result.output == ('Attempting to create groc directory and db\n'
                             'Welcome to groc! You\'re all set up!\n')
Ejemplo n.º 16
0
def test_list_limit_more_than_100(groc_connection, groc_db_url,
                                  connection_function_scope,
                                  stores_and_purchases_function_scope):
    groc_connection.return_value = connection_function_scope
    g = Groc()

    # Make table
    table = from_db_cursor(g.list_purchases_limit(101))
    table.title = f'Last 50 purchase(s)'
    table.align['store'] = 'r'
    table.align['total'] = 'r'
    table.align['description'] = 'l'

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['list', '--verbose'])
    assert result.exit_code == 0
    assert result.output == f'{table.get_string()}\n'
Ejemplo n.º 17
0
def test_delete_no_purchases(groc_select_purchase_ids, groc_connection,
                             groc_db_url, cursor_function_scope):
    groc_select_purchase_ids.return_value = cursor_function_scope
    _ = Groc()
    id = 1

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['delete', '--id', 1])
    assert result.exit_code == 0
    assert result.output == f'No purchases with id(s) {id} to be deleted.\n'
Ejemplo n.º 18
0
def test_list_limit_default_no_verbose(groc_connection, groc_db_url,
                                       connection_function_scope,
                                       stores_and_purchases_function_scope):
    groc_connection.return_value = connection_function_scope
    g = Groc()

    # Make table
    purchases = g.list_purchases_limit(50)
    table = from_db_cursor(purchases)
    table.title = f'Last 50 purchase(s)'
    table.align['store'] = 'r'
    table.align['total'] = 'r'
    table.align['description'] = 'l'
    field_names = [name for name in table.field_names]
    field_names.remove('id')

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['list'])
    assert result.exit_code == 0
    assert result.output == f'{table.get_string(fields=field_names)}\n'
Ejemplo n.º 19
0
def test_add_manual(groc_connection, groc_db_url, connection_function_scope):
    groc_connection.return_value = connection_function_scope
    _ = Groc()

    runner = CliRunner()
    result = runner.invoke(groc_cli, [
        'add', '--date', '2019-01-01', '--total', 20.00, '--store',
        'Whole Foods', '--description', 'bread'
    ])
    assert result.exit_code == 0
    assert result.output == f'Added 1 purchase successfully.\n'
Ejemplo n.º 20
0
def test_list_month_year_limit_verbose(groc_connection, groc_db_url,
                                       connection_function_scope,
                                       stores_and_purchases_function_scope):

    groc_connection.return_value = connection_function_scope
    g = Groc()

    # Make table for Jan 2019 purchases limit by 1
    month, year, limit = '01', '2019', 1
    purchases = g.list_purchases_date_limit(month, year, limit)
    table = from_db_cursor(purchases)
    table.title = f'Last {limit} purchase(s) from {month}/{year}'
    table.align['store'] = 'r'
    table.align['total'] = 'r'
    table.align['description'] = 'l'

    runner = CliRunner()
    result = runner.invoke(
        groc_cli, ['list', '-m', '01', '-y', '2019', '-l', 1, '--verbose'])
    assert result.exit_code == 0
    assert result.output == f'{table.get_string()}\n'
Ejemplo n.º 21
0
def test_breakdown_year_month_year_verbose(
        groc_connection, groc_db_url, connection_function_scope,
        stores_and_purchases_function_scope):

    groc_connection.return_value = connection_function_scope
    g = Groc()
    month, year = ['01', '02'], ['2019']

    data = g.breakdown(month, year)
    table = from_db_cursor(data)
    field_names = [name for name in table.field_names]

    # Rmove columns used for db ordering
    field_names.remove('num_month')
    output_msg = table.get_string(fields=field_names)

    runner = CliRunner()
    result = runner.invoke(
        groc_cli,
        ['breakdown', '-m', '01', '-m', '02', '-y', '2019', '--verbose'])
    assert result.exit_code == 0
    assert result.output == f'{output_msg}\n'
Ejemplo n.º 22
0
def test_add_manual_ignore_duplicate(groc_connection, groc_db_url,
                                     connection_function_scope,
                                     stores_and_purchases_function_scope):
    groc_connection.return_value = connection_function_scope
    _ = Groc()

    runner = CliRunner()
    # Adding a duplicate purchase from pytest fixture
    result = runner.invoke(groc_cli, [
        'add', '--date', '2019-01-01', '--total', 100.00, '--store',
        'Whole Foods', '--ignore-duplicate'
    ])
    assert result.exit_code == 0
    assert result.output == f'Added 0 purchase successfully.\n'
Ejemplo n.º 23
0
def test_add_file(groc_connection, groc_db_url, connection_function_scope,
                  stores_and_purchases_function_scope, create_purchase_csvs):

    groc_connection.return_value = connection_function_scope
    _ = Groc()
    file_path = create_purchase_csvs[0]

    output = (f'Importing data from {file_path}\n'
              f'2 purchase(s) added\n'
              f'Added 2 purchase(s) successfully.')

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['add', '--source', file_path])
    assert result.exit_code == 0
    assert result.output == f'{output}\n'
Ejemplo n.º 24
0
def test_add_dir(groc_connection, groc_db_url, connection_function_scope,
                 purchase_csv_dir):

    groc_connection.return_value = connection_function_scope
    _ = Groc()
    dir_path = purchase_csv_dir
    files = purchase_csv_dir.listdir()

    output = (f'Importing data from {files[0]}\n'
              f'2 purchase(s) added\n'
              f'Importing data from {files[1]}\n'
              f'2 purchase(s) added\n'
              f'Added 4 purchase(s) successfully.')

    runner = CliRunner()
    result = runner.invoke(groc_cli, ['add', '--source', dir_path])
    assert result.exit_code == 0
    assert result.output == f'{output}\n'
Ejemplo n.º 25
0
def test_groc_dir_exists(mock_os_path_exists, mock_create_connection,
                         mock_os_path_expanduser):
    g = Groc()
    g.groc_dir_exists()
    mock_os_path_exists.assert_called_with('my-groc-dir')
Ejemplo n.º 26
0
def test_create_and_setup_db_called_with_connection(
    mock_setup_db, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g._create_and_setup_db()
    mock_setup_db.assert_called_with('some-connection')
Ejemplo n.º 27
0
def test_get_db_url(mock_create_connection, mock_os_path_expanduser):
    g = Groc()
    assert g._get_db_url() == 'my-groc-dir/groc.db'
Ejemplo n.º 28
0
def test_get_connection_exception(mock_db_create_conn, mock_get_db_url, mock_os_path_expanduser):
    with pytest.raises(exceptions.DatabaseError):
        Groc()._get_connection()
Ejemplo n.º 29
0
def test_list_purchases_limit(
    mock_list_purchases_limit, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.list_purchases_limit(20)
    mock_list_purchases_limit.assert_called_with('some-connection', 20)
Ejemplo n.º 30
0
def test_delete_purchase(
    mock_delete_from_db, mock_create_connection, mock_os_path_expanduser
):
    g = Groc()
    g.delete_purchase((1, 2, 3))
    mock_delete_from_db.assert_called_with('some-connection', (1, 2, 3))