@@ -2515,6 +2515,92 @@ def annotate_one_file(options = {})
2515
2515
expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2516
2516
end
2517
2517
end
2518
+
2519
+ context 'adding a new field' do
2520
+ let ( :class_name ) { :users }
2521
+ let ( :primary_key ) { :id }
2522
+ let ( :original_columns ) do
2523
+ [
2524
+ mock_column ( primary_key , :integer ) ,
2525
+ mock_column ( :name , :string )
2526
+ ]
2527
+ end
2528
+
2529
+ before do
2530
+ klass = mock_class ( class_name , primary_key , original_columns )
2531
+ @schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , options )
2532
+ annotate_one_file ( options )
2533
+
2534
+ # confirm we initialized annotaions in file before checking for changes
2535
+ expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2536
+ end
2537
+
2538
+ context 'when option "format_bare" is true' do
2539
+ let :options do
2540
+ { format_bare : true }
2541
+ end
2542
+
2543
+ it 'updates the fields list to include the new column' do
2544
+ new_column_list = original_columns + [ mock_column ( :new_column , :string ) ]
2545
+ klass = mock_class ( class_name , primary_key , new_column_list )
2546
+ @schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , options )
2547
+ annotate_one_file ( options )
2548
+
2549
+ expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2550
+ end
2551
+ end
2552
+
2553
+ context 'when option "format_yard" is true' do
2554
+ let :options do
2555
+ { format_yard : true }
2556
+ end
2557
+
2558
+ it 'updates the fields list to include the new column' do
2559
+ new_column_list = original_columns + [ mock_column ( :new_column , :string ) ]
2560
+ klass = mock_class ( class_name , primary_key , new_column_list )
2561
+ @schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , options )
2562
+ annotate_one_file ( options )
2563
+
2564
+ expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2565
+ end
2566
+ end
2567
+
2568
+ context 'when option "format_rdoc" is true' do
2569
+ let :options do
2570
+ { format_rdoc : true }
2571
+ end
2572
+
2573
+ it 'updates the fields list to include the new column' do
2574
+ new_column_list = original_columns + [ mock_column ( :new_column , :string ) ]
2575
+ klass = mock_class ( class_name , primary_key , new_column_list )
2576
+ @schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , options )
2577
+ annotate_one_file ( options )
2578
+
2579
+ expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2580
+ end
2581
+ end
2582
+
2583
+ context 'when option "format_markdown" is true' do
2584
+ let :options do
2585
+ { format_markdown : true }
2586
+ end
2587
+
2588
+ it 'updates the fields list to include the new column' do
2589
+ # The new column name must be shorter than the existing columns
2590
+ # becuase markdown formatting adds additional spacing. If the
2591
+ # column name is long, the header row has space added triggering a
2592
+ # difference even if we don't properly check the columns. By having
2593
+ # a shorter column name we are testing our column list comparison
2594
+ # and not an unintentional resizing.
2595
+ new_column_list = original_columns + [ mock_column ( :a , :string ) ]
2596
+ klass = mock_class ( class_name , primary_key , new_column_list )
2597
+ @schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , options )
2598
+ annotate_one_file ( options )
2599
+
2600
+ expect ( File . read ( @model_file_name ) ) . to eq ( "#{ @schema_info } #{ @file_content } " )
2601
+ end
2602
+ end
2603
+ end
2518
2604
end
2519
2605
2520
2606
describe 'with existing annotation => :before' do
0 commit comments