def test_translate_non_langs(self, mock_handle, mock_open): mock_handle.side_effect = GrumbleError('some message') # non-existent language self.input.group.return_value = 'spa-zzz ' + self.texts['spa'] mock_open.side_effect = HTTPError('url', 400, 'msg', 'hdrs', None) apy.apertium_translate(self.phenny, self.input) self.assertTrue(mock_handle.called) self.phenny.say.assert_called_once_with('spa-zzz: some message') self.reset_mocks(self.phenny, mock_open, mock_handle) # bad input self.check_exceptions([self.texts['spa'], 'spa ' + self.texts['spa'], 'spa-eng'], apy.apertium_translate) self.check_exceptions(['en-en Translate to the same language?'], apy.apertium_translate, 'self-translation') self.reset_mocks(self.phenny, mock_open, mock_handle) # non-existent language with actual language self.input.group.return_value = 'spa-eng spa-zzz ' + self.texts['spa'] mock_open.side_effect = [mock.MagicMock(read=lambda: self.fake_json('eng')), HTTPError('url', 400, 'msg', 'hdrs', None)] apy.apertium_translate(self.phenny, self.input) self.assertEqual(mock_open.call_args_list, [mock.call(self.format_query('spa', 'eng')), mock.call(self.format_query('spa', 'zzz'))]) self.assertTrue(mock_handle.called) self.phenny.reply.assert_called_once_with(self.texts['eng']) self.phenny.say.assert_called_once_with('spa-zzz: some message')
def test_translate_admin(self, mock_open): self.input.group.return_value = 'eng-spa ' + self.texts['eng_long'] # restricted length for non-admin self.input.admin = False self.check_exceptions(['eng-spa ' + self.texts['eng_long']], apy.apertium_translate, 'long non-admin translation!') self.reset_mocks(self.phenny, mock_open) # non-restricted length for admin self.input.admin = True mock_open.return_value.read.return_value = self.fake_json('spa') apy.apertium_translate(self.phenny, self.input) mock_open.assert_called_once_with(self.trans_query.format( self.phenny.config.APy_url, quote(self.texts['eng_long']), 'eng', 'spa')) self.phenny.reply.assert_called_once_with(self.texts['spa']) self.reset_mocks(self.phenny, mock_open)
def test_translate_admin(self, mock_open): self.input.group.return_value = 'eng-spa ' + self.texts['eng_long'] # restricted length for non-admin self.input.admin = False self.check_exceptions(['eng-spa ' + self.texts['eng_long']], apy.apertium_translate, 'Phrase must be under 350 characters.') self.reset_mocks(self.phenny, mock_open) # non-restricted length for admin self.input.admin = True mock_open.return_value.read.return_value = self.fake_json('spa') apy.apertium_translate(self.phenny, self.input) mock_open.assert_called_once_with(self.trans_query.format( self.phenny.config.APy_url, quote(self.texts['eng_long']), 'eng', 'spa')) self.phenny.reply.assert_called_once_with(self.texts['spa']) self.reset_mocks(self.phenny, mock_open)
def test_translate_langs(self, mock_open): # single language self.input.group.return_value = 'eng-spa ' + self.texts['eng'] mock_open.return_value.read.return_value = self.fake_json('spa') apy.apertium_translate(self.phenny, self.input) mock_open.assert_called_once_with(self.format_query('eng', 'spa')) self.phenny.reply.assert_called_once_with(self.texts['spa']) self.reset_mocks(self.phenny, mock_open) # multiple languages langs = ['eng', 'fra', 'cat'] self.input.group.return_value = '{:s} {:s}'.format( ' '.join(['spa-' + lg for lg in langs]), self.texts['spa']) mock_open.return_value.read.side_effect = [self.fake_json(lg) for lg in langs] apy.apertium_translate(self.phenny, self.input) self.assertEqual(mock_open.call_args_list, [mock.call(self.format_query('spa', lg)) for lg in langs]) self.assertEqual(self.phenny.reply.call_args_list, [mock.call(self.texts[lg]) for lg in langs]) self.reset_mocks(self.phenny, mock_open)