|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class Elasticsearch::Persistence::RepositoryNamingTest < Test::Unit::TestCase |
| 4 | + context "The repository naming" do |
| 5 | + # Fake class for the naming tests |
| 6 | + class ::Foobar; end |
| 7 | + class ::FooBar; end |
| 8 | + module ::Foo; class Bar; end; end |
| 9 | + |
| 10 | + setup do |
| 11 | + @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Naming }.new |
| 12 | + end |
| 13 | + |
| 14 | + context "get Ruby class from the Elasticsearch type" do |
| 15 | + should "get a simple class" do |
| 16 | + assert_equal Foobar, subject.__get_klass_from_type('foobar') |
| 17 | + end |
| 18 | + should "get a camelcased class" do |
| 19 | + assert_equal FooBar, subject.__get_klass_from_type('foo_bar') |
| 20 | + end |
| 21 | + should "get a namespaced class" do |
| 22 | + assert_equal Foo::Bar, subject.__get_klass_from_type('foo/bar') |
| 23 | + end |
| 24 | + should "re-raise a NameError exception" do |
| 25 | + assert_raise NameError do |
| 26 | + subject.__get_klass_from_type('foobarbazbam') |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context "get Elasticsearch type from the Ruby class" do |
| 32 | + should "encode a simple class" do |
| 33 | + assert_equal 'foobar', subject.__get_type_from_class(Foobar) |
| 34 | + end |
| 35 | + should "encode a camelcased class" do |
| 36 | + assert_equal 'foo_bar', subject.__get_type_from_class(FooBar) |
| 37 | + end |
| 38 | + should "encode a namespaced class" do |
| 39 | + assert_equal 'foo/bar', subject.__get_type_from_class(Foo::Bar) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "get an ID from the document" do |
| 44 | + should "get an ID from Hash" do |
| 45 | + assert_equal 1, subject.__get_id_from_document(id: 1) |
| 46 | + assert_equal 1, subject.__get_id_from_document(_id: 1) |
| 47 | + assert_equal 1, subject.__get_id_from_document('id' => 1) |
| 48 | + assert_equal 1, subject.__get_id_from_document('_id' => 1) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context " document class name" do |
| 53 | + should "be nil by default" do |
| 54 | + assert_nil subject.klass |
| 55 | + end |
| 56 | + |
| 57 | + should "be settable" do |
| 58 | + subject.klass = Foobar |
| 59 | + assert_equal Foobar, subject.klass |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + end |
| 64 | +end |
0 commit comments