| | 1 | require File.dirname(__FILE__) + '/../test_helper' |
|---|
| | 2 | |
|---|
| | 3 | class EagerLoadPolyAssocsTest < Test::Unit::TestCase |
|---|
| | 4 | OPTS = { :options => "Engine=MyISAM" } |
|---|
| | 5 | NUM_SIMPLE_OBJS = 50 |
|---|
| | 6 | NUM_SHAPE_EXPRESSIONS = 100 |
|---|
| | 7 | |
|---|
| | 8 | def setup |
|---|
| | 9 | unless ShapeExpression.table_exists? |
|---|
| | 10 | silence_stream(STDOUT) { create_test_tables } |
|---|
| | 11 | generate_test_object_graphs |
|---|
| | 12 | end |
|---|
| | 13 | end |
|---|
| | 14 | |
|---|
| | 15 | def create_test_tables |
|---|
| | 16 | mig = ActiveRecord::Migration |
|---|
| | 17 | [:circles, :squares, :triangles, :non_poly_ones, :non_poly_twos].each do |t| |
|---|
| | 18 | mig.create_table t, OPTS do end |
|---|
| | 19 | end |
|---|
| | 20 | |
|---|
| | 21 | mig.create_table :shape_expressions, OPTS do |
|---|
| | 22 | string :paint_type |
|---|
| | 23 | integer :paint_id |
|---|
| | 24 | string :shape_type |
|---|
| | 25 | integer :shape_id |
|---|
| | 26 | end |
|---|
| | 27 | mig.create_table :paint_colors, OPTS do |
|---|
| | 28 | integer :non_poly_one_id |
|---|
| | 29 | end |
|---|
| | 30 | mig.create_table :paint_textures, OPTS do |
|---|
| | 31 | integer :non_poly_two_id |
|---|
| | 32 | end |
|---|
| | 33 | end |
|---|
| | 34 | |
|---|
| | 35 | def teardown |
|---|
| | 36 | drop_tables |
|---|
| | 37 | end |
|---|
| | 38 | |
|---|
| | 39 | def drop_tables |
|---|
| | 40 | ShapeExpression.connection.reconnect! |
|---|
| | 41 | silence_stream(STDOUT) do |
|---|
| | 42 | mig = ActiveRecord::Migration |
|---|
| | 43 | [:circles, :squares, :triangles, :paint_colors, :paint_textures, |
|---|
| | 44 | :shape_expressions, :non_poly_ones, :non_poly_twos].each do |t| |
|---|
| | 45 | mig.drop_table t |
|---|
| | 46 | end |
|---|
| | 47 | end |
|---|
| | 48 | end |
|---|
| | 49 | |
|---|
| | 50 | # meant to be supplied as an ID, never returns 0 |
|---|
| | 51 | def rand_simple |
|---|
| | 52 | val = (NUM_SIMPLE_OBJS * rand).round |
|---|
| | 53 | val == 0 ? 1 : val |
|---|
| | 54 | end |
|---|
| | 55 | |
|---|
| | 56 | def generate_test_object_graphs |
|---|
| | 57 | 1.upto(NUM_SIMPLE_OBJS) do |
|---|
| | 58 | [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!) |
|---|
| | 59 | end |
|---|
| | 60 | 1.upto(NUM_SIMPLE_OBJS) do |i| |
|---|
| | 61 | PaintColor.create!(:non_poly_one_id => rand_simple) |
|---|
| | 62 | PaintTexture.create!(:non_poly_two_id => rand_simple) |
|---|
| | 63 | end |
|---|
| | 64 | 1.upto(NUM_SHAPE_EXPRESSIONS) do |i| |
|---|
| | 65 | ShapeExpression.create!(:shape_type => [Circle, Square, Triangle].rand.to_s, :shape_id => rand_simple, |
|---|
| | 66 | :paint_type => [PaintColor, PaintTexture].rand.to_s, :paint_id => rand_simple) |
|---|
| | 67 | end |
|---|
| | 68 | end |
|---|
| | 69 | |
|---|
| | 70 | def test_include_query |
|---|
| | 71 | res = 0 |
|---|
| | 72 | res = ShapeExpression.find :all, :include => [ :shape, { :paint => :non_poly } ] |
|---|
| | 73 | assert_equal NUM_SHAPE_EXPRESSIONS, res.size |
|---|
| | 74 | ShapeExpression.connection.disconnect! |
|---|
| | 75 | assert_nothing_raised "confirm we can access associations in memory" do |
|---|
| | 76 | res.each do |se| |
|---|
| | 77 | assert_not_nil se.paint.non_poly, "this is the association that was loading incorrectly before the change" |
|---|
| | 78 | assert_not_nil se.shape, "just making sure other associations still work" |
|---|
| | 79 | end |
|---|
| | 80 | end |
|---|
| | 81 | assert_raise ActiveRecord::StatementInvalid, "An exception should be raised when db connectivity is required" do |
|---|
| | 82 | res[0].reload |
|---|
| | 83 | end |
|---|
| | 84 | end |
|---|
| | 85 | end |
|---|
| | 86 | |
|---|
| | 87 | class ShapeExpression < ActiveRecord::Base |
|---|
| | 88 | belongs_to :shape, :polymorphic => true |
|---|
| | 89 | belongs_to :paint, :polymorphic => true |
|---|
| | 90 | end |
|---|
| | 91 | |
|---|
| | 92 | class Circle < ActiveRecord::Base |
|---|
| | 93 | has_many :shape_expressions, :as => :shape |
|---|
| | 94 | end |
|---|
| | 95 | class Square < ActiveRecord::Base |
|---|
| | 96 | has_many :shape_expressions, :as => :shape |
|---|
| | 97 | end |
|---|
| | 98 | class Triangle < ActiveRecord::Base |
|---|
| | 99 | has_many :shape_expressions, :as => :shape |
|---|
| | 100 | end |
|---|
| | 101 | class PaintColor < ActiveRecord::Base |
|---|
| | 102 | has_many :shape_expressions, :as => :paint |
|---|
| | 103 | belongs_to :non_poly, :foreign_key => "non_poly_one_id", :class_name => "NonPolyOne" |
|---|
| | 104 | end |
|---|
| | 105 | class PaintTexture < ActiveRecord::Base |
|---|
| | 106 | has_many :shape_expressions, :as => :paint |
|---|
| | 107 | belongs_to :non_poly, :foreign_key => "non_poly_two_id", :class_name => "NonPolyTwo" |
|---|
| | 108 | end |
|---|
| | 109 | class NonPolyOne < ActiveRecord::Base |
|---|
| | 110 | has_many :paint_colors |
|---|
| | 111 | end |
|---|
| | 112 | class NonPolyTwo < ActiveRecord::Base |
|---|
| | 113 | has_many :paint_textures |
|---|
| | 114 | end |