|
2 | 2 |
|
3 | 3 | class Elasticsearch::Persistence::RepositoryModuleTest < Test::Unit::TestCase
|
4 | 4 | context "The repository module" do
|
| 5 | + |
| 6 | + class DummyModel |
| 7 | + def initialize(attributes={}) |
| 8 | + @attributes = attributes |
| 9 | + end |
| 10 | + |
| 11 | + def to_hash |
| 12 | + @attributes |
| 13 | + end |
| 14 | + |
| 15 | + def inspect |
| 16 | + "<Note #{@attributes.inspect}>" |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + setup do |
| 21 | + class DummyRepository |
| 22 | + include Elasticsearch::Persistence::Repository |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + teardown do |
| 27 | + Elasticsearch::Persistence::RepositoryModuleTest.__send__ :remove_const, :DummyRepository |
| 28 | + end |
| 29 | + |
| 30 | + context "when included" do |
| 31 | + should "set up the gateway for the class and instance" do |
| 32 | + assert_respond_to DummyRepository, :gateway |
| 33 | + assert_respond_to DummyRepository.new, :gateway |
| 34 | + |
| 35 | + assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.gateway |
| 36 | + assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.new.gateway |
| 37 | + end |
| 38 | + |
| 39 | + should "proxy repository methods from the class to the gateway" do |
| 40 | + class DummyRepository |
| 41 | + include Elasticsearch::Persistence::Repository |
| 42 | + |
| 43 | + index :foobar |
| 44 | + klass DummyModel |
| 45 | + type :my_dummy_model |
| 46 | + mapping { indexes :title, analyzer: 'snowball' } |
| 47 | + end |
| 48 | + |
| 49 | + repository = DummyRepository.new |
| 50 | + |
| 51 | + assert_equal :foobar, DummyRepository.index |
| 52 | + assert_equal DummyModel, DummyRepository.klass |
| 53 | + assert_equal :my_dummy_model, DummyRepository.type |
| 54 | + assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer] |
| 55 | + |
| 56 | + assert_equal :foobar, repository.index |
| 57 | + assert_equal DummyModel, repository.klass |
| 58 | + assert_equal :my_dummy_model, repository.type |
| 59 | + assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer] |
| 60 | + end |
| 61 | + |
| 62 | + should "proxy repository methods from the instance to the gateway" do |
| 63 | + class DummyRepository |
| 64 | + include Elasticsearch::Persistence::Repository |
| 65 | + end |
| 66 | + |
| 67 | + repository = DummyRepository.new |
| 68 | + repository.index :foobar |
| 69 | + repository.klass DummyModel |
| 70 | + repository.type :my_dummy_model |
| 71 | + repository.mapping { indexes :title, analyzer: 'snowball' } |
| 72 | + |
| 73 | + assert_equal :foobar, DummyRepository.index |
| 74 | + assert_equal DummyModel, DummyRepository.klass |
| 75 | + assert_equal :my_dummy_model, DummyRepository.type |
| 76 | + assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer] |
| 77 | + |
| 78 | + assert_equal :foobar, repository.index |
| 79 | + assert_equal DummyModel, repository.klass |
| 80 | + assert_equal :my_dummy_model, repository.type |
| 81 | + assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer] |
| 82 | + end |
| 83 | + |
| 84 | + should "allow to define gateway methods in the class definition" do |
| 85 | + class DummyRepository |
| 86 | + include Elasticsearch::Persistence::Repository |
| 87 | + |
| 88 | + gateway do |
| 89 | + def serialize(document) |
| 90 | + 'FAKE!' |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + repository = DummyRepository.new |
| 96 | + repository.client.transport.logger = Logger.new(STDERR) |
| 97 | + |
| 98 | + client = mock |
| 99 | + client.expects(:index).with do |arguments| |
| 100 | + assert_equal('xxx', arguments[:id]) |
| 101 | + assert_equal('FAKE!', arguments[:body]) |
| 102 | + end |
| 103 | + repository.gateway.expects(:client).returns(client) |
| 104 | + |
| 105 | + repository.gateway.expects(:__get_id_from_document).returns('xxx') |
| 106 | + |
| 107 | + repository.save( id: '123', foo: 'bar' ) |
| 108 | + end |
| 109 | + end |
| 110 | + |
5 | 111 | end
|
6 | 112 | end
|
0 commit comments