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_default_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' attribute(:me) attribute(:batman) attribute(:apple) end.new refute d.me refute d.batman refute d.apple 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' attribute(:a) attribute(:b) end.new({ a: 1, b: 2}) assert_equal 1, d[:a] 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_update_doesnt_trigger_validation d = Class.new(Document) do type 'test' attribute(:a).type(Integer) end.new({ a: 1, b: 2}) assert_equal 1, d[:a] assert_equal 1, d.a hash = { 'a' => 'too', } d.update(hash) assert_equal 'too', d.a assert_raises CouchPillow::ValidationError do assert d.save! end end def test_exclusion_other_keys d = Class.new(Document) do attribute :a attribute :b attribute :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\",\"_type\":\"default\",\"_created_at\":\"#{mock_time.to_s}\",\"_updated_at\":\"#{mock_time.to_s}\"}", d.to_json end def test_to_hash mock_time d = Document.new({}, "1") assert_equal({ :_id => "1", :_created_at => mock_time, :_type => "default", :_updated_at => mock_time }, 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 attribute(:bar) attribute(:other) 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 klass = Class.new(Document) do type 'test' attribute(:foo) end k = klass.new k.foo = 100 k.save! k_id = k._id d = klass.get(k_id) assert_equal 100, d.foo 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 klass = Class.new(Document) do attribute(:key) end d = klass.new({ :key => false }, "1") assert_equal false, d.key end def test_key_with_nil_values klass = Class.new(Document) do attribute(:key) end d = klass.new({ :key => nil }, "1") assert_equal nil, d.key end def test_attribute d = Class.new(Document) do type 'test' attribute(:foo) end.new d.foo = 12345 d.save! end def test_attribute_type d = Class.new(Document) do type 'test' attribute(:foo) .type(String) end.new d.foo = "test" d.save! end def test_attribute_auto_convert d = Class.new(Document) do type 'test' attribute(:foo) .type(String).auto_convert end.new d.foo = 1 d.save! assert_equal "1", d.foo end def test_attribute_with_directives d = Class.new(Document) do type 'test' attribute(:foo) .default { 100 } end.new d.save! assert_equal 100, d.foo end def test_attribute_no_default_but_required klass = Class.new(Document) do type 'test' attribute(:foo) .required end k = klass.new assert_raises CouchPillow::ValidationError do k.save! end end def test_attribute_with_default_but_also_required klass = Class.new(Document) do type 'test' attribute(:foo) .required .default { "batman" } end k = klass.new k.save! end end