use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.
the class MapReduceOptionsTest method mapReduceCommand.
@Test
@SuppressWarnings("deprecation")
public void mapReduceCommand() {
Query<FacebookUser> query = getDs().find(FacebookUser.class);
MapReduceOptions<FacebookUser> options = new MapReduceOptions<FacebookUser>().bypassDocumentValidation(true).collation(Collation.builder().locale("en").build()).finalize("i'm a finalize function").jsMode(true).limit(42).map("i'm a map function").maxTimeMS(42000).outputCollection("output collection").outputDB("output db").outputType(OutputType.INLINE).query(query).readPreference(ReadPreference.primaryPreferred()).reduce("i'm a reduce function").scope(new Document("key", "value").append("key2", "value2")).verbose(true);
MapReduceCommand command = options.toCommand(getMorphia().getMapper());
assertTrue(command.getBypassDocumentValidation());
assertEquals(Collation.builder().locale("en").build(), command.getCollation());
assertTrue(command.getJsMode());
assertEquals(42, command.getLimit());
assertEquals("i'm a map function", command.getMap());
assertEquals(42000, command.getMaxTime(TimeUnit.MILLISECONDS));
assertEquals("output collection", command.getOutputTarget());
assertEquals("output db", command.getOutputDB());
assertEquals(query.getQueryObject(), command.getQuery());
assertEquals(query.getSortObject(), command.getSort());
assertEquals(ReadPreference.primaryPreferred(), command.getReadPreference());
assertEquals("i'm a map function", command.getMap());
assertEquals("i'm a reduce function", command.getReduce());
assertEquals("i'm a finalize function", command.getFinalize());
assertEquals(new Document("key", "value").append("key2", "value2"), command.getScope());
assertTrue(command.isVerbose());
}
use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.
the class TestQuery method testKeyListLookups.
@Test
public void testKeyListLookups() {
final FacebookUser fbUser1 = new FacebookUser(1, "scott");
final FacebookUser fbUser2 = new FacebookUser(2, "tom");
final FacebookUser fbUser3 = new FacebookUser(3, "oli");
final FacebookUser fbUser4 = new FacebookUser(4, "frank");
final Iterable<Key<FacebookUser>> fbKeys = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
assertEquals(1, fbUser1.getId());
final List<Key<FacebookUser>> fbUserKeys = new ArrayList<Key<FacebookUser>>();
for (final Key<FacebookUser> key : fbKeys) {
fbUserKeys.add(key);
}
assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
final KeysKeysKeys k1 = new KeysKeysKeys(null, fbUserKeys);
final Key<KeysKeysKeys> k1Key = getDs().save(k1);
assertEquals(k1.getId(), k1Key.getId());
final KeysKeysKeys k1Reloaded = getDs().get(k1);
final KeysKeysKeys k1Loaded = getDs().getByKey(KeysKeysKeys.class, k1Key);
assertNotNull(k1Reloaded);
assertNotNull(k1Loaded);
for (final Key<FacebookUser> key : k1Loaded.getUsers()) {
assertNotNull(key.getId());
}
assertEquals(4, k1Loaded.getUsers().size());
final List<FacebookUser> fbUsers = getDs().getByKeys(FacebookUser.class, k1Loaded.getUsers());
assertEquals(4, fbUsers.size());
for (final FacebookUser fbUser : fbUsers) {
assertNotNull(fbUser);
assertNotNull(fbUser.getId());
assertNotNull(fbUser.getUsername());
}
}
use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.
the class TestQuery method testKeyList.
@Test
public void testKeyList() {
final Rectangle rect = new Rectangle(1000, 1);
final Key<Rectangle> rectKey = getDs().save(rect);
assertEquals(rectKey.getId(), rect.getId());
final FacebookUser fbUser1 = new FacebookUser(1, "scott");
final FacebookUser fbUser2 = new FacebookUser(2, "tom");
final FacebookUser fbUser3 = new FacebookUser(3, "oli");
final FacebookUser fbUser4 = new FacebookUser(4, "frank");
final Iterable<Key<FacebookUser>> fbKeys = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
assertEquals(1, fbUser1.getId());
final List<Key<FacebookUser>> fbUserKeys = new ArrayList<Key<FacebookUser>>();
for (final Key<FacebookUser> key : fbKeys) {
fbUserKeys.add(key);
}
assertEquals(fbUser1.getId(), fbUserKeys.get(0).getId());
assertEquals(fbUser2.getId(), fbUserKeys.get(1).getId());
assertEquals(fbUser3.getId(), fbUserKeys.get(2).getId());
assertEquals(fbUser4.getId(), fbUserKeys.get(3).getId());
final KeysKeysKeys k1 = new KeysKeysKeys(rectKey, fbUserKeys);
final Key<KeysKeysKeys> k1Key = getDs().save(k1);
assertEquals(k1.getId(), k1Key.getId());
final KeysKeysKeys k1Loaded = getDs().get(k1);
for (final Key<FacebookUser> key : k1Loaded.getUsers()) {
assertNotNull(key.getId());
}
assertNotNull(k1Loaded.getRect().getId());
}
use of org.mongodb.morphia.TestDatastore.FacebookUser in project morphia by mongodb.
the class TestQuery method testGetByKeysHetero.
@Test
public void testGetByKeysHetero() {
final Iterable<Key<Object>> keys = getDs().save(asList(new FacebookUser(1, "scott"), new Rectangle(1, 1)));
final List<Object> entities = getDs().getByKeys(keys);
assertNotNull(entities);
assertEquals(2, entities.size());
int userCount = 0;
int rectCount = 0;
for (final Object o : entities) {
if (o instanceof Rectangle) {
rectCount++;
} else if (o instanceof FacebookUser) {
userCount++;
}
}
assertEquals(1, rectCount);
assertEquals(1, userCount);
}
Aggregations