test_db.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import wirecell.util.wires.db as db
3 
5  print
6  ses = db.session()
7 
8  # parent
9  det = db.Detector()
10  cr1 = db.Crate()
11  cr2 = db.Crate()
12 
13  dce1 = db.DetectorCrateLink(detector=det, crate=cr1, address=42)
14  #dce2 = db.DetectorCrateLink(detector=det, crate=cr2, address=40)
15  # Note: there is a temptation to add a crate directly to the
16  # .crate relationship. This doesn't sync the link. so no address
17  # can ever be set. A Detector.add_crate(crate, address) could
18  # help. But, whatever, don't do the following:
19  #
20  #det.crates.append(cr2)
21  dce2 = det.add_crate(cr2, 40)
22 
23  # for testing, just fill the first of everything
24  crate = cr1
25  wibs = list()
26  for slot in range(5):
27  wib = db.Wib()
28  wibs.append(wib)
29  crate.add_wib(wib, slot)
30 
31  boards = list()
32  for connector in range(4):
33  board = db.Board()
34  boards.append(board)
35  wibs[0].add_board(board, connector)
36 
37  conductors = list()
38  for layer,spots in enumerate([40,40,48]):
39  for spot in range(spots):
40  conductor = db.Conductor()
41  conductors.append(conductor)
42  boards[0].add_conductor(conductor, spot, layer)
43 
44  chips = list();
45  for spot in range(8):
46  chip = db.Chip()
47  chips.append(chip)
48  boards[0].add_chip(chip, spot)
49 
50  chans = list()
51  for address in range(16):
52  # note: this is a totally bogus channel map! just for testing.
53  chan = db.Channel(conductor=conductors[address])
54  chans.append(chan)
55  chips[0].add_channel(chan, address)
56 
57 
58  ses.add(det)
59 
60  ses.commit()
61 
62  det = ses.query(db.Detector).one()
63 
64  print 'DET:',det
65  print 'DET.crates:', det.crates
66  print 'DET.crate_links:', det.crate_links
67  print 'DET.crates[0].detectors:', det.crates[0].detectors
68  print 'DET.crates[1].detectors:', det.crates[1].detectors
69 
70  assert det.crates[0].detectors[0] == det
71  # The *_links should be ordered by the connection attributes
72  assert det.crate_links[0].address == 40
73  assert det.crate_links[1].address == 42
74  # While the direct relationship is ordered by creation (.id)
75  assert det.crates[0] == det.crate_links[1].crate
76  assert det.crates[1] == det.crate_links[0].crate
77 
78  crate42 = ses.query(db.DetectorCrateLink).\
79  filter(db.DetectorCrateLink.address==42).one().crate
80  print crate42
81  assert crate42 == det.crate_links[1].crate
82 
83  print crate42.wibs[0]
84  board = crate42.wibs[0].boards[0]
85  print board
86 
87  print board.conductors
88  print board.chips
89  chip = board.chips[0]
90  print chip.channels
91  for ch in chip.channels:
92  print ch, ch.conductor
93  return
94 
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def test_classes()
Definition: test_db.py:4