File: generated_attribute_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (266 lines) | stat: -rw-r--r-- 8,784 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
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
174
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# frozen_string_literal: true

require "generators/generators_test_helper"
require "rails/generators/generated_attribute"
require "rails/generators/base"

class GeneratedAttributeTest < Rails::Generators::TestCase
  include GeneratorsTestHelper

  def setup
    @old_belongs_to_required_by_default = Rails.application.config.active_record.belongs_to_required_by_default
    Rails.application.config.active_record.belongs_to_required_by_default = true
    ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
  end

  def teardown
    Rails.application.config.active_record.belongs_to_required_by_default = @old_belongs_to_required_by_default
  end

  def test_field_name_with_dangerous_attribute_raises_error
    e = assert_raise Rails::Generators::Error do
      create_generated_attribute :string, :save
    end
    message = "Could not generate field 'save', as it is already defined by Active Record."
    assert_match message, e.message
  end

  def test_field_type_returns_number_field
    assert_field_type :integer, :number_field
  end

  def test_field_type_returns_text_field
    %w(float decimal string).each do |attribute_type|
      assert_field_type attribute_type, :text_field
    end
  end

  def test_field_type_returns_datetime_select
    %w(datetime timestamp).each do |attribute_type|
      assert_field_type attribute_type, :datetime_field
    end
  end

  def test_field_type_returns_time_select
    assert_field_type :time, :time_field
  end

  def test_field_type_returns_date_select
    assert_field_type :date, :date_field
  end

  def test_field_type_returns_text_area
    assert_field_type :text, :text_area
  end

  def test_field_type_returns_check_box
    assert_field_type :boolean, :check_box
  end

  def test_field_type_returns_rich_text_area
    assert_field_type :rich_text, :rich_text_area
  end

  def test_field_type_returns_file_field
    %w(attachment attachments).each do |attribute_type|
      assert_field_type attribute_type, :file_field
    end
  end

  def test_field_type_with_unknown_type_raises_error
    field_type = :unknown
    e = assert_raise Rails::Generators::Error do
      create_generated_attribute field_type
    end
    message = "Could not generate field 'test' with unknown type 'unknown'"
    assert_match message, e.message
  end

  def test_field_type_with_unknown_index_type_raises_error
    index_type = :unknown
    e = assert_raise Rails::Generators::Error do
      create_generated_attribute "string", "name", index_type
    end
    message = "Could not generate field 'name' with unknown index 'unknown'"
    assert_match message, e.message
  end

  def test_default_value_is_integer
    assert_field_default_value :integer, 1
  end

  def test_default_value_is_float
    assert_field_default_value :float, 1.5
  end

  def test_default_value_is_decimal
    assert_field_default_value :decimal, "9.99"
  end

  def test_default_value_is_datetime
    %w(datetime timestamp time).each do |attribute_type|
      assert_field_default_value attribute_type, Time.now.to_fs(:db)
    end
  end

  def test_default_value_is_date
    assert_field_default_value :date, Date.today.to_fs(:db)
  end

  def test_default_value_is_string
    assert_field_default_value :string, "MyString"
  end

  def test_default_value_for_type
    att = Rails::Generators::GeneratedAttribute.parse("type:string")
    assert_equal("", att.default)
  end

  def test_default_value_is_text
    assert_field_default_value :text, "MyText"
  end

  def test_default_value_is_boolean
    assert_field_default_value :boolean, false
  end

  def test_default_value_is_nil
    %w(references belongs_to rich_text attachment attachments).each do |attribute_type|
      assert_field_default_value attribute_type, nil
    end
  end

  def test_default_value_is_empty_string
    %w(digest token).each do |attribute_type|
      assert_field_default_value attribute_type, ""
    end
  end

  def test_human_name
    assert_equal(
      "Full name",
      create_generated_attribute(:string, "full_name").human_name
    )
  end

  def test_size_option_can_be_passed_to_string_text_and_binary
    %w(text binary).each do |attribute_type|
      generated_attribute = create_generated_attribute("#{attribute_type}{medium}")
      assert_equal :medium, generated_attribute.attr_options[:size]
    end
  end

  def test_size_option_raises_exception_when_passed_to_invalid_type
    %w(integer string).each do |attribute_type|
      e = assert_raise Rails::Generators::Error do
        create_generated_attribute("#{attribute_type}{medium}")
      end
      message = "Could not generate field 'test' with unknown type '#{attribute_type}{medium}'"
      assert_match message, e.message
    end
  end

  def test_limit_option_can_be_passed_to_string_text_integer_and_binary
    %w(string text binary integer).each do |attribute_type|
      generated_attribute = create_generated_attribute("#{attribute_type}{65535}")
      assert_equal 65535, generated_attribute.attr_options[:limit]
    end
  end

  def test_reference_is_true
    %w(references belongs_to).each do |attribute_type|
      assert_predicate create_generated_attribute(attribute_type), :reference?
    end
  end

  def test_reference_is_false
    %w(string text float).each do |attribute_type|
      assert_not_predicate create_generated_attribute(attribute_type), :reference?
    end
  end

  def test_polymorphic_reference_is_true
    %w(references belongs_to).each do |attribute_type|
      assert_predicate create_generated_attribute("#{attribute_type}{polymorphic}"), :polymorphic?
    end
  end

  def test_polymorphic_reference_is_false
    %w(references belongs_to).each do |attribute_type|
      assert_not_predicate create_generated_attribute(attribute_type), :polymorphic?
    end
  end

  def test_blank_type_defaults_to_string
    assert_equal :string, create_generated_attribute(nil, "title").type
    assert_equal :string, create_generated_attribute("", "title").type
  end

  def test_handles_index_names_for_references
    assert_equal "post", create_generated_attribute("string", "post").index_name
    assert_equal "post_id", create_generated_attribute("references", "post").index_name
    assert_equal "post_id", create_generated_attribute("belongs_to", "post").index_name
    assert_equal ["post_id", "post_type"], create_generated_attribute("references{polymorphic}", "post").index_name
  end

  def test_handles_column_names_for_references
    assert_equal "post", create_generated_attribute("string", "post").column_name
    assert_equal "post_id", create_generated_attribute("references", "post").column_name
    assert_equal "post_id", create_generated_attribute("belongs_to", "post").column_name
  end

  def test_parse_works_with_adapter_specific_types
    att = Rails::Generators::GeneratedAttribute.parse("document:json")
    assert_equal "document", att.name
    assert_equal :json, att.type
  end

  def test_parse_required_attribute_with_index
    att = Rails::Generators::GeneratedAttribute.parse("supplier:references:index")
    assert_equal "supplier", att.name
    assert_equal :references, att.type
    assert_predicate att, :has_index?
    assert_predicate att, :required?
  end

  def test_parse_required_attribute_with_index_false_when_belongs_to_required_by_default_global_config_is_false
    Rails.application.config.active_record.belongs_to_required_by_default = false
    att = Rails::Generators::GeneratedAttribute.parse("supplier:references:index")
    assert_not_predicate att, :required?
  end

  def test_generated_attribute_to_s
    att = Rails::Generators::GeneratedAttribute.parse("name")
    assert_equal "name:string", att.to_s
  end

  def test_generated_attribute_to_s_with_index
    att = Rails::Generators::GeneratedAttribute.parse("name:index")
    assert_equal "name:string:index", att.to_s
  end

  def test_generated_attribute_to_s_with_uniq_index
    att = Rails::Generators::GeneratedAttribute.parse("name:uniq")
    assert_equal "name:string:uniq", att.to_s
  end

  def test_generated_attribute_to_s_with_limit
    att = Rails::Generators::GeneratedAttribute.parse("name:text{140}")
    assert_equal "name:text{140}", att.to_s
  end

  def test_generated_attribute_to_s_with_size
    att = Rails::Generators::GeneratedAttribute.parse("name:text{medium}")
    assert_equal "name:text{medium}", att.to_s
  end

  def test_generated_attribute_to_s_with_precision_and_scale
    att = Rails::Generators::GeneratedAttribute.parse("name:decimal{1,2}")
    assert_equal "name:decimal{1,2}", att.to_s
  end

  def test_generated_attribute_to_s_with_polymorphic
    att = Rails::Generators::GeneratedAttribute.parse("name:references{polymorphic}")
    assert_equal "name:references{polymorphic}", att.to_s
  end
end