Skip to content

Commit 6583cac

Browse files
committed
improve model validations
1 parent 24cb731 commit 6583cac

File tree

7 files changed

+370
-54
lines changed

7 files changed

+370
-54
lines changed

app/models/post.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Post < ApplicationRecord
2-
validates :title, presence: true
3-
validates :description, presence: true
2+
validates :title, presence: true, length: { minimum: 3, maximum: 255 }
3+
validates :description, presence: true, length: { minimum: 10, maximum: 10000 }
44
end

test/models/post_test.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@ class PostTest < ActiveSupport::TestCase
66
assert_not post.save, "Saved the post without a title and description"
77
end
88

9-
test "should save post with title and description" do
10-
post = Post.new(title: "Sample Title", description: "Sample Description")
11-
assert post.save, "Could not save the post with a title and description"
9+
test "should not save post with too short title" do
10+
post = Post.new(title: "Hi", description: "This is a valid description with enough length")
11+
assert_not post.save, "Saved the post with a too short title"
12+
end
13+
14+
test "should not save post with too long title" do
15+
post = Post.new(title: "a" * 256, description: "This is a valid description with enough length")
16+
assert_not post.save, "Saved the post with a too long title"
17+
end
18+
19+
test "should not save post with too short description" do
20+
post = Post.new(title: "Valid Title", description: "Too short")
21+
assert_not post.save, "Saved the post with a too short description"
22+
end
23+
24+
test "should not save post with too long description" do
25+
post = Post.new(title: "Valid Title", description: "a" * 10_001)
26+
assert_not post.save, "Saved the post with a too long description"
27+
end
28+
29+
test "should save post with valid title and description" do
30+
post = Post.new(
31+
title: "Sample Title",
32+
description: "This is a valid description with enough characters."
33+
)
34+
assert post.save, "Could not save the post with valid title and description"
1235
end
1336
end

test/requests/posts_controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
class PostsControllerTest < ActionDispatch::IntegrationTest
44
test "should get index and return posts ordered by created_at" do
55
# Arrange: Cria posts com datas diferentes
6-
post1 = Post.create!(title: "Primeiro Post", description: "Primeiro", created_at: 2.days.ago)
7-
post2 = Post.create!(title: "Segundo Post", description: "Segundo", created_at: 1.day.ago)
8-
post3 = Post.create!(title: "Terceiro Post", description: "Terceiro", created_at: Time.current)
6+
post1 = Post.create!(title: "Primeiro Post", description: "Primeiro desc", created_at: 2.days.ago)
7+
post2 = Post.create!(title: "Segundo Post", description: "Segundo desc", created_at: 1.day.ago)
8+
post3 = Post.create!(title: "Terceiro Post", description: "Terceiro desc", created_at: Time.current)
99

1010
# Act: Faz a requisição
1111
get posts_path

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ENV["RAILS_ENV"] ||= "test"
22
require_relative "../config/environment"
3+
require "minitest/mock"
34
require "rails/test_help"
45

56
module ActiveSupport

test/tools/posts/create_tool_test.rb

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ class Posts::CreateToolTest < ActiveSupport::TestCase
44
test "should create post with valid attributes" do
55
assert_difference "Post.count", 1 do
66
response = Posts::CreateTool.call(
7-
title: "New Post",
8-
description: "New Description",
7+
title: "New Post Title",
8+
description: "This is a valid description with enough characters",
99
server_context: {}
1010
)
1111

1212
assert_includes response.content.first[:text], "Post criado com sucesso"
1313
end
1414

1515
post = Post.last
16-
assert_equal "New Post", post.title
17-
assert_equal "New Description", post.description
16+
assert_equal "New Post Title", post.title
17+
assert_equal "This is a valid description with enough characters", post.description
1818
end
1919

2020
test "should return post id and title on success" do
2121
response = Posts::CreateTool.call(
22-
title: "Test Post",
23-
description: "Test Description",
22+
title: "Test Post Title",
23+
description: "Test Description with minimum length",
2424
server_context: {}
2525
)
2626

2727
text = response.content.first[:text]
2828
post = Post.last
2929

3030
assert_includes text, "ID: #{post.id}"
31-
assert_includes text, "Título: Test Post"
31+
assert_includes text, "Título: Test Post Title"
3232
end
3333

3434
test "should not create post without title" do
3535
assert_no_difference "Post.count" do
3636
response = Posts::CreateTool.call(
3737
title: "",
38-
description: "Description",
38+
description: "Valid description with enough characters",
3939
server_context: {}
4040
)
4141

