docbrown/spec/models/audience_segment_spec.rb
Joshua Wehner dc1ab81401
Allow DisplayAd to target users via audience segmentation (#19284)
* Trying some models for audience segmentation

* AudienceSegment basics

* Attach AudienceSegment to DisplayAd

* Possibly use a DUS to populate AudienceSegments

* Add to display ad form UI

* Add to display ad API

* Refresh strategy for audience segments

* Add user_id to async ads query

* Maybe :testing -> :manual, for no-refresh segment

* Test & tweak segment refresh

* Testing audience_segment#refresh logic

* Coverage: testing human_readable

* Scope segment refresh to recently active users

* Tweak logic for when to refresh

* Tweak experience levels to match SettingsHelper

* Test for front-end logic

* Fix test, hope this helps coverage?

* Better test names

* One worker for all, many workers for each, perform_bulk

* Fix audience segment UI, needs to use id, not enum

* cron/schedule should RefreshAll

* Singular id in RefreshWorker
2023-04-25 08:41:10 -04:00

45 lines
1.3 KiB
Ruby

require "rails_helper"
RSpec.describe AudienceSegment do
subject(:audience_segment) { build(:audience_segment) }
it { is_expected.to define_enum_for(:type_of) }
it { is_expected.to be_valid }
context "when persisting" do
let(:active_users) { class_double(User) }
before do
audience_segment.save!
allow(User).to receive(:recently_active).and_return(active_users)
allow(active_users).to receive(:where).and_return([])
end
describe "refresh!" do
it "does not refresh when manual" do
expect(audience_segment).to be_manual
audience_segment.refresh!
expect(active_users).not_to have_received(:where)
end
it "queries User to refresh segments" do
audience_segment.type_of = "no_posts_yet"
audience_segment.refresh!
expect(active_users).to have_received(:where).with(articles_count: 0)
end
end
describe "save" do
it "does not query when manual" do
audience_segment.save!
expect(active_users).not_to have_received(:where)
end
it "queries all Users (instead of active_users)" do
audience_segment.type_of = "no_posts_yet"
audience_segment.save!
expect(active_users).to have_received(:where).with(articles_count: 0)
end
end
end
end