Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserving a predictable output #76

Open
blocknotes opened this issue Oct 13, 2022 · 1 comment
Open

Preserving a predictable output #76

blocknotes opened this issue Oct 13, 2022 · 1 comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@blocknotes
Copy link
Contributor

blocknotes commented Oct 13, 2022

Hey :)
Using this gem we noticed that having predictable data in different run can be useful if you are going to regenerate the OpenAPI documentation (fully or partially).

To achieve this goal we are using this configuration block:

if ENV['OPENAPI'].present?
  seed = ENV.fetch('OPENAPI_SEED', 0xFFFF)
  srand(seed)
  Faker::Config.random = Random.new(seed) if defined? Faker

  RSpec.configure do |config|
    config.order = :defined

    config.before do
      allow(Devise).to receive(:friendly_token) { ('A'..'Z').to_a.sample(20).join } if defined? Devise

      ActiveRecord::Base.connection.tables.each do |t|
        # to preserve IDs
        ActiveRecord::Base.connection.reset_pk_sequence!(t)
      end
    end

    config.include ActiveSupport::Testing::TimeHelpers

    config.around do |example|
      time = Time.zone.local(2022, 10, 15, 12, 34, 56)
      travel_to(time) # to preserve dates & times
      example.run
      travel_back
    end
  end
end

Perhaps these kind of options shouldn't be included in the gem (they are configuration) but they could be useful in the README to help others.

WDYT?

@exoego
Copy link
Owner

exoego commented Oct 28, 2022

@blocknotes
Sorry for late response.
Adding example like this sounds great to me.
PR welcome.

@exoego exoego added documentation Improvements or additions to documentation good first issue Good for newcomers labels Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants