Skip to content

Commit

Permalink
Add LegacySearchRepositoryTest and refactor legacy search logic
Browse files Browse the repository at this point in the history
Introduced LegacySearchRepositoryTest to verify legacy search functionalities. Refactored legacy search logic out of ConceptService and into LegacySearchRepository. Updated related tests and cleaned up unused imports and methods.
  • Loading branch information
Gcolon021 committed Nov 18, 2024
1 parent aa457b5 commit 2a7edaa
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static edu.harvard.dbmi.avillach.dictionary.util.QueryUtility.ALLOW_FILTERING_Q;


@Repository
public class ConceptRepository {

Expand All @@ -30,9 +31,9 @@ public class ConceptRepository {

@Autowired
public ConceptRepository(
NamedParameterJdbcTemplate template, ConceptRowMapper mapper, SearchResultRowMapper searchResultRowMapper,
ConceptFilterQueryGenerator filterGen, ConceptMetaExtractor conceptMetaExtractor,
ConceptResultSetExtractor conceptResultSetExtractor, @Value("${filtering.unfilterable_concepts}") List<String> disallowedMetaFields
NamedParameterJdbcTemplate template, ConceptRowMapper mapper, ConceptFilterQueryGenerator filterGen,
ConceptMetaExtractor conceptMetaExtractor, ConceptResultSetExtractor conceptResultSetExtractor,
@Value("${filtering.unfilterable_concepts}") List<String> disallowedMetaFields
) {
this.template = template;
this.mapper = mapper;
Expand Down Expand Up @@ -99,7 +100,8 @@ public Optional<Concept> getConcept(String dataset, String conceptPath) {
LEFT JOIN concept_node_meta AS categorical_values ON concept_node.concept_node_id = categorical_values.concept_node_id AND categorical_values.KEY = 'values'
LEFT JOIN allow_filtering ON concept_node.concept_node_id = allow_filtering.concept_node_id
WHERE
concept_node.concept_path IN :conceptPaths
concept_node.concept_path = :conceptPath
AND ds.REF = :dataset
""";
MapSqlParameterSource params = new MapSqlParameterSource().addValue("conceptPath", conceptPath).addValue("dataset", dataset)
.addValue("disallowed_meta_keys", disallowedMetaFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import edu.harvard.dbmi.avillach.dictionary.concept.model.ConceptShell;
import edu.harvard.dbmi.avillach.dictionary.concept.model.ContinuousConcept;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -72,8 +71,4 @@ public Optional<Concept> conceptDetailWithoutAncestors(String dataset, String co
return getConcept(dataset, conceptPath, false);
}

public List<SearchResult> getLegacySearchResults(Filter filter, Pageable pageable) {
return conceptRepository.getLegacySearchResults(filter, pageable);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch;

import edu.harvard.dbmi.avillach.dictionary.concept.ConceptService;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.Results;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -10,15 +9,15 @@
@Service
public class LegacySearchService {

private final ConceptService conceptService;
private final LegacySearchRepository legacySearchRepository;

@Autowired
public LegacySearchService(ConceptService conceptService) {
this.conceptService = conceptService;
public LegacySearchService(LegacySearchRepository legacySearchRepository) {
this.legacySearchRepository = legacySearchRepository;
}

public Results getSearchResults(Filter filter, Pageable pageable) {
return new Results(conceptService.getLegacySearchResults(filter, pageable));
return new Results(legacySearchRepository.getLegacySearchResults(filter, pageable));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import edu.harvard.dbmi.avillach.dictionary.concept.model.ContinuousConcept;
import edu.harvard.dbmi.avillach.dictionary.facet.Facet;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.SearchResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -325,36 +324,5 @@ void shouldGetContConceptWithDecimalNotation() {
Assertions.assertEquals(6.77f, concept.max());
}

@Test
void shouldGetLegacySearchResults() {
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.unpaged());

Assertions.assertEquals(30, searchResults.size());
}

@Test
void shouldGetLegacySearchResultsBySearch() {
List<SearchResult> searchResults =
subject.getLegacySearchResults(new Filter(List.of(), "phs000007", List.of()), Pageable.unpaged());

searchResults.forEach(searchResult -> Assertions.assertEquals("phs000007", searchResult.result().studyId()));

}

@Test
void shouldGetLegacySearchResultsByPageSize() {
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.ofSize(5));

Assertions.assertEquals(5, searchResults.size());
}

@Test
void legacySearchResultShouldGetEqualCountToConceptSearch() {
// This test will ensure modifications made to the conceptSearch will be reflected in the legacy search result.
// They use near equivalent queries and updates made to one should be made to the other.
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.unpaged());
List<Concept> concepts = subject.getConcepts(new Filter(List.of(), "", List.of()), Pageable.unpaged());

Assertions.assertEquals(searchResults.size(), concepts.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package edu.harvard.dbmi.avillach.dictionary.legacysearch;

import edu.harvard.dbmi.avillach.dictionary.concept.ConceptRepository;
import edu.harvard.dbmi.avillach.dictionary.concept.model.Concept;
import edu.harvard.dbmi.avillach.dictionary.filter.Filter;
import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.SearchResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.MountableFile;

import java.util.List;

@SpringBootTest
@Testcontainers
public class LegacySearchRepositoryTest {

@Autowired
LegacySearchRepository subject;

@Autowired
ConceptRepository conceptService;

@Container
static final PostgreSQLContainer<?> databaseContainer = new PostgreSQLContainer<>("postgres:16").withReuse(true)
.withCopyFileToContainer(MountableFile.forClasspathResource("seed.sql"), "/docker-entrypoint-initdb.d/seed.sql");

@DynamicPropertySource
static void mySQLProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", databaseContainer::getJdbcUrl);
registry.add("spring.datasource.username", databaseContainer::getUsername);
registry.add("spring.datasource.password", databaseContainer::getPassword);
registry.add("spring.datasource.db", databaseContainer::getDatabaseName);
}

@Test
void shouldGetLegacySearchResults() {
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.unpaged());

Assertions.assertEquals(30, searchResults.size());
}

@Test
void shouldGetLegacySearchResultsBySearch() {
List<SearchResult> searchResults =
subject.getLegacySearchResults(new Filter(List.of(), "phs000007", List.of()), Pageable.unpaged());

searchResults.forEach(searchResult -> Assertions.assertEquals("phs000007", searchResult.result().studyId()));

}

@Test
void shouldGetLegacySearchResultsByPageSize() {
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.ofSize(5));

Assertions.assertEquals(5, searchResults.size());
}

@Test
void legacySearchResultShouldGetEqualCountToConceptSearch() {
// This test will ensure modifications made to the conceptSearch will be reflected in the legacy search result.
// They use near equivalent queries and updates made to one should be made to the other.
List<SearchResult> searchResults = subject.getLegacySearchResults(new Filter(List.of(), "", List.of()), Pageable.unpaged());
List<Concept> concepts = conceptService.getConcepts(new Filter(List.of(), "", List.of()), Pageable.unpaged());

Assertions.assertEquals(searchResults.size(), concepts.size());
}

}

0 comments on commit 2a7edaa

Please sign in to comment.