def test_convertString2Dictionary(self): s1 = "abc%3D123" d1 = {'abc':'123'} s2 = "function%3D%20calculatePosition%2C%20sighting%3DBetelgeuse" d2 = {'function':'calculatePosition', 'sighting':'Betelgeuse'} s3 = "function%3D%20calc11latePosition%2C%20si123141ting%3DB123" d3 = {'function':'calc11latePosition', 'si123141ting':'B123'} self.assertEqual(d1,converter.convertString2Dictionary(s1)) self.assertEqual(d2,converter.convertString2Dictionary(s2)) self.assertEqual(d3,converter.convertString2Dictionary(s3)) #negative cases invalid = ['key%3Dvalue%2C%20key%3Dvalue', 'key%3Dkey%3Dkey%3D', 'function%20%3D%20get_stars', 'value', '1key%3Dvalue', '1key%3D%3Dvalue', 'k%20e%20y%20%3D%20value', 'k%20%20y%20%3D%20va%3D%3Dlue', '', 'key1%3Dvalue%3B%20key2%3Dvalue'] error = converter.error_dict() for s in invalid: self.assertEqual(error,converter.convertString2Dictionary(s), s+':'+ str(converter.convertString2Dictionary(s)))
import softwareprocess.convertString2Dictionary as cs2d import urllib # convert human-readable key=value to percent-encoded string inputValue = urllib.quote("key1=value, key2=value") # convert percent-encoded string to dictionary outputValue = cs2d.convertString2Dictionary(inputString = inputValue) # iterate through keys, print key and corresponding value for key in outputValue.keys(): print('key={0}, value={1}'.format(key, outputValue[key]))
''' Created on Feb 13th, 2017 @author Anakin Lau ''' import urllib import softwareprocess.convertString2Dictionary as cs2d print "First test blank" print "Should be False" print cs2d.convertString2Dictionary() print "\n" print "Now test random string" print "Should be False" print cs2d.convertString2Dictionary("a normal string") print "\n" print "abc%3D123" print cs2d.convertString2Dictionary("abc%3D123") print "\n" print "function%3D%20calculatePosition%2C%20sighting%3DBetelgeuse" print cs2d.convertString2Dictionary( "function%3D%20calculatePosition%2C%20sighting%3DBetelgeuse") print "\n" print "Try with more spaces" print "%20function%20%3D%20calculatePosition%20%20%2C%20%20%20sighting%20%20%3DBetelgeuse" print cs2d.convertString2Dictionary( "%20function%20%3D%20calculatePosition%20%20%2C%20%20%20sighting%20%20%3DBetelgeuse"
def test_100_915_ShouldReturnErrorOnDuplicateKey(self): encodedString = urllib.quote("key=value,key=value") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on duplicate key")
def test_100_910_ShouldReturnErrorOnMissingComma(self): encodedString = urllib.quote("key1=value1 key2=value2") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on missing comma")
def test_100_905_ShouldReturnErrorNullString(self): self.assertDictEqual( cs2d.convertString2Dictionary(" "), self.errorDict, "Minor defect: does not return proper result on blank parm")
def test_100_900_ShouldReturnErrorNoInput(self): self.assertDictEqual( cs2d.convertString2Dictionary(), self.errorDict, "Major defect: does not return proper result on missing parm")
def test_100_030_ShouldConvertNominalStringWithMultipleKeyValuePairs(self): encodedString = urllib.quote("key1=value,key2=value,key3=value") expectedResult = {'key1': 'value', 'key2': 'value', 'key3': 'value'} self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), expectedResult, "Major defect: not able to parse multiple comma spearated pairs")
def test_100_010_ShouldConvertNominalStringWithOneKeyValuePair(self): encodedString = urllib.quote("key=value") expectedResult = {'key': 'value'} self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), expectedResult, "Major defect: not able to parse one key-value pair")
def test_100_960_ShouldReturnErrorOnValidInvalidKeyValuePair(self): encodedString = urllib.quote("key1=value,key2=") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on mix of valid and invalid items" )
def test_100_960_ShouldReturnErrorOnLoneComma(self): encodedString = urllib.quote(",") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on empty separator")
def test_100_955_ShouldReturnErrorOnMissingKeyMissingValue(self): encodedString = urllib.quote("=") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on missing key and missing value" )
def test_100_950_ShouldReturnErrorOnValueWithInvalidChar(self): encodedString = urllib.quote("key=va_lue") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on value with invalid characters" )
def test_100_945_ShouldReturnErrorOnValueWithBlank(self): encodedString = urllib.quote("key=va lue") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on value with embedded blanks" )
def test_100_920_ShouldReturnErrorOnBadKeyName(self): encodedString = urllib.quote("1key=value") self.assertDictEqual( cs2d.convertString2Dictionary(encodedString), self.errorDict, "Major defect: does not return proper result on invalid key")
def testConvertString2Dictionary(self): expected = { 'function%3D%20calculatePosition%2C%20sighting%3DBetelgeuse': { 'function': 'calculatePosition', 'sighting': 'Betelgeuse' }, 'abc%3D123': { 'abc': '123' }, 'function%20%3D%20get_stars': { 'error': 'true' }, 'key%3Dvalue%2C%20key%3Dvalue': { 'error': 'true' }, 'key%3D': { 'error': 'true' }, 'value': { 'error': 'true' }, '1key%3Dvalue': { 'error': 'true' }, 'k%20e%20y%20%3D%20value': { 'error': 'true' }, '': { 'error': 'true' }, 'key1%3Dvalue%3B%20key2%3Dvalue': { 'error': 'true' }, '%20key%3Dvalue ': { 'key': 'value' }, '%3Dkey': { 'error': 'true' }, '%3Dkey%3Dvalue': { 'error': 'true' }, 'key%3Dvalue%3Dkey1%3Dvalue2': { 'error': 'true' }, 'key%3D%3Dvalue%20': { 'error': 'true' }, 'key%Dvalue%3D': { 'error': 'true' }, '.key%3Dvalue': { 'error': 'true' }, 'ke.y%3Dvalue': { 'ke.y': 'value' }, 'ke.y%3D.v.a.lue': { 'ke.y': '.v.a.lue' }, 'ke.y%3D%20.v.a.lue': { 'ke.y': '.v.a.lue' }, 'ke.y%3D%20.%20v.a.lue': { 'error': 'true' }, 'key%3D%20value%2C%20%20%3Dvalue2': { 'error': 'true' }, 'function%3D%20calculatePosition%2C%20sighting%3DBet%20.elgeuse': { 'error': 'true' } } for input, expected in expected.iteritems(): actual = convertString2Dictionary(input) self.assertEqual(actual, expected)