backpack_tf 0.8.1 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ module BackpackTF
4
+
5
+ shared_examples 'a class inheriting methods from Response' do |file|
6
+ let(:bp) { Client.new }
7
+ let(:json_obj) {
8
+ fixture = file_fixture(file)
9
+ fixture = JSON.parse(fixture)['response']
10
+ Response.hash_keys_to_sym(fixture)
11
+ }
12
+
13
+ before :all do
14
+ mock_alias
15
+ end
16
+
17
+ it 'should respond to these inherited methods' do
18
+ expect(described_class).to respond_to :responses, :interface, :to_sym, :hash_keys_to_sym
19
+ end
20
+
21
+ it 'should respond to this aliased method (for testing only)' do
22
+ expect(described_class).to respond_to :data_storage
23
+ end
24
+
25
+ describe '::to_sym' do
26
+ it 'returns a Symbol of the name of the name of the class' do
27
+ expect(described_class.to_sym).to eq described_class.to_s.to_sym
28
+ end
29
+ end
30
+
31
+ describe '::responses' do
32
+ before :all do
33
+ expect(Response.responses).to be_empty
34
+ end
35
+
36
+ after :all do
37
+ Response.responses(:reset => :confirm)
38
+ expect(Response.responses).to be_empty
39
+ expect(described_class.response).to be_nil
40
+ end
41
+
42
+ it "Response class can access response for #{described_class} by calling the key #{described_class}" do
43
+ stub_http_response_with(file)
44
+ fetched_data = bp.fetch(make_fetch_sym)
45
+ bp.update(described_class, fetched_data)
46
+ expect(Response.responses[described_class.to_sym]).to eq json_obj
47
+ end
48
+ end
49
+
50
+ describe '::response' do
51
+ before :all do
52
+ expect(described_class.response).to be_nil
53
+ end
54
+
55
+ before :each do
56
+ stub_http_response_with(file)
57
+ fetched_data = bp.fetch(make_fetch_sym)
58
+ bp.update(described_class, fetched_data)
59
+ end
60
+
61
+ after :all do
62
+ Response.responses(:reset => :confirm)
63
+ expect(Response.responses).to be_empty
64
+ expect(described_class.response).to be_nil
65
+ expect(described_class.data_storage).to be_nil
66
+ end
67
+
68
+ it 'can access response information' do
69
+ expect(described_class.response).to eq json_obj
70
+ end
71
+
72
+ it "returns same info as the Response class calling Price key" do
73
+ result = Response.responses[described_class.to_sym]
74
+ expect(described_class.response).to eq result
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ describe Price do
81
+ it_behaves_like('a class inheriting methods from Response', 'prices.json')
82
+ end
83
+
84
+ describe Currency do
85
+ it_behaves_like('a class inheriting methods from Response', 'currencies.json')
86
+ end
87
+
88
+ describe SpecialItem do
89
+ it_behaves_like('a class inheriting methods from Response', 'special_items.json')
90
+ end
91
+
92
+ describe User do
93
+ it_behaves_like('a class inheriting methods from Response', 'users.json')
94
+ end
95
+
96
+ describe UserListing do
97
+ it_behaves_like('a class inheriting methods from Response', 'user_listing.json')
98
+ end
99
+
100
+ end
@@ -1,53 +1,117 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module BackpackTF
4
- describe 'ItemPrice' do
5
- describe 'instance of ItemPrice' do
6
- context "A typical item that does NOT have the quality 'Unusual'" do
7
- let(:item_price_key) { 'Unique_Tradable_Craftable' }
8
- let(:item_price_attr) { '{"currency":"metal","value":0.05,"last_update":1336410088,"difference":0}' }
9
- let (:item_price) { ItemPrice.new(item_price_key, item_price_attr) }
4
+ describe ItemPrice do
10
5
 
