From 486ab057febdaf6763a2f24718fa4bfdad3ff0b4 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 17 Apr 2024 13:29:20 +0200 Subject: [PATCH 1/3] Tests to clarify number conversion --- .../tests/src/tests/shared-realms.ts | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/integration-tests/tests/src/tests/shared-realms.ts b/integration-tests/tests/src/tests/shared-realms.ts index f88d8f67af..3c54bf30c8 100644 --- a/integration-tests/tests/src/tests/shared-realms.ts +++ b/integration-tests/tests/src/tests/shared-realms.ts @@ -134,4 +134,61 @@ describe("SharedRealm operations", () => { expect(this.realm.objectForPrimaryKey("Person", "Bob")).primaryKey.equals("Bob"); }); }); + + describe("Number conversion", () => { + beforeEach(() => { + Realm.clearTestState(); + }); + + it("Int field does not accept Infinity", async () => { + const IntSchema = { + name: "IntSchema", + properties: { + id: "int", + number: "int", + }, + primaryKey: "id", + }; + + const realm = await Realm.open({ + inMemory: true, + schema: [IntSchema], + }); + + expect(() => { + realm.write(() => { + realm.create(IntSchema.name, { + id: 1, + number: Infinity, + }); + }) + }).to.throw("The number Infinity cannot be converted to a BigInt because it is not an integer"); + }); + + it("Double field accepts Infinity", async () => { + const DoubleSchema = { + name: "DoubleSchema", + properties: { + id: "int", + number: "double", + }, + primaryKey: "id", + }; + + const realm = await Realm.open({ + inMemory: true, + schema: [DoubleSchema], + }); + + + const obj = realm.write(() => { + return realm.create(DoubleSchema.name, { + id: 1, + number: Infinity, + }); + }); + + expect(obj.number).equals(Infinity); + }); + }); }); From d588b77e13437d83d414b496671f7095b32bc501 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 17 Apr 2024 13:57:54 +0200 Subject: [PATCH 2/3] Linting --- integration-tests/tests/src/tests/shared-realms.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/integration-tests/tests/src/tests/shared-realms.ts b/integration-tests/tests/src/tests/shared-realms.ts index 3c54bf30c8..3d9b003855 100644 --- a/integration-tests/tests/src/tests/shared-realms.ts +++ b/integration-tests/tests/src/tests/shared-realms.ts @@ -161,7 +161,7 @@ describe("SharedRealm operations", () => { id: 1, number: Infinity, }); - }) + }); }).to.throw("The number Infinity cannot be converted to a BigInt because it is not an integer"); }); @@ -180,7 +180,6 @@ describe("SharedRealm operations", () => { schema: [DoubleSchema], }); - const obj = realm.write(() => { return realm.create(DoubleSchema.name, { id: 1, From 8652a07e2b816f8e46e61c5dec45988913114359 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Wed, 17 Apr 2024 15:23:06 +0200 Subject: [PATCH 3/3] Test should also pass on RN --- integration-tests/tests/src/tests/shared-realms.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/integration-tests/tests/src/tests/shared-realms.ts b/integration-tests/tests/src/tests/shared-realms.ts index 3d9b003855..02afcfc1af 100644 --- a/integration-tests/tests/src/tests/shared-realms.ts +++ b/integration-tests/tests/src/tests/shared-realms.ts @@ -155,6 +155,10 @@ describe("SharedRealm operations", () => { schema: [IntSchema], }); + // Infinity is not a valid integer. Node and RN throw different error messages so we only + // check if throws. + // node: "The number Infinity cannot be converted to a BigInt because it is not an integer" + // RN: "number is not integral" expect(() => { realm.write(() => { realm.create(IntSchema.name, { @@ -162,7 +166,7 @@ describe("SharedRealm operations", () => { number: Infinity, }); }); - }).to.throw("The number Infinity cannot be converted to a BigInt because it is not an integer"); + }).to.throw(); }); it("Double field accepts Infinity", async () => {