Skip to content

Commit a77fbbe

Browse files
committed
Fix bug and add integration tests.
1 parent b215642 commit a77fbbe

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

Firestore/Swift/Tests/Integration/BsonTypesIntegrationTests.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
107107
"binary": BSONBinaryData(subtype: 1, data: Data([1, 2, 3])),
108108
"objectId": BSONObjectId("507f191e810c19729de860ea"),
109109
"int32": Int32Value(1),
110+
"decimal128": Decimal128Value("1.2e3"),
110111
"min": MinKey.shared,
111112
"max": MaxKey.shared,
112113
"regex": RegexValue(pattern: "^foo", options: "i"),
@@ -127,6 +128,10 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
127128
snapshot.get("int32") as? Int32Value,
128129
Int32Value(2)
129130
)
131+
XCTAssertEqual(
132+
snapshot.get("decimal128") as? Decimal128Value,
133+
Decimal128Value("1.2e3")
134+
)
130135
XCTAssertEqual(
131136
snapshot.get("min") as? MinKey,
132137
MinKey.shared
@@ -160,6 +165,7 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
160165
"binary": BSONBinaryData(subtype: 1, data: Data([1, 2, 3])),
161166
"objectId": BSONObjectId("507f191e810c19729de860ea"),
162167
"int32": Int32Value(1),
168+
"decimal128": Decimal128Value("-1.23e-4"),
163169
"min": MinKey.shared,
164170
"max": MaxKey.shared,
165171
"regex": RegexValue(pattern: "^foo", options: "i"),
@@ -179,6 +185,10 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
179185
snapshot.get("int32") as? Int32Value,
180186
Int32Value(2)
181187
)
188+
XCTAssertEqual(
189+
snapshot.get("decimal128") as? Decimal128Value,
190+
Decimal128Value("-1.23e-4")
191+
)
182192
XCTAssertEqual(
183193
snapshot.get("min") as? MinKey,
184194
MinKey.shared
@@ -268,6 +278,42 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
268278
)
269279
}
270280

