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

from builtins import next 

from builtins import range 

import unittest 

import sqlite3 

import os 

import numpy as np 

import tempfile 

import shutil 

 

import lsst.utils.tests 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.catalogs.db import CatalogDBObject, DBObject 

 

ROOT = os.path.abspath(os.path.dirname(__file__)) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class CachingTestCase(unittest.TestCase): 

""" 

This class will contain tests to make sure that CatalogDBObject is 

correctly using its _connection_cache 

""" 

 

@classmethod 

def setUpClass(cls): 

cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix="scratchSpace-") 

cls.db_name = os.path.join(cls.scratch_dir, "connection_cache_test_db.db") 

31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true if os.path.exists(cls.db_name): 

os.unlink(cls.db_name) 

 

conn = sqlite3.connect(cls.db_name) 

c = conn.cursor() 

c.execute('''CREATE TABLE test (id int, i1 int, i2 int)''') 

for ii in range(5): 

c.execute('''INSERT INTO test VALUES (%i, %i, %i)''' % (ii, ii*ii, -ii)) 

conn.commit() 

conn.close() 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

45 ↛ 47line 45 didn't jump to line 47, because the condition on line 45 was never false if os.path.exists(cls.db_name): 

os.unlink(cls.db_name) 

47 ↛ exitline 47 didn't return from function 'tearDownClass', because the condition on line 47 was never false if os.path.exists(cls.scratch_dir): 

shutil.rmtree(cls.scratch_dir) 

 

 

def test_catalog_db_object_cacheing(self): 

""" 

Test that opening multiple CatalogDBObjects that connect to the same 

database only results in one connection being opened and used. We 

will test this by instantiating two CatalogDBObjects and a DBObject 

that connect to the same database. We will then test that the two 

CatalogDBObjects' connections are identical, but that the DBObject has 

its own connection. 

""" 

 

sims_clean_up() 

self.assertEqual(len(CatalogDBObject._connection_cache), 0) 

 

class DbClass1(CatalogDBObject): 

database = self.db_name 

port = None 

host = None 

driver = 'sqlite' 

tableid = 'test' 

idColKey = 'id' 

objid = 'test_db_class_1' 

 

columns = [('identification', 'id')] 

 

class DbClass2(CatalogDBObject): 

database = self.db_name 

port = None 

host = None 

driver = 'sqlite' 

tableid = 'test' 

idColKey = 'id' 

objid = 'test_db_class_2' 

 

columns = [('other', 'i1')] 

 

db1 = DbClass1() 

db2 = DbClass2() 

self.assertEqual(id(db1.connection), id(db2.connection)) 

self.assertEqual(len(CatalogDBObject._connection_cache), 1) 

 

db3 = DBObject(database=self.db_name, driver='sqlite', host=None, port=None) 

self.assertNotEqual(id(db1.connection), id(db3.connection)) 

 

self.assertEqual(len(CatalogDBObject._connection_cache), 1) 

 

# check that if we had passed db1.connection to a DBObject, 

# the connections would be identical 

db4 = DBObject(connection=db1.connection) 

self.assertEqual(id(db4.connection), id(db1.connection)) 

 

self.assertEqual(len(CatalogDBObject._connection_cache), 1) 

 

# verify that db1 and db2 are both useable 

results = db1.query_columns(colnames=['id', 'i1', 'i2', 'identification']) 

results = next(results) 

self.assertEqual(len(results), 5) 

np.testing.assert_array_equal(results['id'], list(range(5))) 

np.testing.assert_array_equal(results['id'], results['identification']) 

np.testing.assert_array_equal(results['id']**2, results['i1']) 

np.testing.assert_array_equal(results['id']*(-1), results['i2']) 

 

results = db2.query_columns(colnames=['id', 'i1', 'i2', 'other']) 

results = next(results) 

self.assertEqual(len(results), 5) 

np.testing.assert_array_equal(results['id'], list(range(5))) 

np.testing.assert_array_equal(results['id']**2, results['i1']) 

np.testing.assert_array_equal(results['i1'], results['other']) 

np.testing.assert_array_equal(results['id']*(-1), results['i2']) 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()