NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/sodar/trunk/sodar/tests/suite.py

Revision 301 (checked in by cbc, 14 years ago)

Fix for Windows .mnd files binary copied to Unix.

Line 
1 """
2 Test suite for the sodar package.
3 """
4
5 __author__ = 'Chris Calloway'
6 __email__ = 'cbc@chriscalloway.org'
7 __copyright__ = 'Copyright 2009 UNC-CH Department of Marine Science'
8 __license__ = 'GPL2'
9
10 import os
11 import unittest
12 import doctest
13
14 def setUpData(data_dir,data_file):
15     """
16     Get data from test file.
17     
18     setUpData(data_dir,data_file) -> (data,data_path,)
19     
20     Where:
21     
22         data_dir is a str representation of a relative path in tests/data,
23         
24         data_file is a str representation of a data file within data_dir,
25         
26         data_path is a str representation of the absolute path of data_file,
27         
28         data is a str representation of the contents of data_file.
29     """
30    
31     module_dir = os.path.abspath(os.path.dirname(__file__))
32     data_path = os.path.join(module_dir,'data',data_dir,data_file)
33     data = open(data_path).read()
34     return (data,data_path,)
35
36 def setUpGoodMndData():
37     """
38     Get data from a known good Scintec .mnd file.
39     
40     setUpGoodMndData() -> (mnd,path,profile,variables,observation,)
41
42     Where:
43     
44         mnd is a str representation of a known good .mnd daily sodar file,
45         
46         mnd_path is a str representation of the absolute path to the file,
47         
48         profile is a list of str objects representing a known good profile
49         from the file,
50         
51         variables is a list of str objects representing a list of known good
52         variables from the file,
53         
54         observation is a list of str objects representing a list of known good
55         observation values from the file in the same order as variables.
56     """
57    
58     mnd_dir = os.path.join('scintec','good',)
59     mnd_file = '091117.mnd'
60     mnd,mnd_path = setUpData(mnd_dir,mnd_file)
61     EOL = '\r\n' # Windows file binary copied to Unix
62     def divide(mnd,eol):
63         blocks = [block.strip()
64                   for block
65                   in mnd.split(eol*2)
66                   if block.strip()]
67         if len(blocks) < 2:
68             eol = '\n' # Windows to Windows or Unix to Unix
69             blocks, eol = divide(mnd,eol)
70         return (blocks,eol)
71     blocks,EOL = divide(mnd,EOL)
72     profile = blocks[2].split(EOL)
73     profile = [line.strip() for line in profile if line]
74     variables = profile[1].split()[1:]
75     observation = profile[2].split()
76     return (mnd,mnd_path,profile,variables,observation,)
77
78 def setUpGoodMnd(test):
79     """
80     Doctest setUp() for a good Scintec .mnd file.
81     """
82    
83     mnd,mnd_path,profile,variables,observation = setUpGoodMndData()
84     test.globs.update(good_mnd=mnd,
85                       good_path=mnd_path,
86                       good_profile=profile,
87                       good_variables=variables,
88                       good_observation=observation)
89
90 def test_suite():
91     """
92     Return unittest.TestSuite for setup.py test.
93     """
94    
95     suite = []
96     suite.append(doctest.DocTestSuite(module='sodar.scintec.maindata',
97                                       setUp=setUpGoodMnd,
98                                       optionflags=
99                                           doctest.NORMALIZE_WHITESPACE))
100     return unittest.TestSuite(suite)
Note: See TracBrowser for help on using the browser.