11
- it 'should respond to these methods' do
12
- expect(item_price).to respond_to :quality, :tradability, :craftability, :priceindex, :currency, :value, :value_high, :value_raw, :value_high_raw, :last_update, :difference
6
+ describe '::quality_name_to_index' do
7
+ it 'returns Quality Index of the name of Quality' do
8
+ expect(described_class.quality_name_to_index('Strange')).to eq 11
9
+ end
10
+ it 'returns nil when you ask for index of `nil`' do
11
+ expect(described_class.quality_name_to_index(nil)).to be_nil
12
+ end
13
+ end
14
+
15
+ describe '::hash_particle_effects' do
16
+ it 'creates a key value pair for each id & name' do
17
+ expect(described_class.hash_particle_effects[17]).to eq 'Sunbeams'
18
+ expect(described_class.hash_particle_effects[5]).to eq 'Holy Glow'
19
+ end
20
+ it 'is nil if it cannot find that key' do
21
+ expect(described_class.hash_particle_effects[nil]).to be_nil
22
+ end
23
+
24
+ context '@@particle_effects' do
25
+ it 'same thing can be accessed through class variable' do
26
+ expect(described_class.particle_effects[17]).to eq 'Sunbeams'
27
+ expect(described_class.particle_effects[5]).to eq 'Holy Glow'
28
+ end
29
+ it 'is nil if it cannot find that key' do
30
+ expect(described_class.particle_effects[nil]).to be_nil
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#initialize' do
36
+ base_json = JSON.parse(file_fixture('item_typical.json'))['Kritzkrieg']
37
+ ok_json = base_json['prices']['6']['Tradable']['Craftable'][0]
38
+ wrong_level = base_json['prices']['6']['Tradable']
39
+
40
+ context 'validating first parameter' do
41
+
42
+ it 'will raise ArgumentError if there is not enough information' do
43
+ expect{described_class.new('Unique_Tradable', ok_json)}.to raise_error ArgumentError
44
+ end
45
+ it 'will raise NameError if any one of the info bits is incorrect' do
46
+ expect{described_class.new('xx_Tradable_Craftable', ok_json)}.to raise_error NameError
47
+ end
48
+ it 'will raise a NameError if name has correct info but is out of order' do
49
+ expect{described_class.new('Craftable_Tradable_Strange', ok_json)}.to raise_error NameError
50
+ end
51
+ end
52
+ context 'validating 2nd parameter' do
53
+ it 'will raise a KeyError if JSON does not have required keys' do
54
+ expect{described_class.new('Unique_Tradable_Craftable', wrong_level)}.to raise_error KeyError
13
55
  end
56
+ end
57
+ end
58
+
59
+ shared_examples 'an ItemPrice object' do
60
+ it 'should respond to these methods' do
61
+ expect(subject).to respond_to :quality, :tradability, :craftability, :priceindex, :currency, :value, :value_high, :value_raw, :value_high_raw, :last_update, :difference
62
+ end
63
+ end
64
+
65
+ describe 'A price for a typical item' do
66
+ item_price_attr = JSON.parse(file_fixture('item_typical.json'))['Kritzkrieg']['prices']['6']['Tradable']['Craftable'][0]
67
+
68
+ subject {
69
+ described_class.new('Unique_Tradable_Craftable', item_price_attr)
70
+ }
71
+
72
+ it_behaves_like('an ItemPrice object')
73
+
74
+ context 'item-specific tests' do
14
75
  it 'should have these values' do
