Coverage for tests/test_apdbSqlSchema.py: 32%

Shortcuts 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

54 statements  

1# This file is part of dax_apdb. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

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

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

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

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

21 

22"""Unit test for ApdbSqlSchema class. 

23""" 

24 

25import os 

26import unittest 

27 

28from lsst.dax.apdb.apdbSqlSchema import ApdbSqlSchema 

29from lsst.utils import getPackageDir 

30import lsst.utils.tests 

31from sqlalchemy import create_engine 

32 

33 

34def _data_file_name(basename): 

35 """Return path name of a data file. 

36 """ 

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

38 

39 

40class ApdbSchemaTestCase(unittest.TestCase): 

41 """A test case for ApdbSqlSchema class 

42 """ 

43 

44 @classmethod 

45 def setUpClass(cls): 

46 pass 

47 

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

49 """validation for tables schema. 

50 

51 Parameters 

52 ---------- 

53 table : `sqlalchemy.Table` 

54 name : `str` 

55 Expected table name 

56 ncol : `int` 

57 Expected number of columns 

58 """ 

59 self.assertIsNotNone(table) 

60 self.assertEqual(table.name, name) 

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

62 

63 def test_makeSchema(self): 

64 """Test for creating schemas. 

65 

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

67 configuration and will need to be updated when configuration changes. 

68 """ 

69 engine = create_engine('sqlite://') 

70 

71 # create standard (baseline) schema 

72 schema = ApdbSqlSchema(engine=engine, 

73 dia_object_index="baseline", 

74 htm_index_column="pixelId", 

75 schema_file=_data_file_name("apdb-schema.yaml")) 

76 schema.makeSchema() 

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

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

79 self.assertIsNone(schema.objects_last) 

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

81 self._assertTable(schema.forcedSources, "DiaForcedSource", 8) 

82 

83 # create schema using prefix 

84 schema = ApdbSqlSchema(engine=engine, 

85 dia_object_index="baseline", 

86 htm_index_column="pixelId", 

87 schema_file=_data_file_name("apdb-schema.yaml"), 

88 prefix="Pfx") 

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

90 schema.makeSchema(drop=True) 

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

92 self.assertIsNone(schema.objects_last) 

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

94 self._assertTable(schema.forcedSources, "PfxDiaForcedSource", 8) 

95 

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

97 schema = ApdbSqlSchema(engine=engine, 

98 dia_object_index="pix_id_iov", 

99 htm_index_column="pixelId", 

100 schema_file=_data_file_name("apdb-schema.yaml"), 

101 extra_schema_file=_data_file_name("apdb-schema-extra.yaml")) 

102 schema.makeSchema(drop=True) 

103 self._assertTable(schema.objects, "DiaObject", 94) 

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

105 self.assertIsNone(schema.objects_last) 

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

107 self._assertTable(schema.forcedSources, "DiaForcedSource", 8) 

108 

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

110 schema = ApdbSqlSchema(engine=engine, 

111 dia_object_index="last_object_table", 

112 htm_index_column="pixelId", 

113 schema_file=_data_file_name("apdb-schema.yaml"), 

114 extra_schema_file=_data_file_name("apdb-schema-extra.yaml")) 

115 schema.makeSchema(drop=True) 

116 self._assertTable(schema.objects, "DiaObject", 94) 

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

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

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

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

121 self._assertTable(schema.forcedSources, "DiaForcedSource", 8) 

122 

123 

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

125 pass 

126 

127 

128def setup_module(module): 

129 lsst.utils.tests.init() 

130 

131 

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

133 lsst.utils.tests.init() 

134 unittest.main()