Skip to content

Commit f6b2230

Browse files
authored
Query withinMiles and withinKilometers was not returning unsorted results when sort=false (#219)
1 parent 38bbd01 commit f6b2230

File tree

4 files changed

+116
-8
lines changed

4 files changed

+116
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Parse-Swift Changelog
22

33
### main
4-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.5...main)
4+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.6...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
### 1.9.6
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.5...1.9.6)
9+
10+
__Fixes__
11+
- Query withinMiles and withinKilometers was not returning unsorted results when sort=false ([#219](https://github.com/parse-community/Parse-Swift/pull/219)), thanks to [Corey Baker](https://github.com/cbaker6).
12+
713
### 1.9.5
814
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.9.4...1.9.5)
915

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "1.9.5"
13+
static let version = "1.9.6"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

Sources/ParseSwift/Types/Query.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ public func withinMiles(key: String,
385385
geoPoint: ParseGeoPoint,
386386
distance: Double,
387387
sorted: Bool = true) -> [QueryConstraint] {
388-
return withinRadians(key: key, geoPoint: geoPoint, distance: (distance / ParseGeoPoint.earthRadiusMiles))
388+
withinRadians(key: key,
389+
geoPoint: geoPoint,
390+
distance: (distance / ParseGeoPoint.earthRadiusMiles),
391+
sorted: sorted)
389392
}
390393

391394
/**
@@ -403,7 +406,10 @@ public func withinKilometers(key: String,
403406
geoPoint: ParseGeoPoint,
404407
distance: Double,
405408
sorted: Bool = true) -> [QueryConstraint] {
406-
return withinRadians(key: key, geoPoint: geoPoint, distance: (distance / ParseGeoPoint.earthRadiusKilometers))
409+
withinRadians(key: key,
410+
geoPoint: geoPoint,
411+
distance: (distance / ParseGeoPoint.earthRadiusKilometers),
412+
sorted: sorted)
407413
}
408414

409415
/**
@@ -560,7 +566,7 @@ public func hasSuffix(key: String, suffix: String, modifiers: String? = nil) ->
560566
- returns: The same instance of `Query` as the receiver.
561567
*/
562568
public func exists(key: String) -> QueryConstraint {
563-
return .init(key: key, value: true, comparator: .exists)
569+
.init(key: key, value: true, comparator: .exists)
564570
}
565571

566572
/**
@@ -569,7 +575,7 @@ public func exists(key: String) -> QueryConstraint {
569575
- returns: The same instance of `Query` as the receiver.
570576
*/
571577
public func doesNotExist(key: String) -> QueryConstraint {
572-
return .init(key: key, value: false, comparator: .exists)
578+
.init(key: key, value: false, comparator: .exists)
573579
}
574580

575581
internal struct RelatedCondition <T>: Encodable where T: ParseObject {

Tests/ParseSwiftTests/ParseQueryTests.swift

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,9 +2013,56 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
20132013
return
20142014
}
20152015
}
2016-
#endif
20172016

