forked from ninech/netbox-client-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation_spec.rb
More file actions
138 lines (106 loc) · 3.41 KB
/
Copy pathlocation_spec.rb
File metadata and controls
138 lines (106 loc) · 3.41 KB
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 'spec_helper'
RSpec.describe NetboxClientRuby::DCIM::Location, faraday_stub: true do
subject { sut.new entity_id }
let(:entity_id) { 1 }
let(:expected_name) { '1' }
let(:sut) { described_class }
let(:base_url) { '/api/dcim/locations/' }
let(:request_url) { "#{base_url}#{entity_id}/" }
let(:response) { File.read("spec/fixtures/dcim/location_#{entity_id}.json") }
describe '#id' do
it 'shall be the expected id' do
expect(subject.id).to eq(entity_id)
end
end
describe '#name' do
it 'fetches the data' do
expect(faraday).to receive(:get).and_call_original
expect(subject.name).to_not be_nil
end
it 'shall be the expected name' do
expect(subject.name).to eq(expected_name)
end
end
describe '.delete' do
let(:request_method) { :delete }
let(:response_status) { 204 }
let(:response) { nil }
it 'deletes the object' do
expect(faraday).to receive(request_method).and_call_original
subject.delete
end
end
describe '.update' do
let(:request_method) { :patch }
let(:request_params) { { 'name' => 'noob' } }
it 'updates the object' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.update(name: 'noob').name).to eq(expected_name)
end
end
describe '.reload' do
it 'reloads the object' do
expect(faraday).to receive(request_method).twice.and_call_original
subject.reload
subject.reload
end
end
describe '.save' do
let(:name) { 'foobar' }
let(:slug) { name }
let(:request_params) { { 'name' => name, 'slug' => slug } }
context 'update' do
subject do
region = sut.new entity_id
region.name = name
region.slug = slug
region
end
let(:request_method) { :patch }
it 'does not call PATCH until save is called' do
expect(faraday).to_not receive(request_method)
expect(faraday).to_not receive(:get)
expect(subject.name).to eq(name)
expect(subject.slug).to eq(slug)
end
it 'calls PATCH when save is called' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.save).to be(subject)
end
it 'Reads the answer from the PATCH answer' do
expect(faraday).to receive(request_method).and_call_original
subject.save
expect(subject.name).to eq(expected_name)
expect(subject.slug).to eq(expected_name)
end
end
context 'create' do
subject do
region = sut.new
region.name = name
region.slug = slug
region
end
let(:request_method) { :post }
let(:request_url) { base_url }
it 'does not POST until save is called' do
expect(faraday).to_not receive(request_method)
expect(faraday).to_not receive(:get)
expect(subject.name).to eq(name)
expect(subject.slug).to eq(slug)
end
it 'POSTs the data upon a call of save' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.save).to be(subject)
end
it 'Reads the answer from the POST' do
expect(faraday).to receive(request_method).and_call_original
subject.save
expect(subject.id).to be(1)
expect(subject.name).to eq(expected_name)
expect(subject.slug).to eq(expected_name)
end
end
end
end