Skip to content

Commit

Permalink
test : tests added. (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadrasabouri committed May 3, 2023
1 parent 25ebf3b commit ea3cf99
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyrgg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def run(input_dict=None):
file_name = input_dict["file_name"]
number_of_files = input_dict["number_of_files"]
if input_dict["config"] is True:
save_config(input_dict)
print("Config --> " + save_config(input_dict))
line(40)
for i in range(number_of_files):
print("Generating {0} from {1}".format(i + 1, number_of_files))
Expand Down
16 changes: 8 additions & 8 deletions pyrgg/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,15 +610,15 @@ def save_config(input_dict):
:param input_dict: input dictionary
:type input_dict: dict
:return: None
:return: path to file
"""
fname = pyrgg.params.CONFIG_FILE_FORMAT.format(input_dict['file_name'])
try:
fname = pyrgg.params.CONFIG_FILE_FORMAT.format(input_dict['file_name'])
with open(fname, "w") as json_file:
json_dump(input_dict, json_file, indent=2)
print("Config --> " + os.path.abspath(fname))
except FileNotFoundError:
print(pyrgg.params.PYRGG_FILE_ERROR_MESSAGE)
return os.path.abspath(fname)
except BaseException:
print(pyrgg.params.PYRGG_INPUT_ERROR_MESSAGE)


def load_config(path):
Expand All @@ -632,11 +632,11 @@ def load_config(path):
try:
with open(path, "r") as json_file:
return input_filter(json_loads(json_file.read()))
except FileNotFoundError:
except BaseException:
print(pyrgg.params.PYRGG_FILE_ERROR_MESSAGE)


def _print_select_config(configs, input_func=input):
def _print_select_config(configs, input_func=input): # pragma: no cover
"""
Print configs in current directory and get input from user.
Expand All @@ -649,7 +649,7 @@ def _print_select_config(configs, input_func=input):
print("Config files detected in current directory are listed below:")
for i, config in enumerate(configs):
print("[{}] - {}".format(i, config))
key = input(
key = input_func(
"Press the config index to load or any other keys to start with new one : ")
try:
return load_config(configs[int(key)])
Expand Down
27 changes: 21 additions & 6 deletions test/functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
>>> from pyrgg.functions import *
>>> import pyrgg.params
>>> import random
>>> import os
>>> pyrgg.params.PYRGG_TEST_MODE = True
>>> description_print()
Webpage : https://www.pyrgg.ir
Expand Down Expand Up @@ -153,7 +154,7 @@
...
TypeError: edge_gen() missing 1 required positional argument: 'sign'
>>> prev_item = ""
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"0","min_edge":"1","max_edge":"1000","sign":"1","direct":"1","self_loop":"1","multigraph":"0","file_name":"File 1","output_format":"2","weight":"1","error":"120","number_of_files":3}
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"0","min_edge":"1","max_edge":"1000","sign":"1","direct":"1","self_loop":"1","multigraph":"0","file_name":"File 1","output_format":"2","weight":"1","error":"120","number_of_files":3,"config":False}
>>> def input_func_test(input_data):
... global prev_item
... for item in pyrgg.params.MENU_ITEMS1:
Expand All @@ -172,6 +173,10 @@
... return input_func_dict[item1]
... else:
... return input_func_dict["error"]
>>> conf_input = "0"
>>> def input_func_conf_test(input_data):
... global conf_input
... return conf_input
>>> input_data = get_input(input_func_test)
>>> input_data["vertices"]
120
Expand All @@ -190,7 +195,7 @@
>>> input_data["weight"]
True
>>> prev_item = ""
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"1.2","min_edge":"10000","max_edge":"2","sign":"1","direct":"1","self_loop":"1","multigraph":"0","file_name":"File 1","output_format":"2","weight":"1","error":"120","number_of_files":-1}
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"1.2","min_edge":"10000","max_edge":"2","sign":"1","direct":"1","self_loop":"1","multigraph":"0","file_name":"File 1","output_format":"2","weight":"1","error":"120","number_of_files":-1,"config":True}
>>> input_data = get_input(input_func_test)
>>> input_data["vertices"]
120
Expand All @@ -208,8 +213,18 @@
True
>>> input_data["weight"]
True
>>> loaded_config = check_for_config(input_func_conf_test)
>>> input_data["config"]
True
>>> config_path = save_config(input_data)
>>> input_data == load_config(config_path)
True
>>> save_config("test")
[Error] Bad Input!
>>> load_config("test123456789")
[Error] Bad Input File!
>>> prev_item = ""
>>> input_func_dict = {"vertices":"120","max_weight":"110.45","min_weight":"test","min_edge":"10000","max_edge":"2","sign":"1","direct":"-2","self_loop":"0","multigraph":"0","file_name":"File 2","output_format":"200","weight":"0","number_of_files":-1,"error":"120"}
>>> input_func_dict = {"vertices":"120","max_weight":"110.45","min_weight":"test","min_edge":"10000","max_edge":"2","sign":"1","direct":"-2","self_loop":"0","multigraph":"0","file_name":"File 2","output_format":"200","weight":"0","number_of_files":-1,"error":"120","config":False}
>>> input_data = get_input(input_func_test)
>>> input_data["vertices"]
120
Expand All @@ -227,17 +242,17 @@
True
>>> input_data["multigraph"]
False
>>> input_func_dict = {"vertices":"120","max_weight":"110.45","min_weight":"test","min_edge":"10000","max_edge":"2","sign":"1","direct":"-2","self_loop":"2","multigraph":"400","file_name":"File 2","output_format":"200","weight":"1","error":"120","number_of_files":2}
>>> input_func_dict = {"vertices":"120","max_weight":"110.45","min_weight":"test","min_edge":"10000","max_edge":"2","sign":"1","direct":"-2","self_loop":"2","multigraph":"400","file_name":"File 2","output_format":"200","weight":"1","error":"120","number_of_files":2,"config":False}
>>> input_data = get_input(input_func_test)
[Error] Bad Input!
>>> input_data["min_weight"]
110.45
>>> input_data["max_weight"]
120
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"0","min_edge":"1","max_edge":"1000","sign":"1","direct":"1","self_loop":"1","multigraph":"1","file_name":"File 1","output_format":"2","weight":"test","error":"2","number_of_files":2}
>>> input_func_dict = {"vertices":"120","max_weight":"110","min_weight":"0","min_edge":"1","max_edge":"1000","sign":"1","direct":"1","self_loop":"1","multigraph":"1","file_name":"File 1","output_format":"2","weight":"test","error":"2","number_of_files":2,"config":False}
>>> input_data = get_input(input_func_test)
[Error] Bad Input!
>>> input_data["weight"]
True
>>> os.remove('File 1.pyrgg.config.json')
"""

0 comments on commit ea3cf99

Please sign in to comment.