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

import os 

import sqlite3 

import unittest 

from astropy.time import Time, TimeDelta 

from lsst.utils import getPackageDir 

import lsst.utils.tests 

from lsst.utils.tests import getTempFilePath 

 

from lsst.sims.downtimeModel import ScheduledDowntimeData 

 

 

class ScheduledDowntimeDataTest(unittest.TestCase): 

 

def setUp(self): 

self.th = Time('2020-01-01', format='isot', scale='tai') 

self.startofnight = -0.34 

self.downtime_db = os.path.join(getPackageDir('sims_downtimeModel'), 'data', 'scheduled_downtime.db') 

 

def test_basic_information_after_creation(self): 

downtimeData = ScheduledDowntimeData(self.th, start_of_night_offset=self.startofnight) 

self.assertEqual(downtimeData.scheduled_downtime_db, self.downtime_db) 

self.assertEqual(self.th + TimeDelta(self.startofnight, format='jd'), downtimeData.night0) 

downtimeData = ScheduledDowntimeData(self.th, start_of_night_offset=0) 

self.assertEqual(downtimeData.night0, self.th) 

 

def test_information_after_initialization(self): 

downtimeData = ScheduledDowntimeData(self.th, start_of_night_offset=self.startofnight) 

downtimeData.read_data() 

self.assertEqual(len(downtimeData.downtime), 31) 

# Check some of the downtime values. 

dnight = downtimeData.downtime['end'] - downtimeData.downtime['start'] 

self.assertEqual(dnight[0].jd, 7) 

self.assertEqual(downtimeData.downtime['activity'][0], 'general maintenance') 

self.assertEqual(dnight[4].jd, 14) 

self.assertEqual(downtimeData.downtime['activity'][4], 'recoat mirror') 

 

def test_alternate_db(self): 

with getTempFilePath('.alt_downtime.db') as tmpdb: 

downtime_table = [] 

downtime_table.append("night INTEGER PRIMARY KEY") 

downtime_table.append("duration INTEGER") 

downtime_table.append("activity TEXT") 

 

with sqlite3.connect(tmpdb) as conn: 

cur = conn.cursor() 

cur.execute("DROP TABLE IF EXISTS Downtime") 

cur.execute("CREATE TABLE Downtime({})".format(",".join(downtime_table))) 

cur.execute("INSERT INTO Downtime VALUES(?, ?, ?)", (100, 7, "something to do")) 

cur.close() 

 

downtimeData = ScheduledDowntimeData(self.th, 

scheduled_downtime_db=tmpdb, 

start_of_night_offset = self.startofnight) 

downtimeData.read_data() 

self.assertEqual(len(downtimeData.downtime), 1) 

self.assertEqual(downtimeData.downtime['activity'][0], 'something to do') 

 

def test_call(self): 

downtimeData = ScheduledDowntimeData(self.th, start_of_night_offset=self.startofnight) 

downtimeData.read_data() 

downtimes = downtimeData() 

self.assertEqual(downtimes['activity'][4], 'recoat mirror') 

 

 

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

pass 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

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

lsst.utils.tests.init() 

unittest.main()