-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_get_file_content.py
More file actions
56 lines (42 loc) · 2.01 KB
/
test_get_file_content.py
File metadata and controls
56 lines (42 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import unittest
import os
from functions.get_file_content import get_file_content
from config import CHARACTER_LIMIT
class TestGetFileContent(unittest.TestCase):
def test_truncation_message_when_file_exceeds_character_limit(self):
print("Results for current directory")
result = get_file_content("calculator", "lorem.txt")
content = result.split("[...")[0] if "[..." in result else result
self.assertEqual(len(content), CHARACTER_LIMIT)
if "[..." in result:
self.assertIn("truncated", result)
def test_short_file_returns_full_content_without_truncation(self):
print("Results for current directory")
result = get_file_content("calculator", "main.py")
content = result.split("[...")[0] if "[..." in result else result
print(content)
self.assertNotEqual(len(content), CHARACTER_LIMIT)
if "[..." in result:
self.assertIn("truncated", result)
def test_file_in_nested_path_returns_content_below_limit(self):
print("Results for current directory")
result = get_file_content("calculator", "pkg/calculator.py")
content = result.split("[...")[0] if "[..." in result else result
print(content)
self.assertNotEqual(len(content), CHARACTER_LIMIT)
if "[..." in result:
self.assertIn("truncated", result)
def test_raises_when_file_path_outside_working_directory(self):
print("Results for current directory")
with self.assertRaises(Exception) as ctx:
get_file_content("calculator", "/bin/cat")
print(ctx.exception, "\n")
self.assertIn("Error:", str(ctx.exception))
def test_raises_when_file_does_not_exist(self):
print("Results for current directory")
with self.assertRaises(Exception) as ctx:
get_file_content("calculator", "pkg/does_not_exist.py")
print(ctx.exception, "\n")
self.assertIn("Error:", str(ctx.exception))
if __name__ == "__main__":
unittest.main(verbosity=2)