Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1732: active_record_bug.diff

File active_record_bug.diff, 4.7 kB (added by david, 3 years ago)

Test case for the problem

  • activerecord/test/associations_test.rb

    old new  
    55require 'fixtures/topic' 
    66require 'fixtures/reply' 
    77require 'fixtures/computer' 
     8require 'fixtures/graphnode' 
    89 
    910# Can't declare new classes in test case methods, so tests before that 
    1011bad_collection_keys = false 
     
    801802 
    802803 
    803804class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase 
    804   fixtures :accounts, :companies, :developers, :projects, :developers_projects 
     805  fixtures :accounts, :companies, :developers, :projects, :developers_projects, :graphnodes, :graphnode_links 
    805806   
    806807  def test_has_and_belongs_to_many 
    807808    david = Developer.find(1) 
     
    829830    assert_equal 2, action_controller.developers(true).size 
    830831  end 
    831832 
     833  def test_finder_sql_cache 
     834    shared_node = graphnodes(:shared_subchild) 
     835    child_one = graphnodes(:child_one) 
     836    child_two = graphnodes(:child_two) 
     837     
     838    assert_equal 2, shared_node.parent_nodes.size 
     839    assert_equal child_one, shared_node.parent_nodes.first 
     840    assert_equal 1, child_one.parent_nodes.size 
     841  end 
     842 
    832843  def test_adding_type_mismatch 
    833844    jamis = Developer.find(2) 
    834845    assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil } 
  • activerecord/test/fixtures/graphnodes.yml

    old new  
     1root_node: 
     2  id: 1 
     3  name: Root Node 
     4child_one: 
     5  id: 2 
     6  name: Child One 
     7child_two: 
     8  id: 3 
     9  name: Child Two 
     10subchild_one: 
     11  id: 4 
     12  name: Subchild One 
     13subchild_two: 
     14  id: 5 
     15  name: Subchild Two 
     16shared_subchild: 
     17  id: 6 
     18  name: Shared Subchild 
     19subsubchild: 
     20  id: 7 
     21  name: Sub-sub child 
  • activerecord/test/fixtures/db_definitions/mysql.sql

    old new  
    180180 
    181181  FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`) 
    182182) TYPE=InnoDB; 
     183 
     184CREATE TABLE `graphnodes` ( 
     185  `id`   INTEGER NOT NULL auto_increment, 
     186  `name` VARCHAR(255) NOT NULL, 
     187  PRIMARY KEY  (`id`) 
     188) TYPE=InnoDB; 
     189 
     190CREATE TABLE `graphnode_links` ( 
     191  `parent_node_id` INTEGER NOT NULL, 
     192  `child_node_id` INTEGER NOT NULL, 
     193  PRIMARY KEY  (`parent_node_id`,`child_node_id`), 
     194  CONSTRAINT `FK_child_node` FOREIGN KEY (`child_node_id`) REFERENCES `graphnodes` (`id`), 
     195  CONSTRAINT `FK_parent_node` FOREIGN KEY (`parent_node_id`) REFERENCES `graphnodes` (`id`) 
     196) TYPE=InnoDB; 
     197 
  • activerecord/test/fixtures/db_definitions/mysql.drop.sql

    old new  
    2121DROP TABLE authors; 
    2222DROP TABLE categories; 
    2323DROP TABLE categories_posts; 
     24DROP TABLE graphnodes; 
     25DROP TABLE graphnode_links; 
  • activerecord/test/fixtures/graphnode_links.yml

    old new  
     1link_one: 
     2  parent_node_id: 1 
     3  child_node_id: 2 
     4link_two: 
     5  parent_node_id: 1 
     6  child_node_id: 3 
     7link_three: 
     8  parent_node_id: 2 
     9  child_node_id: 4 
     10link_four: 
     11  parent_node_id: 2 
     12  child_node_id: 5 
     13link_five: 
     14  parent_node_id: 2 
     15  child_node_id: 6 
     16link_six: 
     17  parent_node_id: 3 
     18  child_node_id: 6 
     19link_seven: 
     20  parent_node_id: 6 
     21  child_node_id: 7 
  • activerecord/test/fixtures/graphnode.rb

    old new  
     1class Graphnode < ActiveRecord::Base 
     2  has_and_belongs_to_many :parent_nodes, 
     3    :class_name => 'Graphnode', 
     4    :finder_sql => 'SELECT n.* FROM graphnodes n, graphnode_links l ' + 
     5                   ' WHERE n.id = l.parent_node_id ' + 
     6                   '   AND l.child_node_id = #{id}' 
     7 
     8  has_and_belongs_to_many :child_nodes, 
     9    :class_name => 'Graphnode', 
     10    :finder_sql => 'SELECT n.* FROM graphnodes n, graphnode_links l ' + 
     11                   ' WHERE n.id = l.child_node_id ' + 
     12                   '   AND l.parent_node_id = #{id}' 
     13end