@@ -3,7 +3,7 @@ mod tests {
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use simd_r_drive:: DataStore ;
5
5
use simd_r_drive_extensions:: {
6
- utils:: prefix_key, StorageOptionExt , TEST_OPTION_TOMBSTONE_MARKER ,
6
+ utils:: prefix_key, StorageOptionExt , TEST_OPTION_PREFIX , TEST_OPTION_TOMBSTONE_MARKER ,
7
7
} ;
8
8
use std:: io:: ErrorKind ;
9
9
use tempfile:: tempdir;
@@ -107,7 +107,7 @@ mod tests {
107
107
) ;
108
108
109
109
// Step 4: Ensure the entry still exists in storage (not fully deleted)
110
- let raw_entry = storage. read ( & prefix_key ( b"--extension-option--" , key) ) ;
110
+ let raw_entry = storage. read ( & prefix_key ( & TEST_OPTION_PREFIX , key) ) ;
111
111
assert ! (
112
112
raw_entry. is_some( ) ,
113
113
"Entry should still exist in storage even after writing None"
@@ -214,4 +214,115 @@ mod tests {
214
214
) ;
215
215
}
216
216
}
217
+
218
+ #[ test]
219
+ fn test_option_prefix_is_applied_for_some ( ) {
220
+ let ( _dir, storage) = create_temp_storage ( ) ;
221
+
222
+ let key = b"test_key_option" ;
223
+ let prefixed_key = prefix_key ( TEST_OPTION_PREFIX , key) ;
224
+ let test_value = Some ( TestData {
225
+ id : 456 ,
226
+ name : "Test Option Value" . to_string ( ) ,
227
+ } ) ;
228
+
229
+ // Write `Some(value)` with option handling
230
+ storage
231
+ . write_option ( key, test_value. as_ref ( ) )
232
+ . expect ( "Failed to write option" ) ;
233
+
234
+ // Ensure the prefixed key exists in storage
235
+ let raw_data = storage. read ( & prefixed_key) ;
236
+ assert ! (
237
+ raw_data. is_some( ) ,
238
+ "Expected data to be stored under the prefixed key"
239
+ ) ;
240
+
241
+ // Ensure the unprefixed key does not exist
242
+ let raw_data_unprefixed = storage. read ( key) ;
243
+ assert ! (
244
+ raw_data_unprefixed. is_none( ) ,
245
+ "Unprefixed key should not exist in storage"
246
+ ) ;
247
+
248
+ // Ensure we can read the value correctly
249
+ let retrieved = storage
250
+ . read_option :: < TestData > ( key)
251
+ . expect ( "Failed to read option" ) ;
252
+
253
+ assert_eq ! (
254
+ retrieved, test_value,
255
+ "Stored and retrieved option values do not match"
256
+ ) ;
257
+ }
258
+
259
+ #[ test]
260
+ fn test_option_prefix_is_applied_for_none ( ) {
261
+ let ( _dir, storage) = create_temp_storage ( ) ;
262
+
263
+ let key = b"test_key_none" ;
264
+ let prefixed_key = prefix_key ( TEST_OPTION_PREFIX , key) ;
265
+
266
+ // Write `None`
267
+ storage
268
+ . write_option :: < TestData > ( key, None )
269
+ . expect ( "Failed to write None with option handling" ) ;
270
+
271
+ // Ensure the prefixed key exists in storage (tombstone marker stored)
272
+ let raw_data = storage. read ( & prefixed_key) ;
273
+ assert ! (
274
+ raw_data. is_some( ) ,
275
+ "Expected tombstone marker to be stored under the prefixed key"
276
+ ) ;
277
+
278
+ // Ensure the unprefixed key does not exist
279
+ let raw_data_unprefixed = storage. read ( key) ;
280
+ assert ! (
281
+ raw_data_unprefixed. is_none( ) ,
282
+ "Unprefixed key should not exist in storage"
283
+ ) ;
284
+
285
+ // Ensure we can read the value correctly
286
+ let retrieved = storage
287
+ . read_option :: < TestData > ( key)
288
+ . expect ( "Failed to read None with option handling" ) ;
289
+
290
+ assert_eq ! (
291
+ retrieved, None ,
292
+ "Expected None when retrieving a stored tombstone marker"
293
+ ) ;
294
+ }
295
+
296
+ #[ test]
297
+ fn test_option_prefixing_does_not_affect_regular_storage ( ) {
298
+ let ( _dir, storage) = create_temp_storage ( ) ;
299
+
300
+ let key = b"test_key_option_non_prefixed" ;
301
+ let test_value = TestData {
302
+ id : 789 ,
303
+ name : "Non-Prefixed Option Value" . to_string ( ) ,
304
+ } ;
305
+
306
+ // Directly write a non-option value to the base storage
307
+ storage
308
+ . write ( key, & bincode:: serialize ( & test_value) . unwrap ( ) )
309
+ . expect ( "Failed to write non-option value" ) ;
310
+
311
+ // Ensure reading from the option-prefixed key fails (since it was not stored as an option)
312
+ let prefixed_key = prefix_key ( TEST_OPTION_PREFIX , key) ;
313
+ let raw_data_prefixed = storage. read ( & prefixed_key) ;
314
+ assert ! (
315
+ raw_data_prefixed. is_none( ) ,
316
+ "No option-prefixed entry should exist for a non-prefixed write"
317
+ ) ;
318
+
319
+ // Ensure we can still retrieve the non-prefixed stored value
320
+ let raw_bytes = storage. read ( key) . expect ( "Failed to read stored data" ) ;
321
+ let retrieved: TestData =
322
+ bincode:: deserialize ( & raw_bytes) . expect ( "Failed to deserialize TestData" ) ;
323
+ assert_eq ! (
324
+ retrieved, test_value,
325
+ "Non-prefixed value should be retrievable as a plain `T`"
326
+ ) ;
327
+ }
217
328
}
0 commit comments