|
19 | 19 | import com.activeandroid.Cache;
|
20 | 20 | import com.activeandroid.Model;
|
21 | 21 | import com.activeandroid.TableInfo;
|
| 22 | +import com.activeandroid.annotation.Column; |
22 | 23 | import com.activeandroid.annotation.Table;
|
23 | 24 | import com.activeandroid.query.Select;
|
24 | 25 |
|
25 | 26 | import java.lang.reflect.Field;
|
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Date; |
26 | 29 | import java.util.HashSet;
|
| 30 | +import java.util.List; |
27 | 31 | import java.util.Set;
|
28 | 32 |
|
29 | 33 | /**
|
@@ -167,11 +171,97 @@ public void testBooleanColumnType() {
|
167 | 171 | assertNull( new Select().from(MockModel.class).where("booleanField = ?", 1).executeSingle() );
|
168 | 172 |
|
169 | 173 | assertNull( new Select().from(MockModel.class).where("booleanField = ?", true).executeSingle() );
|
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Test to check the join of two (or more) tables with some fields in common when not use a projection on select. |
| 178 | + * Test the issue #106 (https://github.com/pardom/ActiveAndroid/issues/106) |
| 179 | + */ |
| 180 | + public void testJoinWithSameNames(){ |
| 181 | + //create a parent entity and store |
| 182 | + ParentJoinMockModel parent = new ParentJoinMockModel(); |
| 183 | + parent.booleanField = true; |
| 184 | + parent.dateField = new Date(); |
| 185 | + parent.doubleField = 2.0; |
| 186 | + parent.intField = 1; |
| 187 | + parent.save(); |
| 188 | + |
| 189 | + //the values to assign to child |
| 190 | + Date dateValue = new Date(); |
| 191 | + double doubleValue = 30.0; |
| 192 | + int intValue = 3; |
| 193 | + |
| 194 | + //create two child entities, relate with parent and save |
| 195 | + ChildMockModel child1 = new ChildMockModel(); |
| 196 | + child1.booleanField = false; |
| 197 | + child1.dateField = dateValue; |
| 198 | + child1.doubleField = doubleValue; |
| 199 | + child1.intField = intValue; |
| 200 | + child1.parent = parent; |
| 201 | + child1.save(); |
| 202 | + |
| 203 | + ChildMockModel child2 = new ChildMockModel(); |
| 204 | + child2.booleanField = false; |
| 205 | + child2.dateField = dateValue; |
| 206 | + child2.doubleField = doubleValue; |
| 207 | + child2.intField = intValue; |
| 208 | + child2.parent = parent; |
| 209 | + child2.save(); |
| 210 | + |
| 211 | + //Store the ids assigned to child entities when persists |
| 212 | + List<Long> ids = new ArrayList<Long>(); |
| 213 | + ids.add(child1.getId()); |
| 214 | + ids.add(child2.getId()); |
| 215 | + |
| 216 | + //make the query with a join |
| 217 | + List<ChildMockModel> result = new Select().from(ChildMockModel.class). |
| 218 | + join(ParentJoinMockModel.class).on("ParentJoinMockModel.Id = ChildMockModel.parent").execute(); |
| 219 | + |
| 220 | + //check result |
| 221 | + assertNotNull(result); |
| 222 | + assertEquals(result.size(), 2); |
| 223 | + for(ChildMockModel currentModel : result){ |
| 224 | + assertFalse(currentModel.booleanField); |
| 225 | + assertEquals(currentModel.intField, intValue); |
| 226 | + assertEquals(currentModel.doubleField, doubleValue); |
| 227 | + assertTrue(ids.contains(currentModel.getId())); |
| 228 | + } |
| 229 | + |
170 | 230 | }
|
171 | 231 |
|
172 | 232 | /**
|
173 | 233 | * Mock model as we need 2 different model classes.
|
174 | 234 | */
|
175 | 235 | @Table(name = "AnotherMockTable")
|
176 | 236 | public static class AnotherMockModel extends Model {}
|
| 237 | + |
| 238 | + /** |
| 239 | + * Mock model to test joins with same names. |
| 240 | + * It's a copy from MockModel. |
| 241 | + */ |
| 242 | + @Table(name = "ParentJoinMockModel") |
| 243 | + public static class ParentJoinMockModel extends Model { |
| 244 | + @Column |
| 245 | + public Date dateField; |
| 246 | + |
| 247 | + @Column |
| 248 | + public double doubleField; |
| 249 | + |
| 250 | + @Column |
| 251 | + public int intField; |
| 252 | + |
| 253 | + @Column |
| 254 | + public boolean booleanField; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Mock model to test joins with same names. |
| 259 | + * Extends from ParentJoinMockModel to have the same columns. |
| 260 | + * Have a relationship with ParentJoinMockModel to make te join query. |
| 261 | + */ |
| 262 | + @Table(name = "ChildMockModel") |
| 263 | + public static class ChildMockModel extends ParentJoinMockModel { |
| 264 | + @Column |
| 265 | + ParentJoinMockModel parent; |
| 266 | + } |
177 | 267 | }
|
0 commit comments