-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii_test.py
78 lines (63 loc) · 5.6 KB
/
ascii_test.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import ascii_transport as ascii
import unittest, json
class TestAdd(unittest.TestCase):
"""
Test the encode and decode functions for ascii_transport
"""
def test_empty_file(self):
"""
Test that encode will raise an error if given an empty file
"""
path = './empty.txt'
image = open(path, 'r')
data = image.read()
image.close()
result = ascii.encode(data)
self.assertEqual(result, False)
def test_encode_output(self):
"""
Test that encode outputs a json object with the anticipated format and content
"""
path = './data.txt'
image = open(path, 'r')
data = image.read()
image.close()
expected = {'data': " 41/,2/\n1/ 39/,1/:1/@2/:1/,1/\n1/ 38/,1/#1/@4/+1/,1/\n1/ 37/,1/#1/@1/'1/,2/+1/@1/+1/,1/\n1/ 36/,1/#1/@1/'1/,4/+1/@1/+1/,1/\n1/ 35/,1/#1/@1/'1/,6/+1/@1/+1/,1/\n1/ 34/,1/#1/@1/'1/,8/+1/@1/+1/,1/\n1/ 33/,1/#1/@1/'1/,10/+1/@1/+1/,1/\n1/ 32/,1/#1/@1/'1/,12/+1/@1/+1/,1/\n1/ 31/,1/#1/@1/'1/,14/+1/@1/+1/,1/\n1/ 30/,1/#1/@1/'1/,16/+1/@1/+1/,1/\n1/ 29/,1/#1/@1/'1/,18/+1/@1/+1/,1/\n1/ 28/,1/#1/@1/'1/,20/+1/@1/+1/,1/\n1/ 27/,1/#1/@1/'1/,22/+1/@1/+1/,1/\n1/ 26/,1/#1/@1/'1/,24/+1/@1/+1/,1/\n1/ 25/,1/#1/@1/'1/,26/+1/@1/+1/,1/\n1/ 24/,1/#1/@1/'1/,28/+1/@1/+1/,1/\n1/ 23/,1/#1/@1/'1/,30/+1/@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/+1/@1/+1/,36/+1/@1/,6/+1/@1/+1/,1/\n1/ 17/,1/+1/@1/+1/,42/+1/@1/+1/,1/\n1/ 18/,1/+1/@1/+1/,40/+1/@1/+1/,1/\n1/ 19/,1/+1/@1/+1/,38/+1/@1/+1/,1/\n1/ 20/,1/+1/@1/+1/,36/+1/@1/+1/,1/\n1/ 21/,1/+1/@1/+1/,34/+1/@1/+1/,1/\n1/ 22/,1/+1/@1/+1/,32/+1/@1/+1/,1/\n1/ 23/,1/+1/@1/+1/,30/+1/@1/+1/,1/\n1/ 24/,1/+1/@1/+1/,28/+1/@1/+1/,1/\n1/ 25/,1/+1/@1/+1/,26/+1/@1/+1/,1/\n1/ 26/,1/+1/@1/+1/,24/+1/@1/+1/,1/\n1/ 27/,1/+1/@1/+1/,22/+1/@1/+1/,1/\n1/ 28/,1/+1/@1/+1/,20/+1/@1/+1/,1/\n1/ 29/,1/+1/@1/+1/,18/+1/@1/+1/,1/\n1/ 30/,1/+1/@1/+1/,16/+1/@1/+1/,1/\n1/ 31/,1/+1/@1/+1/,14/+1/@1/+1/,1/\n1/ 32/,1/+1/@1/+1/,12/+1/@1/+1/,1/\n1/ 33/,1/+1/@1/+1/,10/+1/@1/+1/,1/\n1/ 34/,1/+1/@1/+1/,8/+1/@1/+1/,1/\n1/ 35/,1/+1/@1/+1/,6/+1/@1/+1/,1/\n1/ 36/,1/+1/@1/+1/,4/+1/@1/+1/,1/\n1/ 37/,1/+1/@1/+1/,2/+1/@1/+1/,1/\n1/ 38/,1/+1/@4/+1/,1/\n1/ 39/,1/:1/@2/:1/,1/\n1/ 41/,2/\n1", "encode": 1}
expected = json.dumps(expected)
result = ascii.encode(data)
self.assertEqual(result, expected)
def test_encode_is_smaller_than_file(self):
"""
Test that after encode the compressed file is smaller than the origin file
"""
path = './data.txt'
image = open(path, 'r')
data = image.read()
image.close()
result = ascii.encode(data)
self.assertLess(len(result), len(data))
def test_inefficient_file(self):
"""
Test that if encode is bigger than our original file we just send the original file
"""
path = './cloud.txt'
image = open(path, 'r')
data = image.read()
image.close()
data = ascii.encode(data)
encoded_data = json.loads(data)
result = encoded_data['encode']
self.assertEqual(result, 0)
def test_decode_equals_ascii(self):
"""
Test that after encoding and decoding the returned string matches the original file
"""
path = './akira.txt'
image = open(path, 'r')
data = image.read()
image.close()
compressed = ascii.encode(data)
result = ascii.decode(compressed)
self.assertEqual(result, data)
if __name__ == '__main__':
unittest.main()