def setUp(self): self.exLib = ExLib(_test_script, XML_STR)
class TestExLib(unittest.TestCase): def setUp(self): self.exLib = ExLib(_test_script, XML_STR) def test_set_result(self): test_result = '<Answer><Params><Param1 id="GUID1">read</Param1>' \ '<Paramn id="GUIDn">1234</Paramn></Params><ResultInf>' \ '<LastResultCode>2</LastResultCode><LastResultString>Data OK' \ '</LastResultString></ResultInf></Answer>' result = self.exLib.set_result('2', 'Data OK') self.assertEqual(test_result, result) def test_get_input_params(self): test_result = ['read', '1234'] result = self.exLib._get_input_params('Params') self.assertEqual(test_result, result) def test_set_params(self): param = ['write', '4321'] test_result = '<Answer><Params><Param1 id="GUID1">write</Param1>' \ '<Paramn id="GUIDn">4321</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' xml_res = self.exLib._set_params('Params', param) result = ET.tostring(xml_res).decode('utf-8') self.assertEqual(test_result, result) def test_set_params_empty(self): test_result = XML_STR param = [] xml_res = self.exLib._set_params('Params', param) result = ET.tostring(xml_res).decode('utf-8') self.assertEqual(test_result, result) def test_result_DataOK(self): test_res = '<Answer><Params><Param1 id="GUID1">readsome text</Param1>'\ '<Paramn id="GUIDn">12341234</Paramn></Params><ResultInf>' \ '<LastResultCode>2</LastResultCode><LastResultString>Data OK' \ '</LastResultString></ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_empty_param1(self): test_xml = '<Answer><Params><Param1 id="GUID1"></Param1>' \ '<Paramn id="GUIDn">second</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1" />' \ '<Paramn id="GUIDn">second</Paramn></Params><ResultInf>' \ '<LastResultCode>118</LastResultCode><LastResultString>' \ 'Empty param1</LastResultString></ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_empty_param2(self): test_xml = '<Answer><Params><Param1 id="GUID1">first</Param1>' \ '<Paramn id="GUIDn"></Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">first</Param1>' \ '<Paramn id="GUIDn" /></Params><ResultInf><LastResultCode>' \ '118</LastResultCode><LastResultString>Empty param2' \ '</LastResultString></ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_bad_result_bool(self): self.maxDiff = None test_xml = '<Answer><Params><Param1 id="GUID1">bad</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">bad</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode>' \ '118</LastResultCode><LastResultString>Not correct result format' \ '</LastResultString></ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_bad_result_empty(self): test_xml = '<Answer><Params><Param1 id="GUID1">empty</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">empty</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode>118</LastResultCode><LastResultString>'\ 'No result from user script</LastResultString></ResultInf>'\ '</Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_bad_result_no_res(self): test_xml = '<Answer><Params><Param1 id="GUID1">no_res</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">no_res</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode>118</LastResultCode><LastResultString>' \ 'Error script result (not True or False)</LastResultString>' \ '</ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_bad_result_notcorrect(self): test_xml = '<Answer><Params><Param1 id="GUID1">no_cor</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">no_cor</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode>118</LastResultCode><LastResultString>' \ 'Error script result: (result = no_cor)</LastResultString>' \ '</ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result) def test_result_bad_result_many_param(self): test_xml = '<Answer><Params><Param1 id="GUID1">many</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode /><LastResultString /></ResultInf></Answer>' self.exLib._xml = ET.fromstring(test_xml) test_res = '<Answer><Params><Param1 id="GUID1">many</Param1>' \ '<Paramn id="GUIDn">bad</Paramn></Params><ResultInf>' \ '<LastResultCode>118</LastResultCode><LastResultString>' \ 'Error script result: (result = [None, None])</LastResultString>' \ '</ResultInf></Answer>' result = self.exLib.result() self.assertEqual(test_res, result)