Skip to content

Commit

Permalink
refactor: modify print of configuration to be testable
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielSixwings committed Oct 22, 2023
1 parent 64c3302 commit 5510ec6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 40 deletions.
85 changes: 45 additions & 40 deletions src/cli/commands/cmd_print_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,43 @@
import json
import os

def print_json_tree(data, indent="", isTop = True):
key = data.get("key")

if isTop:
print_info(f"[#FFFFFF]\nCaracterística:")
print_info(f"[#FFFFFF]{indent}{indent}[#458B00]{key}")
else:
print_info(f"[#FFFFFF]{indent}{indent}[#458B00]{key}")

weight = data.get("weight", 0)
print_info(f"[#FFFFFF]{indent}{indent}Peso: [#458B00]{weight}%")

if "subcharacteristics" in data:
for subchar in data["subcharacteristics"]:
print_info(f"[#FFFFFF]{indent}Subcaracteristica(s):")
print_json_tree(subchar, indent + "| ",False)
print_info(f"[#FFFFFF]{indent}{indent}Fim-Subcaracterística")

if "measures" in data:
for measure in data["measures"]:
print_info(f"[#FFFFFF]{indent}{indent}Medida(s):")
print_json_tree(measure, indent + "| ",False)
print_info(f"[#FFFFFF]{indent}{indent}Fim-Medida(s)")

if "min_threshold" in data and "max_threshold" in data:
print_info(f"[#FFFFFF]{indent}{indent}Métrica(s):")
min_threshold = data.get("min_threshold")
max_threshold = data.get("max_threshold")
print_info(f"[#FFFFFF]{indent}| Valores de referência: Min: [#458B00]{min_threshold} [#FFFFFF]e Max: [#458B00]{max_threshold}")
print_info(f"[#FFFFFF]{indent}{indent}Fim-Metrica(s)")
def print_json_tree(data):
result = [] # Initialize an empty list to store the output as strings
stack = [(data, "")]

while stack:
data, indent = stack.pop()

key = data.get("key")
result.append(f"[#FFFFFF]\nCaracterística:")
result.append(f"[#FFFFFF]{indent}[#458B00]{key}")

weight = data.get("weight", 0)
result.append(f"[#FFFFFF]{indent}Peso: [#458B00]{weight}%")

if "subcharacteristics" in data:
for subchar in data["subcharacteristics"]:
result.append(f"[#FFFFFF]{indent}Subcaracteristica(s):")
stack.append((subchar, f"{indent}| "))

if "measures" in data:
result.append(f"[#FFFFFF]{indent}Medida(s):")
for measure in data["measures"]:
result.append(f"[#FFFFFF]{indent}| [#458B00]{measure['key']}")
result.append(f"[#FFFFFF]{indent}| Peso: [#458B00]{measure['weight']}%")
if "min_threshold" in measure and "max_threshold" in measure:
min_threshold = measure.get("min_threshold")
max_threshold = measure.get("max_threshold")
result.append(f"[#FFFFFF]{indent}| Métrica(s):")
result.append(f"[#FFFFFF]{indent}| | Valores de referência: Min: [#458B00]{min_threshold} [#FFFFFF]e Max: [#458B00]{max_threshold}")
result.append(f"[#FFFFFF]{indent}| Fim-Metrica(s)")
result.append(f"[#FFFFFF]{indent}Fim-Medida(s)")
result.append("[#FFFFFF]Fim-SubCaracterística")

result.append("[#FFFFFF]Fim-Característica")

# Join the result list into a single string with newlines
return '\n'.join(result)

def command_list_config(args):
console = Console()
Expand All @@ -46,22 +53,20 @@ def command_list_config(args):
print_rule("[#FFFFFF] Listing Configuration Parameters[/]:")

if not (os.path.exists(DEFAULT_CONFIG_FILE_PATH)):
print_info( f"[#A9A9A9] O arquivo de configuração não foi encontrado. Execute o comando msgram init para criá-lo." )
print_info(f"[#A9A9A9] O arquivo de configuração não foi encontrado. Execute o comando msgram init para criá-lo.")
exit()

print_info(f"MSGram config file [bold red]'{FILE_CONFIG}'[/] exists already!")

f = open(DEFAULT_CONFIG_FILE_PATH)

data = json.load(f)

isTop = True
for data in data.get("characteristics", []):
print_json_tree(data, "", isTop)
print_info(f"[#FFFFFF]Fim-Característica\n")
isTop = False
for characteristic in data.get("characteristics", []):
output_string = print_json_tree(characteristic)
print_info(output_string)

print_info(
"\n[#A9A9A9]Para editar o arquivo de configuração utilize em seu terminal o seguinte comando: vim <caminho_arquivo ../.msgram/.msgram/msgram.json>"
)
)

29 changes: 29 additions & 0 deletions tests/unit/data/expected_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

[#FFFFFF]Característica:
reliability
[#FFFFFF]Peso: 50%
[#FFFFFF]Subcaracteristica(s):
[#FFFFFF]Fim-Subcaracterística
| | testing_status
| | [#FFFFFF]Peso: 100%
| | [#FFFFFF]Medida(s):
| | [#FFFFFF]Fim-Medida(s)
| | [#FFFFFF]Medida(s):
| | [#FFFFFF]Fim-Medida(s)
| | Medida(s):
| | Fim-Medida(s)
| | | | test_coverage
| | | | Peso: 34%
| | | | Métrica(s):
| | | Valores de referência: Min: 60 e Max: 100
| | | | Fim-Metrica(s)
| | | | test_builds
| | | | Peso: 33%
| | | | Métrica(s):
| | | Valores de referência: Min: 0 e Max: 300000
| | | | Fim-Metrica(s)
| | | | passed_tests
| | | | Peso: 33%
| | | | Métrica(s):
| | | Valores de referência: Min: 0 e Max: 1
| | | | Fim-Metrica(s)
27 changes: 27 additions & 0 deletions tests/unit/test_print_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from unittest.mock import patch
from io import StringIO
import sys
import json

from src.cli.commands.cmd_print_config import print_json_tree
from src.cli.utils import print_info, print_rule


def test_print_json_tree():

file = open("tests/unit/data/msgram.json")
data = json.load(file)

captured_output = StringIO()
sys.stdout = captured_output

characteristics = data.get("characteristics", [])

result = print_json_tree(characteristics[0])

fileExpected = open("tests/unit/data/expected_list.txt")

compare = fileExpected.read()

assert result == compare

0 comments on commit 5510ec6

Please sign in to comment.