Python
“”” Test script for module a1 When run as a script, this module invokes several procedures that test the various functions in the module a1. Author: Anqi Bai/Hongyi Lu NetId:ab2897/hl2374 Date: 9/22/2019 “”” import introcs import a1 #Testing function before_space and after_space def testA(): “”” Test Procedure for Part A Testing function before_space and after_space “”” print(‘Testing function before_space’) #testing strings with one space result = a1.before_space(‘2 Bitcon’) introcs.assert_equals(‘2’,result) #testing strings with only space result = a1.before_space(‘ ‘) introcs.assert_equals(”,result) #testing strings with more than one spaces result = a1.before_space(‘6 Japanese Yen’) introcs.assert_equals(‘6’,result) #testing strings with more than one consective spaces result = a1.before_space(‘6 CubanPeso’) introcs.assert_equals(‘6’,result) print(‘Testing function after_space’) #testing strings with one space result = a1.after_space(‘2 Bitcon’) introcs.assert_equals(‘Bitcon’,result) #testing strings with only space result = a1.after_space(‘ ‘) introcs.assert_equals(”,result) #testing strings with more than one spaces result = a1.after_space(‘6 Japanese Yen’) introcs.assert_equals(‘Japanese Yen’,result) #testing strings with more than one consective spaces result = a1.after_space(‘6 CubanPeso’) introcs.assert_equals(‘ CubanPeso’,result) #Testing function first_inside_quotes, get_lhs, get_rhs and has_error def testB(): “”” Test procedure for Part B Testing function first_inside_quotes, get_lhs, get_rhs and has_error “”” print(‘Testing function first_inside_quotes’) #test strings with two quotes result = a1.first_inside_quotes(‘A “B C” D’) introcs.assert_equals(‘B C’,result) #test strings with nothing in quotes result = a1.first_inside_quotes(‘””‘) introcs.assert_equals(”,result) #test strings with all strings in two quotes result = a1.first_inside_quotes(‘”A B C D E F”‘) introcs.assert_equals(‘A B C D E F’,result) #test strings with more than two quotes result = a1.first_inside_quotes(‘A “B C” “D” “E” F’) introcs.assert_equals(‘B C’,result) print(‘Testing function get_lhs’) #Testing json with valid currency and amount result = a1.get_lhs(‘{ “ok”:true, “lhs”:”1 Bitcoin’+ ‘”, “rhs”:”9916.0137939344 Euros”, “err”:”” }’) introcs.assert_equals(‘1 Bitcoin’,result) #Testing json with invalid currency and amount result = a1.get_lhs(‘{ “ok”:false, “lhs”:””, “rhs”:””,’+ ‘ “err”:”Source currency code is invalid.” }’) introcs.assert_equals(”,result) print(‘Testing function get_rhs’) #Testing json with valid currency and amount result = a1.get_rhs(‘{ “ok”:true, “lhs”:”1 Bitcion”, ‘+ ‘”rhs”:”9916.0137 Euros”, “err”:”” }’) introcs.assert_equals(‘9916.0137 Euros’,result) #Testing json with valid currency and amount result = a1.get_rhs(‘{ “ok”:true, “lhs”:”1 Bitcion”,’+ ‘ “rhs”:”233 AED”, “err”:”” }’) introcs.assert_equals(‘233 AED’,result) #Testing json with invalid currency and amount result = a1.get_rhs(‘{ “ok”:false, “lhs”:””, “rhs”:””,’+ ‘ “err”:”Source currency code is invalid.” }’) introcs.assert_equals(”,result) print(‘Testing function has_error’) #Testing Json that is a response for invalid currency result = a1.has_error(‘{ “ok”:True, “lhs”:”1 Bitcoin”, ‘+ ‘”rhs”:”9916.0137 Euros”, “err”:”” }’) introcs.assert_equals(False,result) #Testing Json that is a response for valid currency result = a1.has_error(‘{ “ok”:false, “lhs”:””, “rhs”:””, ‘+ ‘”err”:”Source currency code is invalid.” }’) introcs.assert_equals(True,result) #Testing function currency_response def testC(): “”” Test procedure for Part C Testing function currency_response “”” print(‘Testing function currency_response’) #Testing with proper currency code and amount (same currency) result = a1.currency_response(‘USD’,’USD’,1.0) introcs.assert_equals(‘{ “ok”:true, “lhs”:”1 United States ‘+ ‘Dollar”, “rhs”:”1 United States Dollar”, “err”:”” }’,result) #Testing with proper currency code and amount result = a1.currency_response(‘AUD’,’USD’,2.0) introcs.assert_equals(‘{ “ok”:true, “lhs”:”2 Australian Dollars”, ‘+ ‘”rhs”:”1.4041334881625 United States Dollars”, “err”:”” }’,result) #Testing with improper currency code and amount with one invalid currency result = a1.currency_response(‘USD’,’ABC’,3.0) introcs.assert_equals(‘{ “ok”:false, “lhs”:””, “rhs”:””‘+ ‘, “err”:”Exchange currency code is invalid.” }’,result) #Testing with improper currency code and amount with two invalid currencies result = a1.currency_response(‘TYU’,’ABC’,3.0) introcs.assert_equals(‘{ “ok”:false, “lhs”:””, “rhs”:””, ‘+ ‘”err”:”Source currency code is invalid.” }’,result) #Testing function is_currency and exchange def testD(): “”” Test Procedure for Part D Testing function is_currency and exchange “”” print(‘Testing function is_currency’) #Testing with valid currency code result = a1.is_currency(‘USD’) introcs.assert_equals(True,result) #Testing with valid currency code result = a1.is_currency(‘CNY’) introcs.assert_equals(True,result) #Testing with invalid currency code result = a1.is_currency(‘ZZZ’) introcs.assert_equals(False,result) print(‘Testing function exchange’) #Testing with valid same currency and amount result = a1.exchange(‘USD’,’USD’,666.0) introcs.assert_floats_equal(666.0,result) #Testing with valid currency and amount with multiple decimals result = a1.exchange(‘USD’,’JPY’,666.0) introcs.assert_floats_equal(71871.39,result) testA() testB() testC() testD() print(‘Module a1 passed all tests.’)