def test_importInvalidXML(self): vlines = validXML.splitlines() vlines[6:6] = ["<unspecifiedElement>bla</unspecifiedElement>"] invalidXML = "\n".join(vlines) self.assertRaisesRegexp(XMLSyntaxError, "not expected", lambda : Exercise.fromXMLString(invalidXML.encode('utf-8'))) del vlines[6] vlines[7:7] = ['<tex_exercise lang="DE">double german</tex_exercise>'] invalidXML2 = "\n".join(vlines) self.assertRaisesRegexp(XMLSyntaxError, "Duplicate key-sequence", lambda : Exercise.fromXMLString(invalidXML2.encode('utf-8')))
def test_importInvalidXML(self): vlines = validXML.splitlines() vlines[6:6] = ["<unspecifiedElement>bla</unspecifiedElement>"] invalidXML = "\n".join(vlines) self.assertRaisesRegexp( XMLSyntaxError, "not expected", lambda: Exercise.fromXMLString(invalidXML.encode('utf-8'))) del vlines[6] vlines[7:7] = ['<tex_exercise lang="DE">double german</tex_exercise>'] invalidXML2 = "\n".join(vlines) self.assertRaisesRegexp( XMLSyntaxError, "Duplicate key-sequence", lambda: Exercise.fromXMLString(invalidXML2.encode('utf-8')))
def test_importValidXML(self): exercise = Exercise.fromXMLFile(dataPath("jemand1.xml")) self.assertEqual(exercise.schemaversion, 2) self.assertEqual(len(exercise.tags), 2) self.assertIn('aabc', exercise.tags) self.assertIsInstance(exercise.modified, datetime.datetime) self.assertIn('DE', exercise.tex_exercise) self.assertIn('DE', exercise.tex_solution) self.assertIn('EN', exercise.tex_solution) self.assertNotIn('EN', exercise.tex_exercise) self.assertIsInstance(exercise.number, int)
def test_jsonExport(self): exercise = Exercise.fromXMLString(validXML.encode("utf-8")) jsonEx = exercise.toJSON() jsonDecoded = json.loads(jsonEx) for attrib, value in exercise.items(): if isinstance(value, datetime.datetime): self.assertEqual(datetime.datetime.strptime(jsonDecoded[attrib], Exercise.DATEFMT), value) else: self.assertEqual(jsonDecoded[attrib], value) for attrib in jsonDecoded: assert attrib in exercise
def test_jsonExport(self): exercise = Exercise.fromXMLString(validXML.encode("utf-8")) jsonEx = exercise.toJSON() jsonDecoded = json.loads(jsonEx) for attrib, value in exercise.items(): if isinstance(value, datetime.datetime): self.assertEqual( datetime.datetime.strptime(jsonDecoded[attrib], Exercise.DATEFMT), value) else: self.assertEqual(jsonDecoded[attrib], value) for attrib in jsonDecoded: assert attrib in exercise
def testRepositoryOperations(self): """Adds and removes exercises and checks the history.""" numExercises = len(glob.glob(join(exdb.repo.repoPath(), "exercises", "*", "*.xml" ))) self.assertEqual(len(exdb.sql.exercises()), numExercises) exes = exdb.sql.exercises() self.assertEqual(set(ex.creator for ex in exes), set(("jensmander", "foobar"))) self.assertTrue(all(ex.number == 1 for ex in exes)) exercise = Exercise.fromXMLFile(dataPath("jemand1.xml")) exercise.number = None exdb.addExercise(exercise, {}) self.assertEqual(exercise.number, 1) self.assertEqual(len(exdb.sql.exercises()), numExercises+1) exdb.removeExercise("jensmander", 1) self.assertEqual(len(exdb.sql.exercises()), numExercises) exes = exdb.sql.exercises() self.assertNotIn("jensmander", (ex.creator for ex in exes)) history = exdb.repo.history() self.assertEqual(history[0]["author"], "jensmander") self.assertEqual(history[0]["action"], "REMOVE") self.assertEqual(history[1]["action"], "ADD")
def test_xmlExport(self): self.maxDiff = None self.assertEqual(Exercise.fromXMLString(validXML.encode('utf-8')).toXML(), validXML)
def test_importTooRecentSchema(self): invalidXML = validXML.replace('schemaversion="2"', 'schemaversion="1000"') self.assertRaises(VersionMismatchError, lambda : Exercise.fromXMLString(invalidXML.encode('utf-8')))
def test_MissingExerciseTex(self): import lxml self.assertRaises(lxml.etree.XMLSyntaxError, lambda : Exercise.fromXMLFile(dataPath("invalid14.xml")))
def test_readPlainXML(self): self.assertTrue(Exercise.fromXMLFile(dataPath("noschemaandencoding.xml")))
def test_readPlainXML(self): self.assertTrue( Exercise.fromXMLFile(dataPath("noschemaandencoding.xml")))
def test_importTooRecentSchema(self): invalidXML = validXML.replace('schemaversion="2"', 'schemaversion="1000"') self.assertRaises( VersionMismatchError, lambda: Exercise.fromXMLString(invalidXML.encode('utf-8')))
def loadFromXML(creator, number): from exdb.exercise import Exercise return Exercise.fromXMLFile(xmlPath(creator=creator, number=number))
def test_xmlExport(self): self.maxDiff = None self.assertEqual( Exercise.fromXMLString(validXML.encode('utf-8')).toXML(), validXML)
def test_MissingExerciseTex(self): import lxml self.assertRaises( lxml.etree.XMLSyntaxError, lambda: Exercise.fromXMLFile(dataPath("invalid14.xml")))