/test/unit/model_persistence_test.rb

https://github.com/vwall/tire · Ruby · 477 lines · 370 code · 107 blank · 0 comment · 43 complexity · 307ffbfb30687312b0b1ea20db3fc42d MD5 · raw file

  1. require 'test_helper'
  2. module Tire
  3. module Model
  4. class PersistenceTest < Test::Unit::TestCase
  5. context "Model" do
  6. should "have default index name" do
  7. assert_equal 'persistent_articles', PersistentArticle.index_name
  8. assert_equal 'persistent_articles', PersistentArticle.new(:title => 'Test').index_name
  9. end
  10. should "allow to set custom index name" do
  11. assert_equal 'custom-index-name', PersistentArticleWithCustomIndexName.index_name
  12. PersistentArticleWithCustomIndexName.index_name "another-index-name"
  13. assert_equal 'another-index-name', PersistentArticleWithCustomIndexName.index_name
  14. assert_equal 'another-index-name', PersistentArticleWithCustomIndexName.index.name
  15. end
  16. context "with index prefix" do
  17. setup do
  18. Model::Search.index_prefix 'prefix'
  19. end
  20. teardown do
  21. Model::Search.index_prefix nil
  22. end
  23. should "have configured prefix in index_name" do
  24. assert_equal 'prefix_persistent_articles', PersistentArticle.index_name
  25. assert_equal 'prefix_persistent_articles', PersistentArticle.new(:title => 'Test').index_name
  26. end
  27. end
  28. should "have document_type" do
  29. assert_equal 'persistent_article', PersistentArticle.document_type
  30. assert_equal 'persistent_article', PersistentArticle.new(:title => 'Test').document_type
  31. end
  32. should "allow to define property" do
  33. assert_nothing_raised do
  34. a = PersistentArticle.new
  35. class << a
  36. property :status
  37. end
  38. end
  39. end
  40. end
  41. context "Finders" do
  42. setup do
  43. @first = { '_id' => 1, '_version' => 1, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'First' } }
  44. @second = { '_id' => 2, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'Second' } }
  45. @third = { '_id' => 3, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'Third' } }
  46. @find_all = { 'hits' => { 'hits' => [
  47. @first,
  48. @second,
  49. @third
  50. ] } }
  51. @find_first = { 'hits' => { 'hits' => [ @first ] } }
  52. @find_last_two = { 'hits' => { 'hits' => [ @second, @third ] } }
  53. end
  54. should "find document by numeric ID" do
  55. Configuration.client.expects(:get).returns(mock_response(@first.to_json))
  56. document = PersistentArticle.find 1
  57. assert_instance_of PersistentArticle, document
  58. assert_equal 1, document.id
  59. assert_equal 1, document.attributes['id']
  60. assert_equal 'First', document.attributes['title']
  61. assert_equal 'First', document.title
  62. end
  63. should "have _type, _index, _id, _version attributes" do
  64. Configuration.client.expects(:get).returns(mock_response(@first.to_json))
  65. document = PersistentArticle.find 1
  66. assert_instance_of PersistentArticle, document
  67. assert_equal 1, document.id
  68. assert_equal 1, document.attributes['id']
  69. assert_equal 'persistent_articles', document._index
  70. assert_equal 'persistent_article', document._type
  71. assert_equal 1, document._version
  72. end
  73. should "find document by string ID" do
  74. Configuration.client.expects(:get).returns(mock_response(@first.to_json))
  75. document = PersistentArticle.find '1'
  76. assert_instance_of PersistentArticle, document
  77. assert_equal 1, document.id
  78. assert_equal 1, document.attributes['id']
  79. assert_equal 'First', document.attributes['title']
  80. assert_equal 'First', document.title
  81. end
  82. should "find document by list of IDs" do
  83. Configuration.client.expects(:get).returns(mock_response(@find_last_two.to_json))
  84. documents = PersistentArticle.find 2, 3
  85. assert_equal 2, documents.count
  86. end
  87. should "find document by array of IDs" do
  88. Configuration.client.expects(:get).returns(mock_response(@find_last_two.to_json))
  89. documents = PersistentArticle.find [2, 3]
  90. assert_equal 2, documents.count
  91. end
  92. should "find all documents" do
  93. Configuration.client.stubs(:get).returns(mock_response(@find_all.to_json))
  94. documents = PersistentArticle.all
  95. assert_equal 3, documents.count
  96. assert_equal 'First', documents.first.attributes['title']
  97. assert_equal PersistentArticle.find(:all).map { |e| e.id }, PersistentArticle.all.map { |e| e.id }
  98. end
  99. should "find first document" do
  100. Configuration.client.expects(:get).returns(mock_response(@find_first.to_json))
  101. document = PersistentArticle.first
  102. assert_equal 'First', document.attributes['title']
  103. end
  104. should "raise error when passing incorrect argument" do
  105. assert_raise(ArgumentError) do
  106. PersistentArticle.find :name => 'Test'
  107. end
  108. end
  109. should_eventually "raise error when document is not found" do
  110. assert_raise(DocumentNotFound) do
  111. PersistentArticle.find 'xyz001'
  112. end
  113. end
  114. end
  115. context "Persistent model" do
  116. setup { @article = PersistentArticle.new :title => 'Test', :tags => [:one, :two] }
  117. context "attribute methods" do
  118. should "allow to set attributes on initialization" do
  119. assert_not_nil @article.attributes
  120. assert_equal 'Test', @article.attributes['title']
  121. end
  122. should "allow to leave attributes blank on initialization" do
  123. assert_nothing_raised { PersistentArticle.new }
  124. end
  125. should "have getter methods for attributes" do
  126. assert_not_nil @article.title
  127. assert_equal 'Test', @article.title
  128. assert_equal [:one, :two], @article.tags
  129. end
  130. should "have getter methods for attribute passed as a String" do
  131. article = PersistentArticle.new 'title' => 'Tony Montana'
  132. assert_not_nil article.title
  133. assert_equal 'Tony Montana', article.title
  134. end
  135. should "raise error when getting unknown attribute" do
  136. assert_raise(NoMethodError) do
  137. @article.krapulitz
  138. end
  139. end
  140. should "not raise error when getting unset attribute" do
  141. article = PersistentArticle.new :title => 'Test'
  142. assert_nothing_raised { article.published_on }
  143. assert_nil article.published_on
  144. end
  145. should_eventually "return default value for attribute" do
  146. article = PersistentArticle.new :title => 'Test'
  147. article.class_eval do
  148. property :title
  149. property :tags, :default => []
  150. end
  151. assert_nothing_raised { article.tags }
  152. assert_equal [], article.tags
  153. end
  154. should "have query method for attribute" do
  155. assert_equal true, @article.title?
  156. end
  157. should "raise error when querying for unknown attribute" do
  158. assert_raise(NoMethodError) do
  159. @article.krapulitz?
  160. end
  161. end
  162. should "not raise error when querying for unset attribute" do
  163. article = PersistentArticle.new :title => 'Test'
  164. assert_nothing_raised { article.published_on? }
  165. assert ! article.published_on?
  166. end
  167. should "return true for respond_to? calls for set attributes" do
  168. article = PersistentArticle.new :title => 'Test'
  169. assert article.respond_to?(:title)
  170. end
  171. should "return false for respond_to? calls for unknown attributes" do
  172. article = PersistentArticle.new :title => 'Test'
  173. assert ! article.respond_to?(:krapulitz)
  174. end
  175. should "return true for respond_to? calls for defined but unset attributes" do
  176. article = PersistentArticle.new :title => 'Test'
  177. assert article.respond_to?(:published_on)
  178. end
  179. should "have attribute names" do
  180. article = PersistentArticle.new :title => 'Test', :tags => ['one', 'two']
  181. assert_equal ['published_on', 'tags', 'title'], article.attribute_names
  182. end
  183. should "have setter method for attribute" do
  184. @article.title = 'Updated'
  185. assert_equal 'Updated', @article.title
  186. assert_equal 'Updated', @article.attributes['title']
  187. end
  188. should_eventually "allow to set deeply nested attributes on initialization" do
  189. article = PersistentArticle.new :title => 'Test', :author => { :first_name => 'John', :last_name => 'Smith' }
  190. assert_equal 'John', article.author.first_name
  191. assert_equal 'Smith', article.author.last_name
  192. assert_equal({ :first_name => 'John', :last_name => 'Smith' }, article.attributes['author'])
  193. end
  194. should_eventually "allow to set deeply nested attributes on update" do
  195. article = PersistentArticle.new :title => 'Test', :author => { :first_name => 'John', :last_name => 'Smith' }
  196. article.author.first_name = 'Robert'
  197. article.author.last_name = 'Carpenter'
  198. assert_equal 'Robert', article.author.first_name
  199. assert_equal 'Carpenter', article.author.last_name
  200. assert_equal({ :first_name => 'Robert', :last_name => 'Carpenter' }, article.attributes['author'])
  201. end
  202. end
  203. context "when creating" do
  204. should "save the document with generated ID in the database" do
  205. Configuration.client.expects(:post).
  206. with do |url, payload|
  207. doc = MultiJson.decode(payload)
  208. url == "#{Configuration.url}/persistent_articles/persistent_article/" &&
  209. doc['title'] == 'Test' &&
  210. doc['tags'] == ['one', 'two']
  211. doc['published_on'] == nil
  212. end.
  213. returns(mock_response('{"ok":true,"_id":"abc123"}'))
  214. article = PersistentArticle.create :title => 'Test', :tags => [:one, :two]
  215. assert article.persisted?, "#{article.inspect} should be `persisted?`"
  216. assert_equal 'abc123', article.id
  217. end
  218. should "save the document with custom ID in the database" do
  219. Configuration.client.expects(:post).
  220. with do |url, payload|
  221. doc = MultiJson.decode(payload)
  222. url == "#{Configuration.url}/persistent_articles/persistent_article/r2d2" &&
  223. doc['id'] == 'r2d2' &&
  224. doc['title'] == 'Test' &&
  225. doc['published_on'] == nil
  226. end.
  227. returns(mock_response('{"ok":true,"_id":"r2d2"}'))
  228. article = PersistentArticle.create :id => 'r2d2', :title => 'Test'
  229. assert_equal 'r2d2', article.id
  230. end
  231. should "perform model validations" do
  232. Configuration.client.expects(:post).never
  233. assert ! ValidatedModel.create(:name => nil)
  234. end
  235. end
  236. context "when creating" do
  237. should "set the id property" do
  238. Configuration.client.expects(:post).
  239. with do |url, payload|
  240. doc = MultiJson.decode(payload)
  241. url == "#{Configuration.url}/persistent_articles/persistent_article/" &&
  242. doc['id'] == nil &&
  243. doc['title'] == 'Test'
  244. end.
  245. returns(mock_response('{"ok":true,"_id":"1"}'))
  246. article = PersistentArticle.create :title => 'Test'
  247. assert_equal '1', article.id
  248. end
  249. should "not set the id property if already set" do
  250. Configuration.client.expects(:post).
  251. with do |url, payload|
  252. doc = MultiJson.decode(payload)
  253. url == "#{Configuration.url}/persistent_articles/persistent_article/123" &&
  254. doc['id'] == '123' &&
  255. doc['title'] == 'Test' &&
  256. doc['published_on'] == nil
  257. end.
  258. returns(mock_response('{"ok":true, "_id":"XXX"}'))
  259. article = PersistentArticle.create :id => '123', :title => 'Test'
  260. assert_equal '123', article.id
  261. end
  262. end
  263. context "when saving" do
  264. should "save the document with updated attribute" do
  265. article = PersistentArticle.new :id => '1', :title => 'Test'
  266. Configuration.client.expects(:post).
  267. with do |url, payload|
  268. doc = MultiJson.decode(payload)
  269. url == "#{Configuration.url}/persistent_articles/persistent_article/1" &&
  270. doc['id'] == '1' &&
  271. doc['title'] == 'Test' &&
  272. doc['published_on'] == nil
  273. end.
  274. returns(mock_response('{"ok":true,"_id":"1"}'))
  275. assert article.save
  276. article.title = 'Updated'
  277. Configuration.client.expects(:post).
  278. with do |url, payload|
  279. doc = MultiJson.decode(payload)
  280. url == "#{Configuration.url}/persistent_articles/persistent_article/1" &&
  281. doc['id'] == '1' &&
  282. doc['title'] == 'Updated'
  283. end.
  284. returns(mock_response('{"ok":true,"_id":"1"}'))
  285. assert article.save
  286. end
  287. should "perform validations" do
  288. article = ValidatedModel.new :name => nil
  289. assert ! article.save
  290. end
  291. should "set the id property" do
  292. article = PersistentArticle.new
  293. article.title = 'Test'
  294. Configuration.client.expects(:post).with("#{Configuration.url}/persistent_articles/persistent_article/",
  295. article.to_indexed_json).
  296. returns(mock_response('{"ok":true,"_id":"1"}'))
  297. assert article.save
  298. assert_equal '1', article.id
  299. end
  300. should "not set the id property if already set" do
  301. article = PersistentArticle.new
  302. article.id = '456'
  303. article.title = 'Test'
  304. Configuration.client.expects(:post).
  305. with do |url, payload|
  306. doc = MultiJson.decode(payload)
  307. url == "#{Configuration.url}/persistent_articles/persistent_article/456" &&
  308. doc['id'] == '456' &&
  309. doc['title'] == 'Test'
  310. end.
  311. returns(mock_response('{"ok":true,"_id":"XXX"}'))
  312. assert article.save
  313. assert_equal '456', article.id
  314. end
  315. end
  316. context "when destroying" do
  317. should "delete the document from the database" do
  318. Configuration.client.expects(:post).
  319. with do |url, payload|
  320. doc = MultiJson.decode(payload)
  321. url == "#{Configuration.url}/persistent_articles/persistent_article/123" &&
  322. doc['id'] == '123' &&
  323. doc['title'] == 'Test'
  324. end.returns(mock_response('{"ok":true,"_id":"123"}'))
  325. Configuration.client.expects(:delete).
  326. with("#{Configuration.url}/persistent_articles/persistent_article/123").
  327. returns(mock_response('{"ok":true,"acknowledged":true}', 200))
  328. article = PersistentArticle.new :id => '123', :title => 'Test'
  329. article.save
  330. article.destroy
  331. end
  332. end
  333. context "when updating attributes" do
  334. should "update single attribute" do
  335. @article.expects(:save).returns(true)
  336. @article.update_attribute :title, 'Updated'
  337. assert_equal 'Updated', @article.title
  338. end
  339. should "update all attributes" do
  340. @article.expects(:save).returns(true)
  341. @article.update_attributes :title => 'Updated', :tags => ['three']
  342. assert_equal 'Updated', @article.title
  343. assert_equal ['three'], @article.tags
  344. end
  345. end
  346. end
  347. context "Persistent model with mapping definition" do
  348. should "create the index with mapping" do
  349. expected = {
  350. :settings => {},
  351. :mappings => { :persistent_article_with_mapping => {
  352. :properties => { :title => { :type => 'string', :analyzer => 'snowball', :boost => 10 } }
  353. }}
  354. }
  355. Tire::Index.any_instance.stubs(:exists?).returns(false)
  356. Tire::Index.any_instance.expects(:create).with(expected)
  357. class ::PersistentArticleWithMapping
  358. include Tire::Model::Persistence
  359. include Tire::Model::Search
  360. include Tire::Model::Callbacks
  361. mapping do
  362. property :title, :type => 'string', :analyzer => 'snowball', :boost => 10
  363. end
  364. end
  365. assert_equal 'snowball', PersistentArticleWithMapping.mapping[:title][:analyzer]
  366. end
  367. end
  368. end
  369. end
  370. end