def test_convertHugeNumber(self, logs): requestJson = requestTemplate({ 'text': '99999999999.999 km to m', 'chat': { 'id': 11 } }) expectedResponseJson = responseTemplate({ 'chat_id': 11, 'text': u"Sorry, I can't convert such big numbers." }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'InvalidValueException', 'expression': u'99999999999.999 km to m' })) requestJson = requestTemplate({ 'text': '99999999999.99 km to m', 'chat': { 'id': 9 } }) expectedResponseJson = responseTemplate({ 'chat_id': 9, 'text': '99,999,999,999,990 m', }) self.assertRequest(requestJson, expectedResponseJson)
def test_noChatId(self, logs): requestJson = requestTemplate({ 'text': '/someCommand 1 m² to ft', }) self.assertRequest(requestJson, {'error': 'Chat ID is empty'}) self.checkLogs(logs, ('WARNING', 'Chat ID is empty'))
def test_start(self): requestJson = requestTemplate({'text': '/start', 'chat': {'id': 861}}) expectedResponseJson = responseTemplate({ 'chat_id': 861, 'text': startMessage }) self.assertRequest(requestJson, expectedResponseJson)
def test_defaultHelp(self): requestJson = requestTemplate({ 'text': '/help', 'chat': { 'id': 8654 } }) expectedResponseJson = responseTemplate({ 'chat_id': 8654, 'parse_mode': 'Markdown', 'text': u""" The bot supports following unit categories: - length (ft to m) - area (m² to acre) - volume (litre to pint) - currencies ($ to €) - mass (g to lb) - speed (km/h to mph) - time (min to ms) - temperature (°C to °F) - density (kg/m³ to g/cm³) - information (MB to Kbit) - pressure (atm to Pa) - fuel consumption (mpg to km/l) - power (horsepower to kW) - torque (N-m to lbf-ft) To get all available units of specific category type "/help #category#" ("/help length", for example). While asking me a question tet-a-tet you can omit a command. Just type: - 100 $ to € - 1 sq foot to m2 - 1 year to hours While asking me a question in a group please add the command "convert" (if I'm a group member too) or my username "UnitConversionBot": - /convert 10 yr to mon - @UnitConversionBot 1 ms to second You can also ask me without adding me to the chat. Just type '@UnitConversionBot 100 ft to m' when you need my help. You can use both short (`m2`) and full unit names (square meter). If you have an issue or just want to say thanks, feel free to contact my master @kirillmaltsev or rate me at https://storebot.me/bot/unitconversionbot Thank you for chatting with me :-) """.strip() }) self.assertRequest(requestJson, expectedResponseJson)
def test_convertMultilineExpression(self): requestJson = requestTemplate({ 'text': '1000 m/s to km/h\n10 fl.oz to litres', 'chat': { 'id': 19 } }) expectedResponseJson = responseTemplate({ 'chat_id': 19, 'text': '3,600 km/h' }) self.assertRequest(requestJson, expectedResponseJson)
def test_convertWithBotName(self): requestJson = requestTemplate({ 'text': '/convert@UnitConversionBot 1 day to hours', 'chat': { 'id': 11 } }) expectedResponseJson = responseTemplate({ 'chat_id': 11, 'text': '24 h' }) self.assertRequest(requestJson, expectedResponseJson)
def test_convertWithoutCommandAndWithBotName(self): requestJson = requestTemplate({ 'text': '@UnitConversionBot 1 m/s to km/h', 'chat': { 'id': 900 } }) expectedResponseJson = responseTemplate({ 'chat_id': 900, 'text': '3.6 km/h' }) self.assertRequest(requestJson, expectedResponseJson)
def test_emptyConvertExpression(self): requestJson = requestTemplate({ 'text': '/convert ', 'chat': { 'id': 96 } }) expectedResponseJson = responseTemplate({ 'chat_id': 96, 'text': 'Please type something like "/convert 100 ft to m"' }) self.assertRequest(requestJson, expectedResponseJson)
def test_commandNotFound(self): requestJson = requestTemplate({ 'text': '/someCommand 1 m² to ft', 'chat': { 'id': 469 } }) expectedResponseJson = responseTemplate({ 'chat_id': 469, 'text': "Sorry, I don't understand your command." }) self.assertRequest(requestJson, expectedResponseJson)
def test_convertNotExistsUnits(self): requestJson = requestTemplate({ 'text': '1.2 blablagram to wtfgram', 'chat': { 'id': 9 } }) expectedResponseJson = responseTemplate({ 'chat_id': 9, 'text': "Sorry, I'm just a stupid bot :-( I don't know what does 'blablagram' mean. But my master probably does. I'd ask him to teach me." }) self.assertRequest(requestJson, expectedResponseJson)
def assertHelp(self, unitsCategory, expectedUnits): requestJson = requestTemplate({ 'text': '/help ' + unitsCategory, 'chat': { 'id': 8654 } }) expectedText = u'*The full list of {} units:*\n'.format(unitsCategory) expectedText += expectedUnits.strip() expectedResponseJson = responseTemplate({ 'chat_id': 8654, 'parse_mode': 'Markdown', 'text': expectedText }) self.assertRequest(requestJson, expectedResponseJson)
def test_convertWithoutToUnit(self, logs): requestJson = requestTemplate({ 'text': u'/convert 100 ft to', 'chat': { 'id': 530 } }) expectedResponseJson = responseTemplate({ 'chat_id': 530, 'text': u'30.48 m' }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'success', 'expression': u'100 ft to', 'response': u'30.48 m' }))
def test_convert(self, logs): requestJson = requestTemplate({ 'text': u'/convert 1 ft² to m²', 'chat': { 'id': 536 } }) expectedResponseJson = responseTemplate({ 'chat_id': 536, 'text': u'0.093 m²' }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'success', 'expression': u'1 ft² to m²', 'response': u'0.093 m²' }))
def test_convertWithoutCommand(self, logs): requestJson = requestTemplate({ 'text': '1.2 LB TO Gram', 'chat': { 'id': 928 } }) expectedResponseJson = responseTemplate({ 'chat_id': 928, 'text': '544.311 g' }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'success', 'expression': u'1.2 LB TO Gram', 'response': u'544.311 g' }))
def test_invalidExpression(self, logs): requestJson = requestTemplate({ 'text': u'1 фут в метры', 'chat': { 'id': 113 } }) expectedResponseJson = responseTemplate({ 'chat_id': 113, 'text': u"Sorry, I'm just a stupid bot :-( I don't know what does 'фут' mean. But my master probably does. I'd ask him to teach me." }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'InvalidUnitException', 'expression': u'1 фут в метры' }))
def test_convertIncompatibleUnitCategories(self, logs): requestJson = requestTemplate({ 'text': '1 ft to g', 'chat': { 'id': 110 } }) expectedResponseJson = responseTemplate({ 'chat_id': 110, 'text': "Sorry, I can't convert foot to gram (at least in this universe)." }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'IncompatibleCategoriesException', 'expression': u'1 ft to g' }))
def test_convertInvalidUnitValue(self, logs): requestJson = requestTemplate({ 'text': 'one ft to m', 'chat': { 'id': 8 } }) expectedResponseJson = responseTemplate({ 'chat_id': 8, 'text': u"Sorry, I don't understand the number 'one'. Please type it in a more ordinary way, like 100 or 12.5" }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'InvalidValueException', 'expression': u'one ft to m' }))
def test_convertNotExistsFromUnit(self, logs): requestJson = requestTemplate({ 'text': '1 blablagram² to gram', 'chat': { 'id': 11 } }) expectedResponseJson = responseTemplate({ 'chat_id': 11, 'text': u"Sorry, I'm just a stupid bot :-( I don't know what does 'blablagram²' mean. But my master probably does. I'd ask him to teach me." }) self.assertRequest(requestJson, expectedResponseJson) self.checkLogs(logs, ('INFO', { 'command': 'convert', 'type': 'InvalidUnitException', 'expression': u'1 blablagram² to gram' }))
def test_convertCurrencies(self): responseMessage = '2.014 CZK' if not os.environ['RUN_ON_INSTANCE']: # Add recent exchange rates anotherTestExchangeRates = testExchangeRates.copy() anotherTestExchangeRates['rates']['CZK'] = 1000000.0 futureDate = datetime.datetime.now() + datetime.timedelta(days=1) Rates(content=anotherTestExchangeRates, date=futureDate).put() responseMessage = '99,318.956 CZK' requestJson = requestTemplate({ 'text': u'10.5 Icelandic Króna to Kč', 'chat': { 'id': 901 } }) expectedResponseJson = responseTemplate({ 'chat_id': 901, 'text': responseMessage }) self.assertRequest(requestJson, expectedResponseJson)
def test_noMessageText(self): requestJson = requestTemplate({'chat': {'id': 860}}) self.assertRequest(requestJson, {})