Skip to content

Commit cb1b422

Browse files
committed
Added tests for background tasks
1 parent a0487e4 commit cb1b422

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

background/app/main_test.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,83 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
import json
16+
import main
17+
import pytest
18+
19+
20+
class MockFirestoreClient():
21+
def __init__(self, *args, **kwargs):
22+
pass
23+
24+
def collection(self, name):
25+
return self
26+
27+
def stream(self):
28+
return [self]
29+
30+
def to_dict(self):
31+
return {
32+
'Original': 'This is a test sentence',
33+
'Language': 'de',
34+
'Translated': 'This should be in German in a real environment',
35+
'OriginalLanguage': 'en',
36+
}
37+
38+
39+
class MockPublisherClient():
40+
def __init__(self, *args, **kwargs):
41+
self.messages = []
42+
43+
def publish(self, topic_name, json_message):
44+
message = json.loads(json_message)
45+
self.messages.append({
46+
'topic': topic_name,
47+
'message': message,
48+
})
49+
50+
51+
@pytest.yield_fixture
52+
def db():
53+
client = MockFirestoreClient()
54+
yield client
55+
56+
57+
@pytest.yield_fixture
58+
def publisher():
59+
client = MockPublisherClient()
60+
yield client
61+
62+
63+
64+
def test_index(db, publisher):
65+
main.app.testing = True
66+
main.db = db
67+
main.publisher = publisher
68+
client = main.app.test_client()
69+
70+
r = client.get('/')
71+
assert r.status_code == 200
72+
response_text = r.data.decode('utf-8')
73+
assert 'Text to translate' in response_text
74+
assert 'This should be in German' in response_text
75+
76+
77+
def test_translate(db, publisher):
78+
main.app.testing = True
79+
main.db = db
80+
main.publisher = publisher
81+
client = main.app.test_client()
82+
83+
r = client.post('/request-translation', data={
84+
'v': 'This is a test',
85+
'lang': 'fr',
86+
})
87+
88+
assert r.status_code < 400
89+
assert len(publisher.messages) == 1
90+
assert '/translate' in publisher.messages[0]['topic']
91+
assert publisher.messages[0]['message']['Original'] == 'This is a test'
92+
assert publisher.messages[0]['message']['Language'] == 'fr'
93+

background/function/main_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,51 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
import base64
16+
import json
17+
import main
18+
import pytest
19+
20+
21+
class MockTranslateClient():
22+
def __init__(self, *args, **kwargs):
23+
pass
24+
25+
def translate(self, s, target_language='N/A'):
26+
return {
27+
'translatedText': 'This is the output string',
28+
'detectedSourceLanguage': 'en',
29+
}
30+
31+
32+
@pytest.yield_fixture
33+
def xlate():
34+
client = MockTranslateClient()
35+
yield client
36+
37+
38+
last_message = None
39+
def dummy_update(transaction, message):
40+
global last_message
41+
last_message = message
42+
43+
44+
def test_invocations(xlate):
45+
main.update_database = dummy_update
46+
main.xlate = xlate
47+
48+
event = {
49+
'data': base64.b64encode(json.dumps({
50+
'Original': 'My test message',
51+
'Language': 'de',
52+
}).encode('utf-8'))
53+
}
54+
55+
main.translate_message(event, None)
56+
57+
assert last_message is not None
58+
assert last_message['Original'] == 'My test message'
59+
assert last_message['Language'] == 'de'
60+
assert last_message['Translated'] == 'This is the output string'
61+
assert last_message['OriginalLanguage'] == 'en'

0 commit comments

Comments
 (0)