@@ -47,7 +47,7 @@ class Posts::CreateToolTest < ActiveSupport::TestCase
4747
test "should not create post without description" do
4848
assert_no_difference "Post.count" do
4949
response = Posts::CreateTool.call(
50-
title: "Title",
50+
title: "Valid Title",
5151
description: "",
5252
server_context: {}
5353
)
@@ -56,4 +56,129 @@ class Posts::CreateToolTest < ActiveSupport::TestCase
5656
assert_includes response.content.first[:text].downcase, "description"
5757
end
5858
end
59+
60+
test "should not create post with title too short" do
61+
assert_no_difference "Post.count" do
62+
response = Posts::CreateTool.call(
63+
title: "Ab",
64+
description: "Valid description with enough characters",
65+
server_context: {}
66+
)
67+
68+
assert_includes response.content.first[:text], "Erro ao criar post"
69+
assert_includes response.content.first[:text].downcase, "title"
70+
end
71+
end
72+
73+
test "should not create post with title too long" do
74+
long_title = "a" * 256
75+
76+
assert_no_difference "Post.count" do
77+
response = Posts::CreateTool.call(
78+
title: long_title,
79+
description: "Valid description with enough characters",
80+
server_context: {}
81+
)
82+
83+
assert_includes response.content.first[:text], "Erro ao criar post"
84+
assert_includes response.content.first[:text].downcase, "title"
85+
end
86+
end
87+
88+
test "should not create post with description too short" do
89+
assert_no_difference "Post.count" do
90+
response = Posts::CreateTool.call(
91+
title: "Valid Title",
92+
description: "Short",
93+
server_context: {}
94+
)
95+
96+
assert_includes response.content.first[:text], "Erro ao criar post"
97+
assert_includes response.content.first[:text].downcase, "description"
98+
end
99+
end
100+
101+
test "should not create post with description too long" do
102+
long_description = "a" * 10001
103+
104+
assert_no_difference "Post.count" do
105+
response = Posts::CreateTool.call(
106+
title: "Valid Title",
107+
description: long_description,
108+
server_context: {}
109+
)
110+
111+
assert_includes response.content.first[:text], "Erro ao criar post"
112+
assert_includes response.content.first[:text].downcase, "description"
113+
end
114+
end
115+
116+
test "should create post with minimum valid length" do
117+
assert_difference "Post.count", 1 do
118+
response = Posts::CreateTool.call(
119+
title: "ABC",
120+
description: "1234567890",
121+
server_context: {}
122+
)
123+
124+
assert_includes response.content.first[:text], "Post criado com sucesso"
125+
end
126+
end
127+
128+
test "should create post with maximum valid length" do
129+
max_title = "a" * 255
130+
max_description = "a" * 10000
131+
132+
assert_difference "Post.count", 1 do
133+
response = Posts::CreateTool.call(
134+
title: max_title,
135+
description: max_description,
136+
server_context: {}
137+
)
138+
139+
assert_includes response.content.first[:text], "Post criado com sucesso"
140+
end
141+
142+
post = Post.last
143+
assert_equal 255, post.title.length
144+
assert_equal 10000, post.description.length
145+
end
146+
147+
test "should handle exception during post creation" do
148+
Post.stub :new, ->(*_args, **_kwargs) { raise StandardError.new("Unexpected error") } do
149+
response = Posts::CreateTool.call(
150+
title: "Valid Title",
151+
description: "Valid description with enough characters",
152+
server_context: {}
153+
)
154+
155+
assert_includes response.content.first[:text], "Erro ao criar post"
156+
assert_includes response.content.first[:text], "Unexpected error"
157+
end
158+
end
159+
160+
test "should handle unexpected errors gracefully" do
161+
Post.stub :new, ->(*) { raise StandardError.new("Unexpected error") } do
162+
response = Posts::CreateTool.call(
163+
title: "Valid Title",
164+
description: "Valid description with enough characters",
165+
server_context: {}
166+
)
167+
168+
assert_includes response.content.first[:text], "Erro ao criar post"
169+
assert_includes response.content.first[:text], "Unexpected error"
170+
end
171+
end
172+
173+
test "should trim whitespace from title and description" do
174+
Posts::CreateTool.call(
175+
title: " Valid Title ",
176+
description: " Valid description with enough characters ",
177+
server_context: {}
178+
)
179+
180+
post = Post.last
181+
assert_equal " Valid Title ", post.title
182+
assert_equal " Valid description with enough characters ", post.description
183+
end
59184
end

test/tools/posts/index_tool_test.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
100100
end
101101

102102
test "should apply offset for pagination" do
103-
5.times.map { |i| Post.create!(title: "Post #{i}", description: "Desc #{i}") }
103+
5.times.map { |i| Post.create!(title: "Post #{i} 123", description: "Desc #{i} teste") }
104104

105105
response = Posts::IndexTool.call(offset: 2, limit: 2, server_context: {})
106106

@@ -124,7 +124,7 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
124124
end
125125

126126
test "should use default limit of 20" do
127-
25.times { |i| Post.create!(title: "Post #{i}", description: "Desc #{i}") }
127+
25.times { |i| Post.create!(title: "Post #{i} 123", description: "Desc #{i} teste") }
128128

129129
response = Posts::IndexTool.call(server_context: {})
130130

@@ -151,7 +151,7 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
151151
end
152152

153153
test "should handle search_term with special characters" do
154-
Post.create!(title: "C++ Programming", description: "Learn C++")
154+
Post.create!(title: "C++ Programming", description: "Desc Learn C++")
155155

156156
response = Posts::IndexTool.call(search_term: "C++", server_context: {})
157157

