DEV: Prefabrication (test optimization) (#7414)

* Introduced fab!, a helper that creates database state for a group

It's almost identical to let_it_be, except:

 1. It creates a new object for each test by default,
 2. You can disable it using PREFABRICATION=0
This commit is contained in:
Daniel Waterworth
2019-05-07 03:12:20 +00:00
committed by Sam
parent 329969ea20
commit e219588142
258 changed files with 1402 additions and 1364 deletions

View File

@ -11,11 +11,11 @@ describe TopicQuery do
# we should remove the let! here and use freeze time to communicate how the clock moves
let!(:user) { Fabricate(:coding_horror) }
let(:creator) { Fabricate(:user) }
fab!(:creator) { Fabricate(:user) }
let(:topic_query) { TopicQuery.new(user) }
let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
fab!(:moderator) { Fabricate(:moderator) }
fab!(:admin) { Fabricate(:admin) }
context 'secure category' do
it "filters categories out correctly" do
@ -165,20 +165,20 @@ describe TopicQuery do
end
context 'tag filter' do
let(:tag) { Fabricate(:tag) }
let(:other_tag) { Fabricate(:tag) }
let(:uppercase_tag) { Fabricate(:tag, name: "HeLlO") }
fab!(:tag) { Fabricate(:tag) }
fab!(:other_tag) { Fabricate(:tag) }
fab!(:uppercase_tag) { Fabricate(:tag, name: "HeLlO") }
before do
SiteSetting.tagging_enabled = true
end
context "no category filter" do
let!(:tagged_topic1) { Fabricate(:topic, tags: [tag]) }
let!(:tagged_topic2) { Fabricate(:topic, tags: [other_tag]) }
let!(:tagged_topic3) { Fabricate(:topic, tags: [tag, other_tag]) }
let!(:tagged_topic4) { Fabricate(:topic, tags: [uppercase_tag]) }
let!(:no_tags_topic) { Fabricate(:topic) }
fab!(:tagged_topic1) { Fabricate(:topic, tags: [tag]) }
fab!(:tagged_topic2) { Fabricate(:topic, tags: [other_tag]) }
fab!(:tagged_topic3) { Fabricate(:topic, tags: [tag, other_tag]) }
fab!(:tagged_topic4) { Fabricate(:topic, tags: [uppercase_tag]) }
fab!(:no_tags_topic) { Fabricate(:topic) }
it "returns topics with the tag when filtered to it" do
expect(TopicQuery.new(moderator, tags: tag.name).list_latest.topics)
@ -269,7 +269,7 @@ describe TopicQuery do
end
context 'a bunch of topics' do
let!(:regular_topic) do
fab!(:regular_topic) do
Fabricate(:topic, title: 'this is a regular topic',
user: creator,
views: 100,
@ -278,7 +278,7 @@ describe TopicQuery do
participant_count: 11,
bumped_at: 15.minutes.ago)
end
let!(:pinned_topic) do
fab!(:pinned_topic) do
Fabricate(:topic, title: 'this is a pinned topic',
user: creator,
views: 10,
@ -289,7 +289,7 @@ describe TopicQuery do
pinned_globally: true,
bumped_at: 10.minutes.ago)
end
let!(:archived_topic) do
fab!(:archived_topic) do
Fabricate(:topic, title: 'this is an archived topic',
user: creator,
views: 50,
@ -299,7 +299,7 @@ describe TopicQuery do
participant_count: 1,
bumped_at: 6.minutes.ago)
end
let!(:invisible_topic) do
fab!(:invisible_topic) do
Fabricate(:topic, title: 'this is an invisible topic',
user: creator,
views: 1,
@ -309,7 +309,7 @@ describe TopicQuery do
participant_count: 3,
bumped_at: 5.minutes.ago)
end
let!(:closed_topic) do
fab!(:closed_topic) do
Fabricate(:topic, title: 'this is a closed topic',
user: creator,
views: 2,
@ -319,7 +319,7 @@ describe TopicQuery do
participant_count: 2,
bumped_at: 1.minute.ago)
end
let!(:future_topic) do
fab!(:future_topic) do
Fabricate(:topic, title: 'this is a topic in far future',
user: creator,
views: 30,
@ -417,13 +417,13 @@ describe TopicQuery do
end
context 'categorized' do
let(:category) { Fabricate(:category) }
fab!(:category) { Fabricate(:category) }
let(:topic_category) { category.topic }
let!(:topic_no_cat) { Fabricate(:topic) }
let!(:topic_in_cat1) { Fabricate(:topic, category: category,
fab!(:topic_no_cat) { Fabricate(:topic) }
fab!(:topic_in_cat1) { Fabricate(:topic, category: category,
bumped_at: 10.minutes.ago,
created_at: 10.minutes.ago) }
let!(:topic_in_cat2) { Fabricate(:topic, category: category) }
fab!(:topic_in_cat2) { Fabricate(:topic, category: category) }
describe '#list_new_in_category' do
it 'returns the topic category and the categorized topic' do
@ -486,8 +486,8 @@ describe TopicQuery do
end
context 'with read data' do
let!(:partially_read) { Fabricate(:post, user: creator).topic }
let!(:fully_read) { Fabricate(:post, user: creator).topic }
fab!(:partially_read) { Fabricate(:post, user: creator).topic }
fab!(:fully_read) { Fabricate(:post, user: creator).topic }
before do
TopicUser.update_last_read(user, partially_read.id, 0, 0, 0)
@ -918,7 +918,7 @@ describe TopicQuery do
end
describe '#list_group_topics' do
let(:group) { Fabricate(:group) }
fab!(:group) { Fabricate(:group) }
let(:user) do
user = Fabricate(:user)
@ -932,9 +932,9 @@ describe TopicQuery do
user
end
let(:user3) { Fabricate(:user) }
fab!(:user3) { Fabricate(:user) }
let(:private_category) do
fab!(:private_category) do
Fabricate(:private_category, group: group)
end
@ -970,7 +970,7 @@ describe TopicQuery do
end
describe '#list_private_messages_group' do
let(:group) { Fabricate(:group) }
fab!(:group) { Fabricate(:group) }
let!(:group_message) do
Fabricate(:private_message_topic,
@ -1011,13 +1011,13 @@ describe TopicQuery do
end
context "shared drafts" do
let(:category) { Fabricate(:category) }
let(:shared_drafts_category) { Fabricate(:category) }
let!(:topic) { Fabricate(:topic, category: shared_drafts_category) }
let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }
let(:admin) { Fabricate(:admin) }
let(:user) { Fabricate(:user) }
let(:group) { Fabricate(:group) }
fab!(:category) { Fabricate(:category) }
fab!(:shared_drafts_category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: shared_drafts_category) }
fab!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) }
fab!(:group) { Fabricate(:group) }
before do
shared_drafts_category.set_permissions(group => :full)