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

from __future__ import with_statement 

from builtins import zip 

import unittest 

import os 

import numpy as np 

import shutil 

import tempfile 

import lsst.utils.tests 

from lsst.sims.catalogs.db import fileDBObject 

 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class FileDBObjectTestCase(unittest.TestCase): 

""" 

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

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

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

against. 

""" 

 

def setUp(self): 

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

 

 

def tearDown(self): 

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

shutil.rmtree(self.scratch_dir) 

 

def test_ingest(self): 

""" 

Test that fileDBObject correctly ingests a text file containing 

multiple data types. 

""" 

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

"filedbojb_ingest_test.txt") 

 

rng = np.random.RandomState(8821) 

alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 

n_rows = 34 

n_letters = 72 

f_list = rng.random_sample(n_rows) 

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

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

word_list = [] 

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

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

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

word = '' 

for wwdex in ww: 

word += alphabet[wwdex] 

word_list.append(word) 

self.assertEqual(len(word), n_letters) 

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

 

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

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

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

self.assertEqual(len(results), n_rows) 

for row in results: 

i_row = row[0] 

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

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

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

 

70 ↛ exitline 70 didn't return from function 'test_ingest', because the condition on line 70 was never false if os.path.exists(txt_file_name): 

os.unlink(txt_file_name) 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()