15
- expect(item_price.quality).to eq :Unique
16
- expect(item_price.tradability).to eq :Tradable
17
- expect(item_price.craftability).to eq :Craftable
18
- expect(item_price.currency).to eq :metal
19
- expect(item_price.value).to eq 0.05
20
- expect(item_price.value_high).to be_nil
21
- expect(item_price.value_raw).to be_nil
22
- expect(item_price.value_high_raw).to be_nil
23
- expect(item_price.last_update).to eq 1336410088
24
- expect(item_price.difference).to eq 0
25
- expect(item_price.priceindex).to be_nil
26
- end
27
- end
28
-
29
- context "An item with the quality 'Unusual'" do
30
- # the Unusual item being tested is the 'Barnstormer'
31
- let(:item_price_key) { 'Unusual_Tradable_Craftable' }
32
- #let(:item_price_attr) {'{"6":{"currency":"keys","value":18,"last_update":1418795322,"difference":280,"value_high":22}}'}
33
- let(:item_price_attr) {'{"currency":"keys","value":18,"last_update":1418795322,"difference":280,"value_high":22}'}
34
- let(:item_price) { ItemPrice.new(item_price_key, item_price_attr, 6) }
35
-
36
- it 'should respond to these methods' do
37
- expect(item_price).to respond_to :quality, :tradability, :craftability, :priceindex, :currency, :value, :value_high, :value_raw, :value_high_raw, :last_update, :difference
76
+ expect(subject.quality).to eq :Unique
77
+ expect(subject.tradability).to eq :Tradable
78
+ expect(subject.craftability).to eq :Craftable
79
+ expect(subject.currency).to eq :metal
80
+ expect(subject.value).to eq 0.05
81
+ expect(subject.value_high).to be_nil
82
+ expect(subject.value_raw).to be_nil
83
+ expect(subject.value_high_raw).to be_nil
84
+ expect(subject.last_update).to eq 1336410088
85
+ expect(subject.difference).to eq 0
86
+ expect(subject.priceindex).to be_nil
87
+ expect(subject.effect).to be_nil
38
88
  end
89
+ end
90
+ end
91
+
92
+ describe "A price for an item with 'Unusual' quality" do
93
+ item_price_attr = JSON.parse(file_fixture('item_unusual.json'))['Barnstormer']['prices']['5']['Tradable']['Craftable']['6']
94
+
95
+ subject {
96
+ described_class.new('Unusual_Tradable_Craftable', item_price_attr, 6)
97
+ }
98
+
99
+ it_behaves_like('an ItemPrice object')
100
+
101
+ context 'item-specific tests' do
39
102
  it 'should have these values' do
40
- expect(item_price.quality).to eq :Unusual
41
- expect(item_price.tradability).to eq :Tradable
42
- expect(item_price.craftability).to eq :Craftable
43
- expect(item_price.currency).to eq :keys
44
- expect(item_price.value).to eq 18
45
- expect(item_price.value_high).to eq 22
46
- expect(item_price.value_raw).to be_nil
47
- expect(item_price.value_high_raw).to be_nil
48
- expect(item_price.last_update).to eq 1418795322
49
- expect(item_price.difference).to eq 280
50
- expect(item_price.priceindex).to eq 6
103
+ expect(subject.quality).to eq :Unusual
104
+ expect(subject.tradability).to eq :Tradable
105
+ expect(subject.craftability).to eq :Craftable
106
+ expect(subject.currency).to eq :keys
107
+ expect(subject.value).to eq 18
108
+ expect(subject.value_high).to eq 22
109
+ expect(subject.value_raw).to be_nil
110
+ expect(subject.value_high_raw).to be_nil
111
+ expect(subject.last_update).to eq 1418795322
112
+ expect(subject.difference).to eq 280
113
+ expect(subject.priceindex).to eq 6
114
+ expect(subject.effect).to eq 'Green Confetti'
51
115
  end
52
116
  end
53
117
  end
@@ -1,165 +1,126 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module BackpackTF
4
- describe 'Item' do
5
- let(:expected_keys) do
6
- ["Strange_Tradable_Craftable", "Collector's_Tradable_Craftable", "Vintage_Tradable_Craftable", "Unique_Tradable_Craftable", "Unique_Tradable_Non-Craftable"]
7
- end
4
+ describe Item do
5
+ shared_examples 'a common item' do |passed_item_attr|
6
+ it 'should respond to these methods' do
7
+ expect(subject).to respond_to :item_name, :defindex, :prices, :generate_prices_hash
8
+ end
8
9
 
