/
ModelTest.java
244 lines (205 loc) · 8.26 KB
/
ModelTest.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package leodagdag.play2morphia.test;
import com.github.jmkgreen.morphia.query.Query;
import leodagdag.play2morphia.MorphiaPlugin;
import leodagdag.play2morphia.test.models.*;
import leodagdag.play2morphia.test.utils.TestConfig;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalTime;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.running;
/**
* User: leo
* Date: 06/10/12
* Time: 14:00
*/
public class ModelTest extends AbstractTest {
@Before
public void setUp() {
dropAllCollections();
}
@Test
public void testCreate() {
System.out.println("testCreate");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
Post post = new Post();
post.title = "fake post";
post.insert();
ObjectId id = post.id;
assertThat(Post.find().byId(id).id).isEqualTo(post.id);
assertThat(Post.find().byId(id).title).isEqualTo(post.title);
assertThat(Post.find().asList().size()).isEqualTo(1);
}
});
}
@Test
public void testUpdate() {
System.out.println("testUpdate");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
Post post = new Post();
post.title = "fake post";
post.insert();
post.title = "real title";
post.update();
assertThat(Post.find().byId(post.id).title).isEqualTo(post.title);
}
});
}
@Test
public void testDelete() {
System.out.println("testDelete");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
Post post = new Post();
post.title = "fake post";
post.insert();
assertThat(Post.find().asList().size()).isEqualTo(1);
post.delete();
assertThat(Post.find().asList().size()).isEqualTo(0);
}
});
}
@Test
public void testFindWithOr() {
System.out.println("testFindWithOr");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
Post alpha1 = new Post();
alpha1.title = "post1";
alpha1.type = "ALPHA";
alpha1.insert();
Post beta1 = new Post();
beta1.title = "post1";
beta1.type = "BETA";
beta1.insert();
Post beta2 = new Post();
beta2.title = "post1";
beta2.type = "BETA";
beta2.insert();
Post gamma1 = new Post();
gamma1.title = "post1";
gamma1.type = "GAMMA";
gamma1.insert();
Post zeta1 = new Post();
zeta1.title = "post1";
zeta1.type = "ZETA";
zeta1.insert();
assertThat(Post.find().asList().size()).isEqualTo(5);
Query<Post> q = MorphiaPlugin.ds().createQuery(Post.class);
q.or(
q.criteria("type").equal("BETA"),
q.criteria("type").equal("GAMMA")
);
assertThat(q.countAll()).isEqualTo(3);
List<String> criterias = Arrays.asList("BETA", "GAMMA");
assertThat(Post.find().field("type").in(criterias).countAll()).isEqualTo(3);
}
});
}
@Test
public void testInheritenceAndReference() {
System.out.println("testInheritenceAndReference");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
createRole();
Manager manager = new Manager();
manager.username = "marge";
manager.password = "homer";
manager.email = "homer@simplson.com";
manager.role = Role.as(RoleType.GESTIONNAIRE);
manager.insert();
ObjectId managerId = manager.id;
Employee employee = new Employee();
employee.username = "bart";
employee.password = "bart";
employee.email = "bart@simplson.com";
employee.role = Role.as(RoleType.EMPLOYEE);
employee.manager = manager;
employee.insert();
assertThat(Employee.find().countAll()).isEqualTo(2);
Manager testManager = Manager.find().byId(managerId);
testManager.username = "homer";
testManager.update();
assertThat(Employee.find().byId(employee.id).manager.id).isEqualTo(managerId);
assertThat(Employee.find().byId(employee.id).manager.username).isEqualTo(Manager.find().byId(managerId).username);
assertThat(Employee.find().byId(employee.id).manager.username).isNotEqualTo(manager.username);
assertThat(User.find().field("username").equal("bart").countAll()).isEqualTo(1);
}
private void createRole() {
for (RoleType roleType : RoleType.values()) {
Role role = new Role();
role.roleType = roleType;
role.insert();
}
assertThat(Role.find().countAll()).isEqualTo(RoleType.values().length);
}
});
}
@Test
public void testEmbeddedObject() {
System.out.println("testEmbeddedObject");
running(fakeApplication(TestConfig.getInstance().config()), new Runnable() {
@Override
public void run() {
Employee employee = createEmployee();
createMissions();
Period period = new Period();
period.start = new LocalTime(4, 0);
period.end = new LocalTime(7, 0);
period.mission = Mission.byCode("M0");
// Morning
HalfDay morning = new HalfDay();
morning.addPeriod(period);
// Afternoon
HalfDay afternoon = new HalfDay();
afternoon.mission = Mission.byCode("M1");
Day day = new Day();
day.date = new DateTime(2012, DateTimeConstants.MAY, 3, 0, 0);
day.morning = morning;
day.afternoon = afternoon;
Cra cra = new Cra();
cra.year = 2012;
cra.month = new Month(DateTimeConstants.MAY, "mai");
cra.employee = employee;
cra.days.add(day);
cra.insert();
assertThat(Cra.find().field("year").equal(2012).countAll()).isEqualTo(1);
Cra testCra = Cra.find().field("year").equal(2012).get();
assertThat(testCra.days.size()).isEqualTo(1);
assertThat(testCra.days.get(0).afternoon.isSpecial()).isEqualTo(Boolean.FALSE);
assertThat(testCra.days.get(0).morning.isSpecial()).isEqualTo(Boolean.TRUE);
}
private void createMissions() {
for (int i = 0; i < 5; i++) {
Mission m = new Mission();
m.code = "M" + i;
m.name = "Mission " + i;
m.insert();
}
assertThat(Mission.find().countAll()).isEqualTo(5);
}
private Employee createEmployee() {
Employee employee = new Employee();
employee.username = "bart";
employee.password = "bart";
employee.email = "bart@simplson.com";
employee.role = Role.as(RoleType.EMPLOYEE);
employee.insert();
return employee;
}
});
}
}