0
votes

Or Condition Query Example

SELECT ID, NAME, CREATE_DATE, UPDATE_DATE 
FROM USER 
WHERE ( CREATE_DATE > SYSDATE-1 OR UPDATE_DATE > SYSDATE -1)

How to assert OR Condition Query with junit?

// this test is fail : createDate is "2022-07-28" || updateDate is null
String oneDaysAgo = "2022-07-27";
List<UserInfo> list = jpaRepository.findAllByCreateDateGreaterThanOrUpdateDateGreaterThan(oneDaysAgo,oneDaysAgo);
for (UserInfo vo : list) {
   assertThat(vo.getCreatedDate(), is(greaterThan(oneDaysAgo)));
   assertThat(vo.getUpdateDate(), is(greaterThan(oneDaysAgo)));
   log.info(vo.toString());
}

Expect assert

( createDate > oneDaysAgo ) or ( updateDate > oneDaysAgo )

OneDaysAgo = "2022-07-27";

Create Date Update Date Expect Result Actual Result
null or lessThan or equal null or lessThan or equal False False
"2022-07-28" null or lessThan or equal True False
null or lessThan or equal "2022-07-28" True False
"2022-07-28" "2022-07-28" True True
1
Maybe its enough to create test data were two entries matches and you will check if these entities are returned. Or write a test for every acceptance criteria. 3rd: Helper method that returns true if one criteria match. Edit: Even better: stackoverflow.com/a/19064484/2683501 - pL4Gu33
@pL4Gu33 thx for comment. i think can't work with stackoverflow.com/a/19064484/2683501 i think helper method only way to solve this problem thx ! - Hulk Choi

1 Answers

0
votes

thx for comment @pL4Gu33

Solve problem with Add Helper Method


  @Test
  void test_orCondition() {
    String oneDaysAgo = "2022-07-27";
    List<UserVo> list = getUserVoList(oneDaysAgo);
    assertThat(list.size(), is(greaterThan(0)));
    for (UserVo vo : list) {
      assertThat(getOrConditionBoolean(vo, oneDaysAgo), is(true));
    }
  }


  private Boolean getOrConditionBoolean(UserVo vo, String dayAgoStr) {
    LocalDate createDate = parseDate(vo.getCreateDate());
    LocalDate updateDate = parseDate(vo.getUpdateDate());
    LocalDate dayAgo = parseDate(dayAgoStr);

    if (createDate == null && updateDate == null) {
      return false;
    }
    if (createDate == null) {
      return !dayAgo.isAfter(updateDate);
    }
    if (updateDate == null) {
      return !dayAgo.isAfter(createDate);
    }
    if(createDate.isAfter(dayAgo) || createDate.isEqual(dayAgo) ){
      return true;
    }
    if(updateDate.isAfter(dayAgo) || updateDate.isEqual(dayAgo) ){
      return true;
    }
    return false;
  }

  private LocalDate parseDate(final String dateAsString) {
    if (StringUtils.isEmpty(dateAsString)) {
      return null;
    }
    return LocalDate.parse(StringUtils.substring(dateAsString, 0, 10));
  }