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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

# This file is part of dax_ppdb. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

"""Unit test for PpdbSchema class. 

""" 

 

import os 

import unittest 

 

import lsst.afw.table as afwTable 

from lsst.dax.ppdb import PpdbSchema, make_minimal_dia_object_schema, make_minimal_dia_source_schema 

from lsst.utils import getPackageDir 

import lsst.utils.tests 

from sqlalchemy import create_engine 

 

 

def _make_case_conficting_dia_object_schema(): 

"""Make schema which has column name with case mismatch. 

 

Copy of make_minimal_dia_object_schema with additional column. 

""" 

schema = afwTable.SourceTable.makeMinimalSchema() 

schema.addField("pixelId", type='L', 

doc='Unique spherical pixelization identifier.') 

schema.addField("nDiaSources", type='L') 

# baseline schema has column `radecTai` 

schema.addField("RaDecTai", type='D') 

return schema 

 

 

def _data_file_name(basename): 

"""Return path name of a data file. 

""" 

return os.path.join(getPackageDir("dax_ppdb"), "data", basename) 

 

 

class PpdbSchemaTestCase(unittest.TestCase): 

"""A test case for PpdbSchema class 

""" 

 

@classmethod 

def setUpClass(cls): 

pass 

 

def _assertTable(self, table, name, ncol): 

"""validation for tables schema. 

 

Parameters 

---------- 

table : `sqlalchemy.Table` 

name : `str` 

Expected table name 

ncol : `int` 

Expected number of columns 

""" 

self.assertIsNotNone(table) 

self.assertEqual(table.name, name) 

self.assertEqual(len(table.columns), ncol) 

 

def test_makeSchema(self): 

"""Test for creating schemas. 

 

Schema is defined in YAML files, some checks here depend on that 

configuration and will need to be updated when configuration changes. 

""" 

engine = create_engine('sqlite://') 

 

# create standard (baseline) schema 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml")) 

schema.makeSchema() 

self._assertTable(schema.objects, "DiaObject", 92) 

self.assertEqual(len(schema.objects.primary_key), 2) 

self.assertIsNone(schema.objects_nightly) 

self.assertIsNone(schema.objects_last) 

self._assertTable(schema.sources, "DiaSource", 108) 

self._assertTable(schema.forcedSources, "DiaForcedSource", 7) 

 

# create schema using prefix 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

prefix="Pfx") 

# Drop existing tables (but we don't check it here) 

schema.makeSchema(drop=True) 

self._assertTable(schema.objects, "PfxDiaObject", 92) 

self.assertIsNone(schema.objects_nightly) 

self.assertIsNone(schema.objects_last) 

self._assertTable(schema.sources, "PfxDiaSource", 108) 

self._assertTable(schema.forcedSources, "PfxDiaForcedSource", 7) 

 

# use different indexing for DiaObject, need extra schema for that 

schema = PpdbSchema(engine=engine, 

dia_object_index="pix_id_iov", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

extra_schema_file=_data_file_name("ppdb-schema-extra.yaml")) 

schema.makeSchema(drop=True) 

self._assertTable(schema.objects, "DiaObject", 93) 

self.assertEqual(len(schema.objects.primary_key), 3) 

self.assertIsNone(schema.objects_nightly) 

self.assertIsNone(schema.objects_last) 

self._assertTable(schema.sources, "DiaSource", 108) 

self._assertTable(schema.forcedSources, "DiaForcedSource", 7) 

 

# use DiaObjectLast table for DiaObject, need extra schema for that 

schema = PpdbSchema(engine=engine, 

dia_object_index="last_object_table", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

extra_schema_file=_data_file_name("ppdb-schema-extra.yaml")) 

schema.makeSchema(drop=True) 

self._assertTable(schema.objects, "DiaObject", 93) 

self.assertEqual(len(schema.objects.primary_key), 2) 

self.assertIsNone(schema.objects_nightly) 

self._assertTable(schema.objects_last, "DiaObjectLast", 18) 

self.assertEqual(len(schema.objects_last.primary_key), 2) 

self._assertTable(schema.sources, "DiaSource", 108) 

self._assertTable(schema.forcedSources, "DiaForcedSource", 7) 

 

# baseline schema with nightly DiaObject 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=True, 

schema_file=_data_file_name("ppdb-schema.yaml")) 

schema.makeSchema(drop=True) 

self._assertTable(schema.objects, "DiaObject", 92) 

self._assertTable(schema.objects_nightly, "DiaObjectNightly", 92) 

self.assertIsNone(schema.objects_last) 

self._assertTable(schema.sources, "DiaSource", 108) 

self._assertTable(schema.forcedSources, "DiaForcedSource", 7) 

 

def test_afwSchemaCaseSensitivity(self): 

"""Test for column case mismatch errors. 

 

This is a specific test for when afw schema column names differ from 

PPDB schem in case only which should generate exception. 

 

Like all other tests this depends on the column naming in 

ppdb-schema.yaml. 

""" 

