Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Make tests run faster

Make tests run faster

【オフライン開催】Omotesando.rb #88
https://omotesandorb.connpass.com/event/290594/

Takumi Shotoku

August 04, 2023
Tweet

More Decks by Takumi Shotoku

Other Decks in Technology

Transcript

  1. Make tests run faster
    Omotesando.rb #88 2023/08/03
    1

    View full-size slide

  2. ࣗݾ঺հ
    • ໊લ: ਖ਼ಙ ޼(aka: ਆ଎)
    • ձࣾ: -
    • GitHub: @sinsoku (ը૾ӈ্)
    • Twitter: @sinsoku_listy (ը૾ӈԼ)
    • ޷͖ͳݴޠ: Rust
    • Railsྺ: 8೥͘Β͍
    2

    View full-size slide

  3. ͓લʹ଍Γͳ͍΋ͷ͸ɺͦΕ͸――
    ৘೤ɺࢥ૝ɺཧ೦ɺ಄೴ɺؾ඼ɺ༏խ͞ɺۈษ
    ͞!
    ͦͯ͠ԿΑΓ΋ ―― ଎͕͞଍Γͳ͍!!
    -- ετϨΠτɾΫʔΨʔʢεΫϥΠυʣ
    3

    View full-size slide

  4. ςετͷ࣮ߦ࣌ؒΛ଎͍ͨ͘͠
    4

    View full-size slide

  5. ௚ۙͰ΍ͬͨ͜ͱͷ঺հ1
    1. Database Cleaner -> DatabaseRewinder
    2. γʔυʹґଘ͢Δςετͷमਖ਼
    1 ࠓճ଎ͨ͘͠ͷ͸ʮશςετͷ࣮ߦ࣌ؒʯͰ͸ͳ͘ʮϑΝΠϧΛࢦఆͨ͠ςετͷ࣮ߦ࣌ؒʯʹͳΔɻ
    5

    View full-size slide

  6. มߋ͸Ϋϥε໊ͷมߋ͚ͩ
    SeedFu.seed(SeedFu.fixture_paths, /#{filenames.join('|')}/)
    - DatabaseCleaner.clean_with :truncation, {
    - except: %w[
    - foo
    - bar
    (ུʣ
    + DatabaseRewinder.clean_with :truncation, except: %w[
    + foo
    + bar
    7

    View full-size slide

  7. DatabaseRewinder ΁ͷҠߦ݁Ռ
    SQLΛ൐Θͳ͍ςετͰൺֱ͢Δͱ໿1.5ඵΛߴ଎ԽͰ͖ͨɻ
    $ rspec spec/lib/no_sql_spec.rb
    3.38 -> 1.88 seconds
    8

    View full-size slide

  8. ʢຊ୊ʣγʔυʹґଘ͢Δςετ
    9

    View full-size slide

  9. RSpecͰγʔυΛಡΈࠐΊΔ
    ςετ؀ڥͰ΋ຊ൪؀ڥͱಉ͡γʔυσʔλΛ࢖͑ͯศརʂ
    # spec/rails_helper.rb
    RSpec.configure do |config|
    config.before(:suite) do
    SeedFu.quiet = true
    SeedFu.seed
    end
    end
    10

    View full-size slide

  10. γʔυґଘͷ໰୊
    • ৗʹશͯͷγʔυΛಡΈࠐΜͰ͠·͏
    • σʔλྔ͕ଟ͍ͱ஗͍
    !
    • ςετίʔυ͕ಡΈਏ͍
    • γʔυલఏͰ Prefecture.first ͱॻ͘ͱத਎͕෼͔Βͳ͍
    • ςετͱγʔυͷ྆ํΛಡΉඞཁ͕͋Δ
    11

    View full-size slide

  11. γʔυґଘͷमਖ਼
    γʔυΛಡΈࠐ·ͣɺ֤ςετͰඞཁͳϨίʔυ͚ͩΛ࡞੒͢
    Δɻ
    ͜Εʹؔ࿈ͯ͠ɺҎԼͷରԠ΋ඞཁʹͳΔɻ
    1. FactoryBot Ͱ find_or_create Λ࣮ݱ͢Δ
    2. ओΩʔʢidʣʹґଘ͢ΔॲཧΛ࡟আ͢Δ
    12

    View full-size slide

  12. FactoryBotͰfind_or_create Λ࣮ݱ2
    # factory :foo do
    # extend FactoryBotSingleFactory
    # end
    module FactoryBotSingleFactory
    def self.extended(obj)
    obj.to_create do |instance|
    attributes = instance.class.find_or_create_by(instance.attributes.compact).attributes
    instance.attributes = attributes.except('id')
    instance.id = attributes['id'] # id can't be mass-assigned
    instance.instance_variable_set('@new_record', false) # marks record as persisted
    end
    end
    end
    2 https://gist.github.com/ReganRyanNZ/71e4128d34459d5b00dcb27d2ba1757e Λࢀߟʹͨ͠ɻ
    13

    View full-size slide

  13. ओΩʔͷґଘΛ࡟আ 1
    id Λݻఆʹ͢Δͷ͸೉͍ͨ͠ΊɺओΩʔʹґଘ͠ͳ͍ܗʹ௚͢ɻ
    class Prefecture
    def tokyo?
    - id == 13
    + name == '౦ژ'
    end
    end
    14

    View full-size slide

  14. ओΩʔͷґଘΛ࡟আ 2
    ΧϥϜΛ௥Ճͯ͠ɺϝιουΛফ͢ํ๏΋͋Δɻ
    class AddTokyoToPrefecture < ActiveRecord::Migration[7.0]
    def change
    add_column :prefectures, :tokyo, :boolean
    up_only do
    execute 'UPDATE prefectures SET tokyo = 1 WHERE id == 13;'
    end
    change_column_null :prefectures, :tokyo, false
    end
    end
    15

    View full-size slide

  15. γʔυґଘͷςετΛ௚͢
    ͍͖ͳΓ find_by ͯ͠Δίʔυྫɻ
    - prefecture = Prefecture.find_by(name: '౦ژ౎')
    + prefecture = create(:prefecture, name: '౦ژ౎')
    ݻఆ஋Λ౉ͯ͠Δίʔυྫɻ
    let(:query) do
    {
    - prefecture_ids: [1],
    + prefecture_ids: [create(:prefecture, :hokkaido).id],
    16

    View full-size slide

  16. ൒෼͘Β͍γʔυґଘΛमਖ਼ͨ݁͠Ռ3
    DatabaseRewinder Ͱ࢖ͬͨςετͰվΊͯௐ΂·ͨ͠ɻ
    $ rspec spec/lib/no_sql_spec.rb
    1.88 -> 1.04 seconds
    3 ͪͳΈʹ SeedFu.seed Λফͨ͠ͱ͖͸ 0.23ඵ Ͱ͢ɻ
    17

    View full-size slide

  17. ·ͱΊ

    !
    DatabaseRewinder ͸଎͍

    "
    ςετͰγʔυΛ࢖͏ͷ͸ආ͚Δ

    #
    ओΩʔʢidʣͷґଘ͸ආ͚Δ
    ͦͯ͠ɺςετ͔ΒγʔυͷґଘΛফ͢ઓ͍͸ଓ͘...
    18

    View full-size slide