-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from wearefuturegov/TOP-244-when-searching-for…
…-keyword-and-location-results-arent-ordered-by-distance TOP-244 When searching for keyword and location results aren't ordere…
- Loading branch information
Showing
10 changed files
with
572 additions
and
128 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,112 @@ | ||
const getServices = require("../../../src/controllers/v1/services/routes/get-services") | ||
const locations = require("./../../../src/lib/locations") | ||
|
||
describe("filterLocationNearest", () => { | ||
test("should return null when the list of locations is empty", async () => { | ||
const lat = undefined | ||
const lng = undefined | ||
const { proximity } = await getServices.parseRequestParameters({}) | ||
expect(locations.filterLocationNearest(lat, lng, proximity)).toEqual({}) | ||
}) | ||
|
||
test("should return results when lat and lng is set", async () => { | ||
const lat = parseFloat(1) | ||
const lng = parseFloat(1.2) | ||
const { proximity } = await getServices.parseRequestParameters({ lat, lng }) | ||
expect(locations.filterLocationNearest(lat, lng, proximity)).toEqual({ | ||
"service_at_locations.location.geometry": { | ||
$nearSphere: { | ||
$geometry: { | ||
coordinates: [lng, lat], | ||
type: "Point", | ||
}, | ||
$maxDistance: proximity, | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
test("should return results when one or another lat/lng is 0", async () => { | ||
const lat = parseFloat(0) | ||
const lng = parseFloat(1.2) | ||
const { proximity } = await getServices.parseRequestParameters({ lat, lng }) | ||
expect(locations.filterLocationNearest(lat, lng, proximity)).toEqual({ | ||
"service_at_locations.location.geometry": { | ||
$nearSphere: { | ||
$geometry: { | ||
coordinates: [lng, lat], | ||
type: "Point", | ||
}, | ||
$maxDistance: proximity, | ||
}, | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
describe("filterLocationKeywords", () => { | ||
test("should return an empty object when no keywords", async () => { | ||
const keywords = undefined | ||
const parameters = await getServices.parseRequestParameters({}) | ||
const result = await locations.filterLocationKeywords(keywords, parameters) | ||
expect(result).toEqual({}) | ||
}) | ||
|
||
// test("should return a list of ids", async () => { | ||
// const keywords = "SEND peer support" | ||
// const parameters = await getServices.parseRequestParameters({}) | ||
// const result = await locations.filterLocationKeywords(keywords, parameters) | ||
// expect(locations.filterLocationKeywords(keywords, parameters)).toEqual({ | ||
// $and: [ | ||
// { keyword: "test" }, | ||
// { filter: "test" }, | ||
// { | ||
// "service_at_locations.location.geometry": { | ||
// $exists: true, | ||
// $ne: null, | ||
// }, | ||
// }, | ||
// ], | ||
// }) | ||
// }) | ||
}) | ||
|
||
describe("filterLocation", () => { | ||
it("should return an empty object if lat and lng are not provided", () => { | ||
expect(locations.filterLocation()).toEqual({}) | ||
}) | ||
|
||
it("should return a query object if lat and lng are provided", () => { | ||
const lat = "40.7128" | ||
const lng = "-74.0060" | ||
const expectedQuery = { | ||
"service_at_locations.location.geometry": { | ||
$geoWithin: { | ||
$centerSphere: [[parseFloat(lng), parseFloat(lat)], 20 / 3963.2], | ||
}, | ||
}, | ||
} | ||
expect(locations.filterLocation(lat, lng, false)).toEqual(expectedQuery) | ||
}) | ||
|
||
it("should return a different query object if lat and lng and keywordSearch are provided", () => { | ||
const lat = "40.7128" | ||
const lng = "-74.0060" | ||
const expectedQuery = { | ||
$or: [ | ||
{ | ||
"service_at_locations.location.geometry": { | ||
$geoWithin: { | ||
$centerSphere: [ | ||
[parseFloat(lng), parseFloat(lat)], | ||
20 / 3963.2, // miles x 1609.34 = Distance in meters | ||
], | ||
}, | ||
}, | ||
}, | ||
{ "service_at_locations.location.geometry": { $exists: false } }, | ||
], | ||
} | ||
expect(locations.filterLocation(lat, lng, true)).toEqual(expectedQuery) | ||
}) | ||
}) |
This file contains 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,35 @@ | ||
const queries = require("./../../../src/lib/queries") | ||
|
||
describe("queryType", () => { | ||
test('should return "keyword" when only keywords are provided', () => { | ||
const parameters = { keywords: "test" } | ||
expect(queries.queryType(parameters)).toBe("keyword") | ||
}) | ||
|
||
test('should return "location" when only lat and lng are provided', () => { | ||
const parameters = { lat: 51.5074, lng: -0.1278 } | ||
expect(queries.queryType(parameters)).toBe("location") | ||
}) | ||
|
||
test('should return "keyword_location" when keywords, lat, and lng are all provided', () => { | ||
const parameters = { keywords: "test", lat: 51.5074, lng: -0.1278 } | ||
expect(queries.queryType(parameters)).toBe("keyword_location") | ||
}) | ||
|
||
test("should return undefined when none of the parameters are provided", () => { | ||
const parameters = {} | ||
expect(queries.queryType(parameters)).toBeUndefined() | ||
}) | ||
|
||
test("should return undefined when keywords is provided but lat and lng are missing", () => { | ||
const parameters = { keywords: "test", lat: undefined, lng: undefined } | ||
expect(queries.queryType(parameters)).toBe("keyword") | ||
}) | ||
|
||
test("should return undefined when lat and lng are provided but keywords is missing", () => { | ||
const parameters = { keywords: undefined, lat: 51.5074, lng: -0.1278 } | ||
expect(queries.queryType(parameters)).toBe("location") | ||
}) | ||
}) | ||
|
||
describe("addFilters", () => {}) |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.