def test_delitem(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) u = User("quser", "last", "first", "pass", "*****@*****.**") u2 = User("quser2", "last", "first", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): c.delitem(u) with self.assertRaises(InvalidItemTypeError): c.delitem(int) c.save(WIMS_URL, "myself", "toto") c.additem(u) c.additem(u2) self.assertTrue(c.checkitem("quser", User)) c.delitem(u) self.assertFalse(c.checkitem("quser", User)) self.assertTrue(c.checkitem("quser2", User)) c.delitem("quser2", User) self.assertFalse(c.checkitem("quser2", User))
def test_get_exception(self): with self.assertRaises(NotSavedError): User.get(self.clas, "supervisor") self.clas.save(WIMS_URL, "myself", "toto") with self.assertRaises(AdmRawError) as cm: User.get(self.clas, "unknown") self.assertIn("WIMS' server responded with an ERROR:", str(cm.exception))
def test_init_and_properties(self): c = Class.get(WIMS_URL, "myself", "toto", 9001, "myclass") u = User.get(c, "supervisor") self.assertIn("firstname", u.infos) u = User("supervisor", "last", "first", "pass", "*****@*****.**") self.assertEqual(u.fullname, "First Last") with self.assertRaises(NotSavedError): u.infos
def test_eq(self): u1 = User("Test1", "test", "test", "pass", "*****@*****.**") u2 = User("Test2", "test", "test", "pass", "*****@*****.**") u3 = User("Test3", "test", "test", "pass", "*****@*****.**") self.clas.save(WIMS_URL, "myself", "toto") self.clas.additem(u1) self.clas.additem(u2) self.assertEqual(u1, self.clas.getitem(u1.quser, User)) self.assertNotEqual(u2, self.clas.getitem(u1.quser, User)) self.assertNotEqual(u2, 1) with self.assertRaises(NotSavedError): u1 == u3
def setUpClass(cls): """Create an API and an User to use through the tests.""" cls.api = WimsAPI(WIMS_URL, "myself", "toto") cls.user = User("supervisor", "last", "first", "pass", "*****@*****.**") cls.clas = Class("myclass", "A class", "an institution", "*****@*****.**", "password", cls.user, qclass=999999) cls.api.delclass(999999, "myclass")
def test_list(self): u1 = User("Test1", "test", "test", "pass", "*****@*****.**") u2 = User("Test2", "test", "test", "pass", "*****@*****.**") u3 = User("Test3", "test", "test", "pass", "*****@*****.**") self.clas.save(WIMS_URL, "myself", "toto") self.assertListEqual([], User.list(self.clas)) self.clas.additem(u1) self.clas.additem(u2) self.clas.additem(u3) self.assertListEqual( sorted([u1, u2, u3], key=lambda i: i.quser), sorted(User.list(self.clas), key=lambda i: i.quser))
def test_check(self): self.clas.save(WIMS_URL, "myself", "toto") u = User("Test", "test", "test", "pass", "*****@*****.**") c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): User.check(c, u) self.assertFalse(User.check(self.clas, u)) self.clas.additem(u) self.assertTrue(User.check(self.clas, u))
def test_save_and_refresh(self): self.clas.save(WIMS_URL, "myself", "toto") u = User("Test", "test", "test", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): u.refresh() u.save(self.clas) u2 = User.get(self.clas, u.quser) self.assertEqual(u2.firstname, "test") u.firstname = "modified" u.save() self.assertEqual(u.firstname, "modified") self.assertEqual(u2.firstname, "test") u2.refresh() self.assertEqual(u2.firstname, "modified")
def test_save_adapt(self): self.clas.save(WIMS_URL, "myself", "toto") u = User("ap'ostrophe", "test", "test", "pass", "*****@*****.**") u.save(self.clas) self.assertEqual("apostrophe", u.quser) self.assertTrue(User.check(self.clas, "apostrophe")) self.assertFalse(User.check(self.clas, "ap'ostrophe"))
def test___contains__(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) unknown = User("unknown", "last", "first", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): unknown in c c.save(WIMS_URL, "myself", "toto") self.assertTrue(self.user in c) self.assertFalse(unknown in c)
def test_save_exceptions(self): with self.assertRaises(NotSavedError): self.user.save() with self.assertRaises(NotSavedError): self.user.save(self.clas) self.clas.save(WIMS_URL, "myself", "toto") u = User("ap'ostrophe", "test", "test", "pass", "*****@*****.**") with self.assertRaises(InvalidIdentifier): u.save(self.clas, adapt=False) self.assertFalse(User.check(self.clas, "apostrophe")) self.assertFalse(User.check(self.clas, "ap'ostrophe"))
def test_checkitem(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) unknown = User("unknown", "last", "first", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): c.checkitem("supervisor", User) with self.assertRaises(InvalidItemTypeError): c.checkitem(1) c.save(WIMS_URL, "myself", "toto") self.assertTrue(c.checkitem("supervisor", User)) self.assertTrue(c.checkitem(self.user)) self.assertFalse(c.checkitem("Unknown", User)) self.assertFalse(c.checkitem(unknown))
def test_additem(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) u = User("quser", "last", "first", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): c.additem(u) with self.assertRaises(InvalidItemTypeError): c.additem(int) c.save(WIMS_URL, "myself", "toto") c.additem(u) self.assertEqual(u._class.qclass, c.qclass) self.assertEqual(u.wclass, True) u2 = c.getitem("quser", User) self.assertEqual(u2.firstname, u.firstname)
def test_remove(self): self.clas.save(WIMS_URL, "myself", "toto") c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) u = User("Test", "test", "test", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): u.remove(c, u) u.save(self.clas) User.get(self.clas, u.quser) # Ok User.remove(self.clas, u) with self.assertRaises(AdmRawError): User.get(self.clas, u.quser) # Should raise the exception
def test_delete(self): self.clas.save(WIMS_URL, "myself", "toto") u = User("Test", "test", "test", "pass", "*****@*****.**") with self.assertRaises(NotSavedError): u.delete() u.save(self.clas) User.get(self.clas, u.quser) # Ok u.delete() with self.assertRaises(AdmRawError): User.get(self.clas, u.quser) # Should raise the exception
def setUpClass(cls): """Create an API and an User to use through the tests.""" cls.api = WimsAPI(WIMS_URL, "myself", "toto") cls.user = User("supervisor", "last", "first", "pass", "*****@*****.**")