Skip to content

Commit

Permalink
CLI TEST AND CHART SELECTION METHOD
Browse files Browse the repository at this point in the history
-[x] Finished test for cli:
    -[x] checks if the main cli function produces stdout in the terminal
    -[x] checks for all the supporting functions, that they are returning
            appropriate values and raising errors otherwise

-[x] Encapsulated chart selection into a new method

-[ ] Need to add test that cli can accept and handle various combination of stdin
  • Loading branch information
bexxmodd committed Sep 13, 2020
1 parent e0070d6 commit 2455260
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
21 changes: 14 additions & 7 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ def cli(chart, exclude, header, style, text, graph):
ATTRIBUTES: bold, dim, underlined, blink, reverse, hidden.
"""
ch = None
if chart == Chart.BARH or chart == 'barh':
ch = Chart.BARH
elif chart.lower() == 'barv':
ch = Chart.BARV
elif chart.lower() == 'pie':
ch = Chart.PIE
ch = check_chart(chart)
ex = exclude
d = Color.RED
s = Attr.BOLD
Expand Down Expand Up @@ -79,6 +73,7 @@ def check_color(option: str) -> Color:
for name in Color.__members__.items():
if option.upper() == name[0]:
return name[1]
raise KeyError('Color not available!')

def check_attr(option: str) -> Attr:
"""Checks if the string argument for attribute is in
Expand All @@ -94,6 +89,18 @@ def check_attr(option: str) -> Attr:
for name in Attr.__members__.items():
if option.upper() == name[0]:
return name[1]
raise KeyError('Type of Attribute not found!')

def check_chart(chart: str) -> Chart:
"""Checks what type of bar user wants to be displayed"""
if chart == Chart.BARH or chart == 'barh':
return Chart.BARH
elif chart.lower() == 'barv':
return Chart.BARV
elif chart.lower() == 'pie':
return Chart.PIE
else:
raise NameError("Unsupported chart type!")


if __name__ == '__main__':
Expand Down
30 changes: 15 additions & 15 deletions disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def disk_space(self) -> dict:
"""Creates a dictionary of the media
and disk partitions on the given computer.
rtype:
dict: keys're partition names and disk size're values
returns:
keys as partition names and disk size as values
"""
disks = {}
# First append the root partition
Expand Down Expand Up @@ -117,9 +117,9 @@ def bytes_to_human_readable(self, bytes: int) -> str:
"""Convert bytes into human readable form
args:
bytes (int): bytes
rtype:
str: Digits with accompanied value (GB|MB|KB)
bytes
returns:
Digits with accompanied value (GB|MB|KB)
"""
gb = bytes / 1024 / 1024 / 1024
if gb < 1:
Expand All @@ -132,9 +132,9 @@ def create_horizontal_bar(self, disk: dict) -> str:
for bar and for empty space.
args:
disk (dict): media disk from the host computer
rtype:
str: horizontal bar representing the space usage
media disk from the host computer
returns:
horizontal bar representing the space usage
"""
used = 0
bar = ' '
Expand All @@ -153,11 +153,11 @@ def usage_percent(self, disk: dict) -> float:
"""calculates the disk space usage percentage
args:
disk (dict): disk partition space values
disk partition space values
raises:
ValueError: if any of the dict keys are missing
rtype:
int: percent of the disk space used
returns:
percent of the disk space used
"""
try:
return disk['used'] / disk['total']
Expand All @@ -169,12 +169,12 @@ def integers_to_readable(self, disk: dict) -> dict:
converted into human readable strings
args:
disk (dict): disk partition
disk partition
raises:
TypeError: if the dict has no requiered keys
rtype:
dict: alphanumeric text as values and the total,
used and free space as keys
returns:
alphanumeric text as values and the total,
used and free space as keys
"""
try:
total = self.bytes_to_human_readable(disk['total'])
Expand Down
53 changes: 53 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import io
import random
import psutil
import unittest
import unittest.mock

from access import add_path
from cli import cli, check_color, check_attr, check_chart
from disk import DiskUsage, Color, Attr, Chart
from click.testing import CliRunner

class TestCli(unittest.TestCase):

def test_vizex(self):
self.test_cli()

def test_cli(self):
runner = CliRunner()
result = runner.invoke(cli, ['barh'])
assert result.exit_code == 0

def test_check_color(self):
user_inputs = [
'red', 'green', 'BLUE', 'Yellow',
'beige', 'pink', 'purple', 'oraNge'
]
for color in user_inputs:
self.assertTrue(check_color(color) in Color)
with self.assertRaises(KeyError):
check_color('burgundy')

def test_check_attr(self):
user_inputs = [
'bold', 'dim', 'underlined',
'blink', 'reverse', 'hidden'
]
for attr in user_inputs:
self.assertTrue(check_attr(attr) in Attr)
with self.assertRaises(KeyError):
check_attr('italic')

def test_check_chart(self):
user_inputs = [
'barv', 'barh', 'pie'
]
for chart in user_inputs:
self.assertTrue(check_chart(chart) in Chart)
with self.assertRaises(NameError):
check_chart('scatter')


if __name__ == '__main__':
unittest.main()

0 comments on commit 2455260

Please sign in to comment.