|
22 | 22 | import com.google.common.base.Objects; |
23 | 23 | import com.google.common.util.concurrent.ListenableFuture; |
24 | 24 | import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
25 | 27 | import static org.testng.Assert.*; |
26 | 28 |
|
27 | 29 | import com.datastax.driver.mapping.MapperTest.User.Gender; |
@@ -377,4 +379,40 @@ public void testDynamicEntity() throws Exception { |
377 | 379 | userAccessor.updateNameAndGender("Paule", null, u1.getUserId()); |
378 | 380 | assertEquals(userMapper.get(u1.getUserId()).getGender(), null); |
379 | 381 | } |
| 382 | + |
| 383 | + @Test(groups="short") |
| 384 | + public void should_map_objects_from_partial_queries() throws Exception { |
| 385 | + MappingManager manager = new MappingManager(session); |
| 386 | + |
| 387 | + Mapper<Post> m = manager.mapper(Post.class); |
| 388 | + |
| 389 | + // Insert a few posts |
| 390 | + User u1 = new User("Paul", "paul@gmail.com", User.Gender.MALE); |
| 391 | + Post p1 = new Post(u1, "Something about mapping"); |
| 392 | + Post p2 = new Post(u1, "Something else"); |
| 393 | + Post p3 = new Post(u1, "Something more"); |
| 394 | + |
| 395 | + p1.setDevice(InetAddress.getLocalHost()); |
| 396 | + p2.setTags(new HashSet<String>(Arrays.asList("important", "keeper"))); |
| 397 | + |
| 398 | + m.save(p1); |
| 399 | + m.save(p2); |
| 400 | + m.save(p3); |
| 401 | + |
| 402 | + // Retrieve posts with a projection query that only retrieves some of the fields |
| 403 | + ResultSet rs = session.execute("select user_id, post_id, title from posts where user_id = ?", u1.getUserId()); |
| 404 | + |
| 405 | + Result<Post> result = m.map(rs); |
| 406 | + for (Post post : result) { |
| 407 | + assertThat(post.getUserId()).isEqualTo(u1.getUserId()); |
| 408 | + assertThat(post.getPostId()).isNotNull(); |
| 409 | + assertThat(post.getTitle()).isNotNull(); |
| 410 | + |
| 411 | + assertThat(post.getDevice()).isNull(); |
| 412 | + assertThat(post.getTags()).isNull(); |
| 413 | + } |
| 414 | + |
| 415 | + // cleanup |
| 416 | + session.execute("delete from posts where user_id = ?", u1.getUserId()); |
| 417 | + } |
380 | 418 | } |
0 commit comments