Skip to content

Commit 2d94f89

Browse files
authored
Merge pull request #54 from brootware/dev
Dev
2 parents 5d9c4a0 + 653e425 commit 2d94f89

File tree

7 files changed

+146
-324
lines changed

7 files changed

+146
-324
lines changed
57.1 KB
Loading
26.6 KB
Loading

poetry.lock

Lines changed: 51 additions & 300 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mypy = "^0.961"
4848
flake8 = "^4.0.1"
4949
tox = "^3.25.0"
5050
pytest-cov = "^3.0.0"
51+
pytest-mock = "^3.8.2"
5152

5253
[tool.poetry.scripts]
5354
pyredactkit = "pyredactkit.pyredactkit:main"

pyredactkit/common_jobs.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,49 @@ def valid_options(self) -> tuple:
5555
option_tuple += id['type']
5656
return option_tuple
5757

58-
def process_report(self, filename: str):
59-
"""Function to process calculate and generate report of man hour saved.
58+
def compute_total_words(self, filename: str) -> int:
59+
"""Function to compute total words in a file.
6060
Args:
6161
filename (str): File to count the words
6262
6363
Returns:
64-
Creates a report on estimated man hours/minutes saved.
64+
total_words (int): total words in file
6565
"""
66+
total_words = 0
67+
word_length = 5
6668
try:
67-
# Open a file read pointer as target_file
6869
with open(filename, encoding="utf-8") as target_file:
6970
text_chunk = target_file.read()
70-
71-
# Words per minute
72-
WPM = 75
73-
74-
word_length = 5
75-
total_words = 0
7671
for current_text in text_chunk:
7772
total_words += len(current_text)/word_length
73+
except UnicodeDecodeError:
74+
sys.exit("[-] Unable to read target file")
75+
return math.ceil(total_words)
7876

79-
total_words = math.ceil(total_words)
77+
def compute_reading_minutes(self, total_words: int) -> int:
78+
# Words per minute
79+
WPM = 75
80+
return math.ceil(total_words/WPM)
8081

81-
# Divide total words by words per minute read to get minutes and hour estimate.
82-
reading_minutes = math.ceil(total_words/WPM)
83-
reading_hours = math.floor(reading_minutes/60)
82+
def compute_reading_hours(self, reading_minutes: int) -> int:
83+
return math.floor(reading_minutes/60)
8484

85-
word_report = f"[+] Estimated total words : {total_words}"
86-
minutes_saved = f"[+] Estimated total minutes saved : {reading_minutes}"
87-
man_hours_saved = f"[+] Estimated total man hours saved : {reading_hours}"
85+
def process_report(self, filename: str):
86+
"""Function to process calculate and generate report of man hour saved.
87+
Args:
88+
filename (str): File to count the words
89+
90+
Returns:
91+
Creates a report on estimated man hours/minutes saved.
92+
"""
93+
total_words = self.compute_total_words(filename)
94+
reading_minutes = self.compute_reading_minutes(total_words)
95+
reading_hours = self.compute_reading_hours(reading_minutes)
8896

89-
print(word_report)
90-
print(minutes_saved)
91-
print(man_hours_saved)
97+
word_report = f"[+] Estimated total words : {total_words}"
98+
minutes_saved = f"[+] Estimated total minutes saved : {reading_minutes}"
99+
man_hours_saved = f"[+] Estimated total man hours saved : {reading_hours}"
92100

93-
except UnicodeDecodeError:
94-
os.remove(f"manhour_saved_report_{os.path.basename(filename)}")
95-
print("[-] Removed incomplete report")
96-
sys.exit("[-] Unable to read target file")
101+
print(word_report)
102+
print(minutes_saved)
103+
print(man_hours_saved)

tests/test_common_jobs.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
import json
3+
import os
4+
from pyredactkit.common_jobs import CommonJobs
5+
6+
7+
@pytest.fixture
8+
def common_obj():
9+
return CommonJobs()
10+
11+
12+
@pytest.fixture
13+
def mocker_text_file(mocker):
14+
content = "Message to write on file to be written"
15+
mocked_open = mocker.mock_open(read_data=content)
16+
builtin_open = "builtins.open"
17+
mocker.patch(builtin_open, mocked_open)
18+
19+
20+
def test_write_hashmap_should_create_json_file(common_obj, tmp_path):
21+
hash_map = {"key": "value"}
22+
common_obj.write_hashmap(hash_map, filename='fakefile', savedir=tmp_path)
23+
assert os.path.isfile(f"{tmp_path}.hashshadow_fakefile.json"), f"{tmp_path}.hashshadow_fakefile.json file not created"
24+
25+
26+
def test_valid_options_should_return_tuple(common_obj):
27+
assert isinstance(common_obj.valid_options(), tuple), "valid_options should return a tuple"
28+
29+
30+
def test_compute_total_words_should_return_int(common_obj, mocker_text_file):
31+
assert isinstance(common_obj.compute_total_words(filename='fakefile'), int), "compute_total_words should return an int"
32+
33+
34+
def test_compute_reading_minutes_should_return_int(common_obj, mocker_text_file):
35+
total_words = common_obj.compute_total_words(filename='fakefile')
36+
assert isinstance(common_obj.compute_reading_minutes(total_words), int), "compute_reading_minutes should return an int"
37+
38+
39+
def test_compute_reading_hours_should_return_int(common_obj, mocker_text_file):
40+
total_words = common_obj.compute_total_words(filename='fakefile')
41+
reading_minutes = common_obj.compute_reading_minutes(total_words)
42+
assert isinstance(common_obj.compute_reading_hours(reading_minutes), int), "compute_reading_hours should return an int"

tests/test_core_redact.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import os
23
from pyredactkit.core_redactor import CoreRedactorEngine
34

45
data = """John, please get that article on www.linkedin.com to me by 5:00PM on Jan 9th 2012. 4:00 would be ideal, actually. If you have any questions, You can reach me at(519)-236-2723 or get in touch with my associate at [email protected]
@@ -51,9 +52,29 @@ def redactor_obj():
5152
return CoreRedactorEngine()
5253