9
- describe 'instance of Item' do
10
- context 'a typical game item, which does not have any item qualities of Unusual' do
11
- let(:item_name) { 'Kritzkrieg' }
12
- let(:stub) do
13
- JSON.parse '{"defindex":[35],"prices":{"11":{"Tradable":{"Craftable":[{"currency":"keys","value":18,"last_update":1430665867,"difference":-18.495}]}},"14":{"Tradable":{"Craftable":[{"currency":"keys","value":9,"last_update":1416841372,"difference":11.385}]}},"3":{"Tradable":{"Craftable":[{"currency":"metal","value":0.66,"last_update":1426439779,"difference":0.33}]}},"6":{"Tradable":{"Craftable":[{"currency":"metal","value":0.05,"last_update":1336410088,"difference":0}],"Non-Craftable":[{"currency":"metal","value":0.05,"last_update":1362791812,"difference":0.03}]}}}}'
14
- end
15
- let(:item) { Item.new(item_name, stub) }
16
- let(:prices_hash) { item.gen_prices_hash(stub) }
10
+ describe '#generate_prices_hash' do
11
+ let(:prices_hash) { subject.generate_prices_hash(passed_item_attr) }
17
12
 
18
- it 'should respond to these methods' do
19
- expect(item).to respond_to :item_name, :defindex, :prices, :gen_prices_hash
13
+ it 'generates a Hash' do
14
+ expect(prices_hash).to be_instance_of Hash
20
15
  end
21
-
22
- it 'should have these values' do
23
- expect(item.item_name).to eq 'Kritzkrieg'
24
- expect(item.defindex).to eq 35
16
+ it 'each key is a String' do
17
+ expect(prices_hash.keys).to all be_a String
25
18
  end
26
-
27
- describe '#gen_prices_hash' do
28
- it 'generates a Hash' do
29
- expect(prices_hash).to be_instance_of Hash
30
- end
31
- it 'each key is a String' do
32
- expect(prices_hash.keys).to all be_a String
33
- end
34
- it 'includes these keys' do
35
- prices_hash.keys.each{ |key| expect(expected_keys).to include(key) }
36
- end
37
- it 'value of each key is an ItemPrice object' do
38
- expect(prices_hash.values).to all be_an ItemPrice
39
- end
19
+ it 'value of each key is an ItemPrice object' do
20
+ expect(prices_hash.values).to all be_an ItemPrice
40
21
  end
22
+ end
41
23
 
42
- describe 'the @prices instance variable' do
43
- it 'is a Hash' do
44
- expect(item.prices).to be_a Hash
45
- end
46
- it 'include these keys' do
47
- item.prices.keys.each{|key| expect(expected_keys).to include(key)}
48
- end
49
- xit 'has all of these keys' do
50
- expect(item.prices.keys).to eq expected_keys
51
- end
52
- it 'each value is an ItemPrice object' do
53
- expect(item.prices.values).to all be_an ItemPrice
54
- end
24
+ describe 'the @prices instance variable' do
25
+ it 'is a Hash' do
26
+ expect(subject.prices).to be_a Hash
27
+ end
28
+ it 'each value is an ItemPrice object' do
29
+ expect(subject.prices.values).to all be_an ItemPrice
55
30
  end
56
31
  end
32
+ end
57
33
 
58
- context 'an item with a quality of Unusual' do
59
- let(:item_name) { 'Barnstormer' }
60
- let(:stub) { JSON.parse(file_fixture('item_unusual.json')) }
61
- let(:item) { Item.new(item_name, stub) }
62
- let(:prices_hash) { item.gen_prices_hash(stub) }
34
+ describe 'A typical item' do
35
+ item_attr = JSON.parse(file_fixture('item_typical.json'))['Kritzkrieg']
36
+ item = described_class.new('Kritzkrieg', item_attr)
37
+ subject { item }
38
+ it_behaves_like('a common item', item_attr)
63
39
 
