forked from rails/activeresource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton_test.rb
138 lines (110 loc) · 3.97 KB
/
singleton_test.rb
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true
require "abstract_unit"
require "fixtures/weather"
require "fixtures/inventory"
class SingletonTest < ActiveSupport::TestCase
def setup_weather
weather = { status: "Sunny", temperature: 67 }
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/weather.json", {}, weather.to_json
mock.get "/weather.json?degrees=fahrenheit", {}, weather.merge(temperature: 100).to_json
mock.post "/weather.json", {}, weather.to_json, 201, "Location" => "/weather.json"
mock.delete "/weather.json", {}, nil
mock.put "/weather.json", {}, nil, 204
end
end
def setup_weather_not_found
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/weather.json", {}, nil, 404
end
end
def setup_inventory
inventory = { status: "Sold Out", total: 10, used: 10 }.to_json
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/products/5/inventory.json", {}, inventory
end
end
def test_custom_singleton_name
assert_equal "dashboard", WeatherDashboard.singleton_name
end
def test_singleton_path
assert_equal "/weather.json", Weather.singleton_path
end
def test_singleton_path_with_parameters
assert_equal "/weather.json?degrees=fahrenheit", Weather.singleton_path(degrees: "fahrenheit")
assert_equal "/weather.json?degrees=false", Weather.singleton_path(degrees: false)
assert_equal "/weather.json?degrees=", Weather.singleton_path(degrees: nil)
assert_equal "/weather.json?degrees=fahrenheit", Weather.singleton_path("degrees" => "fahrenheit")
# Use include? because ordering of param hash is not guaranteed
path = Weather.singleton_path(degrees: "fahrenheit", lunar: true)
assert path.include?("weather.json")
assert path.include?("degrees=fahrenheit")
assert path.include?("lunar=true")
path = Weather.singleton_path(days: ["monday", "saturday and sunday", nil, false])
assert_equal "/weather.json?days%5B%5D=monday&days%5B%5D=saturday+and+sunday&days%5B%5D=&days%5B%5D=false", path
path = Inventory.singleton_path(product_id: 5)
assert_equal "/products/5/inventory.json", path
path = Inventory.singleton_path({ product_id: 5 }, { sold: true })
assert_equal "/products/5/inventory.json?sold=true", path
end
def test_find_singleton
setup_weather
weather = Weather.send(:find_singleton, Hash.new)
assert_not_nil weather
assert_equal "Sunny", weather.status
assert_equal 67, weather.temperature
end
def test_find
setup_weather
weather = Weather.find
assert_not_nil weather
assert_equal "Sunny", weather.status
assert_equal 67, weather.temperature
end
def test_find_with_param_options
setup_inventory
inventory = Inventory.find(params: { product_id: 5 })
assert_not_nil inventory
assert_equal "Sold Out", inventory.status
assert_equal 10, inventory.used
assert_equal 10, inventory.total
assert_equal({ product_id: 5 }, inventory.prefix_options)
end
def test_find_with_query_options
setup_weather
weather = Weather.find(params: { degrees: "fahrenheit" })
assert_not_nil weather
assert_equal "Sunny", weather.status
assert_equal 100, weather.temperature
end
def test_not_found
setup_weather_not_found
assert_raise ActiveResource::ResourceNotFound do
Weather.find
end
end
def test_create_singleton
setup_weather
weather = Weather.create(status: "Sunny", temperature: 67)
assert_not_nil weather
assert_equal "Sunny", weather.status
assert_equal 67, weather.temperature
end
def test_destroy
setup_weather
# First Create the Weather
weather = Weather.create(status: "Sunny", temperature: 67)
assert_not_nil weather
# Now Destroy it
weather.destroy
end
def test_update
setup_weather
# First Create the Weather
weather = Weather.create(status: "Sunny", temperature: 67)
assert_not_nil weather
# Then update it
weather.status = "Rainy"
weather.save
end
end