source: exist/trunk/python/ndgutilstests/lib/testatomvalidator.py @ 4641

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgutilstests/lib/testatomvalidator.py@4641
Revision 4641, 6.5 KB checked in by cbyrom, 12 years ago (diff)

Improve test suite, allowing referenced artefacts to be properly set
up and removed before and after tests + set global logging + adjust
file references + add required data files + simplify and fix some tests.

Line 
1'''
2A test class for the Utilities module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest
8from ndgUtils.lib.atomvalidator import AtomValidator as v, ValidationError
9import ndgutilstests.testconstants as tc
10from ndgUtils.models.Atom import Atom
11from ndgUtils.vocabtermdata import VocabTermData as VTD
12
13class testAtomValidator(unittest.TestCase):
14   
15    def setUp(self):
16        '''
17        set up data used in the tests.
18        '''
19        self.atom = tc.createAtom(tc.xmlString)
20        self.v = v(self.atom, tc.DBCONFIG_FILE)
21
22    def __setInvalidAtom(self):
23        self.atom = tc.createAtom(tc.invalidXmlString)
24        self.v.setAtom(self.atom)
25       
26    def testSetNoneAtom(self):
27        self.assertRaises(ValueError, self.v.setAtom, None)
28       
29    def testSetDodgyAtom(self):
30        self.assertRaises(ValueError, self.v.setAtom, [])
31       
32    def testSetAtom(self):
33        try:
34            self.v.setAtom(self.atom)
35        except:
36            self.fail("An exception shouldn't have been thrown")
37           
38    def testValidateAtom(self):
39        try:
40            errors = self.v.validateAtom()
41        except Exception, e:
42            self.fail("An exception shouldn't have been thrown")
43
44
45    def testValidateLinks(self):
46        try:
47            self.v._AtomValidator__validateLinks()
48            self.assertNotEquals(None, self.v.errors)
49            self.assertTrue(isinstance(self.v.errors, dict))
50            if self.v.errors:
51                self.fail("No links errors should have been detected")
52        except Exception, e:
53            self.fail("An exception shouldn't have been thrown")
54
55
56    def testValidateVocabData(self):
57        try:
58            self.v._AtomValidator__validateVocabData()
59            self.assertNotEquals(None, self.v.errors)
60            self.assertTrue(isinstance(self.v.errors, dict))
61            if self.v.errors:
62                self.fail("No vocab data errors should have been detected")
63        except Exception, e:
64            self.fail("An exception shouldn't have been thrown")
65
66
67    def testValidateSchemaCompliance(self):
68        try:
69            self.v._AtomValidator__validateSchemaCompliance()
70            self.assertNotEquals(None, self.v.errors)
71            self.assertTrue(isinstance(self.v.errors, dict))
72            if self.v.errors:
73                self.fail("No schema validation errors should have been detected")
74        except Exception, e:
75            self.fail("An exception shouldn't have been thrown")
76
77
78    def testValidateAtomContent(self):
79        try:
80            self.v._AtomValidator__validateAtomContent()
81            self.assertNotEquals(None, self.v.errors)
82            self.assertTrue(isinstance(self.v.errors, dict))
83            if self.v.errors:
84                self.fail("No atom content errors should have been detected")
85        except Exception, e:
86            self.fail("An exception shouldn't have been thrown")
87
88
89    def testValidateUnicode(self):
90        try:
91            self.v._AtomValidator__validateUnicode()
92            self.assertNotEquals(None, self.v.errors)
93            self.assertTrue(isinstance(self.v.errors, dict))
94            if self.v.errors:
95                self.fail("No unicode errors should have been detected")
96        except Exception, e:
97            self.fail("An exception shouldn't have been thrown")
98           
99    def testInvalidValidateAtom(self):
100        self.__setInvalidAtom()
101        try:
102            errors = self.v.validateAtom()
103            self.fail("An exception should have been thrown")
104        except ValidationError, e:
105            self.assertEqual(ValidationError.BASE_MESSAGE, e.message)
106            self.assertNotEqual(None, e.unpack_errors())
107            self.assertTrue(isinstance(self.v.errors, dict))
108            if not self.v.errors:
109                self.fail("Validation errors should have been detected")
110        except Exception, e:
111            self.fail("A validation exception should have been thrown")
112
113
114    def testInvalidValidateLinks(self):
115        self.__setInvalidAtom()
116        try:
117            self.v._AtomValidator__validateLinks()
118            self.assertNotEquals(None, self.v.errors)
119            self.assertTrue(isinstance(self.v.errors, dict))
120            if not self.v.errors:
121                self.fail("Invalid links errors should have been detected")
122        except Exception, e:
123            self.fail("A validation exception should have been thrown")
124
125
126    def testInvalidValidateVocabData(self):
127        self.__setInvalidAtom()
128        try:
129            self.v._AtomValidator__validateVocabData()
130            self.assertNotEquals(None, self.v.errors)
131            self.assertTrue(isinstance(self.v.errors, dict))
132            if not self.v.errors:
133                self.fail("Vocab errors should have been detected")
134        except Exception, e:
135            self.fail("A validation exception should have been thrown")
136
137
138    def testInvalidValidateSchemaCompliance(self):
139        self.__setInvalidAtom()
140        try:
141            self.v._AtomValidator__validateSchemaCompliance()
142            self.assertNotEquals(None, self.v.errors)
143            self.assertTrue(isinstance(self.v.errors, dict))
144            if not self.v.errors:
145                self.fail("Schema validation errors should have been detected")
146        except Exception, e:
147            self.fail("A validation exception should have been thrown")
148
149
150    def testInvalidValidateAtomContent(self):
151        self.__setInvalidAtom()
152        try:
153            self.v._AtomValidator__validateAtomContent()
154            self.assertNotEquals(None, self.v.errors)
155            self.assertTrue(isinstance(self.v.errors, dict))
156            if not self.v.errors:
157                self.fail("Atom content errors should have been detected")
158        except Exception, e:
159            self.fail("A validation exception should have been thrown")
160
161
162    def testInvalidValidateUnicode(self):
163        self.__setInvalidAtom()
164        self.v._atom.title = tc.INVALID_TITLE
165        try:
166            self.v._AtomValidator__validateUnicode()
167            self.assertNotEquals(None, self.v.errors)
168            self.assertTrue(isinstance(self.v.errors, dict))
169            if not self.v.errors:
170                self.fail("Unicode errors should have been detected")
171            self.assertTrue(self.v.errors['title'].startswith(self.v.ILLEGAL_UNICODE_MESSAGE))
172        except Exception, e:
173            self.fail("A validation exception should have been thrown")
174       
175       
176if __name__ == '__main__':
177
178    unittest.main()
Note: See TracBrowser for help on using the repository browser.