-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_test.py
executable file
·172 lines (145 loc) · 6.71 KB
/
word_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env pytest
# GPLv3 Copyright (C) 2018 Seamus Johnston https://seamusjohnston.com
# What the heck is this file? :D
# When writing short programs, you can test if they work
# by running them.
# When writing longer programs (more than 100 lines?)
# that gets very tiring.
# There are simply too many different things you could do
# to test them all.
# This file uses the pytest library to test my program with
# a bunch of different inputs.
# Not only is it easier than testing by hand, it is a good idea
# to run tests before executing "risky" code.
# You don't want to discover that you have a bug which deletes cells
# after you run it on your important spreadsheet!!
# you can run these tests by installing pytest (python -m pip pytest)
# then running python -m pytest word_test.py
# make sure you are in the same folder as the tests :)
import pytest, requests, re
import word
# At a high level, I want the app to perform these functions
# A. Given a word
# -- tell me what word I'm trying to spell
# -- tell me what a word means
# -- tell me how to say a word
# B. Given a word or a phrase
# -- tell me words that are like it
# These paramaters exist in the datamuse API but haven't been implemnted yet in the code:
# "rel_jja" "rel_jjb" "rel_syn" "rel_trg" "rel_ant" "rel_spc" "rel_gen" "rel_com" "rel_par" "rel_bga" "rel_bgb" "rel_nry" "rel_cns"
class TestWord(object):
# this sets some defaults for all the tests
@pytest.fixture(scope="function", autouse=True)
def globalvars(self,monkeypatch):
word.verbose = 0
word.query_type = None
# whenever requests.get() is called in the code, run TestWord.mockget() instead
monkeypatch.setattr(requests, 'get', TestWord.mockget)
# this creates a "fake" http response without accessing the internet
# it makes the tests faster and also doesn't waste bandwidth
def mockget(*args,**kwargs):
response = requests.Response()
response.request = requests.Request('GET',*args,**kwargs).prepare()
response.url = response.request.url
return response
# Tests start HERE \o/
def test_simple_query_should_encode_correctly(self):
args = ['platypus']
query = word.parse(args, {})
responses = word.go_fetch(query)
# for each test, we just "assert" that something should be true
assert re.search(r'max=20',responses[0].url)
assert re.search(r'ml=platypus',responses[0].url)
# if it's not, the test fails
def test_multiple_words_should_encode_correctly(self):
args = ['elephant', 'trunk']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'ml=elephant\+trunk',responses[0].url)
def test_max_should_be_setable(self):
args = ['elephant', 'trunk', 'and', 'max', 'six']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'max=6',responses[0].url)
assert re.search(r'ml=elephant\+trunk',responses[0].url)
def test_order_should_not_change_the_query(self):
args = ['max', 'six', 'elephant', 'trunk']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'max=6',responses[0].url)
assert re.search(r'ml=elephant\+trunk',responses[0].url)
def test_quotes_should_protect_modifier_words_as_literal(self):
args = ['meaning of life']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'ml=meaning\+of\+life',responses[0].url)
def test_meaning_should_collect_other_modifiers(self):
args = ['meaning', 'pride', 'comes', 'before', 'a', 'fall']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'ml=pride\+comes\+before\+a\+fall',responses[0].url)
def test_meaning_should_collect_until_stop_word(self):
args = ['meaning', 'art', 'without', 'meaning', 'and', 'max', 'ten']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'max=10',responses[0].url)
assert re.search(r'ml=art\+without',responses[0].url)
def test_sounds_like_should_use_sl_paramater(self):
args = ['which', 'sounds', 'like', 'tung']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'sl=tung',responses[0].url)
def test_but_spelled_different_should_use_rel_hom_parameter(self):
args = ['sounding', 'like', 'doe', 'but', 'spelled', 'different']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'rel_hom=doe',responses[0].url)
def test_topic_should_be_setable(self):
args = ['which', 'means', 'laps', 'and', 'is', 'about', 'running']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'ml=laps',responses[0].url)
assert re.search(r'topics=running',responses[0].url)
def test_spelled_like_should_use_sp_parameter(self):
args = ['spelled', 'like', 'platapus']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'sp=platapus',responses[0].url)
def test_sp_and_ml_should_be_combinable(self):
args = ['animal', 'and', 'spelled', 'like', 'platapus']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'ml=animal',responses[0].url)
assert re.search(r'sp=platapus',responses[0].url)
def test_ryhmes_with_should_use_rel_rhy_even_when_spelled_wrong(self):
args = ['ryhmes', 'with', 'cute']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'rel_rhy=cute',responses[0].url)
def test_comes_before_should_use_rc(self):
args = ['ryhmes', 'with', 'cute', 'and', 'comes', 'before', 'bowl']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'rel_rhy=cute',responses[0].url)
assert re.search(r'rc=bowl',responses[0].url)
def test_comes_after_should_use_lc(self):
args = ['ryhmes', 'with', 'cute', 'and', 'comes', 'after', 'leather']
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'rel_rhy=cute',responses[0].url)
assert re.search(r'lc=leather',responses[0].url)
def test_definition_of_should_use_owlbot(self):
args = ['definition', 'of', 'nostrum']
word.query_type = "DEF"
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'owlbot\.info',responses[0].url) or re.search(r'owlbot\.info',responses[1].url)
def test_pernounciation_of_should_use_meta_data_r_even_when_spelled_wrong(self):
args = ['pernounciation', 'of', 'nostrum']
word.query_type = "PRO"
query = word.parse(args, {})
responses = word.go_fetch(query)
assert re.search(r'sp=nostrum',responses[0].url)
assert re.search(r'qe=sp',responses[0].url)
assert re.search(r'md=r',responses[0].url)
assert re.search(r'ipa=1',responses[0].url)