File: factory_prof_spec.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (157 lines) | stat: -rw-r--r-- 4,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true

# Init FactoryProf and patch TestProf::FactoryBot, Fabrication
TestProf::FactoryProf.init
TestProf::FactoryProf.configure do |config|
  # turn on stacks collection
  config.mode = :flamegraph
  config.include_variations = false
end

describe TestProf::FactoryProf, :transactional do
  before { described_class.start }
  after { described_class.stop }

  after { TestProf::FactoryProf.config.include_variations = false }

  # Ensure meta-queries have been performed
  before(:all) { User.first }

  def without_time(xs)
    xs.map do |x|
      expect(x.delete(:total_time)).to be_a(Float)
      expect(x.delete(:top_level_time)).to be_a(Float)
      x[:variations] = without_time(x[:variations]) unless x[:variations].nil?
      x
    end
  end

  describe "#print" do
    let(:started_at) { Time.now }

    subject(:print) { described_class.print(started_at) }

    it "calls the default printer" do
      expect(TestProf::FactoryProf::Printers::Simple).to receive(:dump)

      print
    end

    context "when the printer is customized" do
      let(:custom_printer) { double(dump: lambda { |result, start_time: nil| }) }

      before do
        described_class.configure do |config|
          config.printer = custom_printer
        end
      end

      it "calls the customer printer" do
        expect(custom_printer).to receive(:dump)
        print
      end
    end
  end

  describe "#result" do
    subject(:result) { described_class.result }

    context "when factory_bot used" do
      it "has no stacks when no data created" do
        TestProf::FactoryBot.build_stubbed(:user)
        User.first
        expect(result.stacks.size).to eq 0
      end

      it "contains simple stack" do
        TestProf::FactoryBot.create(:user)
        expect(result.stacks.size).to eq 1
        expect(result.total_count).to eq 1
        expect(result.stacks.first).to eq([:user])
      end

      it "handles associations" do
        TestProf::FactoryBot.create(:post)

        expect(result.stacks).to contain_exactly(
          %i[post user]
        )
        expect(without_time(result.stats)).to eq(
          [
            {name: :post, total_count: 1, top_level_count: 1, variations: []},
            {name: :user, total_count: 1, top_level_count: 0, variations: []}
          ]
        )
      end

      it "contains many stacks with variations" do
        TestProf::FactoryProf.config.include_variations = true

        TestProf::FactoryBot.create_pair(:user)
        TestProf::FactoryBot.create(:post)
        TestProf::FactoryBot.create(:user, :with_posts)

        expect(result.stacks.size).to eq 4
        expect(result.total_count).to eq 9
        expect(result.stacks).to contain_exactly(
          [:user],
          [:user],
          %i[post user],
          %i[user post user post user]
        )
        expect(without_time(result.stats)).to eq(
          [
            {name: :user, total_count: 6, top_level_count: 3, variations: [
              {name: "-", top_level_count: 2, total_count: 5},
              {name: :".with_posts", top_level_count: 1, total_count: 1}
            ]},
            {name: :post, total_count: 3, top_level_count: 1, variations: [
              {name: "-", top_level_count: 1, total_count: 3}
            ]}
          ]
        )
      end
    end

    context "when fabrication used" do
      it "has no stacks when no data created" do
        Fabricate.build(:user)
        User.first
        expect(result.stacks.size).to eq 0
      end

      it "contains simple stack" do
        Fabricate.create(:user)
        expect(result.stacks.size).to eq 1
        expect(result.total_count).to eq 1
        expect(result.stacks.first).to eq([:user])
      end

      it "contains many stacks with variations" do
        TestProf::FactoryProf.config.include_variations = true

        Fabricate.times(2, :user)
        Fabricate.create(:post, text: "some text")
        Fabricate.create(:user) { Fabricate.times(2, :post) }

        expect(result.stacks.size).to eq 4
        expect(result.total_count).to eq 9
        expect(result.stacks).to contain_exactly(
          [:user],
          [:user],
          %i[post user],
          %i[user post user post user]
        )
        expect(without_time(result.stats)).to eq(
          [
            {name: :user, total_count: 6, top_level_count: 3, variations: [{name: "-", top_level_count: 3, total_count: 6}]},
            {name: :post, total_count: 3, top_level_count: 1, variations: [
              {name: "-", top_level_count: 0, total_count: 2},
              {name: :"[text]", top_level_count: 1, total_count: 1}
            ]}
          ]
        )
      end
    end
  end
end