#26 pharmacy: jpa dirty checking test
This commit is contained in:
@@ -25,4 +25,8 @@ public class Pharmacy {
|
||||
private String pharmacyAddress;
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
|
||||
public void changePharmacyAddress(String address) {
|
||||
this.pharmacyAddress = address;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.road.pharmacy.service;
|
||||
|
||||
import com.example.road.pharmacy.entity.Pharmacy;
|
||||
import com.example.road.pharmacy.repository.PharmacyRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PharmacyRepositoryService {
|
||||
|
||||
private final PharmacyRepository pharmacyRepository;
|
||||
|
||||
@Transactional
|
||||
public void updateAddress(Long id, String address) {
|
||||
Pharmacy entity = pharmacyRepository.findById(id).orElse(null);
|
||||
|
||||
if (Objects.isNull(entity)) {
|
||||
log.info("[PharmacyRepositoryService updateAddress] not found id: {}", id);
|
||||
return;
|
||||
}
|
||||
|
||||
entity.changePharmacyAddress(address);
|
||||
}
|
||||
|
||||
public void updateAddressWithoutTransactional(Long id, String address) {
|
||||
Pharmacy entity = pharmacyRepository.findById(id).orElse(null);
|
||||
|
||||
if (Objects.isNull(entity)) {
|
||||
log.info("[PharmacyRepositoryService updateAddress] not found id: {}", id);
|
||||
return;
|
||||
}
|
||||
|
||||
entity.changePharmacyAddress(address);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.example.road.pharmacy.service
|
||||
|
||||
import com.example.road.AbstractIntegrationContainerBaseTest
|
||||
import com.example.road.pharmacy.entity.Pharmacy
|
||||
import com.example.road.pharmacy.repository.PharmacyRepository
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
|
||||
class PharmacyRepositoryServiceTest extends AbstractIntegrationContainerBaseTest {
|
||||
|
||||
@Autowired
|
||||
private PharmacyRepositoryService pharmacyRepositoryService;
|
||||
|
||||
@Autowired
|
||||
private PharmacyRepository pharmacyRepository;
|
||||
|
||||
def setup() {
|
||||
pharmacyRepository.deleteAll();
|
||||
}
|
||||
|
||||
def "PharmacyRepository update - dirty checking success"() {
|
||||
given:
|
||||
String inputAddress = "서울 특별시 성북구 종암동";
|
||||
String modifiedAddress = "서울 광진구 구의동";
|
||||
String name = "은혜 약국";
|
||||
|
||||
def pharmacy = Pharmacy.builder()
|
||||
.pharmacyAddress(inputAddress)
|
||||
.pharmacyName(name)
|
||||
.build();
|
||||
|
||||
when:
|
||||
def entity = pharmacyRepository.save(pharmacy);
|
||||
pharmacyRepositoryService.updateAddress(entity.getId(), modifiedAddress);
|
||||
|
||||
def result = pharmacyRepository.findAll();
|
||||
|
||||
then:
|
||||
result.get(0).getPharmacyAddress() == modifiedAddress;
|
||||
}
|
||||
|
||||
def "PharmacyRepository update - dirty checking fail"() {
|
||||
given:
|
||||
String inputAddress = "서울 특별시 성북구 종암동";
|
||||
String modifiedAddress = "서울 광진구 구의동";
|
||||
String name = "은혜 약국";
|
||||
|
||||
def pharmacy = Pharmacy.builder()
|
||||
.pharmacyAddress(inputAddress)
|
||||
.pharmacyName(name)
|
||||
.build();
|
||||
|
||||
when:
|
||||
def entity = pharmacyRepository.save(pharmacy);
|
||||
pharmacyRepositoryService.updateAddressWithoutTransactional(entity.getId(), modifiedAddress);
|
||||
|
||||
def result = pharmacyRepository.findAll();
|
||||
|
||||
then:
|
||||
result.get(0).getPharmacyAddress() == inputAddress;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user