engine = create_engine('sqlite://') 

 

afw_schemas = dict(DiaObject=_make_case_conficting_dia_object_schema(), 

DiaSource=make_minimal_dia_source_schema()) 

# column case mismatch should cause exception in constructor 

with self.assertRaises(ValueError): 

PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

column_map=_data_file_name("ppdb-afw-map.yaml"), 

afw_schemas=afw_schemas) 

 

def test_getAfwSchema(self): 

"""Test for getAfwSchema method. 

 

Schema is defined in YAML files, some checks here depend on that 

configuration and will need to be updated when configuration changes. 

""" 

engine = create_engine('sqlite://') 

 

# create standard (baseline) schema, but use afw column map 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

column_map=_data_file_name("ppdb-afw-map.yaml")) 

schema.makeSchema() 

 

afw_schema, col_map = schema.getAfwSchema("DiaObject") 

self.assertEqual(len(col_map), 92) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# no BLOBs in afwTable, so count is lower 

self.assertEqual(afw_schema.getFieldCount(), 81) 

 

afw_schema, col_map = schema.getAfwSchema("DiaSource") 

self.assertEqual(len(col_map), 108) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

self.assertEqual(afw_schema.getFieldCount(), 108) 

 

afw_schema, col_map = schema.getAfwSchema("DiaForcedSource") 

self.assertEqual(len(col_map), 7) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# afw table adds 4 columns compared to out standard schema 

self.assertEqual(afw_schema.getFieldCount(), 7+4) 

 

# subset of columns 

afw_schema, col_map = schema.getAfwSchema("DiaObject", 

["diaObjectId", "ra", "decl", "ra_decl_Cov"]) 

self.assertEqual(len(col_map), 4) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# one extra column exists for some reason for DiaObect in afw schema 

self.assertEqual(afw_schema.getFieldCount(), 5) 

 

def test_getAfwSchemaWithExtras(self): 

"""Test for getAfwSchema method using extra afw schemas. 

 

Same as above but use non-default afw schemas, this adds few extra 

columns to the table schema 

""" 

engine = create_engine('sqlite://') 

 

# create standard (baseline) schema, but use afw column map 

afw_schemas = dict(DiaObject=make_minimal_dia_object_schema(), 

DiaSource=make_minimal_dia_source_schema()) 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

column_map=_data_file_name("ppdb-afw-map.yaml"), 

afw_schemas=afw_schemas) 

schema.makeSchema() 

 

afw_schema, col_map = schema.getAfwSchema("DiaObject") 

self.assertEqual(len(col_map), 94) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# no BLOBs in afwTable, so count is lower 

self.assertEqual(afw_schema.getFieldCount(), 82) 

 

afw_schema, col_map = schema.getAfwSchema("DiaSource") 

self.assertEqual(len(col_map), 109) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

self.assertEqual(afw_schema.getFieldCount(), 109) 

 

afw_schema, col_map = schema.getAfwSchema("DiaForcedSource") 

self.assertEqual(len(col_map), 7) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# afw table adds 4 columns compared to out standard schema 

self.assertEqual(afw_schema.getFieldCount(), 7+4) 

 

# subset of columns 

afw_schema, col_map = schema.getAfwSchema("DiaObject", 

["diaObjectId", "ra", "decl", "ra_decl_Cov"]) 

self.assertEqual(len(col_map), 4) 

self.assertIsInstance(afw_schema, afwTable.Schema) 

# one extra column exists for some reason for DiaObect in afw schema 

self.assertEqual(afw_schema.getFieldCount(), 5) 

 

def test_getAfwColumns(self): 

"""Test for getAfwColumns method. 

 

Schema is defined in YAML files, some checks here depend on that 

configuration and will need to be updated when configuration changes. 

""" 

engine = create_engine('sqlite://') 

 

# create standard (baseline) schema, but use afw column map 

schema = PpdbSchema(engine=engine, 

dia_object_index="baseline", 

dia_object_nightly=False, 

schema_file=_data_file_name("ppdb-schema.yaml"), 

column_map=_data_file_name("ppdb-afw-map.yaml")) 

schema.makeSchema() 

 

col_map = schema.getAfwColumns("DiaObject") 

self.assertEqual(len(col_map), 92) 

# check few afw-specific names 

self.assertIn("id", col_map) 

self.assertIn("coord_ra", col_map) 

self.assertIn("coord_dec", col_map) 

 

col_map = schema.getAfwColumns("DiaSource") 

self.assertEqual(len(col_map), 108) 

# check few afw-specific names 

self.assertIn("id", col_map) 

self.assertIn("coord_ra", col_map) 

self.assertIn("coord_dec", col_map) 

 

 

class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

300 ↛ 301line 300 didn't jump to line 301, because the condition on line 300 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()