source: exist/trunk/python/ndgutilstests/lib/testutilities.py @ 4630

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

Extend and update test suite for ndgUtils code - adding new package
to test lib code + new test data and improved test constants to
centralise test data.

Line 
1'''
2A test class for the Utilities module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest
8import ndgUtils.lib.utilities as utilities
9
10class testUtilities(unittest.TestCase):
11   
12    def setUp(self):
13        '''
14        set up data used in the tests.
15        '''
16
17    def testGetISO8601Date(self):
18        date = utilities.getISO8601Date('2007-02-16 23:14:49.781801+00')
19        self.assertEqual("2007-02-16T23:14:49Z", date)
20
21    def testGetISO8601DateWithT(self):
22        date = utilities.getISO8601Date('2007-02-16T23:14:49.781801+00')
23        self.assertEqual("2007-02-16T23:14:49Z", date)
24
25    def testGetISO8601DateWithZ(self):
26        date = utilities.getISO8601Date('2007-02-16 23:14:49.781801+00Z')
27        self.assertEqual("2007-02-16T23:14:49Z", date)
28       
29    def testFormatDateYYYYMMDD(self):
30        date = utilities.formatDateYYYYMMDD('2007-02-16 23:14:49.781801+00Z')
31        self.assertEqual("2007-02-16", date)
32       
33    def testInvalidFormatDateYYYYMMDD(self):
34        self.assertRaises(ValueError, utilities.formatDateYYYYMMDD, '2dd007-02-16 23:14:49.781801+00Z')
35
36    def testGetTripleData(self):
37        tripleString = "1|2|3"
38        data = utilities.getTripleData(tripleString)
39        self.assertEqual("1", data[0])
40        self.assertEqual("2", data[1])
41        self.assertEqual("3", data[2])
42
43    def testGetTripleDataOneVal(self):
44        tripleString = "1"
45        data = utilities.getTripleData(tripleString)
46        self.assertEqual("1", data[0])
47        self.assertEqual("", data[1])
48        self.assertEqual("", data[2])
49
50    def testGetTripleDataOneValWithDelimiter(self):
51        tripleString = "1|"
52        data = utilities.getTripleData(tripleString)
53        self.assertEqual("1", data[0])
54        self.assertEqual("", data[1])
55        self.assertEqual("", data[2])
56
57    def testGetTripleDataTwoVals(self):
58        tripleString = "1|2"
59        data = utilities.getTripleData(tripleString)
60        self.assertEqual("1", data[0])
61        self.assertEqual("2", data[1])
62        self.assertEqual("", data[2])
63       
64
65    def testGetTripleDataWithSpaces(self):
66        tripleString = " 1  | 2 |  3  "
67        data = utilities.getTripleData(tripleString)
68        self.assertEqual("1", data[0])
69        self.assertEqual("2", data[1])
70        self.assertEqual("3", data[2])
71
72    def testTidyUpParametersRemoveMonth(self):
73        janString = "JANUARY"
74        params = "TEMP01"
75        tidiedParams = utilities.tidyUpParameters(janString + " " + params)
76        self.assertEqual(params, tidiedParams)
77
78    def testTidyUpParametersWithTriple(self):
79        params = "TEMP01 | http://blah.org | TEMP"
80        tidiedParams = utilities.tidyUpParameters(params)
81        self.assertEqual(params, tidiedParams)
82
83    def testTidyUpParametersWithTripleAndThingsToTidy(self):
84        params = "TEMP   01 & | http://blah.org/&fred=t | temp > 6 <  ice  "
85        tidiedParams = utilities.tidyUpParameters(params)
86        self.assertEqual("TEMP 01 &AMP; | http://blah.org/&amp;fred=t | TEMP &GT; 6 &LT; ICE", tidiedParams)
87
88    def testTidyUpParametersRemoveSpaces(self):
89        params = "TEMP          01"
90        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
91        self.assertEqual("TEMP 01", tidiedParams)
92
93    def testTidyUpParametersUpperCase(self):
94        params = "temp01"
95        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
96        self.assertEqual("TEMP01", tidiedParams)
97
98    def testTidyUpParametersRemoveMonthRemoveSpacesUpperCase(self):
99        params = "temp   01   JANUARY"
100        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
101        self.assertEqual("TEMP 01", tidiedParams)
102
103    def testEscapeSpecialCharacters(self):
104        str = ">blah< &"
105        escapedStr = utilities.escapeSpecialCharacters(str)
106        self.assertEqual("&gt;blah&lt; &amp;", escapedStr)
107
108
109    def testNormaliseLongitude(self):
110        w,e=1,359
111        w,e=utilities.normaliseLongitude(w,e)
112        self.assertEqual((w,e),(-179.,179.))
113        w,e=190,200
114        w,e=utilities.normaliseLongitude(w,e)
115        self.assertEqual((w,e),(-170.,-160.))
116        w,e=10,20
117        w,e=utilities.normaliseLongitude(w,e)
118        self.assertEqual((w,e),(10.,20.))
119 
120
121    def testNormaliseLongitude(self):
122        w,e=1,359
123        w,e=normaliseLongitude(w,e)
124        self.assertEqual((w,e),(-179.,179.))
125        w,e=190,200
126        w,e=normaliseLongitude(w,e)
127        self.assertEqual((w,e),(-170.,-160.))
128        w,e=10,20
129        w,e=normaliseLongitude(w,e)
130        self.assertEqual((w,e),(10.,20.))
131       
132if __name__=="__main__":
133    unittest.main()
Note: See TracBrowser for help on using the repository browser.