PageRenderTime 76ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/test/globalize3/dynamic_finders_test.rb

https://github.com/mremolt/globalize3
Ruby | 171 lines | 129 code | 40 blank | 2 comment | 4 complexity | 8a236a8744afed31197554e180fda2fb MD5 | raw file
Possible License(s): MIT
  1. # encoding: utf-8
  2. require File.expand_path('../../test_helper', __FILE__)
  3. class DynamicFindersTest < Test::Unit::TestCase
  4. test "Does not break normal finders" do
  5. user = User.create!(:name => "name", :email => "email@example.org")
  6. assert_equal user, User.find_by_email(user.email)
  7. assert_equal [user], User.find_all_by_email([user.email])
  8. assert_equal user, User.find_by_id_and_created_at(user.id, user.created_at)
  9. assert_equal user, User.find_by_id_and_email(user.id, user.email)
  10. assert_equal [user], User.find_all_by_id_and_created_at(user.id, user.created_at)
  11. assert_equal [user], User.find_all_by_id_and_email(user.id, user.email)
  12. end
  13. test "simple dynamic finders do work" do
  14. foo = Post.create!(:title => 'foo')
  15. bar = Post.create!(:title => 'bar')
  16. assert_equal foo, Post.find_by_title('foo')
  17. assert_equal bar, Post.find_by_title('bar')
  18. assert_nil Post.find_by_title("non existing")
  19. assert_equal [foo], Post.find_all_by_title('foo')
  20. assert_equal [], Post.find_all_by_title('non existing')
  21. end
  22. # https://github.com/svenfuchs/globalize3/issues#issue/5
  23. test "simple dynamic finders retruns results from current locale and fallbacks" do
  24. en, de, he = 'title', 'titel', 'שם'
  25. post = Post.create!(:title => en)
  26. post.update_attributes!(:title => de, :locale => :de)
  27. post.update_attributes!(:title => he, :locale => :he)
  28. with_fallbacks do
  29. I18n.fallbacks = {:de => [:de, :en], :he => [:he, :en], :en => [:en]}
  30. with_locale(:en) do
  31. assert Post.find_by_title(en)
  32. assert_nil Post.find_by_title(de)
  33. end
  34. with_locale(:de) do
  35. assert Post.find_by_title(en)
  36. assert Post.find_by_title(de)
  37. assert_nil Post.find_by_title(he)
  38. end
  39. with_locale(:he) do
  40. assert Post.find_by_title(en)
  41. assert Post.find_by_title(he)
  42. assert_nil Post.find_by_title(de)
  43. end
  44. I18n.fallbacks.clear
  45. end
  46. end
  47. test "simple dynamic finders do work on sti models" do
  48. Child.create(:content => 'foo')
  49. Child.create(:content => 'bar')
  50. assert_equal 'foo', Child.find_by_content('foo').content
  51. assert_equal 'foo', Child.find_all_by_content('foo').first.content
  52. end
  53. test "records returned by dynamic finders have writable attributes" do
  54. Post.create(:title => 'original')
  55. post = Post.find_by_title('original')
  56. post.title = 'modified'
  57. assert_nothing_raised(ActiveRecord::ReadOnlyRecord) do
  58. post.save
  59. end
  60. end
  61. test "responds to possible dynamic finders" do
  62. assert Post.respond_to?(:find_by_title)
  63. assert Post.respond_to?(:find_all_by_title)
  64. assert Post.respond_to?(:find_by_title_and_content)
  65. assert Post.respond_to?(:find_all_by_title_and_content)
  66. end
  67. test "does not responds to impossible dynamic finders" do
  68. assert ! Post.respond_to?(:find_by_foo)
  69. assert ! Post.respond_to?(:find_all_by_foo)
  70. assert ! Post.respond_to?(:find_by_title_and_foo)
  71. assert ! Post.respond_to?(:find_all_by_title_and_foo)
  72. end
  73. end
  74. class TwoTranslatedAttributesDynamicFindersTest < Test::Unit::TestCase
  75. def setup
  76. @title1, @title2, @content = "n1", "n2", "desc"
  77. @p1 = Post.create!(:title => @title1, :content => @content)
  78. @p2 = Post.create!(:title => @title2, :content => @content)
  79. end
  80. test "find one element by two translation columns" do
  81. assert_equal @p1, Post.find_by_title_and_content(@title1, @content)
  82. assert_equal @p2, Post.find_by_content_and_title(@content, @title2)
  83. end
  84. test "return nil for none existing values" do
  85. assert_nil Post.find_by_content_and_title(@content, "not exisiting")
  86. assert_nil Post.find_by_content_and_title("not existing", @title2)
  87. assert_nil Post.find_by_title_and_content("not exisiting", @content)
  88. assert_nil Post.find_by_title_and_content(@title2, "not existing")
  89. end
  90. test "find elements by two translation columns" do
  91. two_results = Post.find_all_by_title_and_content([@title1, @title2], @content)
  92. assert two_results.include?(@p1)
  93. assert two_results.include?(@p2)
  94. assert_equal [@p2], Post.find_all_by_content_and_title(@content, @title2)
  95. end
  96. test "returns empty result set for none existing values" do
  97. assert_equal [], Post.find_all_by_title_and_content([@title1, @title2], "not existing")
  98. assert_equal [], Post.find_all_by_title_and_content("not existing", @content)
  99. assert_equal [], Post.find_all_by_content_and_title(["not existing"], @title1)
  100. assert_equal [], Post.find_all_by_content_and_title(@content, ["not existing"])
  101. end
  102. end
  103. class TranslatedAndNormalAttributeDynamicFindersTest < Test::Unit::TestCase
  104. def setup
  105. @name1, @name2, @email = "n1", "n2", "email@example.org"
  106. @p1 = User.create!(:name => @name1, :email => @email)
  107. @p2 = User.create!(:name => @name2, :email => @email)
  108. end
  109. test "find one element by two columns" do
  110. assert_equal @p1, User.find_by_name_and_email(@name1, @email)
  111. assert_equal @p2, User.find_by_email_and_name(@email, @name2)
  112. end
  113. test "return nil for none existing values" do
  114. assert_nil User.find_by_email_and_name(@email, "not exisiting")
  115. assert_nil User.find_by_email_and_name("not existing", @name2)
  116. assert_nil User.find_by_name_and_email("not exisiting", @email)
  117. assert_nil User.find_by_name_and_email(@name2, "not existing")
  118. end
  119. test "find elements by two translation columns" do
  120. two_results = User.find_all_by_name_and_email([@name1, @name2], @email)
  121. assert two_results.include?(@p1)
  122. assert two_results.include?(@p2)
  123. assert_equal [@p2], User.find_all_by_email_and_name(@email, @name2)
  124. end
  125. test "returns empty result set for none existing values" do
  126. assert_equal [], User.find_all_by_name_and_email([@name1, @name2], "not existing")
  127. assert_equal [], User.find_all_by_name_and_email("not existing", @email)
  128. assert_equal [], User.find_all_by_email_and_name(["not existing"], @name1)
  129. assert_equal [], User.find_all_by_email_and_name(@email, ["not existing"])
  130. end
  131. end