64
- it 'should respond to these methods' do
65
- expect(item).to respond_to :item_name, :defindex, :prices, :gen_prices_hash
40
+ context 'item-specific tests' do
41
+ it 'should have these values' do
42
+ expect(subject.item_name).to eq 'Kritzkrieg'
43
+ expect(subject.defindex).to eq 35
66
44
  end
45
+ xit '@prices should have these keys' do
46
+ ans = ['Strange_Tradable_Craftable',
47
+ "Collector's_Tradable_Craftable",
48
+ 'Vintage_Tradable_Craftable',
49
+ 'Unique_Tradable_Craftable',
50
+ 'Unique_Tradable_Non-Craftable']
51
+ expect(subject.prices.keys).to match_array ans
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "An item with the quality of 'Unusual'" do
57
+ item_attr = JSON.parse(file_fixture('item_unusual.json'))['Barnstormer']
58
+ item = described_class.new('Barnstormer', item_attr)
59
+ subject { item }
60
+ it_behaves_like('a common item', item_attr)
61
+
62
+ context 'item-specific tests' do
67
63
  it 'should have these values' do
68
64
  expect(item.item_name).to eq 'Barnstormer'
69
65
  expect(item.defindex).to eq 988
70
66
  end
71
-
72
- describe '#gen_prices_hash' do
73
- it 'generates a Hash' do
74
- expect(prices_hash).to be_instance_of Hash
75
- end
76
- it 'each key is a String' do
77
- expect(prices_hash.keys).to all be_a String
78
- end
79
- it 'value of each key is an ItemPrice object' do
80
- expect(prices_hash.values).to all be_an ItemPrice
81
- end
82
- end
83
- describe 'the @prices instance variable' do
84
- it 'is a Hash' do
85
- expect(item.prices).to be_a Hash
86
- end
87
- it 'each value is an ItemPrice object' do
88
- expect(item.prices.values).to all be_an ItemPrice
89
- end
67
+ it '@prices should have these keys' do
68
+ priceindex_vals = ["Aces High", "Anti-Freeze", "Blizzardy Storm", "Bubbling", "Burning Flames", "Circling Heart", "Circling TF Logo", "Cloud 9", "Dead Presidents", "Death at Dusk", "Disco Beat Down", "Electrostatic", "Green Black Hole", "Green Confetti", "Green Energy", "Haunted Ghosts", "Kill-a-Watt", "Memory Leak", "Miami Nights", "Morning Glory", "Nuts n' Bolts", "Orbiting Fire", "Orbiting Planets", "Overclocked", "Phosphorous", "Power Surge", "Purple Confetti", "Purple Energy", "Roboactive", "Scorching Flames", "Smoking", "Steaming", "Stormy Storm", "Sulphurous", "Terror-Watt", "Time Warp", "Unique_Tradable_Craftable"]
69
+ expect(subject.prices.keys).to match_array priceindex_vals
90
70
  end
91
71
  end
72
+ end
92
73
 
93
- context 'an item with an unconventional structure' do
94
- let(:item_name) { 'Aqua Summer 2013 Coolor' }
95
- let(:stub) { JSON.parse(file_fixture('item_with_unconventional_structure.json')) }
96
- let(:item) { Item.new(item_name, stub) }
97
- let(:prices_hash) { item.gen_prices_hash(stub) }
74
+ describe 'An item with many priceindex values (Crate or Unusual)' do
75
+ item_attr = JSON.parse(file_fixture('item_crate.json'))['Mann Co. Supply Munition']
76
+ item = described_class.new('Mann Co. Supply Munition', item_attr)
77
+ subject { item }
78
+ it_behaves_like('a common item', item_attr)
98
79
 
99
- it 'should respond to these methods' do
100
- expect(item).to respond_to :item_name, :defindex, :prices, :gen_prices_hash
101
- end
80
+ context 'item-specific tests' do
102
81
  it 'should have these values' do
