|
| 1 | +// Copyright 2020, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:cloud_firestore/cloud_firestore.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | + |
| 8 | +void runVectorValueTests() { |
| 9 | + group('$VectorValue', () { |
| 10 | + late FirebaseFirestore firestore; |
| 11 | + |
| 12 | + setUpAll(() async { |
| 13 | + firestore = FirebaseFirestore.instance; |
| 14 | + }); |
| 15 | + |
| 16 | + Future<DocumentReference<Map<String, dynamic>>> initializeTest( |
| 17 | + String path, |
| 18 | + ) async { |
| 19 | + String prefixedPath = 'flutter-tests/$path'; |
| 20 | + await firestore.doc(prefixedPath).delete(); |
| 21 | + return firestore.doc(prefixedPath); |
| 22 | + } |
| 23 | + |
| 24 | + test('sets a $VectorValue & returns one', () async { |
| 25 | + DocumentReference<Map<String, dynamic>> doc = |
| 26 | + await initializeTest('vector-value'); |
| 27 | + |
| 28 | + await doc.set({ |
| 29 | + 'foo': const VectorValue([10.0, -10.0]), |
| 30 | + }); |
| 31 | + |
| 32 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 33 | + |
| 34 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 35 | + expect(vectorValue, isA<VectorValue>()); |
| 36 | + expect(vectorValue.toArray(), equals([10.0, -10.0])); |
| 37 | + }); |
| 38 | + |
| 39 | + test('updates a $VectorValue & returns', () async { |
| 40 | + DocumentReference<Map<String, dynamic>> doc = |
| 41 | + await initializeTest('vector-value-update'); |
| 42 | + |
| 43 | + await doc.set({ |
| 44 | + 'foo': const VectorValue([10.0, -10.0]), |
| 45 | + }); |
| 46 | + |
| 47 | + await doc.update({ |
| 48 | + 'foo': const VectorValue([-10.0, 10.0]), |
| 49 | + }); |
| 50 | + |
| 51 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 52 | + |
| 53 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 54 | + expect(vectorValue, isA<VectorValue>()); |
| 55 | + expect(vectorValue.toArray(), equals([-10.0, 10.0])); |
| 56 | + }); |
| 57 | + |
| 58 | + test('handles empty vector', () async { |
| 59 | + DocumentReference<Map<String, dynamic>> doc = |
| 60 | + await initializeTest('vector-value-empty'); |
| 61 | + |
| 62 | + try { |
| 63 | + await doc.set({ |
| 64 | + 'foo': const VectorValue([]), |
| 65 | + }); |
| 66 | + fail('Should have thrown an exception'); |
| 67 | + } catch (e) { |
| 68 | + expect(e, isA<FirebaseException>()); |
| 69 | + expect( |
| 70 | + (e as FirebaseException).code.contains('invalid-argument'), |
| 71 | + isTrue, |
| 72 | + ); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + test('handles single dimension vector', () async { |
| 77 | + DocumentReference<Map<String, dynamic>> doc = |
| 78 | + await initializeTest('vector-value-single'); |
| 79 | + |
| 80 | + await doc.set({ |
| 81 | + 'foo': const VectorValue([42.0]), |
| 82 | + }); |
| 83 | + |
| 84 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 85 | + |
| 86 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 87 | + expect(vectorValue, isA<VectorValue>()); |
| 88 | + expect(vectorValue.toArray(), equals([42.0])); |
| 89 | + }); |
| 90 | + |
| 91 | + test('handles maximum dimensions vector', () async { |
| 92 | + List<double> maxDimensions = List.filled(2048, 1); |
| 93 | + DocumentReference<Map<String, dynamic>> doc = |
| 94 | + await initializeTest('vector-value-max-dimensions'); |
| 95 | + |
| 96 | + await doc.set({ |
| 97 | + 'foo': VectorValue(maxDimensions), |
| 98 | + }); |
| 99 | + |
| 100 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 101 | + |
| 102 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 103 | + expect(vectorValue, isA<VectorValue>()); |
| 104 | + expect(vectorValue.toArray(), equals(maxDimensions)); |
| 105 | + }); |
| 106 | + |
| 107 | + test('handles maximum dimensions + 1 vector', () async { |
| 108 | + List<double> maxPlusOneDimensions = List.filled(2049, 1); |
| 109 | + DocumentReference<Map<String, dynamic>> doc = |
| 110 | + await initializeTest('vector-value-max-plus-one'); |
| 111 | + |
| 112 | + try { |
| 113 | + await doc.set({ |
| 114 | + 'foo': VectorValue(maxPlusOneDimensions), |
| 115 | + }); |
| 116 | + |
| 117 | + fail('Should have thrown an exception'); |
| 118 | + } catch (e) { |
| 119 | + expect(e, isA<FirebaseException>()); |
| 120 | + expect( |
| 121 | + (e as FirebaseException).code.contains('invalid-argument'), |
| 122 | + isTrue, |
| 123 | + ); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + test('handles very large values in vector', () async { |
| 128 | + DocumentReference<Map<String, dynamic>> doc = |
| 129 | + await initializeTest('vector-value-large-values'); |
| 130 | + |
| 131 | + await doc.set({ |
| 132 | + 'foo': const VectorValue([1e10, -1e10]), |
| 133 | + }); |
| 134 | + |
| 135 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 136 | + |
| 137 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 138 | + expect(vectorValue, isA<VectorValue>()); |
| 139 | + expect(vectorValue.toArray(), equals([1e10, -1e10])); |
| 140 | + }); |
| 141 | + |
| 142 | + test('handles floats in vector', () async { |
| 143 | + DocumentReference<Map<String, dynamic>> doc = |
| 144 | + await initializeTest('vector-value-floats'); |
| 145 | + |
| 146 | + await doc.set({ |
| 147 | + 'foo': const VectorValue([3.14, 2.718]), |
| 148 | + }); |
| 149 | + |
| 150 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 151 | + |
| 152 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 153 | + expect(vectorValue, isA<VectorValue>()); |
| 154 | + expect(vectorValue.toArray(), equals([3.14, 2.718])); |
| 155 | + }); |
| 156 | + |
| 157 | + test('handles negative values in vector', () async { |
| 158 | + DocumentReference<Map<String, dynamic>> doc = |
| 159 | + await initializeTest('vector-value-negative'); |
| 160 | + |
| 161 | + await doc.set({ |
| 162 | + 'foo': const VectorValue([-42.0, -100.0]), |
| 163 | + }); |
| 164 | + |
| 165 | + DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get(); |
| 166 | + |
| 167 | + VectorValue vectorValue = snapshot.data()!['foo']; |
| 168 | + expect(vectorValue, isA<VectorValue>()); |
| 169 | + expect(vectorValue.toArray(), equals([-42.0, -100.0])); |
| 170 | + }); |
| 171 | + }); |
| 172 | +} |
0 commit comments