File: object_spec.rb

package info (click to toggle)
ruby-bullet 7.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 6,133; javascript: 57; sh: 27; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

require 'spec_helper'

describe Object do
  context 'bullet_key' do
    it 'should return class and id composition' do
      post = Post.first
      expect(post.bullet_key).to eq("Post:#{post.id}")
    end

    if mongoid?
      it 'should return class with namespace and id composition' do
        post = Mongoid::Post.first
        expect(post.bullet_key).to eq("Mongoid::Post:#{post.id}")
      end
    end
  end

  context 'bullet_primary_key_value' do
    it 'should return id' do
      post = Post.first
      expect(post.bullet_primary_key_value).to eq(post.id)
    end

    it 'should return primary key value' do
      post = Post.first
      Post.primary_key = 'name'
      expect(post.bullet_primary_key_value).to eq(post.name)
      Post.primary_key = 'id'
    end

    it 'should return value for multiple primary keys from the composite_primary_key gem' do
      post = Post.first
      allow(Post).to receive(:primary_keys).and_return(%i[category_id writer_id])
      expect(post.bullet_primary_key_value).to eq("#{post.category_id},#{post.writer_id}")
    end

    it 'should return value for multiple primary keys from ActiveRecord 7.1' do
      post = Post.first
      allow(Post).to receive(:primary_key).and_return(%i[category_id writer_id])
      expect(post.bullet_primary_key_value).to eq("#{post.category_id},#{post.writer_id}")
    end

    it 'it should return nil for unpersisted records' do
      post = Post.new(id: 123)
      expect(post.bullet_primary_key_value).to be_nil
    end
  end
end