Skip to content

Commit

Permalink
add tests nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Nov 11, 2024
1 parent 772acad commit b5cba0d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ public interface UserRepository extends JpaRepository<User, Integer> {

List<User> findByNameIsNotNull();

//----------------------------------------------------------------------------------
List<User> findByNameOrderByNameAsc(String name);

//-------------------nested fields--------------------------------
List<User> findAllByAddressZipCode(String zipCode);

List<User> findAllByAddressCountryIsoCode(String isoCode);

//
// List<User> findAllByAddressZipCode(String zipCode);
//
// List<User> findAllByAddressCountry(String country);
List<User> findAllByAddressCountry(String country);

//
// List<User> findAllByAddress_Country(String country);
List<User> findAllByAddress_Country(String country);

//
// List<User> findAllByAddress_CountryIsoCode(String isoCode);
List<User> findAllByAddress_CountryIsoCode(String isoCode);

//
// List<User> findAllByAddress_Country_IsoCode(String isoCode);
List<User> findAllByAddress_Country_IsoCode(String isoCode);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.quarkus.spring.data.deployment.nested.fields;

import static io.quarkus.spring.data.deployment.nested.fields.User.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;

import jakarta.transaction.Transactional;

import org.hibernate.query.QueryArgumentException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -36,34 +39,6 @@ class UserRepositoryDerivedMethodsTest {
@Autowired
private UserRepository userRepository;

@BeforeEach
@Transactional
public void setUp() {

// Country spain = new Country("Spain", "SP");
// Country france = new Country("France", "FR");
// Country unitedStates = new Country("United States", "US");
// Country germany = new Country("Germany", "DE");
//
// Address spAddress = new Address("28004", spain);
// Address frAddress = new Address("75001", france);
// Address usAddress = new Address("10001", unitedStates);
// Address deAddress = new Address("90877", germany);
//
// User spanishAdam = new User(USER_NAME_ADAM, 25, BIRTHDATE, true, spAddress);
// User frenchAdam = new User(USER_NAME_ADAM, 20, BIRTHDATE, false, frAddress);
// User eve = new User(USER_NAME_EVE, 20, BIRTHDATE, true, usAddress);
// User nameless = new User(null, 30, BIRTHDATE, false, spAddress);
// User tim = new User(USER_NAME_TIM, 38, BIRTHDATE, false, deAddress);
//
// userRepository.saveAll(Arrays.asList(spanishAdam, frenchAdam, eve, nameless));
}

@AfterEach
public void tearDown() {

// userRepository.deleteAll();
}

@Test
@Transactional
Expand All @@ -77,14 +52,11 @@ void whenFindByNameThenReturnsCorrectResult() {

assertEquals(2, userRepository.findByNameEquals(USER_NAME_ADAM)
.size());
}

@Test
@Transactional
void whenFindByNameIsNull_thenReturnsCorrectResult() {

assertEquals(1, userRepository.findByNameIsNull()
.size());
assertEquals(5, userRepository.findByNameIsNotNull()
.size());
}

@Test
Expand Down Expand Up @@ -241,5 +213,45 @@ void whenByNameOrderByName() {

assertEquals(2, userRepository.findByNameOrderByName(USER_NAME_ADAM)
.size());
assertEquals(2, userRepository.findByNameOrderByNameDesc(USER_NAME_ADAM)
.size());
assertEquals(2, userRepository.findByNameOrderByNameAsc(USER_NAME_ADAM)
.size());
}

//-------------------nested fields--------------------------------
@Test
@Transactional
void findByAddressZipCode() {

assertEquals(2, userRepository.findAllByAddressZipCode("28004")
.size());
}

@Test
@Transactional
void findByAddressCountryIsoCode() {

assertEquals(2, userRepository.findAllByAddressCountryIsoCode("ES")
.size());

assertEquals(2, userRepository.findAllByAddress_CountryIsoCode("ES")
.size());

assertEquals(2, userRepository.findAllByAddress_Country_IsoCode("ES")
.size());
}

@Test
@Transactional
void findByAddressCountry() {

QueryArgumentException exception = assertThrows(QueryArgumentException.class,
() -> userRepository.findAllByAddressCountry("Spain"));
assertThat(exception).hasMessageContaining("Argument [Spain] of type [java.lang.String] did not match parameter type");
assertThrows(QueryArgumentException.class,
() -> userRepository.findAllByAddress_Country("Spain"));
assertThat(exception).hasMessageContaining("Argument [Spain] of type [java.lang.String] did not match parameter type");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/users-db
jdbc.user=person
jdbc.pass=person
jdbc.user=sa
jdbc.pass=sa

quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.database.generation=drop-and-create

0 comments on commit b5cba0d

Please sign in to comment.