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

1from __future__ import with_statement 

2from builtins import zip 

3import unittest 

4import os 

5import numpy as np 

6import shutil 

7import tempfile 

8import lsst.utils.tests 

9from lsst.sims.catalogs.db import fileDBObject 

10 

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

12 

13 

14def setup_module(module): 

15 lsst.utils.tests.init() 

16 

17 

18class FileDBObjectTestCase(unittest.TestCase): 

19 """ 

20 This class will test that fileDBObject can correctly ingest a database, 

21 preserving all of the data it was given. This is it's own test because 

22 a lot of other unit tests depend on fileDBObject to create data to test 

23 against. 

24 """ 

25 

26 def setUp(self): 

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

28 

29 

30 def tearDown(self): 

31 if os.path.exists(self.scratch_dir): 

32 shutil.rmtree(self.scratch_dir) 

33 

34 def test_ingest(self): 

35 """ 

36 Test that fileDBObject correctly ingests a text file containing 

37 multiple data types. 

38 """ 

39 txt_file_name = os.path.join(self.scratch_dir, 

40 "filedbojb_ingest_test.txt") 

41 

42 rng = np.random.RandomState(8821) 

43 alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 

44 n_rows = 34 

45 n_letters = 72 

46 f_list = rng.random_sample(n_rows) 

47 i_list = rng.randint(0, 2**50, n_rows) 

48 word_dex_list = rng.randint(0, len(alphabet)-1, (n_rows, n_letters)) 

49 word_list = [] 

50 with open(txt_file_name, 'w') as output_file: 

51 output_file.write("# a header\n") 

52 for ix, (ff, ii, ww) in enumerate(zip(f_list, i_list, word_dex_list)): 

53 word = '' 

54 for wwdex in ww: 

55 word += alphabet[wwdex] 

56 word_list.append(word) 

57 self.assertEqual(len(word), n_letters) 

58 output_file.write('%d %.13f %ld %s\n' % (ix, ff, ii, word)) 

59 

60 dtype = np.dtype([('id', int), ('float', float), ('int', int), ('word', str, n_letters)]) 

61 db = fileDBObject(txt_file_name, runtable='test', dtype=dtype, idColKey='id') 

62 results = db.execute_arbitrary('SELECT * from test') 

63 self.assertEqual(len(results), n_rows) 

64 for row in results: 

65 i_row = row[0] 

66 self.assertAlmostEqual(f_list[i_row], row[1], 13) 

67 self.assertEqual(i_list[i_row], row[2]) 

68 self.assertEqual(word_list[i_row], row[3]) 

69 

70 if os.path.exists(txt_file_name): 

71 os.unlink(txt_file_name) 

72 

73 

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

75 pass 

76 

77 

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

79 lsst.utils.tests.init() 

80 unittest.main()