def testBug8(self): """ Tests the resolution of bug #8 """ badDefaultName = "gremlin" registry = SkoobotRegistry(self.tempPath) with self.subTest("Setting bad default"): oldDefault = registry.getDefaultName() with self.assertRaises(ValueError): registry.setDefault(badDefaultName) self.assertEqual(oldDefault, registry.getDefaultName()) with self.subTest("Loading bad default"): self.registryDict["default"] = badDefaultName with open(self.tempPath, "w") as registryFile: json.dump(self.registryDict, registryFile, indent=4) registry.load() self.assertEqual(None, registry.getDefaultName()) with self.subTest("Loading good default"): self.registryDict["default"] = self.skooName with open(self.tempPath, "w") as registryFile: json.dump(self.registryDict, registryFile, indent=4) registry.load() self.assertEqual(self.skooName, registry.getDefaultName())
def testSave(self): """ Tests for the save() method Make sure that save() works, except when the registry is marked invalid. """ registry = SkoobotRegistry(self.tempPath) altSkooAddr = "aa:aa:aa:aa:aa:aa" altSkooName = "Alt" extraSkooAddr = "ee:ee:ee:ee:ee:ee" extraSkooName = "Extra" with self.subTest("Undo alterations"): registry.addSkoobot(altSkooAddr, altSkooName) registry.setDefault(altSkooAddr) self.assertEqual(4, len(registry.registry)) registry.load() self.assertEqual(3, len(registry.registry)) self.assertEqual(self.skooName, registry.getDefaultName()) with self.subTest("Alter and save"): registry.addSkoobot(altSkooAddr, altSkooName) registry.setDefault(altSkooAddr) self.assertEqual(4, len(registry.registry)) # Save the state with the AltSkootbot entry registry.save() registry.addSkoobot(extraSkooAddr, extraSkooName) registry.setDefault(extraSkooAddr) self.assertEqual(5, len(registry.registry)) self.assertEqual(extraSkooName, registry.getDefaultName()) # Restore to the save() state registry.load() self.assertEqual(4, len(registry.registry)) self.assertEqual(altSkooName, registry.getDefaultName()) with self.subTest("Don't save invalid"): registry.addSkoobot(extraSkooAddr, altSkooName) registry.setDefault(extraSkooAddr) self.assertEqual(5, len(registry.registry)) registry.valid = False # Fail to save the state with the Extra entry registry.save() # Restore to the previous save() state registry.load() self.assertEqual(4, len(registry.registry)) self.assertEqual(altSkooName, registry.getDefaultName())
def testLoad(self): """ Test for loading the registry. Most of this is already tested by the constructor tests, however, we need to check that a reload works and that a failed load sets the valid flag to false """ registry = SkoobotRegistry(self.tempPath) with self.subTest("Empty dict"): emptyDict = {} with open(self.tempPath, "w") as registryFile: json.dump(emptyDict, registryFile) self.assertEqual(3, len(registry.registry)) registry.load() self.assertEqual(0, len(registry.registry)) self.assertEqual(True, registry.valid) self.assertEqual(None, registry.getDefaultName()) with self.subTest("Invalid dict"): with open(self.tempPath, "w") as registryFile: registryFile.write("rubbish") registry.addSkoobot(self.skooAddr) self.assertEqual(True, registry.valid) with self.assertRaises(json.JSONDecodeError): registry.load() self.assertEqual(0, len(registry.registry)) self.assertEqual(False, registry.valid) self.assertEqual(None, registry.getDefaultName()) with self.subTest("Reload good dict"): with open(self.tempPath, "w") as registryFile: json.dump(self.registryDict, registryFile) self.assertEqual(0, len(registry.registry)) registry.load() self.assertEqual(3, len(registry.registry)) self.assertEqual(True, registry.valid) self.assertEqual(self.skooName, registry.getDefaultName())