Skip to content

Commit

Permalink
Fix FLX error scenario tests (compensating writes) (realm#4615)
Browse files Browse the repository at this point in the history
* Fix FLX error scenario tests (compensating writes)

Co-authored-by: Andrew Meyer <[email protected]>
  • Loading branch information
kneth and takameyer committed Jun 7, 2022
1 parent 0fb0967 commit c203614
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ x.x.x Release notes (yyyy-MM-dd)
* <Either mention core version or upgrade>
* <Using Realm Core vX.Y.Z>
* <Upgraded Realm Core from vX.Y.Z to vA.B.C>
* Fix FLX error scenario tests.
* Fixed a bug preventing opening a synced Realm as a local Realm. (since v10.18.0)

10.19.0 Release notes (2022-6-2)
Expand Down
59 changes: 26 additions & 33 deletions integration-tests/tests/src/tests/sync/flexible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import Realm, { BSON, ClientResetMode, FlexibleSyncConfiguration, SessionStopPol

import { authenticateUserBefore, importAppBefore, openRealmBeforeEach } from "../../hooks";
import { DogSchema, IPerson, PersonSchema } from "../../schemas/person-and-dog-with-object-ids";
import { expectInvalidWriteSyncError } from "../../utils/expect-sync-error";
import { closeAndReopenRealm, closeRealm } from "../../utils/close-realm";

const FlexiblePersonSchema = { ...PersonSchema, properties: { ...PersonSchema.properties, nonQueryable: "string?" } };
Expand Down Expand Up @@ -1569,56 +1568,50 @@ describe.skipIf(environment.missingServer, "Flexible sync", function () {
// TODO test more complex integration scenarios, e.g. links, embedded objects, collections, complex queries

describe("error scenarios", function () {
it("triggers an error if items are added without a subscription", async function (this: RealmContext) {
await expectInvalidWriteSyncError(this.config, this.user, (realm) => {
realm.write(() => {
return realm.create<IPerson>(FlexiblePersonSchema.name, {
it("throw an exception if items are added without a subscription", function (this: RealmContext) {
this.realm.write(() => {
expect(() => {
this.realm.create<IPerson>(FlexiblePersonSchema.name, {
_id: new BSON.ObjectId(),
name: "Tom",
age: 36,
});
});
}).to.throw("Cannot write to class Person when no flexible sync subscription has been created.");
});
});

it("triggers an error and deletes the item if an item not matching the filter is created", async function (this: RealmContext) {
it("deletes the item if an item not matching the filter is created", async function (this: RealmContext) {
await addSubscriptionAndSync(this.realm, this.realm.objects(FlexiblePersonSchema.name).filtered("age < 30"));

await expectInvalidWriteSyncError(
this.config,
this.user,
(realm) => {
realm.write(() => {
realm.create<IPerson>(FlexiblePersonSchema.name, {
_id: new BSON.ObjectId(),
name: "Tom Old",
age: 99,
});
});
},
() => {
expect(this.realm.objects(FlexiblePersonSchema.name)).to.have.length(0);
},
);
this.realm.write(() => {
this.realm.create<IPerson>(FlexiblePersonSchema.name, {
_id: new BSON.ObjectId(),
name: "Tom Old",
age: 99,
});
});

await this.realm.syncSession?.downloadAllServerChanges();
expect(this.realm.objects(FlexiblePersonSchema.name)).to.have.length(0);
});

it("triggers an error if you remove a subscription without waiting for server acknowledgement, then modify objects that were only matched by the now removed subscription", async function (this: RealmContext) {
await expectInvalidWriteSyncError(this.config, this.user, async (realm) => {
const { sub } = await addSubscriptionForPersonAndSync(this.realm);
it("throw an exception if you remove a subscription without waiting for server acknowledgement, then modify objects that were only matched by the now removed subscription", async function (this: RealmContext) {
const { sub } = await addSubscriptionForPersonAndSync(this.realm);

this.realm.subscriptions.update((mutableSubs) => {
mutableSubs.removeSubscription(sub);
});
this.realm.subscriptions.update((mutableSubs) => {
mutableSubs.removeSubscription(sub);
});

// Deliberately not waiting for synchronisation here
// Deliberately not waiting for synchronisation here

realm.write(function () {
return realm.create<IPerson>(FlexiblePersonSchema.name, {
this.realm.write(() => {
expect(() => {
this.realm.create<IPerson>(FlexiblePersonSchema.name, {
_id: new BSON.ObjectId(),
name: "Tom",
age: 36,
});
});
}).to.throw("Cannot write to class Person when no flexible sync subscription has been created.");
});
});

Expand Down

0 comments on commit c203614

Please sign in to comment.