require 'minitest/autorun' require 'minitest/unit' require 'mocha/mini_test' require './test/helper.rb' class TestDocument < Minitest::Test Document = CouchPillow::Document def mock_time return @time if @time @time = Time.now Time.stubs(:now).returns(@time) @time end def setup CouchPillow.db = FakeCouchbaseServer.new end def test_create d = Document.new d.save! end def test_timestamp d = Document.new({}, "1") d.save! assert d.created_at assert d.updated_at end def test_type d = Document.new({}, "1") assert_equal "default", d._type end def test_type_subclasses d = Class.new(Document) do type 'test' end.new assert_equal "test", d._type end def test_update d = Class.new(Document) do type 'test' end.new hash = { 'me' => 'too', :batman => 'robin', 'apple' => 123 } d.update(hash) assert_equal 'too', d[:me] assert_equal 'robin', d[:batman] assert_equal 123, d[:apple] assert d.save! end def test_update_existing_fields d = Class.new(Document) do type 'test' end.new({ a: 1, b: 2}) assert_equal 1, d[:a] hash = { 'a' => 'too', } d.update(hash) assert_equal 'too', d[:a] assert_equal 2, d[:b] assert d.save! end def test_validate_presence d = Class.new(Document) do validate_presence :xyz end.new assert_raises Document::ValidationError do d.save! end d.xyz = 10 d.save! end def test_validate_type d = Class.new(Document) do validate_type :abc, Hash end.new d.abc = "other type" assert_raises Document::ValidationError do d.save! end d.abc = { :hello => "world" } d.save! end def test_validate_type_also_ensures_types_on_create_integer klass = Class.new(Document) do type "test" validate_type :abc, Integer end CouchPillow.db. expects(:get). with('123'). returns( { '_type' => 'test', 'abc' => '100' } ) d = klass.get('123') assert d assert_equal 100, d.abc end def test_validate_type_also_ensures_types_on_create_string klass = Class.new(Document) do type "test" validate_type :abc, String end CouchPillow.db. expects(:get). with('123'). returns( { '_type' => 'test', 'abc' => 100 } ) d = klass.get('123') assert d assert_equal '100', d.abc end def test_validate_type_also_ensures_types_on_create_float klass = Class.new(Document) do type "test" validate_type :abc, Float end CouchPillow.db. expects(:get). with('123'). returns( { '_type' => 'test', 'abc' => '121.21' } ) d = klass.get('123') assert d assert_equal 121.21, d.abc end def test_validate_type_also_ensures_types_on_create_array klass = Class.new(Document) do type "test" validate_type :abc, Array end CouchPillow.db. expects(:get). with('123'). returns( { '_type' => 'test', 'abc' => 1 } ) d = klass.get('123') assert d assert_equal [1], d.abc end def test_validate_type_also_ensures_types_on_create_time klass = Class.new(Document) do type "test" validate_type :abc, Time end CouchPillow.db. expects(:get). with('123'). returns( { '_type' => 'test', 'abc' => "2014/07/04" } ) d = klass.get('123') assert d assert d.abc.is_a?(Time) assert_equal 2014, d.abc.year assert_equal 7, d.abc.month assert_equal 4, d.abc.day end def test_validate_custom d = Class.new(Document) do validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric } end.new d.xyz = "string" assert_raises Document::ValidationError do d.save! end d.xyz = {} assert_raises Document::ValidationError do d.save! end d.xyz = 123 d.save! end def test_whitelist d = Class.new(Document) do whitelist :a, :b, :c end.new d.update({a: 1, b: 2, c: 3, d: 4}) assert d.save! assert_equal 1, d.a assert_equal 2, d.b assert_equal 3, d.c assert_equal false, d.has?(:d) end def test_whitelist_using_symbol_literal d = Class.new(Document) do whitelist %i[a b c] end.new d.update({a: 1, b: 2, c: 3, d: 4}) assert d.save! assert_equal 1, d.a assert_equal 2, d.b assert_equal 3, d.c assert_equal false, d.has?(:d) end def test_to_json mock_time d = Document.new({}, "1") assert_equal "{\"_id\":\"1\",\"created_at\":\"#{mock_time.to_s}\",\"_type\":\"default\"}", d.to_json end def test_to_hash mock_time d = Document.new({}, "1") assert_equal({ :_id => "1", :created_at => mock_time, :_type => "default" }, d.to_hash) end def test_brackets d = Document.new({}) d[123] = 'test' assert_equal 'test', d[123] d['foo'] = 'bar' assert_equal 'bar', d[:foo] d[:stuff] = [ 'john', 'mary' ] assert_equal ['john', 'mary'], d[:stuff] assert_equal nil, d[:something_else] end def test_rename_keys d = Class.new(Document) do rename :foo, :bar end.new( { :foo => 123, :other => 'abc' } ) assert_equal 123, d[:bar] assert_equal 123, d.bar assert_equal nil, d[:foo] refute d.respond_to?(:foo) assert_equal 'abc', d.other end def test_rename_keys_reserved_keys assert_raises ArgumentError do d = Class.new(Document) do rename :_id, :bad end.new end assert_raises ArgumentError do d = Class.new(Document) do rename :_type, :err end.new end assert_raises ArgumentError do d = Class.new(Document) do rename :created_at, :err end.new end assert_raises ArgumentError do d = Class.new(Document) do rename :bla, :updated_at end.new end end def test_get_returns_nil CouchPillow.db.expects(:get).with('123').returns(nil) d = Document.get('123') assert_equal nil, d end def test_get_returns_a_document_of_a_different_type klass = Class.new(Document) do type 'test' end CouchPillow.db .expects(:get) .with('123') .returns( { '_type' => 'something else', 'stuff' => 'data' } ) d = klass.get('123') assert_equal nil, d end def test_key_with_false_values d = Document.new({ :key => false }, "1") assert_equal false, d.key end def test_key_with_nil_values d = Document.new({ :key => nil }, "1") assert_equal nil, d.key end end