Optimize factory usage in deploy_token

What does this MR do and why?

This MR replaces create with build_stubbed in spec/models/deploy_token_spec.rb to optimize factory usage as recommended in our handbook. It was verified that database persistence is not needed using the factory doctor command FDOC=1 bin/rspec spec/models/deploy_token_spec.rb.

The association matchers now use their own build_stubbed subject, and the read-only examples for #scopes, #active?, #deactivated?, #username, #expires_at, #expires_at=, #has_access_to? (revoked, not related) and .impersonated? no longer persist a token.

Two flagged examples keep create:

  • .token: the token is only generated on save, so a stubbed record returns nil.
  • #has_access_to? "revoked and related to project": the context needs the token linked to project.

Factory Usage Optimization Results

Before optimization:

  • Factory time: 17.401s (16.86% of total time)

After optimization:

  • Factory time: 16.137s (35.61% of total time)

Improvement:

  • Factory time reduced by: 1.26s
  • Factory time percentage: not comparable, because total run time differed between the two runs
  • Overall factory usage improvement: 7.26%

Estimated CI Cost Savings

Using the same methodology as !248403 (merged): 1.26s saved per pipeline × $0.03/minute (large runners: 3x cost factor on $10/1,000 base minutes).

Scenario Pipelines/month Time saved/month Savings/year
Pessimistic 15,000 5.3h $113.76
Moderate 30,000 10.5h $227.52
Optimistic 50,000 17.6h $379.20

Factory Doctor Output

Total (potentially) bad examples: 19
Total wasted time: 00:05.708

DeployToken (./spec/models/deploy_token_spec.rb:5) (79 records created, 00:05.708)
  is expected to have many project_deploy_tokens (./spec/models/deploy_token_spec.rb:8) – 4 records created, 00:01.625
  is expected to have many projects through project_deploy_tokens (./spec/models/deploy_token_spec.rb:9) – 4 records created, 00:00.209
  is expected to have many group_deploy_tokens (./spec/models/deploy_token_spec.rb:10) – 4 records created, 00:00.178
  is expected to have many groups through group_deploy_tokens (./spec/models/deploy_token_spec.rb:11) – 4 records created, 00:00.164
  returns scopes assigned to DeployToken (./spec/models/deploy_token_spec.rb:170) – 4 records created, 00:00.210
  returns true (./spec/models/deploy_token_spec.rb:193) – 4 records created, 00:00.170
  returns true (./spec/models/deploy_token_spec.rb:208) – 4 records created, 00:00.460
  returns false (./spec/models/deploy_token_spec.rb:225) – 4 records created, 00:00.186
  returns false (./spec/models/deploy_token_spec.rb:241) – 4 records created, 00:00.168
  returns a default username if none is set (./spec/models/deploy_token_spec.rb:249) – 4 records created, 00:00.179
  returns the username provided if one is set (./spec/models/deploy_token_spec.rb:253) – 4 records created, 00:00.176
  is expected to be falsy (./spec/models/deploy_token_spec.rb:346) – 4 records created, 00:00.195
  is expected to be falsy (./spec/models/deploy_token_spec.rb:352) – 7 records created, 00:00.397
  returns nil (./spec/models/deploy_token_spec.rb:438) – 4 records created, 00:00.443
  returns the personalized date (./spec/models/deploy_token_spec.rb:447) – 4 records created, 00:00.193
  assigns Forever.date (./spec/models/deploy_token_spec.rb:457) – 4 records created, 00:00.182
  respects the value (./spec/models/deploy_token_spec.rb:466) – 4 records created, 00:00.178
  returns false (./spec/models/deploy_token_spec.rb:519) – 4 records created, 00:00.199
  is expected to match /gldt-[A-Za-z0-9_-]{20}/ (./spec/models/deploy_token_spec.rb:529) – 4 records created, 00:00.187

91 examples, 0 failures
[TEST PROF INFO] Time spent in factories: 00:17.323 (17.39% of total time)

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading
Loading