Skip to content

Commit

Permalink
added tests for Pattern (regexp) and or()
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Sep 16, 2017
1 parent 6a64e0d commit 917a526
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Expand Up @@ -11,7 +11,7 @@
@Mongo.Repository
@Value.Immutable
@Gson.TypeAdapters
public interface Person {
interface Person {

@Mongo.Id
String id();
Expand Down
@@ -1,16 +1,19 @@
package org.immutables.mongo.fixture.criteria;

import com.google.common.collect.Range;
import org.immutables.mongo.fixture.MongoContext;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.regex.Pattern;

import static org.immutables.check.Checkers.check;

public class CriteriaTest {
public class PersonCriteriaTest {
@Rule
public final MongoContext context = MongoContext.create();

Expand Down Expand Up @@ -79,6 +82,7 @@ public void age() throws Exception {
public void failingTest_to_be_checked() throws Exception {
Person john = ImmutablePerson.builder().id("p1").name("John").age(30).build();
repository.insert(john).getUnchecked();
check(repository.find(repository.criteria().age(30)).fetchAll().getUnchecked()).hasSize(1);
check(repository.find(repository.criteria().ageNot(30)).fetchAll().getUnchecked()).isEmpty();
}

Expand All @@ -97,6 +101,13 @@ public void stringPattern() throws Exception {
check(repository.find(repository.criteria().nameIn("John", "John")).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().nameNotIn("John", "John")).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().nameNotIn("J1", "J2")).fetchAll().getUnchecked()).hasContentInAnyOrder(john);

// patterns
check(repository.find(repository.criteria().nameMatches(Pattern.compile("J.*n"))).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().nameMatches(Pattern.compile("J\\w+n"))).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().nameMatches(Pattern.compile("J..n"))).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().nameMatches(Pattern.compile(".*"))).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().nameNotMatches(Pattern.compile("J.*n"))).fetchAll().getUnchecked()).isEmpty();
}

@Test
Expand Down Expand Up @@ -129,8 +140,63 @@ public void subCollection() throws Exception {
check(repository.find(repository.criteria().aliasesSize(4)).fetchAll().getUnchecked()).isEmpty();
}

@Test
public void negation() throws Exception {
Date dob = new Date();
Person john = ImmutablePerson.builder().id("p1").name("John").age(30).aliases(Collections.singleton("a1"))
.dateOfBirth(dob).build();
repository.insert(john).getUnchecked();


// id
check(repository.find(repository.criteria().idNot(john.id())).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().idNot("__BAD__")).fetchAll().getUnchecked()).hasSize(1);
check(repository.find(repository.criteria().idNotIn(john.id(), "aaa")).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().idNotIn(Collections.singleton(john.id()))).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().idNotIn(Collections.singleton("__BAD__"))).fetchAll().getUnchecked()).hasSize(1);

// name
check(repository.find(repository.criteria().nameNot(john.name())).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().nameNotMatches(Pattern.compile("J..n"))).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().nameNotMatches(Pattern.compile("A...B"))).fetchAll().getUnchecked()).hasSize(1);

// age this seems to be a bug (?)
// check(repository.find(repository.criteria().ageNot(john.age())).fetchAll().getUnchecked()).isEmpty();

check(repository.find(repository.criteria().ageNotIn(john.age(), john.age())).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().ageNotIn(Collections.singleton(john.age()))).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().ageNotIn(Range.open(john.age() + 1, john.age() + 2))).fetchAll().getUnchecked()).hasSize(1);

check(repository.find(repository.criteria().aliasesNonEmpty()).fetchAll().getUnchecked()).hasSize(1);

check(repository.find(repository.criteria().dateOfBirthNot(dob)).fetchAll().getUnchecked()).isEmpty();

}

@Test
public void or() throws Exception {
Person john = ImmutablePerson.builder().id("p1").name("John").age(30).build();
Person adam = ImmutablePerson.builder().id("a1").name("Adam").age(44).build();

repository.insert(john).getUnchecked();
repository.insert(adam).getUnchecked();

check(repository.find(repository.criteria().age(30).or().age(44)).fetchAll().getUnchecked()).hasContentInAnyOrder(john, adam);
check(repository.find(repository.criteria().age(1).or().age(2)).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().age(30).or().age(2)).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().age(1).or().age(44)).fetchAll().getUnchecked()).hasContentInAnyOrder(adam);
check(repository.find(repository.criteria().age(30).or().name("Adam")).fetchAll().getUnchecked()).hasContentInAnyOrder(john, adam);
check(repository.find(repository.criteria().name("Adam").or().age(30)).fetchAll().getUnchecked()).hasContentInAnyOrder(john, adam);
check(repository.find(repository.criteria().name("Adam").or().name("Adam").or().name("Adam")).fetchAll().getUnchecked()).hasContentInAnyOrder(adam);
check(repository.find(repository.criteria().name("a").or().name("b").or().name("c")).fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria().id("p1").or().name("John").or().age(30)).fetchAll().getUnchecked()).hasContentInAnyOrder(john);
check(repository.find(repository.criteria().id("p1").or().idNot("p1")).fetchAll().getUnchecked()).hasContentInAnyOrder(john, adam);

}

@Test
public void empty() throws Exception {
check(repository.findAll().fetchAll().getUnchecked()).isEmpty();
check(repository.find(repository.criteria()).fetchAll().getUnchecked()).isEmpty();
}
}

0 comments on commit 917a526

Please sign in to comment.