|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # 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 | + |
0 commit comments