def test_from_invalid_json(self): # Invalid JSON with capture_output(): jd = JSONObjectDict.from_json(JSONObject, '{"foo":{}') self.failIf(jd) # Valid but unexpected Command field self.failIf(JSONObjectDict.from_json(JSONObject, '{"bar":{"name":"bar", "invalid":"foo"}'))
def test_save_all(self): data = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR) fd, filename = mkstemp(suffix=".json") os.close(fd) try: ret = data.save(filename) with open(filename, "rb") as f: jstr = f.read() # Check we also return the string as documented... self.failUnlessEqual(jstr, ret) finally: os.unlink(filename) # Check we have the right number of items self.failUnlessEqual(len(json.loads(jstr)), len(data)) # Check them one by one (for convenience of debugging) parsed = JSONObjectDict.from_json(self.WibbleData, jstr) for o in data.values(): self.failUnlessEqual(parsed[o.name], o)
def _get_saved_commands(cls): filename = cls.COMS_FILE print_d("Loading saved commands from '%s'..." % filename) coms = None try: with open(filename, "r", encoding="utf-8") as f: coms = JSONObjectDict.from_json(Command, f.read()) except (IOError, ValueError) as e: print_w("Couldn't parse saved commands (%s)" % e) # Failing all else... if not coms: print_d("No commands found in %s. Using defaults." % filename) coms = {c.name: c for c in cls.DEFAULT_COMS} print_d("Loaded commands: %s" % coms.keys()) return coms
def _get_saved_searches(cls): filename = cls.COMS_FILE print_d("Loading saved commands from '%s'..." % filename) coms = None try: with open(filename) as f: coms = JSONObjectDict.from_json(Command, f.read()) except IOError: pass except ValueError as e: print_w("Couldn't parse saved commands (%s)" % e) # Failing all else... if not coms: print_d("No commands found in %s. Using defaults." % filename) coms = dict([(c.name, c) for c in cls.DEFAULT_COMS]) print_d("Loaded commands: %s" % coms.keys()) return coms
def test_subclass_from_json(self): coms = JSONObjectDict.from_json(self.WibbleData, self.WIBBLE_JSON_STR) self.failUnlessEqual(len(coms), 2) self.failUnlessEqual(coms['foo'].__class__, self.WibbleData)