Class: Syntropy::Test

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/syntropy/test.rb

Overview

Test provides a class for testing a Syntropy app, based on Minitest.

Constant Summary collapse

HTTP =
Syntropy::HTTP

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.envObject

Gets/sets app environment for tests



16
17
18
# File 'lib/syntropy/test.rb', line 16

def env
  @env
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



19
20
21
# File 'lib/syntropy/test.rb', line 19

def app
  @app
end

#machineObject (readonly)

Returns the value of attribute machine.



19
20
21
# File 'lib/syntropy/test.rb', line 19

def machine
  @machine
end

Instance Method Details

#envHash

Returns the test environment.

Returns:

  • (Hash)

    test app environment



24
25
26
# File 'lib/syntropy/test.rb', line 24

def env
  self.class.env
end

#get(path, **headers) ⇒ Syntropy::Request

Makes an HTTP GET request to the test app.

Parameters:

  • path (String)

    request path

  • headers (Hash)

    request headers

Returns:



50
51
52
53
54
55
56
57
# File 'lib/syntropy/test.rb', line 50

def get(path, **headers)
  http_request(
    headers.merge(
      ':method' => 'GET',
      ':path'   => path
    )
  )
end

#http_request(headers, body = nil) ⇒ Syntropy::Request

Makes an HTTP request to the test app.

Parameters:

  • headers (Hash)

    request headers

  • body (String, nil) (defaults to: nil)

    request body

Returns:



41
42
43
# File 'lib/syntropy/test.rb', line 41

def http_request(headers, body = nil)
  @test_harness.request(headers, body)
end

#load_module(ref, raise_on_missing: true) ⇒ any

Loads and returns a module with the given reference.

Parameters:

  • ref (String)

    module reference

Returns:

  • (any)

    module



32
33
34
# File 'lib/syntropy/test.rb', line 32

def load_module(ref, raise_on_missing: true)
  app.module_loader.load(ref, raise_on_missing:)
end

#patch(path, content_type, body, **headers) ⇒ Syntropy::Request

Makes an HTTP PATCH request to the test app.

Parameters:

  • path (String)

    request path

  • content_type (String, nil)

    content MIME type

  • body (String)

    request body

  • headers (Hash)

    request headers

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/syntropy/test.rb', line 108

def patch(path, content_type, body, **headers)
  headers = headers.merge('content-type' => content_type) if content_type
  http_request(
    headers.merge(
      {
        ':method' => 'PATCH',
        ':path'   => path
      }
    ),
    body
  )
end

#post(path, content_type, body, **headers) ⇒ Syntropy::Request

Makes an HTTP POST request to the test app.

Parameters:

  • path (String)

    request path

  • content_type (String, nil)

    content MIME type

  • body (String)

    request body

  • headers (Hash)

    request headers

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/syntropy/test.rb', line 66

def post(path, content_type, body, **headers)
  headers = headers.merge('content-type' => content_type) if content_type
  http_request(
    headers.merge(
      {
        ':method' => 'POST',
        ':path'   => path
      }
    ),
    body
  )
end

#post_form(path, data) ⇒ Syntropy::Request

Makes an HTTP POST request to the test app with a “application/x-www-form-urlencoded” content type. The given data is converted to URL Encoded form format and sent as the request body.

Parameters:

  • path (String)

    request path

  • data (Hash)

    form data

Returns:



97
98
99
# File 'lib/syntropy/test.rb', line 97

def post_form(path, data, **)
  post(path, 'application/x-www-form-urlencoded', URI.encode_www_form(data), **)
end

#post_json(path, data) ⇒ Syntropy::Request

Makes an HTTP POST request to the test app with a “application/json” content type. The given object is converted to JSON and sent as the request body.

Parameters:

  • path (String)

    request path

  • data (any)

    data

Returns:



86
87
88
# File 'lib/syntropy/test.rb', line 86

def post_json(path, data, **)
  post(path, 'application/json', JSON.dump(data), **)
end

#setupvoid

This method returns an undefined value.

Sets up a test instance.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/syntropy/test.rb', line 124

def setup
  env = self.class.env
  raise 'Environment not set' if !env

  Syntropy.load_config(env)

  @machine = UM.new
  @app = Syntropy::App.new(
    **env.merge(
      machine: @machine,
      test_mode: true
    )
  )
  @test_harness = Syntropy::TestHarness.new(@app)

  @db = load_module('/_lib/storage', raise_on_missing: false)
  @db&.migrate! if @db.respond_to?(:migrate!)
end

#teardownvoid

This method returns an undefined value.

Cleans up a test instance.



146
147
148
149
150
# File 'lib/syntropy/test.rb', line 146

def teardown
  @machine = nil
  @app = nil
  @test_harness = nil
end