atn832 / fake_cloud_firestore

BSD 2-Clause "Simplified" License
119 stars 93 forks source link

setData with keys containing dots should create nested documents #15

Closed suztomo closed 4 years ago

suztomo commented 4 years ago

documentReference.setData with keys containing dots should create nested documents. Test case to confirm the expected behavior:

import 'package:cloud_firestore_mocks/cloud_firestore_mocks.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
    testWidgets('Nested documents created by setData with keys containing dots',
      (WidgetTester tester) async {
    final fakeFirestore = MockFirestoreInstance()..setupFieldValueFactory();

    final documentRef1 = fakeFirestore
        .collection('users')
        .document();
    final createdTime =  DateTime.now();
    await documentRef1.setData(<String, dynamic>{
      'favorites.keyA.name': 'Banana',
      'favorites.keyA.created': createdTime,
    });
    await documentRef1.setData(<String, dynamic>{
      'favorites.keyB.name': 'Apple',
      'favorites.keyB.created': createdTime,
    });

    final snapshot = await documentRef1.get();
    expect(snapshot['favorites'], isNotNull); // This fails.
    final favoritesMap = snapshot['favorites'] as Map<dynamic, dynamic>;
    expect(favoritesMap.keys as Iterable<String>, hasLength(2));

    final bananaMap = favoritesMap['keyA'] as Map<dynamic, dynamic>;
    expect(bananaMap['name'] as String, 'Banana');
    expect(bananaMap['created'] as DateTime, createdTime);
  });
}

Screenshot from real Firebase (from a different example):

Screen Shot 2020-02-23 at 11 48 49 PM

I might contribute a PR to this issue.

atn832 commented 4 years ago

I just published your 3 PRs at https://pub.dev/packages/cloud_firestore_mocks#040. Thank you for the great update!

suztomo commented 4 years ago

@atn832 Thank you. The enhancement worked well.