Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import matplotlib 

2matplotlib.use("Agg") 

3import os 

4import unittest 

5import lsst.sims.maf.db as db 

6import lsst.utils.tests 

7from lsst.utils import getPackageDir 

8from lsst.sims.utils.CodeUtilities import sims_clean_up 

9from builtins import str 

10 

11 

12class TestOpsimDb(unittest.TestCase): 

13 """Test opsim specific database class.""" 

14 

15 @classmethod 

16 def tearDownClass(cls): 

17 sims_clean_up() 

18 

19 def setUp(self): 

20 self.database = os.path.join(getPackageDir('sims_data'), 'OpSimData', 

21 'astro-lsst-01_2014.db') 

22 self.oo = db.OpsimDatabaseV4(database=self.database) 

23 

24 def tearDown(self): 

25 del self.oo 

26 del self.database 

27 self.oo = None 

28 

29 def testOpsimDbSetup(self): 

30 """Test opsim specific database class setup/instantiation.""" 

31 # Test tables were connected to. 

32 self.assertIn('SummaryAllProps', self.oo.tableNames) 

33 self.assertEqual(self.oo.defaultTable, 'SummaryAllProps') 

34 

35 def testOpsimDbMetricData(self): 

36 """Test queries for sim data. """ 

37 data = self.oo.fetchMetricData(['seeingFwhmEff', ], 'filter="r" and seeingFwhmEff<1.0') 

38 self.assertEqual(data.dtype.names, ('seeingFwhmEff',)) 

39 self.assertLessEqual(data['seeingFwhmEff'].max(), 1.0) 

40 

41 def testOpsimDbPropID(self): 

42 """Test queries for prop ID""" 

43 propids, propTags = self.oo.fetchPropInfo() 

44 self.assertGreater(len(list(propids.keys())), 0) 

45 self.assertGreater(len(propTags['WFD']), 0) 

46 self.assertGreaterEqual(len(propTags['DD']), 0) 

47 for w in propTags['WFD']: 

48 self.assertIn(w, propids) 

49 for d in propTags['DD']: 

50 self.assertIn(d, propids) 

51 

52 def testOpsimDbFields(self): 

53 """Test queries for field data.""" 

54 # Fetch field data for all fields. 

55 dataAll = self.oo.fetchFieldsFromFieldTable() 

56 self.assertEqual(dataAll.dtype.names, ('fieldId', 'fieldRA', 'fieldDec')) 

57 # Fetch field data for all fields requested by a particular propid. 

58 # Need to reinstate this capability. 

59 

60 def testOpsimDbRunLength(self): 

61 """Test query for length of opsim run.""" 

62 nrun = self.oo.fetchRunLength() 

63 self.assertEqual(nrun, 0.04) 

64 

65 def testOpsimDbSimName(self): 

66 """Test query for opsim name.""" 

67 simname = self.oo.fetchOpsimRunName() 

68 self.assertIsInstance(simname, str) 

69 self.assertEqual(simname, 'astro-lsst-01_2014') 

70 

71 def testOpsimDbConfig(self): 

72 """Test generation of config data. """ 

73 configsummary, configdetails = self.oo.fetchConfig() 

74 self.assertIsInstance(configsummary, dict) 

75 self.assertIsInstance(configdetails, dict) 

76 # self.assertEqual(set(configsummary.keys()), set(['Version', 'RunInfo', 'Proposals', 'keyorder'])) 

77 propids, proptags = self.oo.fetchPropInfo() 

78 propidsSummary = [] 

79 for propname in configsummary['Proposals']: 

80 if propname != 'keyorder': 

81 propidsSummary.append(configsummary['Proposals'][propname]['PropId']) 

82 self.assertEqual(set(propidsSummary), set(propids)) 

83 

84 def testCreateSqlWhere(self): 

85 """ 

86 Test that the createSQLWhere method handles expected cases. 

87 """ 

88 # propTags is a dictionary of lists returned by OpsimDatabase 

89 propTags = {'WFD': [1, 2, 3], 'DD': [4], 'Rolling': [2]} 

90 # If tag is in dictionary with one value, returned sql where clause 

91 # is simply 'propId = 4' 

92 tag = 'DD' 

93 sqlWhere = self.oo.createSQLWhere(tag, propTags) 

94 self.assertEqual(sqlWhere, 'proposalId = 4') 

95 # if multiple proposals with the same tag, all should be in list. 

96 tag = 'WFD' 

97 sqlWhere = self.oo.createSQLWhere(tag, propTags) 

98 self.assertEqual(sqlWhere.split()[0], '(proposalId') 

99 for id_val in propTags['WFD']: 

100 self.assertIn('%s' % (id_val), sqlWhere) 

101 # And the same id can be in multiple proposals. 

102 tag = 'Rolling' 

103 sqlWhere = self.oo.createSQLWhere(tag, propTags) 

104 self.assertEqual(sqlWhere, 'proposalId = 2') 

105 # And tags not in propTags are handled. 

106 badprop = 'proposalId like "NO PROP"' 

107 tag = 'nogo' 

108 sqlWhere = self.oo.createSQLWhere(tag, propTags) 

109 self.assertEqual(sqlWhere, badprop) 

110 # And tags which identify no proposal ID are handled. 

111 propTags['Rolling'] = [] 

112 sqlWhere = self.oo.createSQLWhere(tag, propTags) 

113 self.assertEqual(sqlWhere, badprop) 

114 

115 

116class TestMemory(lsst.utils.tests.MemoryTestCase): 

117 pass 

118 

119 

120def setup_module(module): 

121 lsst.utils.tests.init() 

122 

123 

124if __name__ == "__main__": 124 ↛ 125line 124 didn't jump to line 125, because the condition on line 124 was never true

125 lsst.utils.tests.init() 

126 unittest.main()