Skip to content

Commit

Permalink
test: add tests for numeric data types in repository queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Oct 8, 2024
1 parent da4ce7c commit a53c8ed
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.redis.om.spring.annotations.hash;

import com.redis.om.spring.AbstractBaseEnhancedRedisTest;
import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics;
import com.redis.om.spring.fixtures.hash.repository.HashWithAllTheNumericsRepository;
import com.redis.om.spring.search.stream.EntityStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("SpellCheckingInspection")
class HashWithAllTheNumericsMappingTest extends AbstractBaseEnhancedRedisTest {

@Autowired
HashWithAllTheNumericsRepository repo;

@Autowired
EntityStream es;

@BeforeEach
void createTestDataIfNeeded() {
HashWithAllTheNumerics hwatn1 = HashWithAllTheNumerics.of("hash1", 1.0f, 1.0, BigInteger.valueOf(1),
BigDecimal.valueOf(1.0));
HashWithAllTheNumerics hwatn2 = HashWithAllTheNumerics.of("hash2", 2.0f, 2.0, BigInteger.valueOf(2),
BigDecimal.valueOf(2.0));
HashWithAllTheNumerics hwatn3 = HashWithAllTheNumerics.of("hash3", 3.0f, 3.0, BigInteger.valueOf(3),
BigDecimal.valueOf(3.0));
HashWithAllTheNumerics hwatn4 = HashWithAllTheNumerics.of("hash4", 4.0f, 4.0, BigInteger.valueOf(4),
BigDecimal.valueOf(4.0));
HashWithAllTheNumerics hwatn5 = HashWithAllTheNumerics.of("hash5", 5.0f, 5.0, BigInteger.valueOf(5),
BigDecimal.valueOf(5.0));
HashWithAllTheNumerics hwatn6 = HashWithAllTheNumerics.of("hash6", 6.0f, 6.0, BigInteger.valueOf(6),
BigDecimal.valueOf(6.0));
repo.saveAll(Set.of(hwatn1, hwatn2, hwatn3, hwatn4, hwatn5, hwatn6));
}

@Test
void testValuesAreStoredCorrectly() {
for (int i = 1; i <= 6; i++) {
String id = "hash" + i;
Optional<HashWithAllTheNumerics> hwatn = repo.findById(id);

assertThat(hwatn).isPresent();

HashWithAllTheNumerics entity = hwatn.get();
assertThat(entity.getId()).isEqualTo(id);
assertThat(entity.getAfloat()).isEqualTo((float) i);
assertThat(entity.getAdouble()).isEqualTo((double) i);
assertThat(entity.getAbigInteger()).isEqualTo(BigInteger.valueOf(i));
assertThat(entity.getAbigDecimal()).isEqualTo(new BigDecimal(i + ".0"));
}
}

@Test
void testFindByAFloatBetween() {
Iterable<HashWithAllTheNumerics> result = repo.findByAfloatBetween(2.5f, 4.5f);
assertThat(result).hasSize(2);
assertThat(result).extracting(HashWithAllTheNumerics::getAfloat)
.containsExactlyInAnyOrder(3.0f, 4.0f);
}

@Test
void testFindByADoubleBetween() {
Iterable<HashWithAllTheNumerics> result = repo.findByAdoubleBetween(3.5, 5.5);

assertThat(result).hasSize(2);
assertThat(result).extracting(HashWithAllTheNumerics::getAdouble)
.containsExactlyInAnyOrder(4.0, 5.0);
}

@Test
void testFindByABigDecimalBetween() {
Iterable<HashWithAllTheNumerics> result = repo.findByAbigDecimalBetween(
new BigDecimal("1.5"), new BigDecimal("3.5"));

assertThat(result).hasSize(2);
assertThat(result).extracting(HashWithAllTheNumerics::getAbigDecimal)
.containsExactlyInAnyOrder(new BigDecimal("2.0"), new BigDecimal("3.0"));
}

@Test
void testFindByABigIntegerBetween() {
Iterable<HashWithAllTheNumerics> result = repo.findByAbigIntegerBetween(
BigInteger.valueOf(4), BigInteger.valueOf(6));

assertThat(result).hasSize(3);
assertThat(result).extracting(HashWithAllTheNumerics::getAbigInteger)
.containsExactlyInAnyOrder(BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.redis.om.spring.fixtures.hash.model;

import com.redis.om.spring.annotations.Indexed;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

import java.math.BigDecimal;
import java.math.BigInteger;

@Data
@NoArgsConstructor(force = true)
@RequiredArgsConstructor(staticName = "of")
@RedisHash
public class HashWithAllTheNumerics {
@Id
@NonNull
private String id;

@NonNull
@Indexed
private Float afloat;

@NonNull
@Indexed
private Double adouble;

@NonNull
@Indexed
private BigInteger abigInteger;

@NonNull
@Indexed
private BigDecimal abigDecimal;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.redis.om.spring.fixtures.hash.repository;

import com.redis.om.spring.fixtures.hash.model.HashWithAllTheNumerics;
import com.redis.om.spring.repository.RedisEnhancedRepository;

import java.math.BigDecimal;
import java.math.BigInteger;

public interface HashWithAllTheNumericsRepository extends RedisEnhancedRepository<HashWithAllTheNumerics, String> {
Iterable<HashWithAllTheNumerics> findByAfloatBetween(Float low, Float high);
Iterable<HashWithAllTheNumerics> findByAdoubleBetween(Double low, Double high);
Iterable<HashWithAllTheNumerics> findByAbigDecimalBetween(BigDecimal low, BigDecimal high);
Iterable<HashWithAllTheNumerics> findByAbigIntegerBetween(BigInteger low, BigInteger high);
}



0 comments on commit a53c8ed

Please sign in to comment.