|
107 | 107 | describe 'GET /v2/buscar' do |
108 | 108 | let!(:postal_code1) { create(:postal_code, codigo_postal: '12345') } |
109 | 109 | let!(:postal_code2) { create(:postal_code, codigo_postal: '12346') } |
| 110 | + let!(:postal_code3) { create(:postal_code, codigo_postal: '12347') } |
| 111 | + let!(:postal_code4) { create(:postal_code, codigo_postal: '12348') } |
110 | 112 |
|
111 | 113 | it 'returns matching postal codes in v2 format' do |
112 | 114 | get '/v2/buscar?codigo_postal=123' |
113 | 115 | expect(last_response).to be_ok |
114 | 116 | expect(last_response.headers['Content-Type']).to include('application/json') |
115 | 117 |
|
116 | 118 | json_response = Oj.load(last_response.body) |
117 | | - puts "Response body: #{last_response.body}" |
118 | | - expect(json_response['codigos_postales']).to match_array(%w[12345 12346]) |
| 119 | + expect(json_response['codigos_postales']).to match_array(%w[12345 12346 12347 12348]) |
| 120 | + end |
| 121 | + |
| 122 | + it 'returns limited postal codes when limit parameter is provided' do |
| 123 | + get '/v2/buscar?codigo_postal=123&limit=2' |
| 124 | + expect(last_response).to be_ok |
| 125 | + expect(last_response.headers['Content-Type']).to include('application/json') |
| 126 | + |
| 127 | + json_response = Oj.load(last_response.body) |
| 128 | + expect(json_response['codigos_postales'].length).to eq(2) |
| 129 | + expect(json_response['codigos_postales']).to include('12345', '12346') |
| 130 | + end |
| 131 | + |
| 132 | + it 'returns all postal codes when limit is not a positive number' do |
| 133 | + get '/v2/buscar?codigo_postal=123&limit=0' |
| 134 | + expect(last_response).to be_ok |
| 135 | + |
| 136 | + json_response = Oj.load(last_response.body) |
| 137 | + expect(json_response['codigos_postales'].length).to eq(4) |
| 138 | + expect(json_response['codigos_postales']).to match_array(%w[12345 12346 12347 12348]) |
119 | 139 | end |
120 | 140 | end |
121 | 141 |
|
|
138 | 158 | municipio: test_municipio, |
139 | 159 | colonia: 'Different Colonia') |
140 | 160 |
|
| 161 | + create(:postal_code, |
| 162 | + codigo_postal: '12347', |
| 163 | + estado: test_estado, |
| 164 | + municipio: test_municipio, |
| 165 | + colonia: 'Another Colonia') |
| 166 | + |
| 167 | + create(:postal_code, |
| 168 | + codigo_postal: '12348', |
| 169 | + estado: test_estado, |
| 170 | + municipio: test_municipio, |
| 171 | + colonia: 'Yet Another Colonia') |
| 172 | + |
141 | 173 | # Create a postal code with different estado |
142 | 174 | create(:postal_code, |
143 | 175 | codigo_postal: '54321', |
|
154 | 186 |
|
155 | 187 | json_response = Oj.load(last_response.body) |
156 | 188 | expect(json_response).to have_key('codigos_postales') |
157 | | - expect(json_response['codigos_postales']).to match_array(%w[12345 12346]) |
| 189 | + expect(json_response['codigos_postales']).to match_array(%w[12345 12346 12347 12348]) |
| 190 | + end |
| 191 | + |
| 192 | + it 'returns limited postal codes when limit parameter is provided' do |
| 193 | + get "/v2/buscar_por_ubicacion?estado=#{test_estado}&municipio=#{test_municipio}&limit=2" |
| 194 | + expect(last_response).to be_ok |
| 195 | + expect(last_response.headers['Content-Type']).to include('application/json') |
| 196 | + |
| 197 | + json_response = Oj.load(last_response.body) |
| 198 | + expect(json_response).to have_key('codigos_postales') |
| 199 | + expect(json_response['codigos_postales'].length).to eq(2) |
| 200 | + end |
| 201 | + |
| 202 | + it 'returns all postal codes when limit is not a positive number' do |
| 203 | + get "/v2/buscar_por_ubicacion?estado=#{test_estado}&municipio=#{test_municipio}&limit=0" |
| 204 | + expect(last_response).to be_ok |
| 205 | + |
| 206 | + json_response = Oj.load(last_response.body) |
| 207 | + expect(json_response).to have_key('codigos_postales') |
| 208 | + expect(json_response['codigos_postales'].length).to eq(4) |
| 209 | + expect(json_response['codigos_postales']).to match_array(%w[12345 12346 12347 12348]) |
158 | 210 | end |
159 | 211 | end |
160 | 212 |
|
|
167 | 219 | expect(json_response).to have_key('codigos_postales') |
168 | 220 | expect(json_response['codigos_postales']).to match_array(['12345']) |
169 | 221 | end |
| 222 | + |
| 223 | + it 'applies limit parameter correctly with colonia filter' do |
| 224 | + # Creating additional postal codes for the same colonia |
| 225 | + create(:postal_code, |
| 226 | + codigo_postal: '12349', |
| 227 | + estado: test_estado, |
| 228 | + municipio: test_municipio, |
| 229 | + colonia: test_colonia) |
| 230 | + create(:postal_code, |
| 231 | + codigo_postal: '12350', |
| 232 | + estado: test_estado, |
| 233 | + municipio: test_municipio, |
| 234 | + colonia: test_colonia) |
| 235 | + |
| 236 | + get "/v2/buscar_por_ubicacion?estado=#{test_estado}&municipio=#{test_municipio}&colonia=#{test_colonia}&limit=2" |
| 237 | + expect(last_response).to be_ok |
| 238 | + |
| 239 | + json_response = Oj.load(last_response.body) |
| 240 | + expect(json_response).to have_key('codigos_postales') |
| 241 | + expect(json_response['codigos_postales'].length).to eq(2) |
| 242 | + end |
170 | 243 | end |
171 | 244 |
|
172 | 245 | context 'without authentication' do |
|
0 commit comments