Repository navigation
feat: region-seed 추가 #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/com/daytodo/domain/region/config/RegionDataInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.daytodo.domain.region.config; | ||
|
|
||
| import com.daytodo.domain.region.entity.Region; | ||
| import com.daytodo.domain.region.enums.RegionLevel; | ||
| import com.daytodo.domain.region.repository.RegionRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.boot.ApplicationArguments; | ||
| import org.springframework.boot.ApplicationRunner; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RegionDataInitializer implements ApplicationRunner { | ||
| private static final String SEOUL = "서울특별시"; | ||
| private static final List<String> SEOUL_NAMES = List.of(SEOUL, "서울"); | ||
| private static final List<String> SEOUL_DISTRICTS = List.of( | ||
| "종로구", "중구", "용산구", "성동구", "광진구", | ||
| "동대문구", "중랑구", "성북구", "강북구", "도봉구", | ||
| "노원구", "은평구", "서대문구", "마포구", "양천구", | ||
| "강서구", "구로구", "금천구", "영등포구", "동작구", | ||
| "관악구", "서초구", "강남구", "송파구", "강동구" | ||
| ); | ||
|
|
||
| private final RegionRepository regionRepository; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void run(ApplicationArguments args) { | ||
| List<Region> existingSeoulRegions = regionRepository | ||
| .findAllByParentIsNullAndRegionLevelAndRegionNameIn(RegionLevel.SIDO, SEOUL_NAMES); | ||
|
|
||
| Region seoul = existingSeoulRegions.stream() | ||
| .filter(region -> SEOUL.equals(region.getRegionName())) | ||
| .findFirst() | ||
| .orElseGet(() -> existingSeoulRegions.stream().findFirst() | ||
| .orElseGet(() -> regionRepository.save( | ||
| new Region(null, SEOUL, RegionLevel.SIDO) | ||
| ))); | ||
|
|
||
| List<Region> seoulRegions = existingSeoulRegions.isEmpty() | ||
| ? List.of(seoul) | ||
| : existingSeoulRegions; | ||
|
|
||
| Set<String> existingDistrictNames = regionRepository | ||
| .findAllByParentInAndRegionLevel(seoulRegions, RegionLevel.SIGUNGU) | ||
| .stream() | ||
| .map(Region::getRegionName) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| List<Region> missingDistricts = SEOUL_DISTRICTS.stream() | ||
| .filter(name -> !existingDistrictNames.contains(name)) | ||
| .map(name -> new Region(seoul, name, RegionLevel.SIGUNGU)) | ||
| .toList(); | ||
|
|
||
| if (!missingDistricts.isEmpty()) { | ||
| regionRepository.saveAll(missingDistricts); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/java/com/daytodo/domain/region/config/RegionDataInitializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.daytodo.domain.region.config; | ||
|
|
||
| import com.daytodo.domain.region.entity.Region; | ||
| import com.daytodo.domain.region.enums.RegionLevel; | ||
| import com.daytodo.domain.region.repository.RegionRepository; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DataJpaTest | ||
| @ActiveProfiles("test") | ||
| class RegionDataInitializerTest { | ||
| private static final List<String> SEOUL_DISTRICTS = List.of( | ||
| "종로구", "중구", "용산구", "성동구", "광진구", | ||
| "동대문구", "중랑구", "성북구", "강북구", "도봉구", | ||
| "노원구", "은평구", "서대문구", "마포구", "양천구", | ||
| "강서구", "구로구", "금천구", "영등포구", "동작구", | ||
| "관악구", "서초구", "강남구", "송파구", "강동구" | ||
| ); | ||
|
|
||
| @Autowired | ||
| RegionRepository regionRepository; | ||
|
|
||
| @Test | ||
| void seedsSeoulAndTwentyFiveDistricts() throws Exception { | ||
| RegionDataInitializer initializer = new RegionDataInitializer(regionRepository); | ||
|
|
||
| initializer.run(null); | ||
|
|
||
| List<Region> regions = regionRepository.findAllByOrderByRegionIdAsc(); | ||
| List<Region> districts = regions.stream() | ||
| .filter(region -> region.getRegionLevel() == RegionLevel.SIGUNGU) | ||
| .toList(); | ||
|
|
||
| assertThat(regions).hasSize(26); | ||
| assertThat(districts) | ||
| .extracting(Region::getRegionName) | ||
| .containsExactlyElementsOf(SEOUL_DISTRICTS); | ||
| assertThat(districts) | ||
| .allSatisfy(district -> assertThat(district.getParent().getRegionName()) | ||
| .isEqualTo("서울특별시")); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotInsertDuplicatesWhenRunAgain() throws Exception { | ||
| RegionDataInitializer initializer = new RegionDataInitializer(regionRepository); | ||
|
|
||
| initializer.run(null); | ||
| initializer.run(null); | ||
|
|
||
| assertThat(regionRepository.count()).isEqualTo(26); | ||
| assertThat(regionRepository.findAllByOrderByRegionIdAsc().stream() | ||
| .filter(region -> region.getRegionLevel() == RegionLevel.SIGUNGU) | ||
| .map(Region::getRegionName)) | ||
| .containsExactlyElementsOf(SEOUL_DISTRICTS); | ||
| } | ||
|
|
||
| @Test | ||
| void insertsOnlyMissingDistrictsWhenSomeDataAlreadyExists() throws Exception { | ||
| Region seoul = regionRepository.save(new Region(null, "서울특별시", RegionLevel.SIDO)); | ||
| regionRepository.save(new Region(seoul, "종로구", RegionLevel.SIGUNGU)); | ||
|
|
||
| new RegionDataInitializer(regionRepository).run(null); | ||
|
|
||
| List<Region> regions = regionRepository.findAllByOrderByRegionIdAsc(); | ||
| assertThat(regions).hasSize(26); | ||
| assertThat(regions.stream() | ||
| .filter(region -> "종로구".equals(region.getRegionName()))) | ||
| .hasSize(1); | ||
| assertThat(regions.stream() | ||
| .filter(region -> region.getRegionLevel() == RegionLevel.SIGUNGU) | ||
| .map(Region::getRegionName)) | ||
| .containsExactlyInAnyOrderElementsOf(SEOUL_DISTRICTS); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add coverage for the legacy
서울root.The tests do not verify the stated behavior for an existing
SIDOnamed"서울". Add a case that seeds this root, runs the initializer, and verifies that it remains the only root and has 25 districts.🤖 Prompt for AI Agents