-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_line_dict_test.py
125 lines (112 loc) · 5.17 KB
/
command_line_dict_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import unittest
import os
from mock import MagicMock
from wordnik import *
from mock import patch
import pdb
import sys
from StringIO import StringIO
from command_line_dictionary import CommandLineDict
class TestCommandLineDict(unittest.TestCase):
def setUp(self):
apiUrl = 'http://api.wordnik.com/v4'
apiKey = os.environ['API_KEY']
self.client = swagger.ApiClient(apiKey, apiUrl)
self.test_object = CommandLineDict()
@patch('wordnik.WordApi.WordApi')
def test_get_definitions(self, mock_object):
instance = mock_object.return_value
mock = MagicMock()
self.set_mock_and_test_definitions(mock, instance)
@patch('wordnik.WordApi.WordApi')
def test_get_synonyms(self, mock_object):
instance = mock_object.return_value
mock = MagicMock()
self.set_mock_and_test_synonyms(mock, instance)
@patch('wordnik.WordApi.WordApi')
def test_get_antonyms(self, mock_object):
instance = mock_object.return_value
mock = MagicMock()
self.set_mock_and_test_antonyms(mock, instance)
@patch('wordnik.WordApi.WordApi')
def test_get_example(self, mock_object):
instance = mock_object.return_value
mock = MagicMock()
self.set_mock_and_test_examples(mock, instance)
@patch('wordnik.WordApi.WordApi')
def test_get_dictionary(self, mock_object):
instance = mock_object.return_value
mock = MagicMock()
[mock, instance] = self.set_mock_and_test_antonyms(mock, instance)
[mock, instance] = self.set_mock_and_test_synonyms(mock, instance)
[mock, instance] = self.set_mock_and_test_definitions(mock, instance)
[mock, instance] = self.set_mock_and_test_examples(mock, instance)
saved_stdout = sys.stdout
out = StringIO()
sys.stdout = out
original_call = self.test_object.get_dictionary('test_word')
output = out.getvalue().strip()
result = ("Examples of the word test_word are"
"\n* test1\n* test1\n* test1\nSynonyms of the word test_word"
" are\ntest1\ntest2\ntest3\ntest1\ntest2\ntest3\ntest1"
"\ntest2\ntest3\nNo Antonyms for the word test_word\n"
"Definitions of the word test_word are\ntest1\ntest1\ntest1")
self.assertEqual(output, result)
self.set_mock_and_test_antonyms(mock, instance)
original_call = self.test_object.get_dictionary('test_word')
output = out.getvalue().strip()
result = ("Examples of the word test_word are"
"\n* test1\n* test1\n* test1\nSynonyms of the word test_word"
" are\ntest1\ntest2\ntest3\ntest1\ntest2\ntest3\ntest1"
"\ntest2\ntest3\nNo Antonyms for the word test_word\n"
"Definitions of the word test_word are\ntest1\ntest1\ntest1"
"\nExamples of the word test_word are\n* test1\n* test1\n*"
" test1\nNo Synonyms for the word test_word\nAntonyms of the"
" word test_word are\ntest1\ntest2\ntest1\ntest2\ntest1\n"
"test2\nDefinitions of the word test_word are\ntest1\ntest1"
"\ntest1")
self.assertEqual(output, result)
sys.stdout = saved_stdout
def set_mock_and_test_antonyms(self, mock, method):
mock.relationshipType = 'antonym'
mock.words = ['test1', 'test2']
method.getRelatedWords.return_value = [mock, mock, mock]
original_call = self.test_object.get_antonyms('test')
self.assertEqual(len(original_call), 6)
result = ['test1', 'test2', 'test1', 'test2', 'test1', 'test2']
self.assertEqual(result, original_call)
return [mock, method]
def set_mock_and_test_synonyms(self, mock, method):
mock.relationshipType = "synonym"
mock.words = ['Test1', 'Test2', 'Test3']
method.getRelatedWords.return_value = [mock, mock, mock]
original_call = self.test_object.get_synonyms('test')
self.assertEqual(len(original_call), 9)
result = ['test1', 'test2', 'test3',
'test1', 'test2', 'test3',
'test1', 'test2', 'test3']
self.assertEqual(original_call, result)
return [mock, method]
def set_mock_and_test_definitions(self, mock, method):
mock.text = 1
method.getDefinitions.return_value = [mock, mock, mock]
original_call = self.test_object.get_definitions(mock)
self.assertEqual(len(method.getDefinitions()), len(original_call))
self.assertEqual(original_call, [1, 1, 1])
return [mock, method]
def set_mock_and_test_examples(self, mock, method):
mock.text = 'test1'
method.getExamples.return_value = mock
mock.examples = [mock, mock, mock]
saved_stdout = sys.stdout
out = StringIO()
sys.stdout = out
original_call = self.test_object.get_examples('test_word')
output = out.getvalue().strip()
result = ("Examples of the word test_word are\n* test1\n* test1\n*"
" test1")
self.assertEqual(output, result)
sys.stdout = saved_stdout
return [mock, method]
if __name__ == '__main__':
unittest.main()