Repository navigation
Expand file tree
/
Copy pathdocuments_integration_test.dart
More file actions
102 lines (87 loc) · 2.91 KB
/
Copy pathdocuments_integration_test.dart
File metadata and controls
102 lines (87 loc) · 2.91 KB
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
import 'dart:convert';
import 'dart:io';
import 'package:test/test.dart';
import 'package:typesense/typesense.dart';
void main() {
final env = Platform.environment;
final apiKey = env['TYPESENSE_API_KEY'] ?? 'xyz';
final host = env['TYPESENSE_HOST'] ?? '127.0.0.1';
final port = int.tryParse(env['TYPESENSE_PORT'] ?? '8108') ?? 8108;
final protocolValue = env['TYPESENSE_PROTOCOL'] ?? 'http';
final protocol = protocolValue == 'https' ? Protocol.https : Protocol.http;
final companies = [
{'id': '1', 'company_name': 'Stark Industries', 'num_employees': 5215},
{'id': '2', 'company_name': 'Acme Corp', 'num_employees': 1002},
{'id': '3', 'company_name': 'Dunder Mifflin', 'num_employees': 15},
];
late Client client;
late String collectionName;
setUpAll(() {
client = Client(Configuration(
apiKey,
nodes: {Node(protocol, host, port: port)},
));
});
setUp(() async {
collectionName =
'documents_dart_test_${DateTime.now().microsecondsSinceEpoch}';
await client.collections.create(Schema(
collectionName,
{
Field('company_name', type: Type.string),
Field('num_employees', type: Type.int32),
},
));
await client
.collection(collectionName)
.documents
.importJSONL(companies.map(json.encode).join('\n'));
});
tearDown(() async {
await client.collection(collectionName).delete();
});
test('importJSONL() accepts numeric options', () async {
final results = await client
.collection(collectionName)
.documents
.importJSONL(json.encode(companies.first), options: {
'action': 'upsert',
'batch_size': 100,
});
expect(json.decode(results), equals({'success': true}));
});
test('search() accepts numeric parameters', () async {
final results = await client.collection(collectionName).documents.search({
'q': '*',
'query_by': 'company_name',
'per_page': 2,
});
expect(results['found'], equals(3));
expect(results['hits'], hasLength(2));
});
test('updateByFilter() updates the matching documents', () async {
final documents = client.collection(collectionName).documents;
expect(
await documents.updateByFilter(
{'company_name': 'Large'}, {'filter_by': 'num_employees:>1000'}),
equals({'num_updated': 2}));
expect(
(await documents.search({
'q': 'Large',
'query_by': 'company_name',
}))['found'],
equals(2));
});
test('update() with filter_by updates only the document with the id',
() async {
final documents = client.collection(collectionName).documents;
await documents.update({'id': '3', 'company_name': 'Large'},
options: {'filter_by': 'num_employees:>1000'});
expect(
(await documents.search({
'q': 'Large',
'query_by': 'company_name',
}))['found'],
equals(1));
});
}