|
| 1 | +require_relative "spec_helper" |
| 2 | + |
| 3 | +describe "deprecated_associations plugin" do |
| 4 | + before do |
| 5 | + @db = Sequel.mock(:autoid=>1, :fetch=>[{:id=>1, :c_id=>2}], :numrows=>1) |
| 6 | + @c = Class.new(Sequel::Model(@db[:c])) |
| 7 | + def @c.name; :C end |
| 8 | + @c.columns :id, :c_id |
| 9 | + @c.plugin :deprecated_associations |
| 10 | + @warnings = warnings = [] |
| 11 | + @c.define_singleton_method(:warn){|a,*| warnings << a} |
| 12 | + @o = @c.load(:id=>1, :c_id=>2) |
| 13 | + end |
| 14 | + |
| 15 | + it "does not show warnings for non-deprecated singular associations" do |
| 16 | + @c.many_to_one :c, :class => @c |
| 17 | + @c.association_reflection(:c) |
| 18 | + @o.c |
| 19 | + @o.c_dataset |
| 20 | + @o.c = nil |
| 21 | + @warnings.must_equal [] |
| 22 | + end |
| 23 | + |
| 24 | + it "does not show warnings for non-deprecated plural associations" do |
| 25 | + @c.one_to_many :cs, :key => :c_id, :class => @c |
| 26 | + @c.association_reflection(:cs) |
| 27 | + @o.cs |
| 28 | + @o.cs_dataset |
| 29 | + o = @c.create |
| 30 | + @o.add_c(o) |
| 31 | + @o.remove_c(o) |
| 32 | + @o.remove_all_cs |
| 33 | + @warnings.must_equal [] |
| 34 | + end |
| 35 | + |
| 36 | + it "shows warnings for deprecated singular associations" do |
| 37 | + @c.many_to_one :c, :class => @c, :deprecated => true |
| 38 | + 2.times do |
| 39 | + @c.association_reflection(:c) |
| 40 | + @c.eager(:c) |
| 41 | + @c.eager_graph(:c) |
| 42 | + @c.where(:c=>@o).sql |
| 43 | + @o.c |
| 44 | + @o.c_dataset |
| 45 | + @o.c = nil |
| 46 | + end |
| 47 | + @warnings.must_equal [ |
| 48 | + "Access of association reflection for deprecated association: class:C association:c", |
| 49 | + "Calling deprecated association method: class:C association:c method:c", |
| 50 | + "Calling deprecated association method: class:C association:c method:c_dataset", |
| 51 | + "Calling deprecated association method: class:C association:c method:c=" |
| 52 | + ] |
| 53 | + end |
| 54 | + |
| 55 | + it "shows warnings for deprecated plural associations" do |
| 56 | + @c.one_to_many :cs, :key => :c_id, :class => @c, :deprecated => true |
| 57 | + o = @c.create |
| 58 | + 2.times do |
| 59 | + @c.association_reflection(:cs) |
| 60 | + @c.eager(:cs) |
| 61 | + @c.eager_graph(:cs) |
| 62 | + @c.where(:cs=>@o).sql |
| 63 | + @o.cs |
| 64 | + @o.cs_dataset |
| 65 | + @o.add_c(o) |
| 66 | + @o.remove_c(o) |
| 67 | + @o.remove_all_cs |
| 68 | + end |
| 69 | + @warnings.must_equal [ |
| 70 | + "Access of association reflection for deprecated association: class:C association:cs", |
| 71 | + "Calling deprecated association method: class:C association:cs method:cs", |
| 72 | + "Calling deprecated association method: class:C association:cs method:cs_dataset", |
| 73 | + "Calling deprecated association method: class:C association:cs method:add_c", |
| 74 | + "Calling deprecated association method: class:C association:cs method:remove_c", |
| 75 | + "Calling deprecated association method: class:C association:cs method:remove_all_cs" |
| 76 | + ] |
| 77 | + end |
| 78 | + |
| 79 | + it "does not deduplicate warnings when not caching associations" do |
| 80 | + @c.cache_associations = false |
| 81 | + @c.many_to_one :c, :class => @c, :deprecated => true |
| 82 | + 2.times do |
| 83 | + @c.association_reflection(:c) |
| 84 | + @c.eager(:c) |
| 85 | + @c.eager_graph(:c) |
| 86 | + @c.where(:c=>@o).sql |
| 87 | + @o.c |
| 88 | + @o.c_dataset |
| 89 | + @o.c = nil |
| 90 | + end |
| 91 | + @warnings.must_equal [ |
| 92 | + "Access of association reflection for deprecated association: class:C association:c", |
| 93 | + "Access of association reflection for deprecated association: class:C association:c", |
| 94 | + "Access of association reflection for deprecated association: class:C association:c", |
| 95 | + "Access of association reflection for deprecated association: class:C association:c", |
| 96 | + "Calling deprecated association method: class:C association:c method:c", |
| 97 | + "Calling deprecated association method: class:C association:c method:c_dataset", |
| 98 | + "Calling deprecated association method: class:C association:c method:c=" |
| 99 | + ] * 2 |
| 100 | + end |
| 101 | + |
| 102 | + it "does not deduplicate warnings when using deduplicate: false plugin option" do |
| 103 | + @c.plugin :deprecated_associations, :deduplicate => false |
| 104 | + @c.one_to_many :cs, :key => :c_id, :class => @c, :deprecated => true |
| 105 | + o = @c.create |
| 106 | + 2.times do |
| 107 | + @c.association_reflection(:cs) |
| 108 | + @c.eager(:cs) |
| 109 | + @c.eager_graph(:cs) |
| 110 | + @c.where(:cs=>@o).sql |
| 111 | + @o.cs |
| 112 | + @o.cs_dataset |
| 113 | + @o.add_c(o) |
| 114 | + @o.remove_c(o) |
| 115 | + @o.remove_all_cs |
| 116 | + end |
| 117 | + @warnings.must_equal [ |
| 118 | + "Access of association reflection for deprecated association: class:C association:cs", |
| 119 | + "Access of association reflection for deprecated association: class:C association:cs", |
| 120 | + "Access of association reflection for deprecated association: class:C association:cs", |
| 121 | + "Access of association reflection for deprecated association: class:C association:cs", |
| 122 | + "Calling deprecated association method: class:C association:cs method:cs", |
| 123 | + "Calling deprecated association method: class:C association:cs method:cs_dataset", |
| 124 | + "Calling deprecated association method: class:C association:cs method:add_c", |
| 125 | + "Calling deprecated association method: class:C association:cs method:remove_c", |
| 126 | + "Calling deprecated association method: class:C association:cs method:cs_dataset", |
| 127 | + "Calling deprecated association method: class:C association:cs method:remove_all_cs" |
| 128 | + ] * 2 |
| 129 | + end |
| 130 | + |
| 131 | + it "includes backtrace in warning when using :backtrace plugin option" do |
| 132 | + @c.plugin :deprecated_associations, :backtrace => true |
| 133 | + @c.many_to_one :c, :class => @c, :deprecated => true |
| 134 | + warnings = nil |
| 135 | + @c.singleton_class.send(:remove_method, :warn) |
| 136 | + @c.define_singleton_method(:warn){|*a| warnings = a} |
| 137 | + line = __LINE__ + 1 |
| 138 | + @c.association_reflection(:c) |
| 139 | + warnings[0].must_equal "Access of association reflection for deprecated association: class:C association:c" |
| 140 | + warnings[1][0].must_include "#{__FILE__}:#{line}" |
| 141 | + end |
| 142 | + |
| 143 | + it "raises for deprecated association when using :raise plugin option" do |
| 144 | + @c.plugin :deprecated_associations, :raise => true |
| 145 | + @c.many_to_one :c, :class => @c, :deprecated => true |
| 146 | + proc{@c.association_reflection(:c)}.must_raise Sequel::Plugins::DeprecatedAssociations::Access |
| 147 | + end |
| 148 | +end |
0 commit comments