Skip to content

Commit

Permalink
test: improve JUnit tests, and exception management, #TASK-6020, #TAS…
Browse files Browse the repository at this point in the history
…K-5789
  • Loading branch information
jtarraga committed Apr 17, 2024
1 parent 1777ca2 commit 30f167c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.avro.specific.SpecificRecordBase;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand All @@ -31,9 +32,12 @@
import org.opencb.cellbase.client.config.ClientConfiguration;
import org.opencb.cellbase.client.config.RestConfig;
import org.opencb.cellbase.core.common.GitRepositoryState;
import org.opencb.cellbase.core.models.DataRelease;
import org.opencb.cellbase.core.result.CellBaseDataResponse;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.utils.VersionUtils;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -147,18 +151,23 @@ public void getAllConsequenceTypes(CellBaseClient cellBaseClient) throws Excepti

@Test
public void testSearchSnpBydbSnpId() throws Exception {
Assumptions.assumeTrue(GitRepositoryState.get().getBranch().equals("TASK-5789"));

int dataRelease = 7;
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setDefaultSpecies("hsapiens")
.setVersion("v5.8.3-SNAPSHOT")
.setRest(new RestConfig(Collections.singletonList("https://ws.zettagenomics.com/cellbase"), 2000));

CellBaseClient client = new CellBaseClient(clientConfiguration);

// Assumptions before running the test
ObjectMap result = client.getMetaClient().about().firstResult();
Assumptions.assumeTrue(VersionUtils.isMinVersion("5.8.3-SNAPSHOT", result.getString("Version")));
CellBaseDataResponse<DataRelease> dataReleaseResponse = client.getMetaClient().dataReleases();
Assumptions.assumeTrue(dataReleaseResponse.getResponses().get(0).getResults().stream().map(DataRelease::getRelease).collect(Collectors.toList()).contains(dataRelease));

Query query = new Query();
query.put("id", "rs1570391602,rs41278952");
query.put("dataRelease", 7);
query.put("dataRelease", dataRelease);

CellBaseDataResponse<Snp> response = client.getVariantClient().searchSnp(query, new QueryOptions());
assertEquals(2, response.getResponses().get(0).getNumResults());
Expand All @@ -168,20 +177,25 @@ public void testSearchSnpBydbSnpId() throws Exception {

@Test
public void testSearchSnpByPosition() throws Exception {
Assumptions.assumeTrue(GitRepositoryState.get().getBranch().equals("TASK-5789"));

int dataRelease = 7;
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setDefaultSpecies("hsapiens")
.setVersion("v5.8.3-SNAPSHOT")
.setRest(new RestConfig(Collections.singletonList("https://ws.zettagenomics.com/cellbase"), 2000));

CellBaseClient client = new CellBaseClient(clientConfiguration);

// Assumptions before running the test
ObjectMap result = client.getMetaClient().about().firstResult();
Assumptions.assumeTrue(VersionUtils.isMinVersion("5.8.3-SNAPSHOT", result.getString("Version")));
CellBaseDataResponse<DataRelease> dataReleaseResponse = client.getMetaClient().dataReleases();
Assumptions.assumeTrue(dataReleaseResponse.getResponses().get(0).getResults().stream().map(DataRelease::getRelease).collect(Collectors.toList()).contains(dataRelease));

Query query = new Query();
query.put("chromosome", "1");
query.put("position", "56948509");
query.put("reference", "T");
query.put("dataRelease", 7);
query.put("dataRelease", dataRelease);

CellBaseDataResponse<Snp> response = client.getVariantClient().searchSnp(query, new QueryOptions());
assertEquals(1, response.getResponses().get(0).getNumResults());
Expand All @@ -194,18 +208,23 @@ public void testSearchSnpByPosition() throws Exception {

@Test
public void testStarsWithSnp() throws Exception {
Assumptions.assumeTrue(GitRepositoryState.get().getBranch().equals("TASK-5789"));

int dataRelease = 7;
ClientConfiguration clientConfiguration = new ClientConfiguration()
.setDefaultSpecies("hsapiens")
.setVersion("v5.8.3-SNAPSHOT")
.setRest(new RestConfig(Collections.singletonList("https://ws.zettagenomics.com/cellbase"), 2000));

CellBaseClient client = new CellBaseClient(clientConfiguration);

// Assumptions before running the test
ObjectMap result = client.getMetaClient().about().firstResult();
Assumptions.assumeTrue(VersionUtils.isMinVersion("5.8.3-SNAPSHOT", result.getString("Version")));
CellBaseDataResponse<DataRelease> dataReleaseResponse = client.getMetaClient().dataReleases();
Assumptions.assumeTrue(dataReleaseResponse.getResponses().get(0).getResults().stream().map(DataRelease::getRelease).collect(Collectors.toList()).contains(dataRelease));

Query query = new Query();
query.put("id", "rs157039161");
query.put("dataRelease", 7);
query.put("dataRelease", dataRelease);

CellBaseDataResponse<Snp> response = client.getVariantClient().startsWithSnp(query, new QueryOptions());
assertEquals(9, response.getResponses().get(0).getNumResults());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
public class EtlCommons {

public static final String HOMO_SAPIENS_NAME ="Homo sapiens";

public static final String GENOME_DATA = "genome";
public static final String GENE_DATA = "gene";
public static final String REFSEQ_DATA = "refseq";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.opencb.cellbase.core.config.CellBaseConfiguration;
import org.opencb.cellbase.core.config.DownloadProperties;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.lib.EtlCommons;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -45,7 +46,7 @@ public DownloadFile downloadDbSnp() throws IOException, InterruptedException {
if (!speciesHasInfoToDownload(speciesConfiguration, VARIATION_DATA)) {
return null;
}
if (speciesConfiguration.getScientificName().equals("Homo sapiens")) {
if (speciesConfiguration.getScientificName().equals(EtlCommons.HOMO_SAPIENS_NAME)) {
logger.info("Downloading dbSNP information ...");

Path variation = downloadFolder.resolve(VARIATION_DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public CellBaseDataResult<Snp> startsWith(String id, QueryOptions options, int d
return new CellBaseDataResult<>(mongoDBCollection.find(regex, projection, CONVERTER, options));
}

public Bson parseQuery(SnpQuery query) {
public Bson parseQuery(SnpQuery query) throws CellBaseException {
List<Bson> andBsonList = new ArrayList<>();
try {
for (Map.Entry<String, Object> entry : query.toObjectMap().entrySet()) {
Expand All @@ -145,7 +145,7 @@ public Bson parseQuery(SnpQuery query) {
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new CellBaseException("Error parsing SNP query: " + query, e);
}

logger.info("SnpMongoDBAdaptor parsed query: {}", andBsonList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public Bson parseQuery(VariantQuery query) throws CellBaseException {
}
}
} catch (IllegalAccessException e) {
throw new CellBaseException(e.getMessage());
throw new CellBaseException("Error parsing variant query: " + query, e);
}

logger.debug("variant parsed query: {}", andBsonList);
Expand Down

0 comments on commit 30f167c

Please sign in to comment.