def testReadWriteFull(self): ctx = UserContext( "Full", ["user1", "user2"], {"Pref1": "Super", "Pref2": "Duper"} ) self.assertEquals("Full", ctx.getName()) self.assertEquals(["Pref1", "Pref2"], ctx.getPrefKeys()) self.assertReadWrite(ctx) #Get self.assertEquals("Super", ctx.getPrefValue("Pref1")) #Get missing self.assertIsNone(ctx.getPrefValue("NOPE")) self.assertEquals("MISSING", ctx.getPrefValue("StillNope", "MISSING")) #Pop missing self.assertIsNone(ctx.popPrefValue("NOPE")) self.assertEquals("MISSING", ctx.popPrefValue("StillNope", "MISSING")) #Pop and then re-pop now missing self.assertEquals("Duper", ctx.popPrefValue("Pref2", "WHAT?")) self.assertIsNone(ctx.popPrefValue("Pref2")) self.assertEquals("MISSING", ctx.popPrefValue("Pref2", "MISSING")) #Invasive state check ctx.setPrefValue("Pref1", "NewSuper") self.assertEquals({"Pref1": "NewSuper"}, ctx._prefs) self.assertEquals(["Pref1"], ctx.getPrefKeys())
def testReadWriteFull(self): ctx = UserContext("Full", ["user1", "user2"], { "Pref1": "Super", "Pref2": "Duper" }) self.assertEquals("Full", ctx.getName()) self.assertEquals(["Pref1", "Pref2"], ctx.getPrefKeys()) self.assertReadWrite(ctx) #Get self.assertEquals("Super", ctx.getPrefValue("Pref1")) #Get missing self.assertIsNone(ctx.getPrefValue("NOPE")) self.assertEquals("MISSING", ctx.getPrefValue("StillNope", "MISSING")) #Pop missing self.assertIsNone(ctx.popPrefValue("NOPE")) self.assertEquals("MISSING", ctx.popPrefValue("StillNope", "MISSING")) #Pop and then re-pop now missing self.assertEquals("Duper", ctx.popPrefValue("Pref2", "WHAT?")) self.assertIsNone(ctx.popPrefValue("Pref2")) self.assertEquals("MISSING", ctx.popPrefValue("Pref2", "MISSING")) #Invasive state check ctx.setPrefValue("Pref1", "NewSuper") self.assertEquals({"Pref1": "NewSuper"}, ctx._prefs) self.assertEquals(["Pref1"], ctx.getPrefKeys())
def echo(self, msg): if msg.getSpeechAct() != "experiment": #Not our hack - just echo back resp = self.makeResponse(msg, None, INFORM_ACT) resp.setVerb("HOLLA BACK") return resp #Special, hacky case: create an experiment context dt = datetime.now().isoformat() #Look by ID and name (we're expecting name) ctx = UserContext.findContext("DemoExperiment") if not ctx: ctx = UserContext(name="DemoExperiment") ctx.setPrefValue("ContextCreated", dt) ctx.setPrefValue("ContextUpdated", dt) #Add all users (and set an experimental context variable for them) for user in UserData.objects(): user.setPrefValue(ctx.getName(), "UserAdded", dt) user.save() ctx.addUser(user.getId()) ctx.save() resp = self.makeResponse(msg, None, INFORM_ACT) resp.setVerb("HOLLA BACK") resp.setSpeechAct(resp.getSpeechAct() + " => Created context " + ctx.getId()) return resp
def testReadWriteNoUsers(self): ctx = UserContext("NoUsers", [], {"Pref1": "Super"}) self.assertEquals("NoUsers", ctx.getName()) self.assertReadWrite(ctx)
def testReadWriteNoPrefs(self): ctx = UserContext("NoPrefs", ["user1"]) self.assertEquals("NoPrefs", ctx.getName()) self.assertReadWrite(ctx)
def testReadWriteSuperBlank(self): ctx = UserContext("SuperBlank") self.assertEquals("SuperBlank", ctx.getName()) self.assertReadWrite(ctx)