Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for throwing exceptions on get(), update() and delete() #275

Merged
merged 4 commits into from Oct 24, 2023

Conversation

mrunix00
Copy link
Contributor

@mrunix00 mrunix00 commented Oct 23, 2023

Fixes #272

@atn832 atn832 merged commit 83caafa into atn832:master Oct 24, 2023
3 of 4 checks passed
@atn832
Copy link
Owner

atn832 commented Oct 24, 2023

Thanks for the PR @mrunix00! Interestingly, it works even on a different reference:

// Works on the same reference.
expect(() => doc.set({'name': 'Bob'}), throwsA(isA<FirebaseException>()));
// Works on a different reference of the same document.
expect(() => instance.collection('users').doc(uid).set({'name': 'Bob'}),
  throwsA(isA<FirebaseException>()));

It's probably luck and how Object.hashcode is implemented. Just to be sure, I'll explicitly implement hashcode in a separate PR.

@atn832
Copy link
Owner

atn832 commented Oct 24, 2023

However, it does not work for Queries:

      final movies = firestore.collection('movies');
      final query = firestore
          .collection('movies')
          .where('title', isEqualTo: 'Test Movie');

      await movies.add({
        'title': 'Test Movie',
      });

      whenCalling(Invocation.method(#get, null))
          .on(query)
          .thenThrow(FirebaseException(plugin: 'firestore'));

      // this throws the exception correctly.
      whenCalling(Invocation.method(#get, null))
          .on(query)
          .thenThrow(FirebaseException(plugin: 'firestore'));

      // but this does not throw the exception.
      expect(
        () async => await firestore
            .collection('movies')
            .where('title', isEqualTo: 'Test Movie')
            .get(GetOptions(source: Source.server)),
        throwsA(isA<FirebaseException>()),
      );

@atn832
Copy link
Owner

atn832 commented Oct 24, 2023

Just uploaded your PR to version 2.4.3. Thanks again! https://pub.dev/packages/fake_cloud_firestore/changelog#243

@mrunix00
Copy link
Contributor Author

However, it does not work for Queries:

      final movies = firestore.collection('movies');
      final query = firestore
          .collection('movies')
          .where('title', isEqualTo: 'Test Movie');

      await movies.add({
        'title': 'Test Movie',
      });

      whenCalling(Invocation.method(#get, null))
          .on(query)
          .thenThrow(FirebaseException(plugin: 'firestore'));

      // this throws the exception correctly.
      whenCalling(Invocation.method(#get, null))
          .on(query)
          .thenThrow(FirebaseException(plugin: 'firestore'));

      // but this does not throw the exception.
      expect(
        () async => await firestore
            .collection('movies')
            .where('title', isEqualTo: 'Test Movie')
            .get(GetOptions(source: Source.server)),
        throwsA(isA<FirebaseException>()),
      );

Yeah I just noticed that lately, I'll try to find a good fix for that

@atn832
Copy link
Owner

atn832 commented Oct 24, 2023

I debugged a bit earlier, and it seemed that when we call where, it creates an operator function, and this function has a different hash every time even if it has the same parameters. Perhaps if we set up the operator function to return consistent hashes, it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot throw exception on DocumentReference.get() call (#get)
2 participants