File: request_store_test.rb

package info (click to toggle)
ruby-request-store 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: ruby: 257; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 1,938 bytes parent folder | download | duplicates (2)
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
require 'minitest/autorun'

require 'request_store'

class RequestStoreTest < Minitest::Test
  def setup
    RequestStore.clear!
  end

  def teardown
    RequestStore.clear!
  end

  def test_initial_state
    Thread.current[:request_store] = nil
    assert_equal RequestStore.store, Hash.new
  end

  def test_init_with_hash
    assert_equal Hash.new, RequestStore.store
  end

  def test_assign_store
    store_obj = { test_key: 'test' }
    RequestStore.store = store_obj
    assert_equal 'test', RequestStore.store[:test_key]
    assert_equal store_obj, RequestStore.store
  end

  def test_clear
    RequestStore.store[:foo] = 1
    RequestStore.clear!
    assert_equal Hash.new, RequestStore.store
  end

  def test_quacks_like_hash
    RequestStore.store[:foo] = 1
    assert_equal 1, RequestStore.store[:foo]
    assert_equal 1, RequestStore.store.fetch(:foo)
  end

  def test_read
    RequestStore.store[:foo] = 1
    assert_equal 1, RequestStore.read(:foo)
    assert_equal 1, RequestStore[:foo]
  end

  def test_write
    RequestStore.write(:foo, 1)
    assert_equal 1, RequestStore.store[:foo]
    RequestStore[:foo] = 2
    assert_equal 2, RequestStore.store[:foo]
  end

  def test_fetch
    assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
    assert_equal 2, RequestStore.fetch(:foo) { 2 + 2 }
  end

  def test_delete
    assert_equal 2, RequestStore.fetch(:foo) { 1 + 1 }
    assert_equal 2, RequestStore.delete(:foo) { 2 + 2 }
    assert_equal 4, RequestStore.delete(:foo) { 2 + 2 }
  end

  def test_delegates_to_thread
    RequestStore.store[:foo] = 1
    assert_equal 1, Thread.current[:request_store][:foo]
  end

  def test_active_state
    assert_equal false, RequestStore.active?

    RequestStore.begin!
    assert_equal true, RequestStore.active?

    RequestStore.end!
    assert_equal false, RequestStore.active?
  end
end