|
1 | 1 | require 'test_helper'
|
| 2 | +require 'active_record' |
2 | 3 |
|
3 | 4 | module Elasticsearch
|
4 | 5 | module Model
|
5 | 6 | class DynamicIndexNameTest < Elasticsearch::Test::IntegrationTestCase
|
| 7 | + context "Dynamic index name" do |
| 8 | + setup do |
| 9 | + class ::ArticleWithDynamicIndexName < ActiveRecord::Base |
| 10 | + include Elasticsearch::Model |
| 11 | + include Elasticsearch::Model::Callbacks |
6 | 12 |
|
7 |
| - class ::ArticleWithDynamicIndexName < ActiveRecord::Base |
8 |
| - include Elasticsearch::Model |
9 |
| - include Elasticsearch::Model::Callbacks |
| 13 | + def self.counter=(value) |
| 14 | + @counter = 0 |
| 15 | + end |
10 | 16 |
|
11 |
| - class << self |
12 |
| - attr_accessor :year |
13 |
| - end |
| 17 | + def self.counter |
| 18 | + (@counter ||= 0) && @counter += 1 |
| 19 | + end |
14 | 20 |
|
15 |
| - mapping { indexes :title } |
16 |
| - index_name { "articles-#{year}" } |
17 |
| - end |
| 21 | + mapping { indexes :title } |
| 22 | + index_name { "articles-#{counter}" } |
| 23 | + end |
18 | 24 |
|
19 |
| - context "Dynamic index name" do |
20 |
| - setup do |
21 |
| - ActiveRecord::Schema.define(:version => 1) do |
22 |
| - create_table :article_with_dynamic_index_names do |t| |
23 |
| - t.string :title |
| 25 | + ::ActiveRecord::Schema.define(:version => 1) do |
| 26 | + create_table ::ArticleWithDynamicIndexName.table_name do |t| |
| 27 | + t.string :title |
24 | 28 | end
|
25 | 29 | end
|
26 |
| - end |
27 | 30 |
|
28 |
| - should 'evaluate the index_name value' do |
29 |
| - ArticleWithDynamicIndexName.year = '2014' |
30 |
| - |
31 |
| - assert_equal ArticleWithDynamicIndexName.index_name, "articles-2014" |
| 31 | + ::ArticleWithDynamicIndexName.counter = 0 |
32 | 32 | end
|
33 | 33 |
|
34 |
| - should 'reevaluate the index_name value each time' do |
35 |
| - ArticleWithDynamicIndexName.year = '2015' |
36 |
| - |
37 |
| - assert_equal ArticleWithDynamicIndexName.index_name, "articles-2015" |
| 34 | + should 'evaluate the index_name value' do |
| 35 | + assert_equal ArticleWithDynamicIndexName.index_name, "articles-1" |
38 | 36 | end
|
39 | 37 |
|
40 |
| - should "write and read at the the defined index" do |
41 |
| - ArticleWithDynamicIndexName.year = '2016' |
42 |
| - |
43 |
| - ArticleWithDynamicIndexName.delete_all |
44 |
| - ArticleWithDynamicIndexName.__elasticsearch__.create_index! force: true |
45 |
| - |
46 |
| - ::ArticleWithDynamicIndexName.create! title: 'Test' |
47 |
| - |
48 |
| - ArticleWithDynamicIndexName.__elasticsearch__.refresh_index! |
49 |
| - |
50 |
| - response = ArticleWithDynamicIndexName.search(query: { match_all: {} }) |
51 |
| - |
52 |
| - assert_equal response.results.total, 1 |
53 |
| - assert_equal response.search.definition[:index], ArticleWithDynamicIndexName.index_name |
54 |
| - assert_equal response.search.definition[:index], 'articles-2016' |
| 38 | + should 're-evaluate the index_name value each time' do |
| 39 | + assert_equal ArticleWithDynamicIndexName.index_name, "articles-1" |
| 40 | + assert_equal ArticleWithDynamicIndexName.index_name, "articles-2" |
| 41 | + assert_equal ArticleWithDynamicIndexName.index_name, "articles-3" |
55 | 42 | end
|
56 | 43 | end
|
57 | 44 |
|
|
0 commit comments