-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcollections_test.dart
133 lines (124 loc) · 4.24 KB
/
collections_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
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:typesense/src/collections.dart';
import 'package:typesense/src/models/models.dart';
import 'test_utils.mocks.dart';
void main() {
late Collections collections;
late MockApiCall mockApiCall;
late MockCollectionsApiCall mockCollectionsApiCall;
setUp(() {
mockApiCall = MockApiCall();
mockCollectionsApiCall = MockCollectionsApiCall();
collections = Collections(mockApiCall, mockCollectionsApiCall);
});
group('Collections', () {
test('has a resourcepath', () {
expect(Collections.resourcepath, equals('/collections'));
});
test('retrieve() calls CollectionsApiCall.get()', () async {
final schemaList = [
{
"num_documents": 1250,
"name": "companies",
"fields": [
{"name": "company_name", "type": "string"},
{"name": "num_employees", "type": "int32"},
{"name": "country", "type": "string", "facet": true}
],
"default_sorting_field": "num_employees"
},
{
"num_documents": 1250,
"name": "ceos",
"fields": [
{"name": "company_name", "type": "string"},
{"name": "full_name", "type": "string"},
{"name": "from_year", "type": "int32"}
],
"default_sorting_field": "from_year"
}
];
when(mockCollectionsApiCall.get('/collections'))
.thenAnswer((realInvocation) => Future.value(schemaList));
final schemas = await collections.retrieve();
expect(schemas[0].name, equals('companies'));
expect(schemas[0].documentCount, equals(1250));
expect(
schemas[0].fields,
equals({
Field('company_name', type: Type.string),
Field('num_employees', type: Type.int32),
Field('country', type: Type.string, isFacetable: true),
}));
expect(schemas[0].defaultSortingField,
equals(Field('num_employees', type: Type.int32)));
expect(schemas[1].name, equals('ceos'));
expect(schemas[1].documentCount, equals(1250));
expect(
schemas[1].fields,
equals({
Field('company_name', type: Type.string),
Field('from_year', type: Type.int32),
Field('full_name', type: Type.string),
}));
expect(schemas[1].defaultSortingField,
equals(Field('from_year', type: Type.int32)));
});
test('create() calls ApiCall.post()', () async {
when(mockApiCall.post(
'/collections',
bodyParameters: {
"name": "companies",
"fields": [
{
"name": "company_name",
"type": "string",
},
{
"name": "num_employees",
"type": "int32",
},
{
"name": "country",
"type": "string",
'facet': true,
}
],
"default_sorting_field": "num_employees"
},
)).thenAnswer((realInvocation) => Future.value({
"name": "companies",
"num_documents": 0,
"fields": [
{"name": "company_name", "type": "string"},
{"name": "num_employees", "type": "int32"},
{"name": "country", "type": "string", "facet": true}
],
"default_sorting_field": "num_employees"
}));
final schema = await collections.create(
Schema(
'companies',
{
Field('company_name', type: Type.string),
Field('num_employees', type: Type.int32),
Field('country', type: Type.string, isFacetable: true),
},
defaultSortingField: Field('num_employees', type: Type.int32),
),
);
expect(schema.name, equals('companies'));
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)));
expect(schema.documentCount, equals(0));
});
});
}