contrib/opportunity: Added tests for opportunity
[nit.git] / contrib / opportunity / tests / db_tests.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License
14
15 # Tests for the model of Opportunity
16 module db_tests
17
18 import opportunity_model
19
20 redef class OpportunityDB
21
22 fun wipe do
23 execute("DROP TABLE people;")
24 execute("DROP TABLE meetups;")
25 execute("DROP TABLE answers;")
26 execute("DROP TABLE part_answers;")
27 execute("DROP INDEX answers_clean;")
28 execute("DROP INDEX ans_clean;")
29 execute("DROP INDEX ppl_clean;")
30 end
31
32 end
33
34 print "Opening DB"
35
36 var db = new OpportunityDB.open("opportunity")
37
38 print "DB opened"
39
40 db.wipe
41
42 print "Wiped"
43
44 db.close
45
46 db = new OpportunityDB.open("opportunity")
47
48 var hj = new People("Jack", "Handsome")
49
50 var m = new Meetup("Awaken the warrior", "2024/05/28", "Vault of the Warrior")
51 assert m.commit(db)
52
53 var vh = new People("Hunter", "Vault")
54
55 var ll = new People("", "Lilith")
56
57 var y = new Answer("Yes")
58 y.meetup = m
59 y.commit(db)
60
61 var n = new Answer("No")
62 n.meetup = m
63 n.commit(db)
64
65 var h = new Answer("I have no choice, I'm a hostage")
66 h.meetup = m
67 h.commit(db)
68
69 hj.answer(y) = true
70 hj.answer(n) = false
71 hj.answer(h) = false
72
73 vh.answer(y) = true
74 vh.answer(n) = false
75 vh.answer(h) = false
76
77 ll.answer(y) = true
78 ll.answer(n) = false
79 ll.answer(h) = true
80
81 hj.commit db
82 vh.commit db
83 ll.commit db
84
85 assert hj.commit(db)
86 assert vh.commit(db)
87 assert ll.commit(db)
88
89 print db.find_meetup_by_id(m.id) or else "null"
90
91 for i in m.participants(db) do
92 print "Answers for {i.to_s.trim}"
93 i.load_answers(db, m)
94 for k,v in i.answers do
95 print "{k.to_s.trim} => {v.to_s.trim}"
96 end
97 end
98
99 db.close