5354

55+
@pytest.fixture
56+
def mocker_text_file(mocker):
57+
content = "Message to write on file to be written"
58+
mocked_open = mocker.mock_open(read_data=content)
59+
builtin_open = "builtins.open"
60+
mocker.patch(builtin_open, mocked_open)
61+
62+
5463
def test_redact_all_function_should_return_string_and_dictionary(redactor_obj):
5564
set1 = redactor_obj.redact_all(data)
5665
set2 = ("This is a string", hash_table)
5766
assert type(set1[0]) == type(set2[0]), "1st element of redact_all function should return string"
5867
assert type(set1[1]) == type(set2[1]), "2nd element of redact_all function should return dictionary"
5968
assert type(set1) == type(set2), "redact_all function should return a tuple"
69+
70+
71+
# def test_process_text_function_should_create_redacted_file_and_json(redactor_obj, tmp_path):
72+
# redactor_obj.process_text(data, tmp_path)
73+
# assert os.path.isfile(tmp_path / "redacted_file.txt"), "redacted_file.txt should be created"
74+
# assert os.path.isfile(tmp_path / "redacted_file.json"), "redacted_file.json should be created"
75+
76+
77+
# def test_process_core_file_function_should_create_redacted_file_and_json(redactor_obj, mocker_text_file, tmp_path):
78+
# redactor_obj.process_core_file(filename='fakefile', savedir=tmp_path)
79+
# assert os.path.isfile(tmp_path / "redacted_fakefile.txt"), "redacted_fakefile.txt should be created"
80+
# assert os.path.isfile(tmp_path / "redacted_fakefile.json"), "redacted_fakefile.json should be created"

0 commit comments

Comments
 (0)