diff options
| author | Teddy Wing | 2018-04-06 16:37:33 +0200 |
|---|---|---|
| committer | Alban Peignier | 2018-04-16 09:09:20 +0200 |
| commit | b05676bdda09c10a7130e0f86527a75e6535dad8 (patch) | |
| tree | 928784b2203a5838cae56ddd7f10cd0422efcfc2 /spec/models/chouette | |
| parent | 2346cdedc9f75cc3af36f856d2b732209af8caab (diff) | |
| download | chouette-core-b05676bdda09c10a7130e0f86527a75e6535dad8.tar.bz2 | |
Route: Change `#calculate_costs!` to `after_commit` callback
Use an `after_commit` instead of an `after_save`. The `after_save` could
cause intermittent problems due to Sidekiq starting before the
record/transaction actually gets committed to the database. In those
cases, the record wouldn't be found and cause an error.
With an `after_commit` callback, the record is sure to be committed
before Sidekiq takes over.
Additionally, limit the callback to `:create` and `:update` because
otherwise it will be active on `:destroy` by default also. This work
isn't relevant on destroy, so don't do it then.
Update the tests:
* Update labels
* Use `build` instead of `create` because we're saving the record at the
end anyway so we don't need an object that starts out persisted
* Use `truncation: true` for these tests because otherwise the `commit`
callback doesn't get called due to transactional teardown
Refs #6407
Diffstat (limited to 'spec/models/chouette')
| -rw-r--r-- | spec/models/chouette/route/route_base_spec.rb | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb index 3d4a87791..43ff28c40 100644 --- a/spec/models/chouette/route/route_base_spec.rb +++ b/spec/models/chouette/route/route_base_spec.rb @@ -62,17 +62,17 @@ RSpec.describe Chouette::Route, :type => :model do end context "callbacks" do - it "calls #calculate_costs! after_save when TomTom is enabled" do + it "calls #calculate_costs! after_commit when TomTom is enabled", truncation: true do allow(TomTom).to receive(:enabled?).and_return(true) - route = create(:route) + route = build(:route) expect(route).to receive(:calculate_costs!) route.save end - it "doesn't call #calculate_costs! after_save if TomTom is disabled" do + it "doesn't call #calculate_costs! after_commit if TomTom is disabled", truncation: true do allow(TomTom).to receive(:enabled?).and_return(false) - route = create(:route) + route = build(:route) expect(route).not_to receive(:calculate_costs!) route.save |
