Class: Wrappix::Templates::Tests

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappix/templates/tests.rb

Class Method Summary collapse

Class Method Details

.client_tests(module_name, resource_names) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wrappix/templates/tests.rb', line 104

def self.client_tests(module_name, resource_names)
  resource_assertions = resource_names.map do |name|
    "    assert_respond_to client, :#{name}"
  end.join("\n")

  <<~RUBY
    def test_client_initialization
      client = #{module_name}::Client.new

      assert_instance_of #{module_name}::Client, client
    #{resource_assertions}
    end
  RUBY
end

.config_tests(module_name, config) ⇒ Object



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
# File 'lib/wrappix/templates/tests.rb', line 46

def self.config_tests(module_name, config)
  auth_config = case config["auth_type"]
                when "oauth"
                  <<~RUBY.strip
                    config.client_id = "test_client_id"
                    config.client_secret = "test_client_secret"
                    config.token_url = "#{config["token_url"] || "https://api.example.com/oauth/token"}"
                  RUBY
                when "basic"
                  <<~RUBY.strip
                    config.username = "test_username"
                    config.password = "test_password"
                  RUBY
                when "api_key"
                  <<~RUBY.strip
                    config.api_key = "test_api_key"
                    config.api_key_header = "#{config["api_key_header"] || "X-Api-Key"}"
                  RUBY
                else
                  ""
                end

  <<~RUBY
    def test_configuration
      #{module_name}.configure do |config|
        config.base_url = "https://api.test.org"
        config.timeout = 60
        #{auth_config}
      end

      assert_equal "https://api.test.org", #{module_name}.configuration.base_url
      assert_equal 60, #{module_name}.configuration.timeout
      #{verify_auth_config(module_name, config)}
    end
  RUBY
end

.create_default_test_case(endpoint) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/wrappix/templates/tests.rb', line 147

def self.create_default_test_case(endpoint)
  name = endpoint["name"]
  path = endpoint["path"] || name
  is_collection = endpoint["collection"] || %w[all list index search].include?(name)

  # Obtener los parámetros del path
  path_params = path.scan(/\{([^}]+)\}/).flatten
  path_params_values = path_params.map { |p| [p, "123"] }.to_h

  # Crear un caso de prueba predeterminado
  {
    "description" => "funciona correctamente",
    "request" => {
      "path_params" => path_params_values,
      "params" => endpoint["params"] ? { "page" => 1 } : {},
      "body" => %w[post put patch].include?(endpoint["method"].to_s) ? { "name" => "Test" } : {}
    },
    "response" => {
      "status" => 200,
      "body" => if is_collection
                  { "data" => [{ "id" => 1, "name" => "Test" }], "next_href" => "/next" }
                else
                  { "id" => 1, "name" => "Test" }
                end
    }
  }
end

.endpoint_test(module_name, resource_name, endpoint, resource_config, global_config) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wrappix/templates/tests.rb', line 132

def self.endpoint_test(module_name, resource_name, endpoint, resource_config, global_config)
  name = endpoint["name"]
  method = endpoint["method"] || "get"
  path = endpoint["path"] || name

  # Obtener los casos de prueba definidos o crear uno predeterminado
  test_cases = endpoint["tests"] || [create_default_test_case(endpoint)]

  # Generar un test para cada caso
  test_cases.map.with_index do |test_case, index|
    generate_test_method(module_name, resource_name, name, method, path, test_case, index, resource_config,
                         global_config)
  end.join("\n\n")
end

.error_tests(module_name) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/wrappix/templates/tests.rb', line 342

