Added iterate test

This commit is contained in:
Lassi Immonen 2010-09-22 05:44:23 +00:00
parent e27ace0f2d
commit e993b80737
4 changed files with 51 additions and 5 deletions

View File

@ -138,8 +138,7 @@ public class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>,
@Override
public List<K> listDistinct() {
queryMixin.setDistinct(true);
return list();
throw new UnsupportedOperationException();
}
@Override
@ -164,8 +163,7 @@ public class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>,
@Override
public long countDistinct() {
queryMixin.setDistinct(true);
return count();
throw new UnsupportedOperationException();
}
private DBObject createQuery() {

View File

@ -8,6 +8,7 @@ package com.mysema.query.mongodb;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import java.util.Iterator;
import java.util.List;
import org.junit.Before;
@ -88,6 +89,36 @@ public class MongodbQueryTest {
assertQuery(user.firstName.matches("Jaakko").not(), u3, u4, u2);
}
//This is not supported yet
// @Test
// public void testUniqueResult() {
//
// addUser("Dille", "Duplikaatti");
// addUser("Dille", "Duplikaatti");
//
// assertEquals(2, where(user.firstName.eq("Dille")).count());
// assertEquals(1, where(user.firstName.eq("Dille")).countDistinct());
//
// }
@Test
public void testIterate() {
User a = addUser("A", "A");
User b = addUser("A1", "B");
User c = addUser("A2", "C");
Iterator<User> i = where(user.firstName.startsWith("A"))
.orderBy(user.firstName.asc())
.iterate();
assertEquals(a, i.next());
assertEquals(b, i.next());
assertEquals(c, i.next());
assertEquals(false, i.hasNext());
}
private void assertQuery(Predicate e, User ... expected) {
assertQuery(where(e).orderBy(user.lastName.asc(), user.firstName.asc()), expected );
}
@ -102,7 +133,7 @@ public class MongodbQueryTest {
}
private void assertQuery(MongodbQuery<User> query, User ... expected ) {
System.out.println(query.toString());
//System.out.println(query.toString());
List<User> results = query.list();
assertNotNull(results);

View File

@ -5,8 +5,12 @@
*/
package com.mysema.query.mongodb.domain;
import java.util.Date;
import com.mysema.query.types.PathMetadataFactory;
import com.mysema.query.types.path.DatePath;
import com.mysema.query.types.path.EntityPathBase;
import com.mysema.query.types.path.NumberPath;
import com.mysema.query.types.path.StringPath;
public class QUser extends EntityPathBase<User> {
@ -21,4 +25,6 @@ public class QUser extends EntityPathBase<User> {
public final StringPath firstName = createString("firstName");
public final StringPath lastName = createString("lastName");
public final DatePath<Date> created = createDate("date", Date.class);
public final NumberPath<Integer> age = createNumber("age", Integer.class);
}

View File

@ -5,6 +5,8 @@
*/
package com.mysema.query.mongodb.domain;
import java.util.Date;
import org.bson.types.ObjectId;
import com.google.code.morphia.annotations.Entity;
@ -18,11 +20,20 @@ public class User {
String firstName;
String lastName;
Date created;
int age;
public User() {
}
public User(String firstName, String lastName) {
this.firstName = firstName; this.lastName = lastName;
this.created = new Date();
}
public User(String firstName, String lastName, int age, Date created) {
this.firstName = firstName; this.lastName = lastName; this.age = age; this.created = created;
}
@Override