281+
func testCanFilterAndOrderDecimal128Values() async throws {
282+
let testDocs: [String: [String: Any]] = [
283+
"a": ["key": Decimal128Value("-Infinity")],
284+
"b": ["key": Decimal128Value("NaN")],
285+
"c": ["key": Decimal128Value("-0")],
286+
"d": ["key": Decimal128Value("0")],
287+
"e": ["key": Decimal128Value("0.0")],
288+
"f": ["key": Decimal128Value("-01.23e-4")],
289+
"g": ["key": Decimal128Value("1.5e6")],
290+
"h": ["key": Decimal128Value("Infinity")],
291+
]
292+
293+
let collection = collectionRef()
294+
await setDocumentData(testDocs, toCollection: collection)
295+
296+
var query = collection
297+
.whereField("key", isGreaterThanOrEqualTo: Decimal128Value("0"))
298+
.order(by: "key", descending: true)
299+
try await assertSdkQueryResultsConsistentWithBackend(
300+
testDocs,
301+
collection: collection,
302+
query: query,
303+
expectedResult: ["h", "g", "e", "d", "c"]
304+
)
305+
306+
query = collection
307+
.whereField("key", notIn: [Decimal128Value("NaN"), Decimal128Value("Infinity")])
308+
.order(by: "key", descending: true)
309+
try await assertSdkQueryResultsConsistentWithBackend(
310+
testDocs,
311+
collection: collection,
312+
query: query,
313+
expectedResult: ["g", "e", "d", "c", "f", "a"]
314+
)
315+
}
316+
271317
func testCanFilterAndOrderTimestampValues() async throws {
272318
let testDocs: [String: [String: Any]] = [
273319
"a": ["key": BSONTimestamp(seconds: 1, increment: 1)],
@@ -584,9 +630,17 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
584630
"bsonBinary1": ["key": BSONBinaryData(subtype: 1, data: Data([1, 2, 3]))],
585631
"bsonBinary2": ["key": BSONBinaryData(subtype: 1, data: Data([1, 2, 4]))],
586632
"bsonBinary3": ["key": BSONBinaryData(subtype: 2, data: Data([1, 2, 2]))],
633+
"decimal128Value1": ["key": Decimal128Value("NaN")],
634+
"decimal128Value2": ["key": Decimal128Value("-Infinity")],
635+
"decimal128Value3": ["key": Decimal128Value("-1.0")],
587636
"int32Value1": ["key": Int32Value(-1)],
637+
"decimal128Value4": ["key": Decimal128Value("1.0")],
588638
"int32Value2": ["key": Int32Value(1)],
639+
"decimal128Value5": ["key": Decimal128Value("-0.0")],
640+
"decimal128Value6": ["key": Decimal128Value("0.0")],
589641
"int32Value3": ["key": Int32Value(0)],
642+
"decimal128Value7": ["key": Decimal128Value("1.23e-4")],
643+
"decimal128Value8": ["key": Decimal128Value("Infinity")],
590644
"minKey1": ["key": MinKey.shared],
591645
"minKey2": ["key": MinKey.shared],
592646
"maxKey1": ["key": MaxKey.shared],
@@ -614,9 +668,17 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
614668
"bsonTimestamp1",
615669
"bsonTimestamp2",
616670
"bsonTimestamp3",
671+
"decimal128Value8",
672+
"decimal128Value7",
617673
"int32Value2",
674+
"decimal128Value4",
618675
"int32Value3",
676+
"decimal128Value6",
677+
"decimal128Value5",
619678
"int32Value1",
679+
"decimal128Value3",
680+
"decimal128Value2",
681+
"decimal128Value1",
620682
"minKey2",
621683
"minKey1",
622684
])
@@ -631,9 +693,13 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
631693
"minValue": ["key": MinKey.shared],
632694
"booleanValue": ["key": true],
633695
"nanValue": ["key": Double.nan],
696+
"nanValue2": ["key": Decimal128Value("NaN")],
697+
"negativeInfinity": ["key": Decimal128Value("-Infinity")],
634698
"int32Value": ["key": Int32Value(1)],
635699
"doubleValue": ["key": 2.0],
636700
"integerValue": ["key": 3],
701+
"decimal128Value": ["key": Decimal128Value("3.4e-5")],
702+
"infinity": ["key": Decimal128Value("Infinity")],
637703
"timestampValue": ["key": Timestamp(seconds: 100, nanoseconds: 123_456_000)],
638704
"bsonTimestampValue": ["key": BSONTimestamp(seconds: 1, increment: 2)],
639705
"stringValue": ["key": "string"],
@@ -660,9 +726,13 @@ class BsonTypesIntegrationTests: FSTIntegrationTestCase {
660726
"minValue",
661727
"booleanValue",
662728
"nanValue",
729+
"nanValue2",
730+
"negativeInfinity",
663731
"int32Value",
664732
"doubleValue",
665733
"integerValue",
734+
"decimal128Value",
735+
"infinity",
666736
"timestampValue",
667737
"bsonTimestampValue",
668738
"stringValue",

Firestore/Swift/Tests/Integration/SnapshotListenerSourceTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ class SnapshotListenerSourceTests: FSTIntegrationTestCase {
800800
testData["a"]!["key"]
801801
)
802802

803+
// Add a 32-bit int value.
803804
let newData = ["key": Int32Value(2)]
804805
collection.document("g").setData(newData)
805806

@@ -834,6 +835,45 @@ class SnapshotListenerSourceTests: FSTIntegrationTestCase {
834835
testData["a"]!["key"]
835836
)
836837

838+
// Add a 128-bit decimal value.
839+
let decimalData = ["key": Decimal128Value("-4.123e-5")]
840+
collection.document("h").setData(decimalData)
841+
842+
querySnap = eventAccumulator.awaitEvent(withName: "snapshot") as! QuerySnapshot
843+
XCTAssertEqual(querySnap.isEmpty, false)
844+
XCTAssertEqual(
845+
querySnap.documents[0].data()["key"] as! MinKey,
846+
testData["b"]!["key"]
847+
)
848+
XCTAssertEqual(
849+
querySnap.documents[1].data()["key"] as! Decimal128Value,
850+
decimalData["key"]!
851+
)
852+
XCTAssertEqual(
853+
querySnap.documents[2].data()["key"] as! Int32Value,
854+
newData["key"]!
855+
)
856+
XCTAssertEqual(
857+
querySnap.documents[3].data()["key"] as! BSONTimestamp,
858+
testData["c"]!["key"]
859+
)
860+
XCTAssertEqual(
861+
querySnap.documents[4].data()["key"] as! BSONBinaryData,
862+
testData["e"]!["key"]
863+
)
864+
XCTAssertEqual(
865+
querySnap.documents[5].data()["key"] as! BSONObjectId,
866+
testData["d"]!["key"]
867+
)
868+
XCTAssertEqual(
869+
querySnap.documents[6].data()["key"] as! RegexValue,
870+
testData["f"]!["key"]
871+
)
872+
XCTAssertEqual(
873+
querySnap.documents[7].data()["key"] as! MaxKey,
874+
testData["a"]!["key"]
875+
)
876+
837877
registration.remove()
838878
}
839879
}