2018-
#if !os(Linux) && !os(Android)
2017+
func testWhereKeyNearGeoPointWithinMilesNotSorted() throws {
2018+
let expected: [String: AnyCodable] = [
2019+
"yolo": ["$centerSphere": ["latitude": 10, "longitude": 20, "__type": "GeoPoint"],
2020+
"$geoWithin": 1
2021+
]
2022+
]
2023+
let geoPoint = try ParseGeoPoint(latitude: 10, longitude: 20)
2024+
let constraint = withinMiles(key: "yolo",
2025+
geoPoint: geoPoint,
2026+
distance: 3958.8,
2027+
sorted: false)
2028+
let query = GameScore.query(constraint)
2029+
let queryWhere = query.`where`
2030+
2031+
do {
2032+
let encoded = try ParseCoding.jsonEncoder().encode(queryWhere)
2033+
let decodedDictionary = try JSONDecoder().decode([String: AnyCodable].self, from: encoded)
2034+
XCTAssertEqual(expected.keys, decodedDictionary.keys)
2035+
2036+
guard let expectedValues = expected.values.first?.value as? [String: Any],
2037+
let expectedNear = expectedValues["$centerSphere"] as? [String: Any],
2038+
let expectedLongitude = expectedNear["longitude"] as? Int,
2039+
let expectedLatitude = expectedNear["latitude"] as? Int,
2040+
let expectedType = expectedNear["__type"] as? String,
2041+
let expectedDistance = expectedValues["$geoWithin"] as? Int else {
2042+
XCTFail("Should have casted")
2043+
return
2044+
}
2045+
2046+
guard let decodedValues = decodedDictionary.values.first?.value as? [String: Any],
2047+
let decodedNear = decodedValues["$centerSphere"] as? [String: Any],
2048+
let decodedLongitude = decodedNear["longitude"] as? Int,
2049+
let decodedLatitude = decodedNear["latitude"] as? Int,
2050+
let decodedType = decodedNear["__type"] as? String,
2051+
let decodedDistance = decodedValues["$geoWithin"] as? Int else {
2052+
XCTFail("Should have casted")
2053+
return
2054+
}
2055+
XCTAssertEqual(expectedLongitude, decodedLongitude)
2056+
XCTAssertEqual(expectedLatitude, decodedLatitude)
2057+
XCTAssertEqual(expectedType, decodedType)
2058+
XCTAssertEqual(expectedDistance, decodedDistance)
2059+
2060+
} catch {
2061+
XCTFail(error.localizedDescription)
2062+
return
2063+
}
2064+
}
2065+
20192066
func testWhereKeyNearGeoPointWithinKilometers() throws {
20202067
let expected: [String: AnyCodable] = [
20212068
"yolo": ["$nearSphere": ["latitude": 10, "longitude": 20, "__type": "GeoPoint"],
@@ -2062,6 +2109,55 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
20622109
}
20632110
}
20642111

2112+
func testWhereKeyNearGeoPointWithinKilometersNotSorted() throws {
2113+
let expected: [String: AnyCodable] = [
2114+
"yolo": ["$centerSphere": ["latitude": 10, "longitude": 20, "__type": "GeoPoint"],
2115+
"$geoWithin": 1
2116+
]
2117+
]
2118+
let geoPoint = try ParseGeoPoint(latitude: 10, longitude: 20)
2119+
let constraint = withinKilometers(key: "yolo",
2120+
geoPoint: geoPoint,
2121+
distance: 6371.0,
2122+
sorted: false)
2123+
let query = GameScore.query(constraint)
2124+
let queryWhere = query.`where`
2125+
2126+
do {
2127+
let encoded = try ParseCoding.jsonEncoder().encode(queryWhere)
2128+
let decodedDictionary = try JSONDecoder().decode([String: AnyCodable].self, from: encoded)
2129+
XCTAssertEqual(expected.keys, decodedDictionary.keys)
2130+
2131+
guard let expectedValues = expected.values.first?.value as? [String: Any],
2132+
let expectedNear = expectedValues["$centerSphere"] as? [String: Any],
2133+
let expectedLongitude = expectedNear["longitude"] as? Int,
2134+
let expectedLatitude = expectedNear["latitude"] as? Int,
2135+
let expectedType = expectedNear["__type"] as? String,
2136+
let expectedDistance = expectedValues["$geoWithin"] as? Int else {
2137+
XCTFail("Should have casted")
2138+
return
2139+
}
2140+
2141+
guard let decodedValues = decodedDictionary.values.first?.value as? [String: Any],
2142+
let decodedNear = decodedValues["$centerSphere"] as? [String: Any],
2143+
let decodedLongitude = decodedNear["longitude"] as? Int,
2144+
let decodedLatitude = decodedNear["latitude"] as? Int,
2145+
let decodedType = decodedNear["__type"] as? String,
2146+
let decodedDistance = decodedValues["$geoWithin"] as? Int else {
2147+
XCTFail("Should have casted")
2148+
return
2149+
}
2150+
XCTAssertEqual(expectedLongitude, decodedLongitude)
2151+
XCTAssertEqual(expectedLatitude, decodedLatitude)
2152+
XCTAssertEqual(expectedType, decodedType)
2153+
XCTAssertEqual(expectedDistance, decodedDistance)
2154+
2155+
} catch {
2156+
XCTFail(error.localizedDescription)
2157+
return
2158+
}
2159+
}
2160+
20652161
func testWhereKeyNearGeoPointWithinRadians() throws {
20662162
let expected: [String: AnyCodable] = [
20672163
"yolo": ["$nearSphere": ["latitude": 10, "longitude": 20, "__type": "GeoPoint"],

0 commit comments

Comments
 (0)