-
Notifications
You must be signed in to change notification settings - Fork 16
/
collection_test.dart
172 lines (159 loc) · 5.26 KB
/
collection_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:typesense/src/collection.dart';
import 'package:typesense/src/document.dart';
import 'package:typesense/src/override.dart';
import 'package:typesense/src/synonym.dart';
import 'package:typesense/src/models/models.dart';
import 'package:typesense/src/documents.dart';
import 'package:typesense/src/overrides.dart';
import 'package:typesense/src/synonyms.dart';
import 'test_utils.mocks.dart';
void main() {
late MockApiCall mockApiCall;
late MockDocumentsApiCall mockDocumentsApiCall;
late Collection collection;
setUp(() {
mockApiCall = MockApiCall();
mockDocumentsApiCall = MockDocumentsApiCall();
collection = Collection('companies', mockApiCall, mockDocumentsApiCall);
});
group('Collection', () {
final schemaMap = {
"name": "companies",
"num_documents": 1250,
"fields": [
{"name": "company_name", "type": "string"},
{"name": "num_employees", "type": "int32"},
{"name": "country", "type": "string", "facet": true}
],
"default_sorting_field": "num_employees"
};
test('retrieve() calls ApiCall.get()', () async {
when(
mockApiCall.get(
'/collections/companies',
),
).thenAnswer((realInvocation) => Future.value(schemaMap));
final schema = await collection.retrieve();
expect(schema.name, equals('companies'));
expect(schema.documentCount, equals(1250));
expect(
schema.fields,
equals(
{
Field('company_name', type: Type.string),
Field('num_employees', type: Type.int32),
Field('country', type: Type.string, isFacetable: true),
},
));
expect(schema.defaultSortingField,
equals(Field('num_employees', type: Type.int32)));
});
test('delete() calls ApiCall.delete()', () async {
when(
mockApiCall.delete(
'/collections/companies',
),
).thenAnswer((realInvocation) => Future.value(schemaMap));
final schema = await collection.delete();
expect(schema.name, equals('companies'));
expect(schema.documentCount, equals(1250));
expect(
schema.fields,
equals({
Field('company_name', type: Type.string),
Field('num_employees', type: Type.int32),
Field('country', type: Type.string, isFacetable: true),
}));
expect(schema.defaultSortingField,
equals(Field('num_employees', type: Type.int32)));
});
test('update() calls ApiCall.patch()', () async {
when(
mockApiCall.patch('/collections/companies', bodyParameters: {
"fields": [
{
"name": "company_category",
"type": "string",
},
{
"name": "num_employees",
"type": "int32",
"drop": true,
}
]
}),
).thenAnswer(
(realInvocation) => Future.value({
"fields": [
{
"name": "company_category",
"facet": false,
"index": true,
"infix": false,
"locale": "",
"optional": false,
"sort": false,
"type": "string"
},
{"drop": true, "name": "num_employees"}
]
}),
);
final updateSchema = UpdateSchema(
{
UpdateField('company_category', type: Type.string),
UpdateField('num_employees', type: Type.int32, shouldDrop: true),
},
);
final UpdateSchema schema = await collection.update(updateSchema);
expect(
schema.fields,
equals(
{
UpdateField('num_employees', shouldDrop: true),
UpdateField('company_category', type: Type.string, locale: '')
},
),
);
});
test('has a documents getter', () {
expect(collection.documents, isA<Documents>());
});
test('has a document method', () {
expect(collection.document('124'), isA<Document>());
});
test('has a overrides getter', () {
expect(collection.overrides, isA<Overrides>());
});
test('has a override method', () {
expect(collection.override('customize-apple'), isA<Override>());
});
test('has a synonyms getter', () {
expect(collection.synonyms, isA<Synonyms>());
});
test('has a synonym method', () {
expect(collection.synonym('employee'), isA<Synonym>());
});
});
test(
'Collection.document returns the same document object for a particular documentId',
() {
final document = collection.document('124');
expect(collection.document('124').hashCode, equals(document.hashCode));
});
test(
'Collection.override returns the same override object for a particular overrideId',
() {
final override = collection.override('customize-apple');
expect(collection.override('customize-apple').hashCode,
equals(override.hashCode));
});
test(
'Collection.synonym returns the same synonym object for a particular synonymId',
() {
final synonym = collection.synonym('employee');
expect(collection.synonym('employee').hashCode, equals(synonym.hashCode));
});
}