def makeJSONFile(self, content=None, load=True): """ Create a JSONFile. """ json_file = JSONFile(file=StringIO(content)) if load: json_file.load() return json_file
def test_load_file_empty(self): """ The parsed data will be available for read/write if it is valid. """ json_file = JSONFile(file=StringIO('')) json_file.load() self.assertEqual(0, len(json_file.data))
def test_load_file_good_format(self): """ The parsed data will be available for read/write if it is valid. """ string_value = manufacture.getUniqueString() content = '{ "some-good": 1, "utf8": "%s"}' % (string_value) json_file = JSONFile(file=StringIO(content)) json_file.load() self.assertNotEqual({}, json_file.data) self.assertContains(u'some-good', json_file.data) self.assertEqual(1, json_file.data['some-good']) self.assertEqual(string_value, json_file.data['utf8'])
def test_load_file_bad_format(self): """ An UtilsError is raised for a bad formated JSON file. """ content = '{ some-bad: "JSON"}' json_file = JSONFile(file=StringIO(content)) with self.assertRaises(UtilsError) as context: json_file.load() self.assertExceptionID(u'1028', context.exception) self.assertExceptionData( { u'path': None, u'details': self.Contains(u'Expecting property'), }, context.exception, )
def test_load_file_io_error(self): """ An UtilsError is raised for any IO/OS Error. """ path = manufacture.makeFilename() json_file = JSONFile(path=path) with self.assertRaises(UtilsError) as context: json_file.load() self.assertExceptionID(u'1027', context.exception) self.assertExceptionData( { u'path': path, u'details': self.Contains(u'No such file'), }, context.exception, )