/spec/models/content_model_spec.rb

https://github.com/jjuglans/Webiva · Ruby · 143 lines · 101 code · 42 blank · 0 comment · 26 complexity · f41de38ba2c5735df309fb3a028c8fff MD5 · raw file

  1. require File.dirname(__FILE__) + "/../spec_helper"
  2. require File.dirname(__FILE__) + "/../content_spec_helper"
  3. describe ContentModel do
  4. include ContentSpecHelper
  5. reset_domain_tables :content_models,:content_model_fields,:content_model_features,:content_nodes,:content_types
  6. describe "Table Migration" do
  7. before(:each) do
  8. DataCache.reset_local_cache
  9. connect_to_migrator_database
  10. ContentModel.connection.execute('DROP TABLE IF EXISTS cms_spec_tests')
  11. end
  12. it "should be able to create a database table and a content type" do
  13. cm = ContentModel.create(:name => 'spec_test')
  14. cm.create_table # Should create a table
  15. ContentModel.connection.execute('DROP TABLE cms_spec_tests')
  16. end
  17. it "should correctly generate valid table names" do
  18. cm = ContentModel.create(:name => 'This---is a test of the emergency broadcast system')
  19. migrator_mock = mock("ContentMigrator",:update_up => nil, :migrate_domain => nil, :suppress_messages => nil)
  20. ContentMigrator.should_receive(:clone).at_least(:once).and_return(migrator_mock)
  21. cm.create_table # Should create a table
  22. cm.table_name.should == 'cms_this_is_a_test_of_the_emes'
  23. cm.name = 'My Model - List of things'
  24. cm.create_table
  25. cm.table_name.should == 'cms_my_model_list_of_things'
  26. cm.name = 'ALLUPPERCASE!!!'
  27. cm.create_table
  28. cm.table_name.should == 'cms_alluppercases'
  29. cm = ContentModel.create(:name => 'ALLUPPERCASE')
  30. cm.create_table
  31. cm.table_name.should == 'cms_alluppercase_2s'
  32. end
  33. it "should be able to add entries to the table (no fields, just id)" do
  34. cm = ContentModel.create(:name => 'spec_test')
  35. cm.create_table # Should create a table
  36. cls = cm.content_model
  37. entry = cls.create
  38. entry.id.should == 1
  39. cls.count.should == 1
  40. entry = cls.create
  41. cls.count.should == 2
  42. ContentModel.connection.execute('DROP TABLE cms_spec_tests')
  43. end
  44. end
  45. describe "Content Node Creation" do
  46. before(:each) do
  47. DataCache.reset_local_cache
  48. connect_to_migrator_database
  49. ContentModel.connection.execute('DROP TABLE IF EXISTS cms_spec_tests')
  50. end
  51. it "should create a content type and content nodes after save" do
  52. ContentType.count.should == 0
  53. @cm = create_spec_test_content_model
  54. ContentType.count.should == 1
  55. ContentNode.count.should == 0
  56. @cm.content_model.create
  57. ContentNode.count.should == 1
  58. end
  59. it "should destroy content nodes after updating content node" do
  60. ContentType.count.should == 0
  61. @cm = create_spec_test_content_model
  62. ContentType.count.should == 1
  63. ContentNode.count.should == 0
  64. @cm.content_model.create
  65. ContentNode.count.should == 1
  66. @cm = ContentModel.find(:last)
  67. @cm.create_nodes = false
  68. @cm.save
  69. ContentNode.count.should == 0
  70. end
  71. it "should destroy content nodes after setting create_nodes to false" do
  72. ContentType.count.should == 0
  73. @cm = create_spec_test_content_model
  74. ContentType.count.should == 1
  75. ContentNode.count.should == 0
  76. @cm.content_model.create
  77. ContentNode.count.should == 1
  78. @cm = ContentModel.find(:last)
  79. @cm.create_nodes = false
  80. @cm.save
  81. ContentNode.count.should == 0
  82. end
  83. it "should try to recreate content_nodes after setting create_nodes to true" do
  84. ContentType.count.should == 0
  85. @cm = create_spec_test_content_model(:create_nodes => false)
  86. ContentType.count.should == 1
  87. ContentNode.count.should == 0
  88. @cm.content_model.create
  89. @cm.content_model.create
  90. @cm.content_model.create
  91. ContentNode.count.should == 0
  92. @cm = ContentModel.find(:last)
  93. @cm.should_receive(:run_worker).with(:recreate_all_content_nodes)
  94. @cm.create_nodes = true
  95. @cm.save
  96. @cm = ContentModel.find(:last)
  97. @cm.recreate_all_content_nodes
  98. ContentNode.count.should == 3
  99. end
  100. end
  101. end