spec/models/notification_spec.rb in activity_notification-1.7.1 vs spec/models/notification_spec.rb in activity_notification-2.0.0

- old
+ new

@@ -1,15 +1,32 @@ +# To run as single test for debugging +# require Rails.root.join('../../spec/concerns/apis/notification_api_spec.rb').to_s +# require Rails.root.join('../../spec/concerns/renderable_spec.rb').to_s + describe ActivityNotification::Notification, type: :model do it_behaves_like :notification_api it_behaves_like :renderable describe "with association" do - it "belongs to target" do - target = create(:confirmed_user) - notification = create(:notification, target: target) - expect(notification.reload.target).to eq(target) + context "belongs to target" do + before do + @target = create(:confirmed_user) + @notification = create(:notification, target: @target) + end + + it "responds to target" do + expect(@notification.reload.target).to eq(@target) + end + + it "responds to target_id" do + expect(@notification.reload.target_id.to_s).to eq(@target.id.to_s) + end + + it "responds to target_type" do + expect(@notification.reload.target_type).to eq("User") + end end it "belongs to notifiable" do notifiable = create(:article) notification = create(:notification, notifiable: notifiable) @@ -40,22 +57,20 @@ expect(notification.reload.notifier).to eq(notifier) end end describe "with serializable column" do - if ActivityNotification.config.orm == :active_record - it "has parameters for hash with symbol" do - parameters = {a: 1, b: 2, c: 3} - notification = create(:notification, parameters: parameters) - expect(notification.reload.parameters).to eq(parameters) - end + it "has parameters for hash with symbol" do + parameters = {a: 1, b: 2, c: 3} + notification = create(:notification, parameters: parameters) + expect(notification.reload.parameters.symbolize_keys).to eq(parameters) end it "has parameters for hash with string" do parameters = {'a' => 1, 'b' => 2, 'c' => 3} notification = create(:notification, parameters: parameters) - expect(notification.reload.parameters).to eq(parameters) + expect(notification.reload.parameters.stringify_keys).to eq(parameters) end end describe "with validation" do before { @notification = build(:notification) } @@ -65,23 +80,23 @@ end it "is invalid with blank target" do @notification.target = nil expect(@notification).to be_invalid - expect(@notification.errors[:target].size).to eq(1) + expect(@notification.errors[:target]).not_to be_empty end it "is invalid with blank notifiable" do @notification.notifiable = nil expect(@notification).to be_invalid - expect(@notification.errors[:notifiable].size).to eq(1) + expect(@notification.errors[:notifiable]).not_to be_empty end it "is invalid with blank key" do @notification.key = nil expect(@notification).to be_invalid - expect(@notification.errors[:key].size).to eq(1) + expect(@notification.errors[:key]).not_to be_empty end end describe "with scope" do context "to filter by notification status" do @@ -97,78 +112,78 @@ notifications = ActivityNotification::Notification.group_owners_only expect(notifications.to_a.size).to eq(2) expect(notifications.unopened_only.first).to eq(@unopened_group_owner) expect(notifications.opened_only!.first).to eq(@opened_group_owner) end - + it "works with group_members_only scope" do notifications = ActivityNotification::Notification.group_members_only expect(notifications.to_a.size).to eq(2) expect(notifications.unopened_only.first).to eq(@unopened_group_member) expect(notifications.opened_only!.first).to eq(@opened_group_member) end - + it "works with unopened_only scope" do notifications = ActivityNotification::Notification.unopened_only expect(notifications.to_a.size).to eq(2) expect(notifications.group_owners_only.first).to eq(@unopened_group_owner) expect(notifications.group_members_only.first).to eq(@unopened_group_member) end - + it "works with unopened_index scope" do notifications = ActivityNotification::Notification.unopened_index expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@unopened_group_owner) end - + it "works with opened_only! scope" do notifications = ActivityNotification::Notification.opened_only! expect(notifications.to_a.size).to eq(2) expect(notifications.group_owners_only.first).to eq(@opened_group_owner) expect(notifications.group_members_only.first).to eq(@opened_group_member) end - + context "with opened_only scope" do it "works" do notifications = ActivityNotification::Notification.opened_only(4) expect(notifications.to_a.size).to eq(2) expect(notifications.group_owners_only.first).to eq(@opened_group_owner) expect(notifications.group_members_only.first).to eq(@opened_group_member) end - + it "works with limit" do notifications = ActivityNotification::Notification.opened_only(1) expect(notifications.to_a.size).to eq(1) end end - + context "with opened_index scope" do it "works" do notifications = ActivityNotification::Notification.opened_index(4) expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@opened_group_owner) end - + it "works with limit" do notifications = ActivityNotification::Notification.opened_index(0) expect(notifications.to_a.size).to eq(0) end end - + it "works with unopened_index_group_members_only scope" do notifications = ActivityNotification::Notification.unopened_index_group_members_only expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@unopened_group_member) end - + context "with opened_index_group_members_only scope" do it "works" do notifications = ActivityNotification::Notification.opened_index_group_members_only(4) expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@opened_group_member) end - + it "works with limit" do notifications = ActivityNotification::Notification.opened_index_group_members_only(0) expect(notifications.to_a.size).to eq(0) end end @@ -237,11 +252,11 @@ notifications = ActivityNotification::Notification.filtered_by_options({ filtered_by_type: @notifiable_2.to_class_name }) expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@notification_2) end end - + context 'with filtered_by_group options' do it "works with filtered_by_options scope" do notifications = ActivityNotification::Notification.filtered_by_options({ filtered_by_group: @group_1 }) expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@notification_1) @@ -274,22 +289,28 @@ end end context 'with custom_filter options' do it "works with filtered_by_options scope" do - if ActivityNotification.config.orm == :active_record - notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: ["notifications.key = ?", @key_1] }) - expect(notifications.to_a.size).to eq(1) - expect(notifications.first).to eq(@notification_1) - end - notifications = ActivityNotification::Notification.filtered_by_options({ custom_filter: { key: @key_2 } }) expect(notifications.to_a.size).to eq(1) expect(notifications.first).to eq(@notification_2) end + + it "works with filtered_by_options scope with filter depending on ORM" do + options = + case ActivityNotification.config.orm + when :active_record then { custom_filter: ["notifications.key = ?", @key_1] } + when :mongoid then { custom_filter: { key: {'$eq': @key_1} } } + when :dynamoid then { custom_filter: {'key.begins_with': @key_1} } + end + notifications = ActivityNotification::Notification.filtered_by_options(options) + expect(notifications.to_a.size).to eq(1) + expect(notifications.first).to eq(@notification_1) + end end - + context 'with no options' do it "works with filtered_by_options scope" do notifications = ActivityNotification::Notification.filtered_by_options expect(notifications.to_a.size).to eq(2) end @@ -298,39 +319,137 @@ end context "to make order by created_at" do before do ActivityNotification::Notification.delete_all - unopened_group_owner = create(:notification, group_owner: nil) - unopened_group_member = create(:notification, group_owner: unopened_group_owner, created_at: unopened_group_owner.created_at + 10.second) - opened_group_owner = create(:notification, group_owner: nil, opened_at: Time.current, created_at: unopened_group_owner.created_at + 20.second) - opened_group_member = create(:notification, group_owner: opened_group_owner, opened_at: Time.current, created_at: unopened_group_owner.created_at + 30.second) + @target = create(:confirmed_user) + unopened_group_owner = create(:notification, target: @target, group_owner: nil) + unopened_group_member = create(:notification, target: @target, group_owner: unopened_group_owner, created_at: unopened_group_owner.created_at + 10.second) + opened_group_owner = create(:notification, target: @target, group_owner: nil, opened_at: Time.current, created_at: unopened_group_owner.created_at + 20.second) + opened_group_member = create(:notification, target: @target, group_owner: opened_group_owner, opened_at: Time.current, created_at: unopened_group_owner.created_at + 30.second) @earliest_notification = unopened_group_owner @latest_notification = opened_group_member end - it "works with latest_order scope" do - notifications = ActivityNotification::Notification.latest_order + unless ActivityNotification.config.orm == :dynamoid + context "using ORM other than dynamoid, you can directly call latest/earliest order method from class objects" do + + it "works with latest_order scope" do + notifications = ActivityNotification::Notification.latest_order + expect(notifications.to_a.size).to eq(4) + expect(notifications.first).to eq(@latest_notification) + expect(notifications.last).to eq(@earliest_notification) + end + + it "works with earliest_order scope" do + notifications = ActivityNotification::Notification.earliest_order + expect(notifications.to_a.size).to eq(4) + expect(notifications.first).to eq(@earliest_notification) + expect(notifications.last).to eq(@latest_notification) + end + + it "returns the latest notification with latest scope" do + notification = ActivityNotification::Notification.latest + expect(notification).to eq(@latest_notification) + end + + it "returns the earliest notification with earliest scope" do + notification = ActivityNotification::Notification.earliest + expect(notification).to eq(@earliest_notification) + end + + end + else + context "using dynamoid, you can call latest/earliest order method only with query using partition key of Global Secondary Index" do + + it "works with latest_order scope" do + notifications = ActivityNotification::Notification.filtered_by_target(@target).latest_order + expect(notifications.to_a.size).to eq(4) + expect(notifications.first).to eq(@latest_notification) + expect(notifications.last).to eq(@earliest_notification) + end + + it "works with earliest_order scope" do + notifications = ActivityNotification::Notification.filtered_by_target(@target).earliest_order + expect(notifications.to_a.size).to eq(4) + expect(notifications.first).to eq(@earliest_notification) + expect(notifications.last).to eq(@latest_notification) + end + + it "returns the latest notification with latest scope" do + notification = ActivityNotification::Notification.filtered_by_target(@target).latest + expect(notification).to eq(@latest_notification) + end + + it "returns the earliest notification with earliest scope" do + notification = ActivityNotification::Notification.filtered_by_target(@target).earliest + expect(notification).to eq(@earliest_notification) + end + + end + end + + it "works with latest_order! scope" do + notifications = ActivityNotification::Notification.latest_order! expect(notifications.to_a.size).to eq(4) expect(notifications.first).to eq(@latest_notification) expect(notifications.last).to eq(@earliest_notification) end - it "works with earliest_order scope" do - notifications = ActivityNotification::Notification.earliest_order + it "works with latest_order!(reverse=true) scope" do + notifications = ActivityNotification::Notification.latest_order!(true) expect(notifications.to_a.size).to eq(4) expect(notifications.first).to eq(@earliest_notification) expect(notifications.last).to eq(@latest_notification) end - it "returns the latest notification with latest scope" do - notification = ActivityNotification::Notification.latest + it "works with earliest_order! scope" do + notifications = ActivityNotification::Notification.earliest_order! + expect(notifications.to_a.size).to eq(4) + expect(notifications.first).to eq(@earliest_notification) + expect(notifications.last).to eq(@latest_notification) + end + + it "returns the latest notification with latest! scope" do + notification = ActivityNotification::Notification.latest! expect(notification).to eq(@latest_notification) end - it "returns the earliest notification with earliest scope" do - notification = ActivityNotification::Notification.earliest + it "returns the earliest notification with earliest! scope" do + notification = ActivityNotification::Notification.earliest! expect(notification).to eq(@earliest_notification) + end + end + + context "to include with associated records" do + before do + ActivityNotification::Notification.delete_all + create(:notification) + @notifications = ActivityNotification::Notification.filtered_by_key("default.default") + end + + it "works with_target" do + expect(@notifications.with_target.count).to eq(1) + end + + it "works with_notifiable" do + expect(@notifications.with_notifiable.count).to eq(1) + end + + it "works with_group" do + expect(@notifications.with_group.count).to eq(1) + end + + it "works with_group_owner" do + expect(@notifications.with_group_owner.count).to eq(1) + end + + it "works with_group_members" do + expect(@notifications.with_group_members.count).to eq(1) + end + + it "works with_notifier" do + expect(@notifications.with_notifier.count).to eq(1) end end end end