-
Notifications
You must be signed in to change notification settings - Fork 199
/
BuiltinTypeMappingTest.java
157 lines (133 loc) · 3.39 KB
/
BuiltinTypeMappingTest.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
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.datastore.mongodb.test.mapping;
import static org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.assertDbObject;
import org.hibernate.Transaction;
import org.hibernate.ogm.OgmSession;
import org.hibernate.ogm.backendtck.type.Bookmark;
import org.hibernate.ogm.utils.OgmTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for the mappings of built-in types into MongoDB.
*
* @author Gunnar Morling
*/
public class BuiltinTypeMappingTest extends OgmTestCase {
private String bookmarkId;
@Before
public void setUpTestData() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
Bookmark bookmark = new Bookmark();
bookmark.setFavourite( Boolean.TRUE );
bookmark.setPrivate( true );
bookmark.setRead( true );
bookmark.setShared( true );
session.persist( bookmark );
transaction.commit();
session.close();
this.bookmarkId = bookmark.getId();
}
@After
public void removeTestData() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
session.delete( session.get( Bookmark.class, bookmarkId ) );
transaction.commit();
session.close();
}
@Test
public void booleanMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'favourite' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'favourite' : true" +
"}"
);
transaction.commit();
session.close();
}
@Test
public void trueFalseTypeMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'isPrivate' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'isPrivate' : 'T'" +
"}"
);
transaction.commit();
session.close();
}
@Test
public void yesNoTypeMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'isRead' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'isRead' : 'Y'" +
"}"
);
transaction.commit();
session.close();
}
@Test
public void numericBooleanMapping() {
OgmSession session = openSession();
Transaction transaction = session.beginTransaction();
assertDbObject(
session.getSessionFactory(),
// collection
"Bookmark",
// query
"{ '_id' : '" + bookmarkId + "' }",
// fields
"{ 'isShared' : '1' }",
// expected
"{ " +
"'_id' : '" + bookmarkId + "', " +
"'isShared' : 1" +
"}"
);
transaction.commit();
session.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Bookmark.class };
}
}