103
- expect(item.item_name).to eq 'Aqua Summer 2013 Coolor'
104
- expect(item.defindex).to eq 5650
82
+ expect(subject.item_name).to eq 'Mann Co. Supply Munition'
83
+ expect(subject.defindex).to match_array [5734, 5735, 5742, 5752, 5781, 5802, 5803]
84
+ end
85
+ it 'prices array should have these keys' do
86
+ ans = ['Unique_Tradable_Craftable_#82',
87
+ 'Unique_Tradable_Craftable_#83',
88
+ 'Unique_Tradable_Craftable_#84',
89
+ 'Unique_Tradable_Craftable_#85',
90
+ 'Unique_Tradable_Craftable_#90',
91
+ 'Unique_Tradable_Craftable_#91',
92
+ 'Unique_Tradable_Craftable_#92']
93
+ expect(subject.prices.keys).to match_array ans
105
94
  end
95
+ end
96
+ end
106
97
 
107
- describe '#gen_prices_hash' do
108
- it 'generates a Hash' do
109
- expect(prices_hash).to be_instance_of Hash
110
- end
111
- it 'each key is a String' do
112
- expect(prices_hash.keys).to all be_a String
113
- end
114
- it 'value of each key is an ItemPrice object' do
115
- expect(prices_hash.values).to all be_an ItemPrice
116
- end
117
- end
118
- describe 'the @prices instance variable' do
119
- it 'is a Hash' do
120
- expect(item.prices).to be_a Hash
121
- end
122
- it 'each value is an ItemPrice object' do
123
- expect(item.prices.values).to all be_an ItemPrice
124
- end
98
+ describe 'An item with an unconventional structure' do
99
+ item_attr = JSON.parse(file_fixture('item_with_unconventional_structure.json'))['Aqua Summer 2013 Cooler']
100
+ item = described_class.new('Aqua Summer 2013 Cooler', item_attr)
101
+ subject { item }
102
+ it_behaves_like('a common item', item_attr)
103
+
104
+ context 'item-specific tests' do
105
+ it 'should have these values' do
106
+ expect(item.item_name).to eq 'Aqua Summer 2013 Cooler'
107
+ expect(item.defindex).to eq 5650
125
108
  end
126
109
  end
110
+ end
127
111
 
128
- context 'an item that has no `defindex` or has a negative `defindex`' do
129
- let(:item_name) { 'Strange Part: Fires Survived' }
130
- let(:stub) { JSON.parse(file_fixture('item_without_defindex.json')) }
131
- let(:item) { Item.new(item_name, stub) }
132
- let(:prices_hash) { item.gen_prices_hash(stub) }
112
+ describe "An item that has a negative 'defindex' or no 'defindex' at all" do
113
+ item_attr = JSON.parse(file_fixture('item_without_defindex.json'))['Strange Part: Fires Survived']
114
+ item = described_class.new('Strange Part: Fires Survived', item_attr)
115
+ subject { item }
116
+ it_behaves_like('a common item', item_attr)
133
117
 
134
- it 'should respond to these methods' do
135
- expect(item).to respond_to :item_name, :defindex, :prices, :gen_prices_hash
136
- end
118
+ context 'item-specific tests' do
137
119
  it 'should have these values' do
138
120
  expect(item.item_name).to eq 'Strange Part: Fires Survived'
139
121
  expect(item.defindex).to eq nil
140
122
  end
141
-
142
- describe '#gen_prices_hash' do
143
- it 'generates a Hash' do
144
- expect(prices_hash).to be_instance_of Hash
145
- end
146
- it 'each key is a String' do
147
- expect(prices_hash.keys).to all be_a String
148
- end
149
- it 'value of each key is an ItemPrice object' do
150
- expect(prices_hash.values).to all be_an ItemPrice
151
- end
152
- end
153
- describe 'the @prices instance variable' do
154
- it 'is a Hash' do
155
- expect(item.prices).to be_a Hash
156
- end
157
- it 'each value is an ItemPrice object' do
158
- expect(item.prices.values).to all be_an ItemPrice
159
- end
160
- end
161
123
  end
162
-
163
124
  end
164
125
  end
165
126
  end