@@ -86,12 +86,48 @@ _get_index_count (mongoc_collection_t *collection)
8686 return n ;
8787}
8888
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+
89127void
90128_test_upload_and_download (bson_t * create_index_cmd )
91129{
92130 mongoc_gridfs_bucket_t * gridfs ;
93- mongoc_stream_t * upload_stream ;
94- mongoc_stream_t * download_stream ;
95131 bson_value_t file_id ;
96132 mongoc_database_t * db ;
97133 mongoc_client_t * client ;
@@ -128,22 +164,10 @@ _test_upload_and_download (bson_t *create_index_cmd)
128164
129165 gridfs = mongoc_gridfs_bucket_new (db , opts , NULL , NULL );
130166
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 );
142168
143169 /* 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 );
147171
148172 /* compare. */
149173 ASSERT (strcmp (buf , str ) == 0 );
@@ -152,7 +176,6 @@ _test_upload_and_download (bson_t *create_index_cmd)
152176 ASSERT_CMPINT (_get_index_count (gridfs -> chunks ), = = , 2 );
153177
154178 bson_destroy (opts );
155- mongoc_stream_destroy (download_stream );
156179 mongoc_gridfs_bucket_destroy (gridfs );
157180 mongoc_database_destroy (db );
158181 mongoc_client_destroy (client );
@@ -869,10 +892,11 @@ test_upload_error (void *ctx)
869892 mongoc_gridfs_bucket_t * gridfs ;
870893 mongoc_stream_t * source ;
871894 bson_error_t error = {0 };
895+ char * const dbname = gen_collection_name ("test_upload_error" );
872896 bool r ;
873897
874898 client = test_framework_new_default_client ();
875- db = mongoc_client_get_database (client , "test" );
899+ db = mongoc_client_get_database (client , dbname );
876900 gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
877901 source = mongoc_stream_file_new_for_path (
878902 BSON_BINARY_DIR "/test1.bson" , O_RDONLY , 0 );
@@ -904,7 +928,7 @@ test_upload_error (void *ctx)
904928 source = mongoc_stream_file_new_for_path (
905929 BSON_BINARY_DIR "/test1.bson" , O_RDONLY , 0 );
906930 BSON_ASSERT (source );
907- db = mongoc_client_get_database (client , "test" );
931+ db = mongoc_client_get_database (client , dbname );
908932 gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
909933 mongoc_gridfs_bucket_upload_from_stream (
910934 gridfs , "test1" , source , NULL /* opts */ , NULL /* file id */ , & error );
@@ -914,6 +938,7 @@ test_upload_error (void *ctx)
914938 mongoc_stream_close (source );
915939 mongoc_stream_destroy (source );
916940 mongoc_gridfs_bucket_destroy (gridfs );
941+ bson_free (dbname );
917942 mongoc_database_destroy (db );
918943 mongoc_client_destroy (client );
919944}
@@ -928,10 +953,11 @@ test_find_w_session (void *ctx)
928953 bson_error_t error = {0 };
929954 bson_t opts ;
930955 mongoc_client_session_t * session ;
956+ char * dbname = gen_collection_name ("test_find_w_session" );
931957 bool r ;
932958
933959 client = test_framework_new_default_client ();
934- db = mongoc_client_get_database (client , "test" );
960+ db = mongoc_client_get_database (client , dbname );
935961 gridfs = mongoc_gridfs_bucket_new (db , NULL , NULL , NULL );
936962 session = mongoc_client_start_session (client , NULL , & error );
937963 ASSERT_OR_PRINT (session , error );
@@ -949,6 +975,68 @@ test_find_w_session (void *ctx)
949975 mongoc_gridfs_bucket_destroy (gridfs );
950976 mongoc_client_session_destroy (session );
951977 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 );
9521040 mongoc_client_destroy (client );
9531041}
9541042
@@ -1079,5 +1167,12 @@ test_gridfs_bucket_install (TestSuite *suite)
10791167 NULL ,
10801168 test_framework_skip_if_no_sessions ,
10811169 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 );
10821177 TestSuite_AddLive (suite , "/gridfs/options" , test_gridfs_bucket_opts );
10831178}
0 commit comments