@@ -162,8 +162,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
162162
# Testes de Ordenação
163163

164164
test "should sort by created_at ascending" do
165-
Post.create!(title: "Old Post", description: "Old", created_at: 2.days.ago)
166-
Post.create!(title: "New Post", description: "New", created_at: 1.day.ago)
165+
Post.create!(title: "Old Post", description: "Desc Post Old", created_at: 2.days.ago)
166+
Post.create!(title: "New Post", description: "Desc Post New", created_at: 1.day.ago)
167167

168168
response = Posts::IndexTool.call(sort_by: "created_at", sort_order: "asc", server_context: {})
169169

@@ -175,8 +175,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
175175
end
176176

177177
test "should sort by created_at descending" do
178-
Post.create!(title: "Old Post", description: "Old", created_at: 2.days.ago)
179-
Post.create!(title: "New Post", description: "New", created_at: 1.day.ago)
178+
Post.create!(title: "Old Post", description: "Desc Post Old", created_at: 2.days.ago)
179+
Post.create!(title: "New Post", description: "Desc Post New", created_at: 1.day.ago)
180180

181181
response = Posts::IndexTool.call(sort_by: "created_at", sort_order: "desc", server_context: {})
182182

@@ -188,8 +188,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
188188
end
189189

190190
test "should sort by updated_at ascending" do
191-
Post.create!(title: "First", description: "Desc", updated_at: 2.days.ago)
192-
Post.create!(title: "Second", description: "Desc", updated_at: 1.day.ago)
191+
Post.create!(title: "First Post", description: "Desc Post 123", updated_at: 2.days.ago)
192+
Post.create!(title: "Second Post", description: "Desc Post 123", updated_at: 1.day.ago)
193193

194194
response = Posts::IndexTool.call(sort_by: "updated_at", sort_order: "asc", server_context: {})
195195

@@ -201,8 +201,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
201201
end
202202

203203
test "should sort by updated_at descending" do
204-
Post.create!(title: "First", description: "Desc", updated_at: 2.days.ago)
205-
Post.create!(title: "Second", description: "Desc", updated_at: 1.day.ago)
204+
Post.create!(title: "First", description: "Desc first", updated_at: 2.days.ago)
205+
Post.create!(title: "Second", description: "Desc second", updated_at: 1.day.ago)
206206

207207
response = Posts::IndexTool.call(sort_by: "updated_at", sort_order: "desc", server_context: {})
208208

@@ -246,9 +246,9 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
246246
end
247247

248248
test "should handle case-insensitive title sorting" do
249-
Post.create!(title: "zebra", description: "Lowercase")
250-
Post.create!(title: "Apple", description: "Capitalized")
251-
Post.create!(title: "MONKEY", description: "Uppercase")
249+
Post.create!(title: "zebra", description: "Lowercase 132")
250+
Post.create!(title: "Apple", description: "Capitalized 123")
251+
Post.create!(title: "MONKEY", description: "Uppercase 123")
252252

253253
response = Posts::IndexTool.call(sort_by: "title", sort_order: "asc", server_context: {})
254254

@@ -262,8 +262,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
262262
end
263263

264264
test "should use default sort_by when not specified" do
265-
Post.create!(title: "Old", description: "Desc", created_at: 2.days.ago)
266-
Post.create!(title: "New", description: "Desc", created_at: 1.day.ago)
265+
Post.create!(title: "Old", description: "Desc 12345", created_at: 2.days.ago)
266+
Post.create!(title: "New", description: "Desc 12345", created_at: 1.day.ago)
267267

268268
response = Posts::IndexTool.call(server_context: {})
269269

@@ -275,8 +275,8 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
275275
end
276276

277277
test "should use default sort_order when not specified" do
278-
Post.create!(title: "First", description: "Desc", created_at: 2.days.ago)
279-
Post.create!(title: "Second", description: "Desc", created_at: 1.day.ago)
278+
Post.create!(title: "First", description: "Desc 12345", created_at: 2.days.ago)
279+
Post.create!(title: "Second", description: "Desc 12345", created_at: 1.day.ago)
280280

281281
response = Posts::IndexTool.call(sort_by: "created_at", server_context: {})
282282

@@ -333,9 +333,9 @@ class Posts::IndexToolTest < ActiveSupport::TestCase
333333

334334
test "should handle sorting with identical timestamps" do
335335
timestamp = 1.day.ago
336-
Post.create!(title: "Post A", description: "Desc A", created_at: timestamp)
337-
Post.create!(title: "Post B", description: "Desc B", created_at: timestamp)
338-
Post.create!(title: "Post C", description: "Desc C", created_at: timestamp)
336+
Post.create!(title: "Post A", description: "Desc A 123", created_at: timestamp)
337+
Post.create!(title: "Post B", description: "Desc B 123", created_at: timestamp)
338+
Post.create!(title: "Post C", description: "Desc C 123", created_at: timestamp)
339339

340340
response = Posts::IndexTool.call(sort_by: "created_at", sort_order: "desc", server_context: {})
341341

0 commit comments

Comments
 (0)