def self.error_tests(module_name)
  <<~RUBY
    def test_error_handling
      error = #{module_name}::Error.new("Test error", {error: "details"}, 400)

      assert_equal "Test error", error.message
      assert_equal({error: "details"}, error.body)
      assert_equal 400, error.status
    end

    def test_http_error_response
      @stubs.get("error_test") do
        [404, {'Content-Type' => 'application/json'}, {error: "Resource not found"}.to_json]
      end

      error = assert_raises(#{module_name}::Error) do
        #{module_name}::Request.new("error_test").get
      end

      assert_equal 404, error.status
      assert_includes error.message, "Resource not found"
    end
  RUBY
end

.generate_item_assertions(item) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/wrappix/templates/tests.rb', line 330

def self.generate_item_assertions(item)
  return "# No item data to assert" if !item || !item.is_a?(Hash) || item.empty?

  item.map do |key, value|
    if value.is_a?(Hash) || value.is_a?(Array)
      "assert_not_nil result.#{key}"
    else
      "assert_equal #{value.inspect}, result.#{key}"
    end
  end.join("\n      ")
end

.generate_response_assertions(module_name, endpoint_name, response, response_format) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/wrappix/templates/tests.rb', line 274

def self.generate_response_assertions(module_name, endpoint_name, response, response_format)
  status = response["status"] || 200

  if status >= 400
    "  end  # assert_raises"
  else
    body = response["body"] || {}

    # Verificar si el cuerpo es un hash antes de intentar usar key?
    if body.is_a?(String)
      # Si el cuerpo es una cadena (como en el caso de las imágenes binarias)
      <<~RUBY
        assert_instance_of #{module_name}::Object, result
        # El resultado es probablemente una URL o datos binarios
      RUBY
    else
      is_collection = %w[all list index search query].include?(endpoint_name) ||
                      (body.is_a?(Hash) && (body.key?("data") || body.key?(:data) ||
                       (response_format["collection_root"] &&
                        (body.key?(response_format["collection_root"]) ||
                         body.key?(response_format["collection_root"].to_sym)))))

      if is_collection
        collection_root = response_format["collection_root"] || "data"
        items = body[collection_root] || body[collection_root.to_sym] || []

        if items.empty?
          <<~RUBY
            assert_instance_of #{module_name}::Collection, result
            assert_empty result.data
          RUBY
        else
          <<~RUBY
            assert_instance_of #{module_name}::Collection, result
            assert_equal #{items.size}, result.data.size

            # Verify first item properties
            if result.data.any?
              first_item = result.data.first
              #{generate_item_assertions(items.first)}
            end
          RUBY
        end
      else
        item_root = response_format["item_root"]
        item = item_root ? (body[item_root] || body[item_root.to_sym] || {}) : body

        <<~RUBY
          assert_instance_of #{module_name}::Object, result
          #{generate_item_assertions(item)}
        RUBY
      end
    end
  end
end

.generate_test_method(module_name, resource_name, endpoint_name, method, path, test_case, index, resource_config, global_config) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wrappix/templates/tests.rb', line 175

def self.generate_test_method(module_name, resource_name, endpoint_name, method, path, test_case, index, resource_config, global_config)
  description = test_case["description"] || "case #{index + 1}"
  # Normalizar la descripción para el nombre del método
  method_name_desc = description.downcase.gsub(/[^a-z0-9]/, "_")

  request = test_case["request"] || {}
  response = test_case["response"] || {}

  # Preparar los parámetros del path
  path_params = request["path_params"] || {}
  path_params.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")

  # Preparar los parámetros de la consulta y el cuerpo
  query_params = request["params"] || {}
  body = request["body"] || {}

  # Construir la URL con los parámetros del path reemplazados
  test_url = path.dup
  path_params.each do |key, value|
    test_url.gsub!(/\{#{key}\}/, value.to_s)
  end

  # Si la URL comienza con http:// o https://, extraer solo el path
  if test_url.start_with?("http://", "https://")
    # Eliminar el protocolo y el dominio para quedarnos solo con el path
    test_url = test_url.sub(%r{^https?://[^/]+}, "")
    test_url = test_url[1..] if test_url.start_with?("/") # Quitar el slash inicial
  end

  # Construir los argumentos de la llamada
  call_args = []
  call_args.concat(path_params.values.map(&:inspect))
  call_args << query_params.inspect if query_params.any?
  call_args << body.inspect if body.any?

  # Generar el código para verificar la respuesta
  response_format = resource_config["response_format"] || global_config["response_format"] || {}
  response_assertions = generate_response_assertions(module_name, endpoint_name, response, response_format)

  <<~RUBY
    def test_#{resource_name}_#{endpoint_name}_#{method_name_desc}
      # Stub the HTTP request
      status = #{response["status"] || 200}
      response_body = #{response["body"].inspect}

      @stubs.#{method}("#{test_url}") do |env|
        # Verificar que los parámetros de consulta coincidan
        if env.body.is_a?(String) && !env.body.empty?
          request_body = JSON.parse(env.body)
          # Aquí podrías agregar verificaciones de body si es necesario
        end

        [status, {'Content-Type' => 'application/json'}, response_body.is_a?(String) ? response_body : response_body.to_json]
      end

      client = #{module_name}::Client.new

      #{handle_error_case(response)}
      result = client.#{resource_name}.#{endpoint_name}(#{call_args.join(", ")})

      #{response_assertions}
    end
  RUBY
end

.handle_error_case(response) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/wrappix/templates/tests.rb', line 262

def self.handle_error_case(response)
  status = response["status"] || 200

  if status >= 400
    <<~RUBY
      assert_raises(#{module_name}::Error) do
    RUBY
  else
    ""
  end
end

.render(_api_name, module_name, config) ⇒ Object



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
# File 'lib/wrappix/templates/tests.rb', line 6

def self.render(_api_name, module_name, config)
  resources = config["resources"] || {}

  test_content = <<~RUBY
    # frozen_string_literal: true

    require "test_helper"

    class #{module_name}Test < Minitest::Test
      def setup
        # Reset configuration before each test
        #{module_name}.configuration = #{module_name}::Configuration.new

        # Configure base settings
        #{module_name}.configure do |config|
          config.base_url = "#{config["base_url"] || "https://api.example.com"}"
        end
      end

      def teardown
        # No need to restore anything
      end

      #{config_tests(module_name, config)}

      #{client_tests(module_name, resources.keys)}

      #{error_tests(module_name)}
    end
  RUBY

  # Generar tests de recursos en archivos separados
  resources.each do |resource_name, resource_config|
    resource_test(module_name, resource_name, resource_config, config)
    # Aquí podrías devolver o guardar estos tests en archivos separados
  end

  test_content
end

.resource_test(module_name, resource_name, resource_config, global_config) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wrappix/templates/tests.rb', line 119

def self.resource_test(module_name, resource_name, resource_config, global_config)
  endpoints = resource_config["endpoints"] || []

  endpoint_tests = endpoints.map do |endpoint|
    endpoint_test(module_name, resource_name, endpoint, resource_config, global_config)
  end.join("\n\n")

  <<~RUBY
    # Tests for #{resource_name} resource
    #{endpoint_tests}
  RUBY
end

.verify_auth_config(module_name, config) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/wrappix/templates/tests.rb', line 83

def self.verify_auth_config(module_name, config)
  case config["auth_type"]
  when "oauth"
    <<~RUBY
      assert_equal "test_client_id", #{module_name}.configuration.client_id
      assert_equal "test_client_secret", #{module_name}.configuration.client_secret
    RUBY
  when "basic"
    <<~RUBY
      assert_equal "test_username", #{module_name}.configuration.username
      assert_equal "test_password", #{module_name}.configuration.password
    RUBY
  when "api_key"
    <<~RUBY
      assert_equal "test_api_key", #{module_name}.configuration.api_key
    RUBY
  else
    ""
  end
end

.verify_request_env(query_params, body, method) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/wrappix/templates/tests.rb', line 240

def self.verify_request_env(query_params, body, method)
  checks = []

  if query_params.any?
    params_checks = query_params.map do |key, value|
      "assert_equal #{value.inspect}, Rack::Utils.parse_nested_query(env.url.query)[#{key.to_s.inspect}]"
    end
    checks.concat(params_checks)
  end

  if %w[post put patch].include?(method.to_s) && body.any?
    checks << "request_body = JSON.parse(env.body)"

    body_checks = body.map do |key, value|
      "assert_equal #{value.inspect}, request_body[#{key.to_s.inspect}]"
    end
    checks.concat(body_checks)
  end

  checks.join("\n              ")
end