Firestore/Swift/Tests/Integration/TypeTest.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ class TypeTest: FSTIntegrationTestCase {
137137
XCTAssertTrue(v1 != v3)
138138
}
139139

140+
func testDecimal128ValueEquality() {
141+
let v1 = Decimal128Value("1.2e3");
142+
let v2 = Decimal128Value("12e2");
143+
let v3 = Decimal128Value("0.12e4");
144+
let v4 = Decimal128Value("12000e-1");
145+
let v5 = Decimal128Value("1.2");
146+
147+
XCTAssertTrue(v1 == v2)
148+
XCTAssertTrue(v1 == v3)
149+
XCTAssertTrue(v1 == v4)
150+
XCTAssertFalse(v1 == v5)
151+
152+
XCTAssertFalse(v1 != v2)
153+
XCTAssertFalse(v1 != v3)
154+
XCTAssertFalse(v1 != v4)
155+
XCTAssertTrue(v1 != v5)
156+
}
157+
140158
func testBsonTimestampEquality() {
141159
let v1 = BSONTimestamp(seconds: 1, increment: 1)
142160
let v2 = BSONTimestamp(seconds: 1, increment: 1)
@@ -207,6 +225,13 @@ class TypeTest: FSTIntegrationTestCase {
207225
)
208226
}
209227

228+
func testCanReadAndWriteDecimal128Fields() async throws {
229+
_ = try await expectRoundtrip(
230+
coll: collectionRef(),
231+
data: ["decimal128": Decimal128Value("-1.234e-5")]
232+
)
233+
}
234+
210235
func testCanReadAndWriteBsonTimestampFields() async throws {
211236
_ = try await expectRoundtrip(
212237
coll: collectionRef(),
@@ -244,6 +269,7 @@ class TypeTest: FSTIntegrationTestCase {
244269
BSONObjectId("507f191e810c19729de860ea"),
245270
BSONTimestamp(seconds: 123, increment: 456),
246271
Int32Value(1),
272+
Decimal128Value("1.2e3"),
247273
MinKey.shared,
248274
MaxKey.shared,
249275
RegexValue(pattern: "^foo", options: "i"),
@@ -259,6 +285,7 @@ class TypeTest: FSTIntegrationTestCase {
259285
"objectId": BSONObjectId("507f191e810c19729de860ea"),
260286
"bsonTimestamp": BSONTimestamp(seconds: 123, increment: 456),
261287
"int32": Int32Value(1),
288+
"decimal128": Decimal128Value("-Infinity"),
262289
"min": MinKey.shared,
263290
"max": MaxKey.shared,
264291
"regex": RegexValue(pattern: "^foo", options: "i"),
@@ -310,9 +337,13 @@ class TypeTest: FSTIntegrationTestCase {
310337
"minValue": ["key": MinKey.shared],
311338
"booleanValue": ["key": true],
312339
"nanValue": ["key": Double.nan],
340+
"nanValue2": ["key": Decimal128Value("NaN")],
341+
"negativeInfinity": ["key": Decimal128Value("-Infinity")],
313342
"int32Value": ["key": Int32Value(1)],
314343
"doubleValue": ["key": 2.0],
315344
"integerValue": ["key": 3],
345+
"decimal128Value": ["key": Decimal128Value("345e-2")],
346+
"infinity": ["key": Decimal128Value("Infinity")],
316347
"timestampValue": ["key": Timestamp(seconds: 100, nanoseconds: 123_456_000)],
317348
"bsonTimestampValue": ["key": BSONTimestamp(seconds: 1, increment: 2)],
318349
"stringValue": ["key": "string"],
@@ -340,9 +371,13 @@ class TypeTest: FSTIntegrationTestCase {
340371
"minValue",
341372
"booleanValue",
342373
"nanValue",
374+
"nanValue2",
375+
"negativeInfinity",
343376
"int32Value",
344377
"doubleValue",
345378
"integerValue",
379+
"decimal128Value",
380+
"infinity",
346381
"timestampValue",
347382
"bsonTimestampValue",
348383
"stringValue",

Firestore/core/src/model/value_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pb_bytes_array_s* kInt32TypeFieldValue =
101101
/** The key of a decimal128 in a map proto. */
102102
const char* kRawDecimal128TypeFieldValue = "__decimal128__";
103103
pb_bytes_array_s* kDecimal128TypeFieldValue =
104-
nanopb::MakeBytesArray(kRawInt32TypeFieldValue);
104+
nanopb::MakeBytesArray(kRawDecimal128TypeFieldValue);
105105

106106
/** The key of a BSON ObjectId in a map proto. */
107107
const char* kRawBsonObjectIdTypeFieldValue = "__oid__";

0 commit comments

Comments
 (0)