def test_init_and_properties(self): c = Class.get(WIMS_URL, "myself", "toto", 9001, "myclass") self.assertEqual(c.url, WIMS_URL) self.assertEqual(c.ident, "myself") self.assertEqual(c.passwd, "toto") self.assertIn("description", c.infos) c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): c.url with self.assertRaises(NotSavedError): c.ident with self.assertRaises(NotSavedError): c.passwd with self.assertRaises(NotSavedError): c.infos with self.assertRaises(ValueError): Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999, lang="Wrong") with self.assertRaises(ValueError): Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999, level="Wrong") with self.assertRaises(ValueError): Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999, expiration="Wrong")
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___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_without_qclass(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user) with self.assertRaises(NotSavedError): c.save() c.save(WIMS_URL, "myself", "toto") self.assertIsNotNone(c.qclass) c.delete()
def test_scores(self): # Put a dummy class with existing scores if not already added qclass = 6948902 if not Class.check(self.api.url, self.api.ident, self.api.passwd, qclass, "myclass"): archive = os.path.join(os.path.dirname(__file__), "resources/6948902.tgz") command("docker cp %s wims-minimal:/home/wims/log/classes/" % archive) command('docker exec wims-minimal bash -c ' '"tar -xzf /home/wims/log/classes/6948902.tgz -C /home/wims/log/classes/"' ) command( 'docker exec wims-minimal bash -c "chmod 644 /home/wims/log/classes/6948902/.def"' ) command( 'docker exec wims-minimal bash -c ' '"chown wims:wims /home/wims/log/classes/6948902 -R"' ) command('docker exec wims-minimal bash -c "rm /home/wims/log/classes/6948902.tgz"') command( "docker exec wims-minimal bash -c " "\"echo ':6948902,20200626,Institution,test,en,0,H4,dmi,S S,+myself/myclass+,' " '>> /home/wims/log/classes/.index"' ) with self.assertRaises(NotSavedError): Sheet().scores() c = Class.get(self.api.url, self.api.ident, self.api.passwd, qclass, "myclass") s1 = c.getitem(1, Sheet) s2 = c.getitem(2, Sheet) u = c.getitem("qcoumes", User) es1_1 = ExerciseScore(None, u, 3.3, 10, 10, 10, 0, 10, 1, 3) ss1 = SheetScore(s1, u, 7.17, 3.3, 100, 100, 100, 1, [es1_1]) es2_1 = ExerciseScore(None, u, 4.59, 10, 10, 10, 0, 10, 1, 2) es2_2 = ExerciseScore(None, u, 5.41, 10, 10, 10, 10, 10, 1, 2) ss2 = SheetScore(s2, u, 8.12, 5, 100, 100, 100, 1, [es2_1, es2_2]) self.assertEqual(s1.scores("qcoumes"), ss1) self.assertEqual(s1.scores(), [ss1]) self.assertEqual(s2.scores("qcoumes"), ss2) self.assertEqual(s2.scores(), [ss2]) with self.assertRaises(ValueError): s1.scores("unknown")
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_getitem(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): c.getitem("supervisor", User) with self.assertRaises(InvalidItemTypeError): c.getitem(1, int) c.save(WIMS_URL, "myself", "toto") user = c.getitem("supervisor", User) self.assertEqual(user.firstname, self.user.firstname)
def test_check(self): self.clas.save(WIMS_URL, "myself", "toto") s = Sheet("Title", "Description") c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): Sheet.check(c, s) self.assertFalse(Sheet.check(self.clas, s)) self.clas.additem(s) self.assertTrue(Sheet.check(self.clas, s))
def test_remove(self): self.clas.save(WIMS_URL, "myself", "toto") c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) s = Sheet("Title", "Description") with self.assertRaises(NotSavedError): s.remove(c, s) s.save(self.clas) Sheet.get(self.clas, s.qsheet) # Ok Sheet.remove(self.clas, s) with self.assertRaises(AdmRawError): Sheet.get(self.clas, s.qsheet) # Should raise the exception
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_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_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_save_and_refresh(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): c.save() with self.assertRaises(NotSavedError): c.refresh() c.save(WIMS_URL, "myself", "toto") c = Class.get(WIMS_URL, "myself", "toto", 999999, "myclass") c2 = Class.get(WIMS_URL, "myself", "toto", 999999, "myclass") self.assertEqual(c.institution, "an institution") self.assertEqual(c2.institution, "an institution") c.institution = "modified" c.save() self.assertEqual(c.institution, "modified") self.assertEqual(c2.institution, "an institution") c2.refresh() self.assertEqual(c2.institution, "modified") self.api.delclass(99999999, "myclass")
def test_append_slash(self): url = WIMS_URL if not WIMS_URL.endswith('/') else WIMS_URL[:-1] c = Class.get(url, "myself", "toto", 9001, "myclass") self.assertEqual(c.url, url + "/")
def test_listitem(self): c = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): c.listitem(Sheet) with self.assertRaises(InvalidItemTypeError): c.listitem(int) s1 = Sheet("First", "First one") s2 = Sheet("Second", "Second one") s3 = Sheet("Third", "Third one") c.save(WIMS_URL, "myself", "toto") self.assertListEqual([], c.listitem(Sheet)) c.additem(s1) c.additem(s2) c.additem(s3) self.assertListEqual(sorted([s1, s2, s3], key=lambda i: i.qsheet), sorted(c.listitem(Sheet), key=lambda i: i.qsheet)) c.delete()
def test_eq(self): c1 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) c2 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) c3 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) with self.assertRaises(NotSavedError): c1 == c3 c1.save(WIMS_URL, "myself", "toto") c2.save(WIMS_URL, "myself", "toto") c3.save(WIMS_URL, "myself", "toto") self.assertEqual( c1, Class.get(WIMS_URL, "myself", "toto", c1.qclass, c1.rclass)) self.assertNotEqual( c2, Class.get(WIMS_URL, "myself", "toto", c1.qclass, c1.rclass)) self.assertNotEqual(c2, 1) c1.delete() c2.delete() c3.delete()
def test_list(self): c1 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) c2 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) c3 = Class("rclass", "A class", "an institution", "*****@*****.**", "password", self.user) c1.save(WIMS_URL, "myself", "toto") c2.save(WIMS_URL, "myself", "toto") c3.save(WIMS_URL, "myself", "toto") self.assertListEqual( sorted([c1, c2, c3], key=lambda i: i.qclass), sorted(Class.list(WIMS_URL, "myself", "toto", "rclass"), key=lambda i: i.qclass)) self.assertListEqual([], Class.list(WIMS_URL, "myself", "toto", "unknown_rclass")) c1.delete() c2.delete() c3.delete()
def test_delete(self): c = Class("myclass", "A class", "an institution", "*****@*****.**", "password", self.user, qclass=999999) with self.assertRaises(NotSavedError): c.delete() c.save(WIMS_URL, "myself", "toto") Class.get(WIMS_URL, "myself", "toto", c.qclass, c.rclass) # Ok c.delete() with self.assertRaises(AdmRawError): Class.get(WIMS_URL, "myself", "toto", c.qclass, c.rclass) # Should raise the exception
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_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)