Ticket #1151: testParentChildWithRefToNonPK.py

File testParentChildWithRefToNonPK.py, 4.6 kB (added by kweinert, 7 months ago)

unittest testcase

Line 
1 #!/usr/bin/env python
2 # -*- coding: iso8859_15 -*-
3
4 import dabo
5 import unittest
6 from dabo.dLocalize import _
7 from dabo.dApp import dApp
8 from dabo.db.dConnectInfo import dConnectInfo
9
10 class BizMaster(dabo.biz.dBizobj):
11     def initProperties(self):
12         self.DataSource = "master"
13         self.KeyField = "id"
14         self.AutoPopulatePK = True
15         self.DataStructure = (
16                 ("id", "I", True, "master", "id"),
17                 ("key", "I", False, "master", "key"),
18                 ("info", "C", False, "master", "info"),
19         )       
20         self.addFrom("master")
21         self.addField("id")
22         self.addField("key")
23         self.addField("info")
24
25 class BizDetails(dabo.biz.dBizobj):
26     def initProperties(self):
27         self.DataSource = "details"
28         self.KeyField = "id"
29         self.AutoPopulatePK = True
30         self.DataStructure = (
31                 ("id", "I", True, "details", "id"),
32                 ("key_ref", "I", False, "details", "key_ref"),
33                 ("optional_info", "C", False, "details", "optional_info"),
34         )       
35         self.addFrom("details")
36         self.addField("id")
37         self.addField("optional_info")
38         self.addField("key_ref")
39            
40 class BlackHole:
41     def write(self, s):
42         pass
43
44 class TestParentChildWithRefToNonPK(unittest.TestCase):
45     def setUp(self):
46         dabo.infoLog.LogObject = BlackHole()
47         app = dApp()
48        
49         app.MainFormClass = None
50
51         dbFileName = ":memory:"
52         connInfo = dConnectInfo(Name="myconn", DbType="SQLite", Database=dbFileName)
53         app.addConnectInfo(connInfo)
54            
55         ## create database on the fly:
56         conn = app.getConnectionByName("myconn")
57         curs= conn.getDaboCursor()
58         # create tables and testdata
59         scriptList = """
60             create table master (
61                 id integer primary key autoincrement not null,
62                 key integer,
63                 info text
64             );
65             create table details (
66                 id integer primary key autoincrement not null,
67                 key_ref integer,
68                 optional_info text
69             );
70             insert into master (key, info) values (1, 'row1key1');
71             insert into master (key, info) values (1, 'row2key1');
72             insert into master (key, info) values (2, 'row3key2');
73             insert into details (key_ref, optional_info) values (1, 'option1forkey1');
74             insert into details (key_ref, optional_info) values (1, 'option2forkey1');
75             insert into details (key_ref, optional_info) values (1, 'option3forkey1');
76             insert into details (key_ref, optional_info) values (2, 'option1forkey2');
77             """.split(";")
78         for stmt in scriptList:
79             if stmt.strip():
80                 curs.execute(stmt)
81
82         ## start the app
83         app.setup()
84         app.start()
85  
86         # create bizobjs
87         conn = app.getConnectionByName("myconn")
88         self.master = master = BizMaster(conn)
89         self.details = details = BizDetails(conn)
90        
91         # link details to master
92         # we use details only for lookup, so do not
93         # create new child rows on master.new()
94         details.LinkField = "key_ref"
95         details.ParentLinkField = "key"
96         details.NewRecordOnNewParent = False
97         details.FillLinkFromParent = False
98         master.NewChildOnNew = False
99         master.addChild(details)
100        
101         master.requery()
102
103     def test_setFieldVal(self):
104         """After the key field is set, does the right child dataset show up?"""
105         master = self.master
106         details = self.details
107         master.setFieldVal("key", 2)
108         master.requeryAllChildren()
109         self.assertEqual(details.RowCount, 1)
110         self.assertEqual(details.getFieldVal("optional_info"), "option1forkey2")
111         master.next()
112         master.prior()
113         self.assertEqual(details.RowCount, 1)
114         self.assertEqual(details.getFieldVal("optional_info"), "option1forkey2")
115        
116     def test_new(self):
117         """After new, after the key field is set, does the right child dataset show up?"""
118         master = self.master
119         details = self.details
120         master.new()
121         master.setFieldVal("key", 1)
122         master.requeryAllChildren()
123         print "details.getDataSet:", details.getDataSet()
124         self.assertEqual(details.RowCount, 3)
125    
126 if __name__ == "__main__":
127     suite = unittest.TestLoader().loadTestsFromTestCase(TestParentChildWithRefToNonPK)
128     unittest.TextTestRunner(verbosity=2).run(suite)