Sha256: 7c48a16947717682c900340b9b03c5ccc42924ed9ab10bd4cd9b37664743a8ae

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require_relative './spec_helper'

describe Groovy::Model do

  before :all do
    Groovy.open('tmp/vector', 'vector_spec')
    load_schema! 'vector_spec'
  end

  after :all do
    Groovy.close('vector_spec')
  end

  def load_vector_schema!
    klass = Class.new
    Object.const_set("Comment", klass)
    Comment.class_eval do
      include Groovy::Model
      schema(context: 'vector_spec') do |t|
        t.string :content
        t.timestamps
      end
    end

    klass = Class.new
    Object.const_set("User", klass)
    User.class_eval do
      include Groovy::Model
      schema(context: 'vector_spec') do |t|
        t.string :name
        t.timestamps
      end
    end

    Comment.add_reference :author, "Users"
    User.add_reference :comments, "Comments", type: :vector
  end

  describe 'relations' do

    before do
      load_vector_schema!
    end

    it 'loads records and adds reverse relations automatically' do
      user = User.new(name: 'John')
      expect(user.save).to eq(true)
      comment = Comment.new(author: user, content: 'Hello there!')
      expect(comment.save).to eq(true)
      # user.comments << comment
      expect(user.reload.comments).to eq([comment])

      user2 = User.new(name: 'Paul')
      expect(user2.save).to eq(true)

      comment.author = user2
      comment.save

      expect(user.reload.comments).to eq([])
      expect(user2.reload.comments).to eq([comment])

      comment.delete
      expect(user2.reload.comments).to eq([])
    end

    it 'finds records using relations too' do
      user = User.create(name: 'John')
      comment = Comment.create(author: user, content: 'Hello there!')

      expect(Comment.find_by(author: user)).to eq(comment)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groovy-0.7.1 spec/vector_spec.rb
groovy-0.7.0 spec/vector_spec.rb