Skip to content

Commit

Permalink
refactoring sort by postion
Browse files Browse the repository at this point in the history
- added SortDirection string enum
  • Loading branch information
brouillette committed Sep 17, 2021
1 parent 43b35d0 commit c4dd9ce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/api/models/constants/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ package constants
*/
type GenotypeQuery string
type SearchOperation string
type SortDirection string

type Zygosity int
23 changes: 23 additions & 0 deletions src/api/models/constants/sort/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sort

import (
"api/models/constants"
"strings"
)

const (
Undefined constants.SortDirection = ""
Ascending constants.SortDirection = "asc"
Descending constants.SortDirection = "desc"
)

func CastToSortDirection(text string) constants.SortDirection {
switch strings.ToLower(text) {
case "asc":
return Ascending
case "desc":
return Descending
default:
return Undefined
}
}
3 changes: 2 additions & 1 deletion src/api/mvc/variants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"api/models"
"api/models/constants"
gq "api/models/constants/genotype-query"
s "api/models/constants/sort"
"api/models/ingest"
esRepo "api/repositories/elasticsearch"
"api/utils"
Expand Down Expand Up @@ -322,7 +323,7 @@ func executeGetByIds(c echo.Context, ids []string, isVariantIdQuery bool) error
}
}

sortByPosition := c.QueryParam("sortByPosition")
sortByPosition := s.CastToSortDirection(c.QueryParam("sortByPosition"))

includeSamplesInResultSetQP := c.QueryParam("includeSamplesInResultSet")
var (
Expand Down
25 changes: 6 additions & 19 deletions src/api/repositories/elasticsearch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

c "api/models/constants"
gq "api/models/constants/genotype-query"
s "api/models/constants/sort"
z "api/models/constants/zygosity"

"github.com/elastic/go-elasticsearch"
Expand All @@ -21,7 +22,7 @@ func GetDocumentsContainerVariantOrSampleIdInPositionRange(es *elasticsearch.Cli
chromosome string, lowerBound int, upperBound int,
variantId string, sampleId string,
reference string, alternative string,
size int, sortByPosition string,
size int, sortByPosition c.SortDirection,
includeSamplesInResultSet bool, genotype c.GenotypeQuery) map[string]interface{} {

// begin building the request body.
Expand Down Expand Up @@ -163,28 +164,14 @@ func GetDocumentsContainerVariantOrSampleIdInPositionRange(es *elasticsearch.Cli
}

// set up sorting
sortDirection := "asc"
if sortByPosition != "" {
switch sortByPosition {
case "asc":
fmt.Println("Already set 'sortByPosition' keyword 'asc' to query")
break
case "desc":
fmt.Println("Setting 'sortByPosition' keyword 'desc' to query")
sortDirection = "desc"
break
default:
fmt.Printf("Found unknown 'sortByPosition' keyword : %s -- ignoring\n", sortByPosition)
break
}

} else {
fmt.Println("Found empty 'sortByPosition' keyword -- defaulting to 'asc'")
if sortByPosition == s.Undefined {
// default to ascending order
sortByPosition = s.Ascending
}

// append sorting components
query["sort"] = map[string]string{
"pos": sortDirection,
"pos": string(sortByPosition),
}

// encode the query
Expand Down
20 changes: 9 additions & 11 deletions src/tests/integration/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"api/models"
c "api/models/constants"
gq "api/models/constants/genotype-query"
s "api/models/constants/sort"
z "api/models/constants/zygosity"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -88,18 +89,19 @@ func TestGetIngestionRequests(t *testing.T) {

func TestCanGetVariantsWithoutSamplesInResultset(t *testing.T) {

allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, "", string(gq.UNCALLED))
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, s.Undefined, string(gq.UNCALLED))

// assert that all responses from all combinations have no results
for _, dtoResponse := range allDtoResponses {
firstDataPointResults := dtoResponse.Data[0].Results
assert.Nil(t, firstDataPointResults[0].Samples)

}
}

func TestCanGetVariantsWithSamplesInResultset(t *testing.T) {

allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, true, "", string(gq.UNCALLED))
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, true, s.Undefined, string(gq.UNCALLED))

// assert that all of the responses include valid sample sets
// - * accumulate all samples into a single list using the set of
Expand All @@ -122,7 +124,7 @@ func TestCanGetVariantsWithSamplesInResultset(t *testing.T) {

func TestCanGetVariantsInAscendingPositionOrder(t *testing.T) {
// retrieve responses in ascending order
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, "asc", string(gq.UNCALLED))
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, s.Ascending, string(gq.UNCALLED))

// assert the dto response slice is plentiful
assert.NotNil(t, allDtoResponses)
Expand Down Expand Up @@ -151,7 +153,7 @@ func TestCanGetVariantsInAscendingPositionOrder(t *testing.T) {

func TestCanGetVariantsInDescendingPositionOrder(t *testing.T) {
// retrieve responses in descending order
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, "desc", string(gq.UNCALLED))
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(t, false, s.Descending, string(gq.UNCALLED))

// assert the dto response slice is plentiful
assert.NotNil(t, allDtoResponses)
Expand Down Expand Up @@ -214,7 +216,7 @@ func TestCanGetHomozygousAlternateSamples(t *testing.T) {
// -- Common utility functions for api tests
func runAndValidateGenotypeQueryResults(_t *testing.T, genotypeQuery c.GenotypeQuery, specificValidation func(__t *testing.T, sample models.Sample)) {

allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(_t, true, "", string(genotypeQuery))
allDtoResponses := getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(_t, true, s.Undefined, string(genotypeQuery))

// assert that all of the responses include heterozygous sample sets
// - * accumulate all samples into a single list using the set of
Expand All @@ -239,11 +241,7 @@ func runAndValidateGenotypeQueryResults(_t *testing.T, genotypeQuery c.GenotypeQ
})
}

func buildQueryAndMakeGetVariantsCall(chromosome string, sampleId string, includeSamples bool, sortByPosition string, genotype string, _t *testing.T, _cfg *models.Config) models.VariantsResponseDTO {

if sortByPosition != "asc" && sortByPosition != "desc" {
sortByPosition = "" // default to empty (will trigger ascending)
}
func buildQueryAndMakeGetVariantsCall(chromosome string, sampleId string, includeSamples bool, sortByPosition c.SortDirection, genotype string, _t *testing.T, _cfg *models.Config) models.VariantsResponseDTO {

queryString := fmt.Sprintf("?chromosome=%s&ids=%s&includeSamplesInResultSet=%t&sortByPosition=%s&genotype=%s", chromosome, sampleId, includeSamples, sortByPosition, genotype)
url := fmt.Sprintf(VariantsGetBySampleIdsPathWithQueryString, _cfg.Api.Url, queryString)
Expand Down Expand Up @@ -304,7 +302,7 @@ func getChromsAndSampleIDs(chromosomeStruct interface{}, sampleIdsStruct interfa
return allCombinations
}

func getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(_t *testing.T, includeSamples bool, sortByPosition string, genotype string) []models.VariantsResponseDTO {
func getAllDtosOfVariousCombinationsOfChromosomesAndSampleIds(_t *testing.T, includeSamples bool, sortByPosition c.SortDirection, genotype string) []models.VariantsResponseDTO {
cfg := common.InitConfig()

// todo: deduplicate
Expand Down

0 comments on commit c4dd9ce

Please sign in to comment.