def test_write_read_files(self): '''test_write_read_files will test the functions write_file and read_file ''' print("Testing utils.write_file...") from sutils import write_file import json tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "hello!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from sutils import read_file content = read_file(tmpfile)[0] self.assertEqual("hello!", content) from sutils import write_json print("Testing utils.write_json...") print("Case 1: Providing bad json") bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError) as cm: write_json(bad_json, tmpfile) print("Case 2: Providing good json") good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_json(good_json, tmpfile) content = json.load(open(tmpfile, 'r')) self.assertTrue(isinstance(content, dict)) self.assertTrue("Wakkawakkawakka" in content)
def test_dump(self): '''test_add_delete will test the add and delete functions ''' print('Testing json DUMP') from sutils import write_json, read_json print('Case 1: Dumping file.') jsondump = {'HELLO': 'KITTY', 'BATZ': 'MARU', 'MY': 'MELODY'} write_json(jsondump, self.file) self.assertTrue(os.path.exists(self.file)) script_path = "%s/helpers/json/dump.py" % self.here if VERSION == 2: testing_command = ["python2", script_path, '--file', self.file] else: testing_command = ["python3", script_path, '--file', self.file] output = Popen(testing_command, stderr=PIPE, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 0) output = result['message'] if isinstance(output, bytes): output = output.decode(encoding='UTF-8') dump = ['HELLO:"KITTY"', 'BATZ:"MARU"', 'MY:"MELODY"'] result = output.strip('\n').split('\n')[-3:] for res in result: self.assertTrue(res in dump)
def test_dump(self): '''test_add_delete will test the add and delete functions ''' print('Testing json DUMP') from sutils import write_json, read_json print('Case 1: Dumping file.') jsondump = {'HELLO':'KITTY', 'BATZ':'MARU', 'MY':'MELODY' } write_json(jsondump,self.file) self.assertTrue(os.path.exists(self.file)) script_path = "%s/helpers/json/dump.py" %(self.here) if VERSION == 2: testing_command = ["python2",script_path,'--file',self.file] else: testing_command = ["python3",script_path,'--file',self.file] output = Popen(testing_command,stderr=PIPE,stdout=PIPE) t = output.communicate()[0],output.returncode result = {'message':t[0], 'return_code':t[1]} self.assertEqual(result['return_code'],0) output = result['message'] if isinstance(output,bytes): output = output.decode(encoding='UTF-8') dump = ['HELLO:"KITTY"', 'BATZ:"MARU"', 'MY:"MELODY"'] result = output.strip('\n').split('\n')[-3:] for res in result: self.assertTrue(res in dump)
def test_get(self): '''test_get will test the get function ''' print('Testing json GET') print('Case 1: Get exiting key') from sutils import write_json write_json({"PASTA": "rigatoni!"}, self.file) self.assertTrue(os.path.exists(self.file)) script_path = "%s/helpers/json/get.py" % self.here if VERSION == 2: testing_command = ["python2", script_path, '--key', 'PASTA', '--file', self.file] else: testing_command = ["python3", script_path, '--key', 'PASTA', '--file', self.file] output = Popen(testing_command, stderr=PIPE, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 0) output = result['message'] if isinstance(output, bytes): output = output.decode(encoding='UTF-8') self.assertEqual('rigatoni!', output.strip('\n').split('\n')[-1]) print('Case 2: Get non-existing key exits') if VERSION == 2: testing_command = ["python2", script_path, '--key', 'LASAGNA', '--file', self.file] else: testing_command = ["python3", script_path, '--key', 'LASAGNA', '--file', self.file] output = Popen(testing_command, stderr=PIPE, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 1)
def test_get(self): '''test_get will test the get function ''' print('Testing json GET') print('Case 1: Get exiting key') from sutils import write_json write_json({"PASTA": "rigatoni!"}, self.file) self.assertTrue(os.path.exists(self.file)) script_path = "%s/helpers/json/get.py" % self.here if VERSION == 2: testing_command = [ "python2", script_path, '--key', 'PASTA', '--file', self.file ] else: testing_command = [ "python3", script_path, '--key', 'PASTA', '--file', self.file ] output = Popen(testing_command, stderr=PIPE, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 0) output = result['message'] if isinstance(output, bytes): output = output.decode(encoding='UTF-8') self.assertEqual('rigatoni!', output.strip('\n').split('\n')[-1]) print('Case 2: Get non-existing key exits') if VERSION == 2: testing_command = [ "python2", script_path, '--key', 'LASAGNA', '--file', self.file ] else: testing_command = [ "python3", script_path, '--key', 'LASAGNA', '--file', self.file ] output = Popen(testing_command, stderr=PIPE, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 1)
def DELETE(key,jsonfile): '''DELETE will remove a key from a json file ''' key = format_keyname(key) bot.debug("DELETE %s from %s" %(key,jsonfile)) if not os.path.exists(jsonfile): bot.error("Cannot find %s, exiting." %jsonfile) sys.exit(1) contents = read_json(jsonfile) if key in contents: del contents[key] if len(contents) > 0: write_json(contents,jsonfile) else: bot.debug('%s is empty, deleting.' %jsonfile) os.remove(jsonfile) return True else: bot.debug('Warning, %s not found in %s' %(key,jsonfile)) return False
def ADD(key,value,jsonfile,force=False): '''ADD will write or update a key in a json file ''' key = format_keyname(key) bot.debug("Adding label: '%s' = '%s'" %(key, value)) bot.debug("ADD %s from %s" %(key,jsonfile)) if os.path.exists(jsonfile): contents = read_json(jsonfile) if key in contents: bot.debug('Warning, %s is already set. Overwrite is set to %s' %(key,force)) if force == True: contents[key] = value else: bot.error('%s found in %s and overwrite set to %s.' %(key,jsonfile,force)) sys.exit(1) else: contents[key] = value else: contents = {key:value} bot.debug('%s is %s' %(key,value)) write_json(contents,jsonfile) return value