File: machine_with_scopes_test.rb

package info (click to toggle)
ruby-state-machines-activerecord 0.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: ruby: 2,527; makefile: 6
file content (116 lines) | stat: -rw-r--r-- 3,428 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true

require_relative 'test_helper'

class MachineWithScopesTest < BaseTestCase
  def setup
    @model = new_model do
      connection.add_column table_name, :name, :string
    end
    @machine = StateMachines::Machine.new(@model)
    @machine.state :parked, :first_gear
    @machine.state :idling, value: -> { 'idling' }
  end

  def test_should_allow_chaining_scopes_with_queries
    named = @model.create state: 'parked', name: 'a_name'
    @model.create state: 'parked'

    assert_equal [named], @model.where(name: 'a_name').with_state(:parked)
  end

  def test_should_create_singular_with_scope
    assert @model.respond_to?(:with_state)
  end

  def test_should_only_include_records_with_state_in_singular_with_scope
    parked = @model.create state: 'parked'
    @model.create state: 'idling'

    assert_equal [parked], @model.with_state(:parked).all
  end

  def test_should_allow_transparent_with_state_in_singular_with_scope
    @model.create state: 'parked'
    @model.create state: 'idling'

    assert_equal @model.all, @model.with_state(nil).all
  end

  def test_should_create_plural_with_scope
    assert @model.respond_to?(:with_states)
  end

  def test_should_only_include_records_with_states_in_plural_with_scope
    parked = @model.create state: 'parked'
    idling = @model.create state: 'idling'

    assert_equal [parked, idling], @model.with_states(:parked, :idling).all
  end

  def test_should_allow_transparent_with_states_in_plural_with_scope
    @model.create state: 'parked'
    @model.create state: 'idling'

    assert_equal @model.all, @model.with_states(nil).all
  end

  def test_should_allow_lookup_by_string_name
    parked = @model.create state: 'parked'
    idling = @model.create state: 'idling'

    assert_equal [parked, idling], @model.with_states('parked', 'idling').all
  end

  def test_should_create_singular_without_scope
    assert @model.respond_to?(:without_state)
  end

  def test_should_only_include_records_without_state_in_singular_without_scope
    parked = @model.create state: 'parked'
    @model.create state: 'idling'

    assert_equal [parked], @model.without_state(:idling).all
  end

  def test_allow_transparent_without_state_in_singular_without_scope
    @model.create state: 'parked'
    @model.create state: 'idling'

    assert_equal @model.all, @model.without_state(nil).all
  end

  def test_should_create_plural_without_scope
    assert @model.respond_to?(:without_states)
  end

  def test_should_only_include_records_without_states_in_plural_without_scope
    parked = @model.create state: 'parked'
    idling = @model.create state: 'idling'
    @model.create state: 'first_gear'

    assert_equal [parked, idling], @model.without_states(:first_gear).all
  end

  def test_allow_transparent_without_states_in_plural_without_scope
    @model.create state: 'parked'
    @model.create state: 'idling'
    @model.create state: 'first_gear'

    assert_equal @model.all, @model.without_states(nil).all
  end

  def test_should_allow_chaining_scopes
    @model.create state: 'parked'
    idling = @model.create state: 'idling'

    assert_equal [idling], @model.without_state(:parked).with_state(:idling).all
  end

  def test_should_allow_chaining_transparent_scopes
    @model.create state: 'parked'
    idling = @model.create state: 'idling'

    assert_equal [idling], @model.with_state(nil).with_state(:idling).all
  end
end