Пример #1
0
 def collection(self, name) -> 'CollectionReference':
     from mockfirestore.collection import CollectionReference
     document = get_by_path(self._data, self._path)
     new_path = self._path + [name]
     if name not in document:
         set_by_path(self._data, new_path, {})
     return CollectionReference(self._data, new_path, parent=self)
Пример #2
0
 def document(self, name: Optional[str] = None) -> DocumentReference:
     collection = get_by_path(self._data, self._path)
     if name is None:
         name = generate_random_string()
     new_path = self._path + [name]
     if name not in collection:
         set_by_path(self._data, new_path, {})
     return DocumentReference(self._data, new_path, parent=self)
Пример #3
0
 def set(self, data: Dict, merge=False):
     if merge:
         try:
             self.update(deepcopy(data))
         except NotFound:
             self.set(data)
     else:
         set_by_path(self._data, self._path, deepcopy(data))
Пример #4
0
    def _update_data(new_values: dict, default: Any):
        for key, value in new_values.items():
            path = key.split('.')

            try:
                item = get_by_path(document, path)
            except (TypeError, KeyError):
                item = default

            set_by_path(data, path, item + value)
Пример #5
0
 def document(self, name=None):
     collection = get_by_path(self._data, self._path)
     if name is None:
         name = generate_random_string()
     new_path = self._path + [name]
     if name not in collection:
         set_by_path(self._data, new_path, {})
     return MyMockFirestore.MyDocumentReference(self._data,
                                                new_path,
                                                parent=self)
Пример #6
0
def _apply_transformations(document: Dict[str, Any], data: Dict[str, Any]) -> Dict[str, Any]:
    """Handles special fields like INCREMENT."""
    increments: Dict[str, int] = {}

    for key, value in get_document_iterator(data):
        if not value.__class__.__module__.startswith('google.cloud.firestore'):
            # Unfortunately, we can't use `isinstance` here because that would require
            # us to declare google-cloud-firestore as a dependency for this library.
            # However, it's somewhat strange that the mocked version of the library
            # requires the library itself, so we'll just leverage this heuristic as a
            # means of identifying it.
            #
            # Furthermore, we don't hardcode the full module name, since the original
            # library seems to use a thin shim to perform versioning. e.g. at the time
            # of writing, the full module name is `google.cloud.firestore_v1.transforms`,
            # and it can evolve to `firestore_v2` in the future.
            continue

        transformer = value.__class__.__name__
        if transformer == 'Increment':
            increments[key] = value.value

        # All other transformations can be applied as needed.
        # See #29 for tracking.

    for key, value in increments.items():
        path = key.split('.')

        try:
            item = get_by_path(document, path)
        except (TypeError, KeyError):
            item = 0

        set_by_path(data, path, item + value)

    document.update(data)
Пример #7
0
 def set(self, data: Dict, merge=False):
     if merge:
         self.update(deepcopy(data))
     else:
         set_by_path(self._data, self._path, deepcopy(data))