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# This file is part of daf_butler. 

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 

22import os 

23from contextlib import contextmanager 

24import secrets 

25import unittest 

26import gc 

27 

28try: 

29 # It's possible but silly to have testing.postgresql installed without 

30 # having the postgresql server installed (because then nothing in 

31 # testing.postgresql would work), so we use the presence of that module 

32 # to test whether we can expect the server to be available. 

33 import testing.postgresql 

34except ImportError: 

35 testing = None 

36 

37import sqlalchemy 

38 

39from lsst.daf.butler import ddl 

40from lsst.daf.butler.registry import RegistryConfig 

41from lsst.daf.butler.registry.databases.postgresql import PostgresqlDatabase 

42from lsst.daf.butler.registry import Registry 

43from lsst.daf.butler.registry.tests import DatabaseTests, RegistryTests 

44 

45 

46@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

47class PostgresqlDatabaseTestCase(unittest.TestCase, DatabaseTests): 

48 

49 @classmethod 

50 def setUpClass(cls): 

51 cls.server = testing.postgresql.Postgresql() 

52 

53 @classmethod 

54 def tearDownClass(cls): 

55 # Clean up any lingering SQLAlchemy engines/connections 

56 # so they're closed before we shut down the server. 

57 gc.collect() 

58 cls.server.stop() 

59 

60 def makeEmptyDatabase(self, origin: int = 0) -> PostgresqlDatabase: 

61 namespace = f"namespace_{secrets.token_hex(8).lower()}" 

62 return PostgresqlDatabase.fromUri(origin=origin, uri=self.server.url(), namespace=namespace) 

63 

64 def getNewConnection(self, database: PostgresqlDatabase, *, writeable: bool) -> PostgresqlDatabase: 

65 return PostgresqlDatabase.fromUri(origin=database.origin, uri=self.server.url(), 

66 namespace=database.namespace, writeable=writeable) 

67 

68 @contextmanager 

69 def asReadOnly(self, database: PostgresqlDatabase) -> PostgresqlDatabase: 

70 yield self.getNewConnection(database, writeable=False) 

71 

72 def testNameShrinking(self): 

73 """Test that too-long names for database entities other than tables 

74 and columns (which we preserve, and just expect to fit) are shrunk. 

75 """ 

76 db = self.makeEmptyDatabase(origin=1) 

77 with db.declareStaticTables(create=True) as context: 

78 # Table and field names are each below the 63-char limit even when 

79 # accounting for the prefix, but their combination (which will 

80 # appear in sequences and constraints) is not. 

81 tableName = "a_table_with_a_very_very_long_42_char_name" 

82 fieldName1 = "a_column_with_a_very_very_long_43_char_name" 

83 fieldName2 = "another_column_with_a_very_very_long_49_char_name" 

84 context.addTable( 

85 tableName, 

86 ddl.TableSpec( 

87 fields=[ 

88 ddl.FieldSpec( 

89 fieldName1, 

90 dtype=sqlalchemy.BigInteger, 

91 autoincrement=True, 

92 primaryKey=True 

93 ), 

94 ddl.FieldSpec( 

95 fieldName2, 

96 dtype=sqlalchemy.String, 

97 length=16, 

98 nullable=False, 

99 ), 

100 ], 

101 unique={(fieldName2,)}, 

102 ) 

103 ) 

104 # Add another table, this time dynamically, with a foreign key to the 

105 # first table. 

106 db.ensureTableExists( 

107 tableName + "_b", 

108 ddl.TableSpec( 

109 fields=[ 

110 ddl.FieldSpec( 

111 fieldName1 + "_b", 

112 dtype=sqlalchemy.BigInteger, 

113 autoincrement=True, 

114 primaryKey=True 

115 ), 

116 ddl.FieldSpec( 

117 fieldName2 + "_b", 

118 dtype=sqlalchemy.String, 

119 length=16, 

120 nullable=False, 

121 ), 

122 ], 

123 foreignKeys=[ 

124 ddl.ForeignKeySpec(tableName, source=(fieldName2 + "_b",), target=(fieldName2,)), 

125 ] 

126 ) 

127 ) 

128 

129 

130@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

131class PostgresqlRegistryTestCase(unittest.TestCase, RegistryTests): 

132 """Tests for `Registry` backed by a PostgreSQL database. 

133 """ 

134 

135 @classmethod 

136 def setUpClass(cls): 

137 cls.server = testing.postgresql.Postgresql() 

138 

139 @classmethod 

140 def tearDownClass(cls): 

141 cls.server.stop() 

142 

143 @classmethod 

144 def getDataDir(cls) -> str: 

145 return os.path.normpath(os.path.join(os.path.dirname(__file__), "data", "registry")) 

146 

147 def makeRegistry(self) -> Registry: 

148 namespace = f"namespace_{secrets.token_hex(8).lower()}" 

149 config = RegistryConfig() 

150 config["db"] = self.server.url() 

151 config["namespace"] = namespace 

152 return Registry.fromConfig(config, create=True) 

153 

154 

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

156 unittest.main()