def test_should_run_correct_on_key_function(self, mock_input): #Given mock_func = Mock() schema = {'id': int} on_key = {'id': [mock_func]} mock_input.return_value = 1 call_count = 1 #When dict_builder(schema, on_key=on_key) #Then self.assertEqual(mock_func.call_count, call_count)
def test_should_return_empty_if_no_schema(self, mock_input): #Given expected = {} #When actual = dict_builder({}) #Then self.assertEqual(expected, actual)
def test_should_build_dict_no_kwargs(self, mock_input): #Given schema = {'id': int, 'name': str} mock_input.side_effect = [1, 'Test Name'] expected = {'id': 1, 'name': 'Test Name'} #When actual = dict_builder(schema) #Then self.assertEqual(expected, actual)
def test_should_skip_on_cancel(self, mock_input): #Given mock_input.side_effect = [1, False] schema = {'id': 1, 'name': str} on_cancel = 'skip' expected = {'id': 1} #When actual = dict_builder(schema, on_cancel=on_cancel) #Then self.assertEqual(expected, actual)
def test_should_apply_correct_defaults(self, mock_input): #Given schema = {'id': int, 'name': str} defaults = {'name': 'default_value'} mock_input.side_effect = [1, False] expected = {'id': 1, 'name': 'default_value'} #When actual = dict_builder(schema, defaults=defaults) #Then self.assertEqual(expected, actual)
def test_should_ignore_given_keys(self, mock_input): #Given schema = {'id': int, 'name': str} ignore_keys = ['id'] mock_input.return_value = 'Test Name' expected = {'name': 'Test Name'} #When actual = dict_builder(schema, ignore_keys=ignore_keys) #Then self.assertEqual(expected, actual)
def test_should_return_correct_dict_on_override(self, mock_input): #Given mock_input.side_effect = [False, 'New Name'] schema = {'id': int, 'name': str} override = {'id': 2} defaults = {'id': 1, 'name': 'Test Name'} expected = {'id': 2, 'name': 'New Name'} #When actual = dict_builder(schema, defaults=defaults, override=override) #Then self.assertEqual(expected, actual)
def show_add_item_menu(get_key: str) -> None: data = get_cats(DbController.instance().get_all_rows(get_key, '*'), action='delete') current_names = [item['name'] for item in data] schema = {k.lower(): type(v) for (k, v) in data[0].items()} validation = { 'all': { 'min_length': 1, 'cancel_on': '0', 'fg': 'Blue' }, 'name': { 'unique': current_names } } is_looping = True while is_looping: clear() dicts_to_table(data) new_dict = dict_builder(schema, ['id', 'basket'], validation=validation) if not new_dict: return DbController.instance().insert(get_key, new_dict) clear() data = get_cats(DbController.instance().get_all_rows(get_key, '*'), action='delete') dicts_to_table(data) if input( fmt_string( 'Item SuccessFully Added. Would You Like To Add Another?[y/n]\n', fg='Green')) == 'n': is_looping = False
def show_update_item_menu(get_key: str) -> None: data = get_cats(DbController.instance().get_all_rows(get_key, '*'), action='delete') current_ids = [item['id'] for item in data] schema = {k.lower(): type(v) for (k, v) in data[0].items()} validation = {'all': {'min_length': 1, 'cancel_on': '0', 'fg': 'Blue'}} is_looping = True while is_looping: clear() dicts_to_table(data) id = get_validated_input('Please Enter An ID To Edit: ', int, fg='Blue', min_length=1, is_present=current_ids, cancel_on='0') if not id: return new_dict = dict_builder(schema, ['id', 'items'], validation=validation) if not new_dict: return DbController.instance().update(get_key, id, new_dict) clear() data = get_cats(DbController.instance().get_all_rows(get_key, '*'), action='delete') dicts_to_table(data) if input( fmt_string( 'Item SuccessFully Added. Would You Like To Add Another?[y/n]\n', fg='Green')) == 'n': is_looping = False
def show_add_order_menu() -> None: display_data = get_order_data() schema = {k.lower(): type(v) for (k, v) in display_data[0].items()} validation = {'all': {'min_length': 1, 'cancel_on': '0', 'fg': 'Blue'}} is_looping = True while is_looping: clear() dicts_to_table(display_data) new_dict = dict_builder(schema, ['id', 'courier', 'status'], validation) if not new_dict: return clear() dicts_to_table(display_data) print(fmt_string('\nOrder Complete!', fg='Green')) dicts_to_table([new_dict], headers=list(display_data[0].keys())[1:-2]) if input(fmt_string('Does This Look Correct?[y/n]\n', fg='Green')).lower() == 'n': continue DbController.instance().insert('orders', new_dict) clear() display_data = get_order_data() dicts_to_table(display_data) if input( fmt_string( 'Item SuccessFully Added. Would You Like To Add Another?[y/n]\n', fg='Green')) == 'n': is_looping = False
def show_update_order_menu() -> None: data = get_order_data() current_ids = [item['id'] for item in data] status_list = DbController.instance().get_all_rows('status', '*') status_ids = [status['id'] for status in status_list] courier_list = DbController.instance().get_all_rows('couriers', '*') courier_ids = [courier['id'] for courier in courier_list] is_looping = True while is_looping: clear() dicts_to_table(data) id = get_validated_input('Please Enter An ID To Edit: ', int, fg='Blue', min_length=1, is_present=current_ids, cancel_on='0') if not id: return order = DbController.instance().get_joins_where( fields=[ 'o.id', 'o.name', 'o.address', 'o.area', 'o.phone', 'courier.name AS courier', 's.code AS status' ], source='orders o', targets=['couriers courier', 'status s'], conditions=['courier.id = o.courier', 's.id = o.status'], where=f'o.id = {id}', order='ORDER BY o.status') clear() dicts_to_table(order) schema = {k.lower(): type(v) for (k, v) in data[0].items()} schema['courier'] = int schema['status'] = int validation = { 'all': { 'min_length': 0, 'cancel_on': '', 'cancel_text': 'SKIP', 'fg': 'Blue' }, 'status': { 'is_present': status_ids, 'cancel_on': '0' }, 'courier': { 'is_present': courier_ids, 'cancel_on': '0' } } on_key = { 'status': [ clear, lambda: dicts_to_table(order), lambda: dicts_to_table(status_list) ], 'courier': [ clear, lambda: dicts_to_table(order), lambda: dicts_to_table(courier_list) ] } new_dict = dict_builder(schema, ['id'], validation, on_key=on_key, on_cancel='skip') if new_dict: DbController.instance().update('orders', id, new_dict) select_order_items(id) clear() data = get_order_data() dicts_to_table(data) if input( fmt_string( 'Item SuccessFully Updated. Would You Like To Update Another?[y/n]\n', fg='Green')) == 'n': is_looping = False