I have the following test
from unittest.mock import ANY
try:
from unittest import mock # python 3.3+
except ImportError:
import mock # python 2.6-3.2
import pytest
from data_cleaning import __main__ as data_cleaning
@mock.patch('Repositories.repository.Repository.delete')
@pytest.mark.parametrize("argv", [
(['-t', 'signals']),
(['-t', 'signals', 'visualizations']),
(['-t', 'signals', 'visualizations']),
(['-d', '40', '--processes', 'historicals'])])
def test_get_from_user(mock_delete, argv):
with mock.patch('data_cleaning.__main__.sys.argv', [''] + argv):
data_cleaning.main()
mock_delete.assert_has_calls([ANY])
pytest.main('-x ../data_cleaning'.split())
which tries to cover the following code
import argparse
import logging
import sys
from Repositories import repository
from common import config
_JSONCONFIG = config.config_json()
config.initial_configuration_for_logging()
def parse_args(args):
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--days', help='Dias de datos que se quiere mantener.',
required=False, type=int, default=30)
parser.add_argument('-p', '--processes', help='Tablas a procesar.',
required=True, nargs='+', choices=['signals', 'not_historicals', 'historicals'])
return parser.parse_args(args)
def main():
args = parse_args(sys.argv[1:])
try:
repo = repository.Repository()
for process in args.processes:
if process == 'signals':
tables_with_column = get_tables_with_column(_JSONCONFIG['SIGNAL_TABLES'])
for table, column in tables_with_column:
repo.delete(column, table, args.days)
elif process == 'not_historicals':
tables_with_column = get_tables_with_column(_JSONCONFIG['NOT_HISTORICALS_TABLES'])
for table, column in tables_with_column:
repo.delete(column, table, args.days)
elif process == 'historicals':
tables_with_column = get_tables_with_column(_JSONCONFIG['HISTORICAL_TABLES'])
for table, column in tables_with_column:
repo.delete(column, table, args.days)
repo.execute_copy_table_data_from(table, 'historica')
except AttributeError as error:
logging.exception(f'AttributeError: {repr(error)}')
except KeyError as error:
logging.exception(f'KeyError: {repr(error)}')
except TypeError as error:
logging.exception(f'TypeError: {repr(error)}')
except Exception as error:
logging.exception(f'Exception: {repr(error)}')
def get_tables_with_column(json_object):
tables_with_column = convert_values_to_pairs_from(json_object, 'table_name', 'column_name')
return tables_with_column
def convert_values_to_pairs_from(obj: [dict], ket_to_key: str, key_to_value: str) -> [tuple]:
return [(item[ket_to_key], item[key_to_value]) for item in obj]
How can I cover 100% of my code through tests? the test case that specified how well implemented is it? What do I have to cover in my tests so that this module is fully covered?
How should I cover testing for this code? I am starting in unit tests, but I have been more than 2 months and I have a hard time understanding what I should test.