@@ -86,12 +86,48 @@ _get_index_count (mongoc_collection_t *collection)
86
86
return n ;
87
87
}
88
88
89
+ /* Util that downloads a file content into the given buffer. Returns num bytes
90
+ * read. */
91
+ static size_t
92
+ _download_file_into_buf (mongoc_gridfs_bucket_t * bucket ,
93
+ const bson_value_t * file_id ,
94
+ char * buf ,
95
+ size_t len )
96
+ {
97
+ bson_error_t error ;
98
+ size_t nread ;
99
+ mongoc_stream_t * down =
100
+ mongoc_gridfs_bucket_open_download_stream (bucket , file_id , & error );
101
+ ASSERT_OR_PRINT (down , error );
102
+ nread =
103
+ mongoc_stream_read (down , buf , len , 1 /* min read */ , 0 /* No timeout */ );
104
+ mongoc_stream_destroy (down );
105
+ ASSERT (nread > 0 );
106
+ return nread ;
107
+ }
108
+
109
+ /* Util for uploading a file with the given string as content */
110
+ static void
111
+ _upload_file_from_str (mongoc_gridfs_bucket_t * bucket ,
112
+ const char * filename ,
113
+ const char * content ,
114
+ const bson_t * opts ,
115
+ bson_value_t * file_id )
116
+ {
117
+ bson_error_t error ;
118
+ size_t nwritten ;
119
+ mongoc_stream_t * up = mongoc_gridfs_bucket_open_upload_stream (
120
+ bucket , filename , opts , file_id , & error );
121
+ ASSERT_OR_PRINT (up , error );
122
+ nwritten = mongoc_stream_write (up , (void * ) content , strlen (content ), 0 );
123
+ ASSERT_CMPINT (nwritten , = = , strlen (content ));
124
+ mongoc_stream_destroy (up );
125
+ }
126
+
89
127
void
90
128
_test_upload_and_download (bson_t * create_index_cmd )
91
129
{
92
130
mongoc_gridfs_bucket_t * gridfs ;
93
- mongoc_stream_t * upload_stream ;
94
- mongoc_stream_t * download_stream ;
95
131
bson_value_t file_id ;
96
132
mongoc_database_t * db ;
97
133
mongoc_client_t * client ;
@@ -128,22 +164,10 @@ _test_upload_and_download (bson_t *create_index_cmd)
128
164
129
165
gridfs = mongoc_gridfs_bucket_new (db , opts , NULL , NULL );
130
166
131
- upload_stream = mongoc_gridfs_bucket_open_upload_stream (
132
- gridfs , "my-file" , NULL , & file_id , NULL );
133
-
134
- ASSERT (upload_stream );
135
-
136
- /* write str to gridfs. */
137
- ASSERT_CMPINT (
138
- mongoc_stream_write (upload_stream , (void * ) str , strlen (str ), 0 ),
139
- = = ,
140
- strlen (str ));
141
- mongoc_stream_destroy (upload_stream );
167
+ _upload_file_from_str (gridfs , "my-file" , str , opts , & file_id );
142
168
143
169
/* download str into the buffer from gridfs. */
144
- download_stream =
145
- mongoc_gridfs_bucket_open_download_stream (gridfs , & file_id , NULL );
146
- mongoc_stream_read (download_stream , buf , 100 , 1 , 0 );
170
+ _download_file_into_buf (gridfs , & file_id , buf , sizeof buf );
147
171
148
172
/* compare. */
149
173
ASSERT (strcmp (buf , str ) == 0 );
@@ -152,7 +176,6 @@ _test_upload_and_download (bson_t *create_index_cmd)
152
176
ASSERT_CMPINT (_get_index_count (gridfs -> chunks ), = = , 2 );
153
177
154
178
bson_destroy (opts );
155
- mongoc_stream_destroy (download_stream );
156
179
mongoc_gridfs_bucket_destroy (gridfs );
157
180
mongoc_database_destroy (db );
158
181
mongoc_client_destroy (client );
@@ -869,10 +892,11 @@ test_upload_error (void *ctx)
869
892
mongoc_gridfs_bucket_t * gridfs ;
870
893
mongoc_stream_t * source ;
871
894
bson_error_t error = {0 };
895
+ char * const dbname = gen_collection_name ("test_upload_error" );
872
896
bool r ;
873
897
874
898
client = test_framework_new_default_client ();
875
- db = mongoc_client_get_database (client , "test" );
899
+ db = mongoc_client_get_database (client , dbname );
876
900
gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
877
901
source = mongoc_stream_file_new_for_path (
878
902
BSON_BINARY_DIR "/test1.bson" , O_RDONLY , 0 );
@@ -904,7 +928,7 @@ test_upload_error (void *ctx)
904
928
source = mongoc_stream_file_new_for_path (
905
929
BSON_BINARY_DIR "/test1.bson" , O_RDONLY , 0 );
906
930
BSON_ASSERT (source );
907
- db = mongoc_client_get_database (client , "test" );
931
+ db = mongoc_client_get_database (client , dbname );
908
932
gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
909
933
mongoc_gridfs_bucket_upload_from_stream (
910
934
gridfs , "test1" , source , NULL /* opts */ , NULL /* file id */ , & error );
@@ -914,6 +938,7 @@ test_upload_error (void *ctx)
914
938
mongoc_stream_close (source );
915
939
mongoc_stream_destroy (source );
916
940
mongoc_gridfs_bucket_destroy (gridfs );
941
+ bson_free (dbname );
917
942
mongoc_database_destroy (db );
918
943
mongoc_client_destroy (client );
919
944
}
@@ -928,10 +953,11 @@ test_find_w_session (void *ctx)
928
953
bson_error_t error = {0 };
929
954
bson_t opts ;
930
955
mongoc_client_session_t * session ;
956
+ char * dbname = gen_collection_name ("test_find_w_session" );
931
957
bool r ;
932
958
933
959
client = test_framework_new_default_client ();
934
- db = mongoc_client_get_database (client , "test" );
960
+ db = mongoc_client_get_database (client , dbname );
935
961
gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
936
962
session = mongoc_client_start_session (client , NULL , & error );
937
963
ASSERT_OR_PRINT (session , error );
@@ -949,6 +975,68 @@ test_find_w_session (void *ctx)
949
975
mongoc_gridfs_bucket_destroy (gridfs );
950
976
mongoc_client_session_destroy (session );
951
977
mongoc_database_destroy (db );
978
+ bson_free (dbname );
979
+ mongoc_client_destroy (client );
980
+ }
981
+
982
+ static void
983
+ test_find (void * ctx )
984
+ {
985
+ mongoc_client_t * const client = test_framework_new_default_client ();
986
+ char * const dbname = gen_collection_name ("test_find" );
987
+ mongoc_database_t * const db = mongoc_client_get_database (client , dbname );
988
+ mongoc_gridfs_bucket_t * const gridfs =
989
+ mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
990
+ mongoc_cursor_t * cursor ;
991
+ bson_error_t error = {0 };
992
+ bson_t const * found ;
993
+ bson_iter_t iter ;
994
+ char buffer [256 ] = {0 };
995
+ bool ok ;
996
+ bson_value_t const * found_id ;
997
+ const bson_t * const find_opts =
998
+ tmp_bson ("{'limit': 1, 'skip': 2, 'sort': {'metadata.testOrder': -1}}" );
999
+
1000
+ _upload_file_from_str (gridfs ,
1001
+ "file1" ,
1002
+ "First file" ,
1003
+ tmp_bson ("{'metadata': {'testOrder': 1}}" ),
1004
+ NULL );
1005
+ _upload_file_from_str (gridfs ,
1006
+ "file2" ,
1007
+ "Second file" ,
1008
+ tmp_bson ("{'metadata': {'testOrder': 2}}" ),
1009
+ NULL );
1010
+ _upload_file_from_str (gridfs ,
1011
+ "file3" ,
1012
+ "Third file" ,
1013
+ tmp_bson ("{'metadata': {'testOrder': 3}}" ),
1014
+ NULL );
1015
+ _upload_file_from_str (gridfs ,
1016
+ "file4" ,
1017
+ "Fourth file" ,
1018
+ tmp_bson ("{'metadata': {'testOrder': 4}}" ),
1019
+ NULL );
1020
+
1021
+ cursor = mongoc_gridfs_bucket_find (gridfs , tmp_bson ("{}" ), find_opts );
1022
+ ASSERT_OR_PRINT (!mongoc_cursor_error (cursor , & error ), error );
1023
+
1024
+ ok = mongoc_cursor_next (cursor , & found );
1025
+ ASSERT (ok && "No files returned" );
1026
+ ok = bson_iter_init_find (& iter , found , "_id" );
1027
+ ASSERT (ok && "Document has no '_id' ??" );
1028
+ found_id = bson_iter_value (& iter );
1029
+
1030
+ _download_file_into_buf (gridfs , found_id , buffer , sizeof buffer );
1031
+ ASSERT_CMPSTR (buffer , "Second file" );
1032
+
1033
+ ok = mongoc_cursor_next (cursor , & found );
1034
+ ASSERT (!(ok && "More than one file returned" ));
1035
+
1036
+ mongoc_cursor_destroy (cursor );
1037
+ mongoc_gridfs_bucket_destroy (gridfs );
1038
+ bson_free (dbname );
1039
+ mongoc_database_destroy (db );
952
1040
mongoc_client_destroy (client );
953
1041
}
954
1042
@@ -1079,5 +1167,12 @@ test_gridfs_bucket_install (TestSuite *suite)
1079
1167
NULL ,
1080
1168
test_framework_skip_if_no_sessions ,
1081
1169
test_framework_skip_if_no_crypto );
1170
+ TestSuite_AddFull (suite ,
1171
+ "/gridfs/find" ,
1172
+ test_find ,
1173
+ NULL ,
1174
+ NULL ,
1175
+ test_framework_skip_if_no_sessions ,
1176
+ test_framework_skip_if_no_crypto );
1082
1177
TestSuite_AddLive (suite , "/gridfs/options" , test_gridfs_bucket_opts );
1083
1178
}
0 commit comments