diff options
Diffstat (limited to 'test/models')
| -rw-r--r-- | test/models/comment_test.rb | 18 | ||||
| -rw-r--r-- | test/models/search_test.rb | 90 | ||||
| -rw-r--r-- | test/models/tag_test.rb | 16 | ||||
| -rw-r--r-- | test/models/thing_test.rb | 95 | ||||
| -rw-r--r-- | test/models/user_test.rb | 28 |
5 files changed, 247 insertions, 0 deletions
diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb new file mode 100644 index 0000000..087963a --- /dev/null +++ b/test/models/comment_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'test_helper' + +class CommentTest < ActiveSupport::TestCase + test 'content is present' do + comment = Comment.new(thing_id: things(:thing1).id) + assert_raise(ActiveRecord::RecordInvalid) { comment.save! } + + comment.content = 'whatever' + assert_difference('Comment.count') { comment.save! } + end + + test 'has many tags through tag_references' do + comment = comments(:comment1) + assert comment.tags.size == 1 + end +end diff --git a/test/models/search_test.rb b/test/models/search_test.rb new file mode 100644 index 0000000..9614bc0 --- /dev/null +++ b/test/models/search_test.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'test_helper' + +class SearchTest < ActiveSupport::TestCase + test 'name and body have to be present and unique' do + search = Search.new(user_id: users(:user).id) + assert_raise(ActiveRecord::RecordInvalid) { search.save! } + + search.name = 'new search' + assert_raise(ActiveRecord::RecordInvalid) { search.save! } + + search.name = nil + search.body = 'body' + assert_raise(ActiveRecord::RecordInvalid) { search.save! } + + search.name = searches(:search1).name + assert_raise(ActiveRecord::RecordInvalid) { search.save! } + + search.name = 'another' + search.body = searches(:search1).body + assert_raise(ActiveRecord::RecordInvalid) { search.save! } + + search.body = 'another body' + assert_difference('Search.count') { search.save! } + end + + ## + # Results. + + test 'returns all things when an empty body is given' do + res = Search.new.results + + assert res[:things].size == 2 + assert res[:things][0][:target] == things(:thing2).target + assert res[:things][1][:target] == things(:thing1).target + end + + test 'returns everything matching a specific tag' do + res = Search.new(body: "tag:'#{tags(:tag1).name}'").results + + assert res[:things].size == 2 + assert res[:things][0][:target] == things(:thing2).target + assert res[:things][1][:target] == things(:thing1).target + assert res[:comments].size == 1 + assert res[:comments][0][:id] == comments(:comment1).id + end + + test 'returns everything matching two tags' do + res = Search.new(body: "tag:'#{tags(:tag1).name}' tag:'#{tags(:tag2).name}'").results + + assert res[:things].size == 1 + assert res[:things][0][:target] == things(:thing1).target + assert res[:comments].empty? + end + + test 'returns everything matching a given text' do + res = Search.new(body: 'Miquel').results + + assert res[:things].size == 2 + assert res[:things][0][:target] == things(:thing2).target + assert res[:things][1][:target] == things(:thing1).target + assert res[:comments].size == 1 + assert res[:comments][0][:id] == comments(:comment1).id + end + + test 'returns everything matching two tags and a given text' do + res = Search.new(body: "Miquel tag:'#{tags(:tag1).name}' tag:'#{tags(:tag2).name}'").results + + assert res[:things].size == 1 + assert res[:things][0][:target] == things(:thing1).target + assert res[:comments].empty? + end + + test 'returns everything matching a given compound text' do + res = Search.new(body: 'some other').results + + assert res[:things].size == 2 + assert res[:things][0][:target] == things(:thing2).target + assert res[:things][1][:target] == things(:thing1).target + assert res[:comments].empty? + end + + test 'returns an empty result with unknown fields' do + res = Search.new(body: 'whatever:"unknown"').results + + assert res[:things].empty? + assert res[:comments].empty? + end +end diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..dafb8e4 --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'test_helper' + +class TagTest < ActiveSupport::TestCase + test 'name is present and unique' do + tag = Tag.new + assert_raise(ActiveRecord::RecordInvalid) { tag.save! } + + tag = Tag.new(name: tags(:tag1).name) + assert_raise(ActiveRecord::RecordInvalid) { tag.save! } + + tag.name = 'another' + assert_difference('Tag.count') { tag.save! } + end +end diff --git a/test/models/thing_test.rb b/test/models/thing_test.rb new file mode 100644 index 0000000..0b3961e --- /dev/null +++ b/test/models/thing_test.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ThingTest < ActiveSupport::TestCase + test 'validates presence' do + assert_raise(ActiveRecord::RecordInvalid) { Thing.new.save! } + + # Missing 'title' + assert_raise(ActiveRecord::RecordInvalid) do + Thing.new(target: 'target', authors: 'Author', + user_id: users(:user).id, rate: 5, + status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save! + end + + # Missing 'target' + assert_raise(ActiveRecord::RecordInvalid) do + Thing.new(title: 'title', authors: 'Author', + user_id: users(:user).id, rate: 5, + status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save! + end + + # Missing 'authors' + assert_raise(ActiveRecord::RecordInvalid) do + Thing.new(title: 'title', target: 'target', + user_id: users(:user).id, rate: 5, + status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save! + end + + # Valid! + assert_difference('Thing.count') do + Thing.new(title: 'title', target: 'target', authors: 'Author', + user_id: users(:user).id, rate: 5, + status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save! + end + end + + test "'rate' has to be between 0 and 10" do + thing = things(:thing1) + + thing.rate = -1 + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.rate = 11 + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.rate = 5 + thing.save! + end + + test "'status' has to have a value as defined by its enum" do + thing = things(:thing1) + + thing.status = 'whatever' + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.status = Thing.statuses[:tobepublished] + thing.save! + end + + test "'kind' has to have a value as defined by its enum" do + thing = things(:thing1) + + thing.kind = 'whatever' + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.kind = Thing.kinds[:other] + thing.save! + end + + test 'title and target must be unique' do + thing = things(:thing1).dup + + thing.title = 'another' + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.target = 'also another' + thing.title = things(:thing1).title + assert_raise(ActiveRecord::RecordInvalid) { thing.save! } + + thing.target = 'also another' + thing.title = 'now for real' + assert_difference('Thing.count') { thing.save! } + end + + test 'has many comments' do + thing = things(:thing1) + assert thing.comments.size == 1 + end + + test 'has many tags through tag_references' do + thing = things(:thing1) + assert thing.tags.size == 2 + end +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..6ff5e19 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + test 'has to provide username and password' do + user = User.new + assert_raise(ActiveRecord::RecordInvalid) { user.save! } + + user.username = 'whatever' + assert_raise(ActiveRecord::RecordInvalid) { user.save! } + + user.password = '1234' + user.password_confirmation = '12345' + assert_raise(ActiveRecord::RecordInvalid) { user.save! } + + user.password_confirmation = '1234' + assert_difference('User.count') do + user.save! + end + end + + test 'username has to be unique' do + user = User.new(username: users(:user).username, + password: '1234', password_confirmation: '1234') + assert_raise(ActiveRecord::RecordInvalid) { user.save! } + end +end |
