/
gridfs_tests.dart
165 lines (155 loc) · 5.48 KB
/
gridfs_tests.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
library gridfs_tests;
import 'package:mongo_dart/mongo_dart.dart';
import 'dart:uri';
import 'dart:io';
import 'dart:crypto';
import 'dart:async';
import 'package:unittest/unittest.dart';
const DefaultUri = 'mongodb://127.0.0.1/';
class MockConsumer<S, T> implements StreamConsumer<S, T> {
List<S> data = <S>[];
Future<T> consume(Stream<S> stream) {
var completer = new Completer();
stream.listen(_onData, onDone: () => completer.complete(null));
return completer.future;
}
_onData(chunk) {
data.addAll(chunk);
}
}
clearFSCollections(GridFS gridFS) {
gridFS.files.remove();
gridFS.chunks.remove();
}
testSmall(){
Db db = new Db('${DefaultUri}mongo_dart-test');
db.open().then(expectAsync1((c){
List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff];
GridFS gridFS = new GridFS(db);
clearFSCollections(gridFS);
return testInOut(data, gridFS);
})).then((c){
db.close();
});
}
testBig(){
Db db = new Db('${DefaultUri}mongo_dart-test');
db.open().then(expectAsync1((c){
List<int> smallData = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff];
int target = GridFS.DEFAULT_CHUNKSIZE * 3;
List<int> data = new List();
while (data.length < target) {
data.addAll(smallData);
}
GridFS gridFS = new GridFS(db);
clearFSCollections(gridFS);
return testInOut(data, gridFS);
})).then((c){
db.close();
});
}
tesSomeChunks(){
Db db = new Db('${DefaultUri}mongo_dart-test');
db.open().then(expectAsync1((c){
List<int> smallData = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff];
GridFS.DEFAULT_CHUNKSIZE = 9;
int target = GridFS.DEFAULT_CHUNKSIZE * 3;
List<int> data = new List();
while (data.length < target) {
data.addAll(smallData);
}
GridFS gridFS = new GridFS(db);
clearFSCollections(gridFS);
return testInOut(data, gridFS);
})).then((c){
db.close();
});
}
Future testInOut(List<int> data, GridFS gridFS) {
var consumer = new MockConsumer();
var out = new IOSink(consumer);
return getInitialState(gridFS).then(expectAsync1((List<int> initialState){
var inputStream = new Stream.fromIterable([data]);
GridIn input = gridFS.createFile(inputStream, "test");
return input.save().then(expectAsync1((c) {
return gridFS.findOne(where.eq("_id", input.id)).then(expectAsync1((GridOut gridOut) {
expect(gridOut,isNotNull, reason: "Did not find file by Id");
expect(input.id, gridOut.id, reason: "Ids not equal.");
expect(GridFS.DEFAULT_CHUNKSIZE, gridOut.chunkSize, reason: "Chunk size not the same.");
expect("test", gridOut.filename, reason: "Filename not equal");
return gridOut.writeTo(out);
})).then(expectAsync1((c){
expect(data, orderedEquals(consumer.data));
return getInitialState(gridFS);
}));
}));
}));
}
Future<List<int>> getInitialState(GridFS gridFS) {
Completer completer = new Completer();
List<Future<int>> futures = new List();
futures.add(gridFS.files.count());
futures.add(gridFS.chunks.count());
Future.wait(futures).then((List<double> futureResults) {
List<int> result = new List<int>.fixedLength(2);
result[0] = futureResults[0].toInt();
result[1] = futureResults[1].toInt();
completer.complete(result);
});
return completer.future;
}
testChunkTransformerOneChunk(){
new Stream.fromIterable([[1,2,3,4,5,6,7,8,9,10,11]]).transform(new ChunkTransformer(3))
.toList().then(expectAsync1((chunkedList){
expect(chunkedList[0],orderedEquals([1,2,3]));
expect(chunkedList[1],orderedEquals([4,5,6]));
expect(chunkedList[2],orderedEquals([7,8,9]));
expect(chunkedList[3],orderedEquals([10,11]));
}));
}
testChunkTransformerSeveralChunks(){
new Stream.fromIterable([[1,2,3,4],[5],[6,7],[8,9,10,11]]).transform(new ChunkTransformer(3))
.toList().then(expectAsync1((chunkedList){
expect(chunkedList[0],orderedEquals([1,2,3]));
expect(chunkedList[1],orderedEquals([4,5,6]));
expect(chunkedList[2],orderedEquals([7,8,9]));
expect(chunkedList[3],orderedEquals([10,11]));
}));
}
testFileToGridFSToFile() {
GridFS.DEFAULT_CHUNKSIZE = 30;
GridIn input;
var path = new Path(new Options().script).directoryPath;
var inputStream = new File('$path/gridfs_testdata_in.txt').openRead();
Db db = new Db('${DefaultUri}mongo_dart-test');
db.open().then(expectAsync1((c){
var gridFS = new GridFS(db);
clearFSCollections(gridFS);
input = gridFS.createFile(inputStream, "test");
return input.save();
})).then(expectAsync1((c) {
var gridFS = new GridFS(db);
return gridFS.getFile('test');
})).then(expectAsync1((GridOut gridOut) {
return gridOut.writeToFilename('$path/gridfs_testdata_out.txt');
})).then(expectAsync1((c){
List<int> dataIn = new File('$path/gridfs_testdata_in.txt').readAsBytesSync();
List<int> dataOut = new File('$path/gridfs_testdata_out.txt').readAsBytesSync();
expect(dataOut, orderedEquals(dataIn));
db.close();
}));
}
main(){
initBsonPlatform();
group('ChunkTransformer tests:', (){
test('testChunkTransformer',testChunkTransformerOneChunk);
test('testChunkTransformerSeveralChunks',testChunkTransformerSeveralChunks);
});
group('GridFS tests:', (){
setUp(() => GridFS.DEFAULT_CHUNKSIZE = 256 * 1024);
test('testSmall',testSmall);
test('tesSomeChunks',tesSomeChunks);
test('testBig',testBig);
test('testFileToGridFSToFile',testFileToGridFSToFile);
});
}