59 lines
1.9 KiB
Ruby
59 lines
1.9 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'test_helper'
|
||
|
require_relative 'metadata_test'
|
||
|
|
||
|
class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase
|
||
|
include MetadataTest
|
||
|
|
||
|
test 'se pueden relacionar artículos' do
|
||
|
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
|
||
|
post = @site.posts.create(layout: :post, title: SecureRandom.hex)
|
||
|
|
||
|
post.authors.value = [author.uuid.value]
|
||
|
assert post.save
|
||
|
|
||
|
assert_includes author.posts.has_many, post
|
||
|
assert_includes post.authors.has_many, author
|
||
|
end
|
||
|
|
||
|
test 'se puede eliminar la relación' do
|
||
|
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
|
||
|
post = @site.posts.create(layout: :post, title: SecureRandom.hex, authors: [author.uuid.value])
|
||
|
|
||
|
assert_includes post.authors.value, author.uuid.value
|
||
|
assert_includes author.posts.value, post.uuid.value
|
||
|
|
||
|
post.authors.value = []
|
||
|
ENV['HOLA'] = 'hola'
|
||
|
assert post.save
|
||
|
|
||
|
assert_not_includes author.posts.has_many, post
|
||
|
assert_not_includes post.authors.has_many, author
|
||
|
|
||
|
assert_includes author.posts.had_many, post
|
||
|
assert_includes post.authors.had_many, author
|
||
|
end
|
||
|
|
||
|
test 'se puede cambiar la relación' do
|
||
|
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
|
||
|
post1 = @site.posts.create(layout: :post, title: SecureRandom.hex, authors: [author.uuid.value])
|
||
|
post2 = @site.posts.create(layout: :post, title: SecureRandom.hex)
|
||
|
|
||
|
author.posts.value = [post2.uuid.value]
|
||
|
assert author.save
|
||
|
|
||
|
assert_not_includes author.posts.has_many, post1
|
||
|
assert_not_includes post1.authors.has_many, author
|
||
|
|
||
|
assert_includes author.posts.had_many, post1
|
||
|
assert_includes post1.authors.had_many, author
|
||
|
|
||
|
assert_not_includes author.posts.had_many, post2
|
||
|
assert_not_includes post2.authors.had_many, author
|
||
|
|
||
|
assert_includes author.posts.has_many, post2
|
||
|
assert_includes post2.authors.has_many, author
|
||
|
end
|
||
|
end
|