Skip to content

Commit

Permalink
Refactoring: Migrate ticket_article_store_empty to RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Lue authored and znuny-robo committed Feb 10, 2019
1 parent 63214c9 commit 107fd5e
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 442 deletions.
12 changes: 12 additions & 0 deletions spec/factories/activity_stream.rb
@@ -0,0 +1,12 @@
FactoryBot.define do
factory :activity_stream do
transient do
o { Ticket.first }
end

association :type, factory: :type_lookup
activity_stream_object_id { ObjectLookup.by_name(o.class.name) }
o_id { o.id }
created_by_id { 1 }
end
end
11 changes: 9 additions & 2 deletions spec/factories/history.rb
@@ -1,8 +1,15 @@
FactoryBot.define do FactoryBot.define do
factory :history do factory :history do
transient do
o { Ticket.first }
end

association :history_type, factory: :'history/type' association :history_type, factory: :'history/type'
association :history_object, factory: :'history/object' o_id { o.id }
o_id { history_object.name.constantize.pluck(:id).sample }
created_by_id { 1 } created_by_id { 1 }

history_object_id do
History::Object.lookup(name: o.class.name)&.id || create(:'history/object', name: o.class.name).id
end
end end
end end
14 changes: 14 additions & 0 deletions spec/factories/karma/activity_log.rb
@@ -0,0 +1,14 @@
FactoryBot.define do
factory :'karma/activity_log', aliases: %i[karma_activity_log] do
transient do
o { Ticket.first }
end

o_id { o.id }
object_lookup_id { ObjectLookup.by_name(o.class.name) }
user_id { 1 }
activity_id { Karma::Activity.pluck(:id).sample }
score { 100 }
score_total { 100 }
end
end
13 changes: 10 additions & 3 deletions spec/factories/link.rb
@@ -1,7 +1,14 @@
FactoryBot.define do FactoryBot.define do
factory :link do factory :link do
link_type_id { Link::Type.find_by(name: 'normal').id } transient do
link_object_source_id { Link::Object.find_by(name: 'Ticket').id } from { Ticket.first }
link_object_target_id { Link::Object.find_by(name: 'Ticket').id } to { Ticket.last }
end

link_type_id { Link::Type.find_by(name: 'normal').id }
link_object_source_id { Link::Object.find_by(name: 'Ticket').id }
link_object_target_id { Link::Object.find_by(name: 'Ticket').id }
link_object_source_value { from.id }
link_object_target_value { to.id }
end end
end end
22 changes: 13 additions & 9 deletions spec/factories/online_notification.rb
@@ -1,13 +1,17 @@
FactoryBot.define do FactoryBot.define do
factory :online_notification do factory :online_notification do
object_lookup_id { ObjectLookup.by_name('Ticket') } transient do
o_id 1 o { Ticket.first }
type_lookup_id { TypeLookup.by_name('updated') } end
seen false
user_id 1 object_lookup_id { ObjectLookup.by_name(o.class.name) }
created_by_id 1 o_id { o.id }
updated_by_id 1 type_lookup_id { TypeLookup.by_name('updated') }
created_at Time.zone.now seen { false }
updated_at Time.zone.now user_id { 1 }
created_by_id { 1 }
updated_by_id { 1 }
created_at { Time.zone.now }
updated_at { Time.zone.now }
end end
end end
17 changes: 3 additions & 14 deletions spec/factories/recent_view.rb
@@ -1,23 +1,12 @@
FactoryBot.define do FactoryBot.define do
factory :recent_view do factory :recent_view do
transient do transient do
type { :ticket } o { Ticket.first }
user_role { :agent } user_role { :agent }
end end


recent_view_object_id { ObjectLookup.by_name(type.to_s.camelcase) } recent_view_object_id { ObjectLookup.by_name(o.class.name) }

o_id { o.id }
# select a random record of the given object class
o_id do
random_function = case ActiveRecord::Base.connection_config[:adapter]
when 'mysql2'
'RAND'
when 'postgresql'
'RANDOM'
end

type.to_s.camelcase.constantize.order("#{random_function}()").first.id
end


# assign to an existing user, if possible # assign to an existing user, if possible
created_by_id do created_by_id do
Expand Down
14 changes: 11 additions & 3 deletions spec/factories/tag.rb
@@ -1,7 +1,15 @@
FactoryBot.define do FactoryBot.define do
factory :tag do factory :tag do
tag_object_id { Tag::Object.lookup_by_name_and_create('Ticket').id } transient do
tag_item_id { Tag::Item.lookup_by_name_and_create('blub').id } o { Ticket.first }
created_by_id 1 end

tag_item_id { Tag::Item.lookup_by_name_and_create('blub').id }
o_id { o.id }
created_by_id { 1 }

tag_object_id do
Tag::Object.lookup(name: o.class.name)&.id || create(:'tag/object', name: o.class.name).id
end
end end
end end
5 changes: 5 additions & 0 deletions spec/factories/tag/object.rb
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :'tag/object', aliases: %i[tag_object] do
name { (ApplicationModel.descendants.select(&:any?).map(&:name) - Tag::Object.pluck(:name)).sample }
end
end
26 changes: 13 additions & 13 deletions spec/factories/ticket/article.rb
@@ -1,25 +1,25 @@
FactoryBot.define do FactoryBot.define do
factory :'ticket/article', aliases: %i[ticket_article] do factory :'ticket/article', aliases: %i[ticket_article] do
transient do transient do
type_name 'email' type_name { 'email' }
sender_name 'Customer' sender_name { 'Customer' }
end end


ticket ticket
from 'factory-customer-1@example.com' from { 'factory-customer-1@example.com' }
to 'factory-customer-1@example.com' to { 'factory-customer-1@example.com' }
subject 'factory article' subject { 'factory article' }
message_id 'factory@id_com_1' message_id { 'factory@id_com_1' }
body 'some message 123' body { 'some message 123' }
internal false internal { false }
sender { Ticket::Article::Sender.find_by(name: sender_name) } sender { Ticket::Article::Sender.find_by(name: sender_name) }
type { Ticket::Article::Type.find_by(name: type_name) } type { Ticket::Article::Type.find_by(name: type_name) }
updated_by_id 1 updated_by_id { 1 }
created_by_id 1 created_by_id { 1 }


factory :twitter_article do factory :twitter_article do
transient do transient do
type_name 'twitter status' type_name { 'twitter status' }
end end


association :ticket, factory: :twitter_ticket association :ticket, factory: :twitter_ticket
Expand Down
17 changes: 17 additions & 0 deletions spec/factories/type_lookup.rb
@@ -0,0 +1,17 @@
FactoryBot.define do
factory :type_lookup do
name do
# The following line ensures that the name generated by Faker
# does not conflict with any existing names in the DB.
# There's a special syntax for this
# (Faker::Verb.unique.exclude(:past_participle, [], Ticket::StateType.pluck(:name)),
# but it's not available yet in the current release of Faker (1.9.1).
Faker::Verb.unique
.instance_variable_get(:@previous_results)
.dig([:base, []])
.merge(TypeLookup.pluck(:name))

Faker::Verb.unique.base
end
end
end

0 comments on commit 107fd5e

Please sign in to comment.