|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +require 'elasticsearch/persistence/model' |
| 4 | + |
| 5 | +module Elasticsearch |
| 6 | + module Persistence |
| 7 | + class PersistenceModelBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase |
| 8 | + |
| 9 | + class ::Person |
| 10 | + include Elasticsearch::Persistence::Model |
| 11 | + |
| 12 | + settings index: { number_of_shards: 1 } |
| 13 | + |
| 14 | + attribute :name, String, |
| 15 | + mapping: { fields: { |
| 16 | + name: { type: 'string', analyzer: 'snowball' }, |
| 17 | + raw: { type: 'string', analyzer: 'keyword' } |
| 18 | + } } |
| 19 | + |
| 20 | + attribute :birthday, Date |
| 21 | + attribute :department, String |
| 22 | + attribute :salary, Integer |
| 23 | + attribute :admin, Boolean, default: false |
| 24 | + |
| 25 | + validates :name, presence: true |
| 26 | + end |
| 27 | + |
| 28 | + context "A basic persistence model" do |
| 29 | + should "save and find the object" do |
| 30 | + person = Person.new name: 'John Smith', birthday: Date.parse('1970-01-01') |
| 31 | + person.save |
| 32 | + |
| 33 | + assert_not_nil person.id |
| 34 | + document = Person.find(person.id) |
| 35 | + |
| 36 | + assert_instance_of Person, document |
| 37 | + assert_equal 'John Smith', document.name |
| 38 | + assert_equal 'John Smith', Person.find(person.id).name |
| 39 | + |
| 40 | + assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id |
| 41 | + end |
| 42 | + |
| 43 | + should "delete the object" do |
| 44 | + person = Person.create name: 'John Smith', birthday: Date.parse('1970-01-01') |
| 45 | + |
| 46 | + person.destroy |
| 47 | + assert person.frozen? |
| 48 | + |
| 49 | + assert_raise Elasticsearch::Transport::Transport::Errors::NotFound do |
| 50 | + Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + should "update an object attribute" do |
| 55 | + person = Person.create name: 'John Smith' |
| 56 | + |
| 57 | + person.update name: 'UPDATED' |
| 58 | + |
| 59 | + assert_equal 'UPDATED', person.name |
| 60 | + assert_equal 'UPDATED', Person.find(person.id).name |
| 61 | + end |
| 62 | + |
| 63 | + should "increment an object attribute" do |
| 64 | + person = Person.create name: 'John Smith', salary: 1_000 |
| 65 | + |
| 66 | + person.increment :salary |
| 67 | + |
| 68 | + assert_equal 1_001, person.salary |
| 69 | + assert_equal 1_001, Person.find(person.id).salary |
| 70 | + end |
| 71 | + |
| 72 | + should "update the object timestamp" do |
| 73 | + person = Person.create name: 'John Smith' |
| 74 | + updated_at = person.updated_at |
| 75 | + |
| 76 | + sleep 1 |
| 77 | + person.touch |
| 78 | + |
| 79 | + assert person.updated_at > updated_at, [person.updated_at, updated_at].inspect |
| 80 | + |
| 81 | + found = Person.find(person.id) |
| 82 | + assert found.updated_at > updated_at, [found.updated_at, updated_at].inspect |
| 83 | + end |
| 84 | + |
| 85 | + should "find instances by search" do |
| 86 | + Person.create name: 'John Smith' |
| 87 | + Person.create name: 'Mary Smith' |
| 88 | + Person.gateway.refresh_index! |
| 89 | + |
| 90 | + people = Person.search query: { match: { name: 'smith' } } |
| 91 | + |
| 92 | + assert_equal 2, people.total |
| 93 | + assert_equal 2, people.size |
| 94 | + |
| 95 | + assert people.map_with_hit { |o,h| h._score }.all? { |s| s > 0 } |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments