44import os
55from dls_barcode .data_store import Store
66
7- ID0 = "id1 "
8- ID1 = "id2 "
9- ID2 = "id3 "
10- ID3 = "id4 "
7+ ID0 = "id0 "
8+ ID1 = "id1 "
9+ ID2 = "id2 "
10+ ID3 = "id3 "
1111
1212class TestStore (unittest .TestCase ):
1313
@@ -52,6 +52,7 @@ def test_new_store_has_no_records_if_store_file_does_not_exist(self):
5252
5353 # Assert
5454 self .assertFalse (store .records )
55+ self .assert
5556 self .assertEquals (store .size (), 0 )
5657
5758 def test_new_store_loads_records_from_existing_store_file (self ):
@@ -65,6 +66,10 @@ def test_new_store_loads_records_from_existing_store_file(self):
6566 # Assert
6667 self .assertEquals (len (store .records ), len (record_strings ))
6768 self .assertEquals (store .size (), len (record_strings ))
69+ self .assertEquals (store .records [0 ].id , ID0 )
70+ self .assertEquals (store .records [1 ].id , ID1 )
71+ self .assertEquals (store .records [2 ].id , ID2 )
72+ self .assertEquals (store .records [3 ].id , ID3 )
6873
6974 def test_new_store_skips_invalid_lines_when_loading_records_from_file (self ):
7075 # Arrange
@@ -75,6 +80,8 @@ def test_new_store_skips_invalid_lines_when_loading_records_from_file(self):
7580
7681 # Assert
7782 self .assertEquals (len (store .records ), 2 )
83+ self .assertEquals (store .records [0 ].id , ID0 )
84+ self .assertEquals (store .records [1 ].id , ID2 )
7885
7986 def test_max_number_of_stored_records_is_the_store_capacity (self ):
8087 # Arrange
@@ -108,7 +115,7 @@ def test_minimum_store_capacity_is_2(self):
108115
109116 def test_new_store_has_records_sorted_most_recent_first (self ):
110117 # Arrange
111- self ._file_manager .read_lines .return_value = self ._get_record_strings ()
118+ self ._file_manager .read_lines .return_value = self ._get_unordered_record_strings ()
112119
113120 # Act
114121 store = self ._create_store ()
@@ -152,7 +159,7 @@ def test_given_a_store_when_merging_a_record_then_an_image_is_saved_to_file(self
152159 # Assert
153160 self ._pins_img_copy .save_as .assert_called_once ()
154161 ((image_filename_used ,), kwargs ) = self ._pins_img_copy .save_as .call_args
155- self .assertTrue (self ._expected_img_dir in image_filename_used )
162+ self .assertIn (self ._expected_img_dir , image_filename_used )
156163
157164 def test_given_an_empty_store_when_merging_a_record_then_the_record_is_added_to_the_store (self ):
158165 # Arrange
@@ -238,7 +245,7 @@ def test_given_a_store_when_merging_a_record_then_store_is_saved_to_backing_file
238245 self ._file_manager .write_lines .assert_called ()
239246 ((filename_used , record_lines_used ), kwargs ) = self ._file_manager .write_lines .call_args_list [0 ]
240247 self .assertEquals (filename_used , self ._expected_store_file )
241- self .assertTrue (holder_barcode in record_lines_used [0 ])
248+ self .assertIn (holder_barcode , record_lines_used [0 ])
242249
243250 def test_given_a_store_when_merging_a_record_then_store_is_saved_to_csv_file (self ):
244251 # Arrange
@@ -253,7 +260,7 @@ def test_given_a_store_when_merging_a_record_then_store_is_saved_to_csv_file(sel
253260 self ._file_manager .write_lines .assert_called ()
254261 ((filename_used , record_lines_used ), kwargs ) = self ._file_manager .write_lines .call_args_list [1 ]
255262 self .assertEquals (filename_used , self ._expected_csv_file )
256- self .assertTrue (holder_barcode in record_lines_used [0 ])
263+ self .assertIn (holder_barcode , record_lines_used [0 ])
257264
258265 def test_given_a_non_empty_store_of_size_equal_to_capacity_when_a_new_record_is_merged_then_oldest_record_is_deleted (self ):
259266 # Arrange
@@ -264,6 +271,7 @@ def test_given_a_non_empty_store_of_size_equal_to_capacity_when_a_new_record_is_
264271 old_store_size = store .size ()
265272 self .assertEquals (old_store_size , capacity )
266273 holder_barcode = "asd"
274+ oldest_record = store .records [- 1 ]
267275
268276 # Act
269277 store .merge_record (holder_barcode , self ._plate , self ._holder_img , self ._pins_img )
@@ -272,15 +280,14 @@ def test_given_a_non_empty_store_of_size_equal_to_capacity_when_a_new_record_is_
272280 self .assertEquals (store .size (), old_store_size )
273281 latest_rec = store .get_record (0 )
274282 self .assertEquals (latest_rec .holder_barcode , holder_barcode )
275- ids = [r .id for r in store .records ]
276- self .assertFalse (ID3 in ids )
283+ self .assertNotIn (oldest_record , store .records )
277284
278285 def test_multiple_records_can_be_deleted (self ):
279286 # Arrange
280287 self ._file_manager .read_lines .return_value = self ._get_record_strings ()
281288 store = self ._create_store ()
282289 old_store_size = store .size ()
283- records_to_delete = [r for r in store .records if r .id == ID1 or r .id == ID3 ]
290+ records_to_delete = [r for r in store .records if r .id == ID1 or r .id == ID3 ]
284291 self .assertTrue (records_to_delete )
285292
286293 # Act
@@ -347,6 +354,51 @@ def test_when_records_are_deleted_then_csv_file_is_updated(self):
347354 for r , l in zip (store .records , record_lines_used ):
348355 self .assertIn (r .to_csv_string (), l )
349356
357+ def test_given_a_non_empty_store_when_capacity_is_reduced_then_records_are_truncated_at_next_merge (self ):
358+ # Arrange
359+ old_capacity = 4
360+ self ._store_capacity .value .return_value = old_capacity
361+ self ._file_manager .read_lines .return_value = self ._get_record_strings ()
362+ store = self ._create_store ()
363+ self .assertEquals (store .size (), old_capacity )
364+
365+ new_capacity = 2
366+ self ._store_capacity .value .return_value = new_capacity
367+ holder_barcode = "asd"
368+ expected_truncated_records = store .records [1 :3 ]
369+ self .assertTrue (expected_truncated_records )
370+
371+ # Act
372+ store .merge_record (holder_barcode , self ._plate , self ._holder_img , self ._pins_img )
373+
374+ # Assert
375+ self .assertEquals (store .size (), new_capacity )
376+ latest_rec = store .get_record (0 )
377+ self .assertEquals (latest_rec .holder_barcode , holder_barcode )
378+ for r in expected_truncated_records :
379+ self .assertNotIn (r , store .records )
380+
381+ def test_given_a_non_empty_store_when_capacity_is_reduced_then_records_are_truncated_at_next_delete (self ):
382+ # Arrange
383+ old_capacity = 4
384+ self ._store_capacity .value .return_value = old_capacity
385+ self ._file_manager .read_lines .return_value = self ._get_record_strings ()
386+ store = self ._create_store ()
387+ self .assertEquals (store .size (), old_capacity )
388+
389+ new_capacity = 2
390+ self ._store_capacity .value .return_value = new_capacity
391+ record_to_delete = store .records [1 ]
392+ expected_truncated_record = store .records [3 ]
393+
394+ # Act
395+ store .delete_records ([record_to_delete ])
396+
397+ # Assert
398+ self .assertEquals (store .size (), new_capacity )
399+ self .assertNotIn (record_to_delete , store .records )
400+ self .assertNotIn (expected_truncated_record , store .records )
401+
350402 def _create_store (self ):
351403 return Store (self ._directory , self ._store_capacity , self ._file_manager )
352404
@@ -358,10 +410,18 @@ def _get_record_strings(self):
358410 str_rep .append (ID3 + ";1494238920.0;test" + ID3 + ".png;None;DLSL-004,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
359411 return str_rep
360412
413+ def _get_unordered_record_strings (self ):
414+ str_rep = list ()
415+ str_rep .append (ID0 + ";1494238905.0;test" + ID0 + ".png;None;DLSL-001,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
416+ str_rep .append (ID1 + ";1494238921.0;test" + ID1 + ".png;None;DLSL-002,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
417+ str_rep .append (ID2 + ";1494238901.0;test" + ID2 + ".png;None;DLSL-003,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
418+ str_rep .append (ID3 + ";1494238930.0;test" + ID3 + ".png;None;DLSL-004,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
419+ return str_rep
420+
361421 def _invalid_record_strings (self ):
362422 str_rep = list ()
363- str_rep .append ("f59c92c1;1494238920 .0;test.png;None;DLSL-009,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
364- str_rep .append (" Invalid record string" )
365- str_rep .append ("f59c92c2 ;1494238921.0;test.png;None;DLSL-008,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
423+ str_rep .append (ID0 + ";1494238925 .0;test.png;None;DLSL-009,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
424+ str_rep .append (ID1 + "; Invalid record string" )
425+ str_rep .append (ID2 + " ;1494238921.0;test.png;None;DLSL-008,DLSL-010,DLSL-011,DLSL-012;1569:1106:70-2307:1073:68-1944:1071:68" )
366426 return str_rep
367427
0 commit comments