Skip to content

Commit

Permalink
Implement AFTER and GREATER_THAN keyword #144 (#164)
Browse files Browse the repository at this point in the history
* Implement AFTER and GREATER_THAN keyword

* Refactor AFTER and GREATER_THAN impl

* Fix memo IT failure
  • Loading branch information
miaosakurai committed Aug 20, 2018
1 parent 1d75aa5 commit 8b08168
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ private String generateQueryBody(@NonNull Criteria criteria, @NonNull List<Pair<
return "";
case IS_EQUAL:
case BEFORE:
case AFTER:
case GREATER_THAN:
return this.generateUnaryQuery(criteria, parameters);
case AND:
case OR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public enum CriteriaType {
IS_EQUAL("="),
OR("OR"),
AND("AND"),
BEFORE("<");
BEFORE("<"),
AFTER(">"),
GREATER_THAN(">");

@Getter
private String sqlKeyword;
Expand All @@ -34,6 +36,8 @@ public enum CriteriaType {

map.put(Part.Type.SIMPLE_PROPERTY, CriteriaType.IS_EQUAL);
map.put(Part.Type.BEFORE, CriteriaType.BEFORE);
map.put(Part.Type.AFTER, CriteriaType.AFTER);
map.put(Part.Type.GREATER_THAN, CriteriaType.GREATER_THAN);

PART_TREE_TYPE_TO_CRITERIA = Collections.unmodifiableMap(map);
}
Expand Down Expand Up @@ -94,6 +98,8 @@ public static boolean isUnary(CriteriaType type) {
switch (type) {
case IS_EQUAL:
case BEFORE:
case AFTER:
case GREATER_THAN:
return true;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,46 @@ public void testFindByBeforeWithAndOr() {
Assert.assertEquals(memos.size(), reference.size());
Assert.assertEquals(memos, reference);
}

@Test
public void testFindByAfter() {
List<Memo> memos = this.repository.findByDateAfter(memoDateAfter);

Assert.assertTrue(memos.isEmpty());

memos = this.repository.findByDateAfter(memoDate);

Assert.assertEquals(memos.size(), 1);
Assert.assertEquals(memos.get(0), testMemo3);

memos = this.repository.findByDateAfter(memoDateBefore);
final List<Memo> reference = Arrays.asList(testMemo2, testMemo3);

memos.sort(Comparator.comparing(Memo::getId));
reference.sort(Comparator.comparing(Memo::getId));

Assert.assertEquals(memos.size(), reference.size());
Assert.assertEquals(memos, reference);
}

@Test
public void testFindByAfterWithAndOr() {
List<Memo> memos = this.repository.findByDateAfterAndMessage(memoDate, TestConstants.MESSAGE);

Assert.assertTrue(memos.isEmpty());

memos = this.repository.findByDateAfterAndMessage(memoDate, TestConstants.NEW_MESSAGE);

Assert.assertEquals(memos.size(), 1);
Assert.assertEquals(memos.get(0), testMemo3);

memos = this.repository.findByDateAfterOrMessage(memoDateBefore, TestConstants.MESSAGE);
final List<Memo> reference = Arrays.asList(testMemo1, testMemo2, testMemo3);

memos.sort(Comparator.comparing(Memo::getId));
reference.sort(Comparator.comparing(Memo::getId));

Assert.assertEquals(memos.size(), reference.size());
Assert.assertEquals(memos, reference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,40 @@ public void testFindByWithOrAndOr() {

Assert.assertEquals(projects.size(), 0);
}

@Test
public void testFindByGreaterThan() {
List<Project> projects = repository.findByForkCountGreaterThan(FORK_COUNT_1);
final List<Project> reference = Arrays.asList(PROJECT_2, PROJECT_3);

projects.sort(Comparator.comparing(Project::getId));
reference.sort(Comparator.comparing(Project::getId));

Assert.assertEquals(projects.size(), reference.size());
Assert.assertEquals(projects, reference);

projects = repository.findByForkCountGreaterThan(FAKE_COUNT);

Assert.assertEquals(projects.size(), 0);
}

@Test
public void testFindByGreaterThanWithAndOr() {
List<Project> projects = repository.findByCreatorAndForkCountGreaterThan(CREATOR_2, FORK_COUNT_1);

Assert.assertEquals(projects.get(0), PROJECT_2);

projects = repository.findByCreatorAndForkCountGreaterThan(CREATOR_0, FORK_COUNT_1);

Assert.assertEquals(projects.size(), 0);

projects = repository.findByCreatorOrForkCountGreaterThan(CREATOR_0, FORK_COUNT_2);
final List<Project> reference = Arrays.asList(PROJECT_0, PROJECT_3, PROJECT_4);

projects.sort(Comparator.comparing(Project::getId));
reference.sort(Comparator.comparing(Project::getId));

Assert.assertEquals(projects.size(), reference.size());
Assert.assertEquals(projects, reference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public interface MemoRepository extends DocumentDbRepository<Memo, String> {
List<Memo> findByDateBeforeAndMessage(Date date, String message);

List<Memo> findByDateBeforeOrMessage(Date date, String message);

List<Memo> findByDateAfter(Date date);

List<Memo> findByDateAfterAndMessage(Date date, String message);

List<Memo> findByDateAfterOrMessage(Date date, String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public interface ProjectRepository extends DocumentDbRepository<Project, String>

List<Project> findByNameOrCreatorAndForkCountOrStarCount(String name, String creator,
Long forkCount, Long starCount);

List<Project> findByForkCountGreaterThan(Long forkCount);

List<Project> findByCreatorAndForkCountGreaterThan(String creator, Long forkCount);

List<Project> findByCreatorOrForkCountGreaterThan(String creator, Long forkCount);

List<Project> findByNameOrCreator(String name, String creator, Sort sort);

List<Project> findByNameAndCreator(String name, String creator, Sort sort);
Expand Down

0 comments on commit 8b08168

Please sign in to comment.