source: ndgCommon/trunk/ndg/common/unittests/lib/testutilities.py @ 6037

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/unittests/lib/testutilities.py@6037
Revision 6037, 5.7 KB checked in by aharwood, 10 years ago (diff)

Port number can now be specified in exist configuration file

Line 
1'''
2A test class for the Utilities module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest
8import ndg.common.src.lib.utilities as utilities
9import ndg.common.unittests.testconstants as tc
10
11class testUtilities(unittest.TestCase):
12   
13    VALID_URL = 'http://bbc.co.uk'
14    INVALID_URL = 'http://bbc.co.ukblah'
15   
16    def setUp(self):
17        '''
18        set up data used in the tests.
19        '''
20
21       
22    def testLoadConfigDetailsValid(self):
23        id, pw = utilities.loadConfigDetails(tc.EXIST_DBCONFIG_FILE, dbName = tc.EXIST_DB)[0:2]
24        self.assertTrue(id)
25        self.assertTrue(pw)
26
27       
28    def testLoadConfigDetailsValidAllOutput(self):
29        id, pw, host, port = utilities.loadConfigDetails(tc.EXIST_DBCONFIG_FILE, dbName = tc.EXIST_DB)
30        self.assertTrue(id)
31        self.assertTrue(pw)
32        self.assertTrue(host)
33       
34    def testLoadConfigDetailsInvalid(self):
35        self.assertRaises(ValueError, utilities.loadConfigDetails, "blah" + tc.EXIST_DBCONFIG_FILE)
36
37    def testOpenURLWithDefaultProxy(self):
38        pageData = utilities.openURLWithDefaultProxy(self.VALID_URL)
39        self.assertNotEqual(None, pageData)
40
41    def testInvalidOpenURLWithDefaultProxy(self):
42        pageData = utilities.openURLWithDefaultProxy(self.INVALID_URL)
43        self.assertTrue(pageData.find('The requested URL could not be retrieved') > -1)
44
45    def testSimpleURLCheck(self):
46        self.assertEquals(1, utilities.simpleURLCheck(self.VALID_URL))
47
48    def testInvalidSimpleURLCheck(self):
49        self.assertEquals(0, utilities.simpleURLCheck(self.INVALID_URL))
50
51    def testGetISO8601Date(self):
52        date = utilities.getISO8601Date('2007-02-16 23:14:49.781801+00')
53        self.assertEqual("2007-02-16T23:14:49Z", date)
54
55    def testGetISO8601DateWithT(self):
56        date = utilities.getISO8601Date('2007-02-16T23:14:49.781801+00')
57        self.assertEqual("2007-02-16T23:14:49Z", date)
58
59    def testGetISO8601DateWithZ(self):
60        date = utilities.getISO8601Date('2007-02-16 23:14:49.781801+00Z')
61        self.assertEqual("2007-02-16T23:14:49Z", date)
62       
63    def testFormatDateYYYYMMDD(self):
64        date = utilities.formatDateYYYYMMDD('2007-02-16 23:14:49.781801+00Z')
65        self.assertEqual("2007-02-16", date)
66       
67    def testInvalidFormatDateYYYYMMDD(self):
68        self.assertRaises(ValueError, utilities.formatDateYYYYMMDD, '2dd007-02-16 23:14:49.781801+00Z')
69
70    def testGetTripleData(self):
71        tripleString = "1|2|3"
72        data = utilities.getTripleData(tripleString)
73        self.assertEqual("1", data[0])
74        self.assertEqual("2", data[1])
75        self.assertEqual("3", data[2])
76
77    def testGetTripleDataOneVal(self):
78        tripleString = "1"
79        data = utilities.getTripleData(tripleString)
80        self.assertEqual("1", data[0])
81        self.assertEqual("", data[1])
82        self.assertEqual("", data[2])
83
84    def testGetTripleDataOneValWithDelimiter(self):
85        tripleString = "1|"
86        data = utilities.getTripleData(tripleString)
87        self.assertEqual("1", data[0])
88        self.assertEqual("", data[1])
89        self.assertEqual("", data[2])
90
91    def testGetTripleDataTwoVals(self):
92        tripleString = "1|2"
93        data = utilities.getTripleData(tripleString)
94        self.assertEqual("1", data[0])
95        self.assertEqual("2", data[1])
96        self.assertEqual("", data[2])
97       
98
99    def testGetTripleDataWithSpaces(self):
100        tripleString = " 1  | 2 |  3  "
101        data = utilities.getTripleData(tripleString)
102        self.assertEqual("1", data[0])
103        self.assertEqual("2", data[1])
104        self.assertEqual("3", data[2])
105
106    def testTidyUpParametersRemoveMonth(self):
107        janString = "JANUARY"
108        params = "TEMP01"
109        tidiedParams = utilities.tidyUpParameters(janString + " " + params)
110        self.assertEqual(params, tidiedParams)
111
112    def testTidyUpParametersWithTriple(self):
113        params = "TEMP01 | http://blah.org | TEMP"
114        tidiedParams = utilities.tidyUpParameters(params)
115        self.assertEqual(params, tidiedParams)
116
117    def testTidyUpParametersWithTripleAndThingsToTidy(self):
118        params = "TEMP   01 & | http://blah.org/&fred=t | temp > 6 <  ice  "
119        tidiedParams = utilities.tidyUpParameters(params)
120        self.assertEqual("TEMP 01 & | http://blah.org/&fred=t | TEMP > 6 < ICE", tidiedParams)
121
122    def testTidyUpParametersRemoveSpaces(self):
123        params = "TEMP          01"
124        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
125        self.assertEqual("TEMP 01", tidiedParams)
126
127    def testTidyUpParametersUpperCase(self):
128        params = "temp01"
129        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
130        self.assertEqual("TEMP01", tidiedParams)
131
132    def testTidyUpParametersRemoveMonthRemoveSpacesUpperCase(self):
133        params = "temp   01   JANUARY"
134        tidiedParams = utilities.tidyUpParameters(" " + params + "    ")
135        self.assertEqual("TEMP 01", tidiedParams)
136
137    def testEscapeSpecialCharacters(self):
138        str = ">blah< &"
139        escapedStr = utilities.escapeSpecialCharacters(str)
140#        self.assertEqual("&gt;blah&lt; &amp;", escapedStr)
141        self.assertEqual(str, escapedStr)
142
143
144    def testNormaliseLongitude(self):
145        w,e=1,359
146        w,e=utilities.normaliseLongitude(w,e)
147        self.assertEqual((w,e),(-179.,179.))
148        w,e=190,200
149        w,e=utilities.normaliseLongitude(w,e)
150        self.assertEqual((w,e),(-170.,-160.))
151        w,e=10,20
152        w,e=utilities.normaliseLongitude(w,e)
153        self.assertEqual((w,e),(10.,20.))
154 
155if __name__=="__main__":
156    unittest.main()
Note: See TracBrowser for help on using the repository browser.