<commit_before>require "spec_helper"

describe Spree::Marketing::Recipient, type: :model do
  let(:campaign) { build(:marketing_campaign) }
  let(:contact) { create(:marketing_contact) }

  describe "Validations" do
    it { is_expected.to validate_presence_of(:campaign) }
    it { is_expected.to validate_presence_of(:contact) }
    it { is_expected.to validate_uniqueness_of(:contact_id).scoped_to(:campaign_id) }
  end

  describe "Associations" do
    it { is_expected.to belong_to(:campaign).class_name("Spree::Marketing::Campaign") }
    it { is_expected.to belong_to(:contact).class_name("Spree::Marketing::Contact") }
  end

  describe 'Scopes' do
    before { allow(campaign).to receive_messages(enqueue_update: nil) }

    context '.email_unopened' do
      let(:recipient_with_email_opened_at) { create(:marketing_recipient, campaign: campaign, email_opened_at: Time.current) }
      let(:recipient_without_email_opened_at) { create(:marketing_recipient, campaign: campaign, email_opened_at: nil) }

      it { expect(Spree::Marketing::Recipient.email_unopened).to include recipient_without_email_opened_at }
      it { expect(Spree::Marketing::Recipient.email_unopened).to_not include recipient_with_email_opened_at }
    end

    context '.with_emails' do
      let(:recipient_with_contact_email) { create(:marketing_recipient, campaign: campaign, contact: contact) }
      let(:recipient_without_contact_email) { create(:marketing_recipient, campaign: campaign) }
      let(:recipients_with_email) { Spree::Marketing::Recipient.with_emails([contact.email]) }

      it { expect(recipients_with_email).to include recipient_with_contact_email }
      it { expect(recipients_with_email).to_not include recipient_without_contact_email }
    end
  end

  describe 'Delegates' do
    it { is_expected.to delegate_method(:uid).to(:contact).with_prefix(true) }
    it { is_expected.to delegate_method(:email).to(:contact).with_prefix(true) }
    it { is_expected.to delegate_method(:user).to(:contact).with_prefix(true) }
  end

  describe '.update_email_opened_at' do
    let(:recipient) { create(:marketing_recipient, contact: contact, campaign: campaign) }
    let(:recipients_data) { [{ email_id: contact.uid, email_address: contact.email, last_open: Time.current }.with_indifferent_access] }

    before do
      allow(campaign).to receive_messages(enqueue_update: nil)
      campaign.recipients << recipient
      campaign.recipients.includes(:contact).update_email_opened_at(recipients_data)
    end

    it 'updates unopened recipients\'s email_opened_at' do
      expect(recipient.reload.email_opened_at).to eq(recipients_data[0][:last_open])
    end
  end
end<commit_msg>start rspec of recipient model<commit_after>require "spec_helper"

describe Spree::Marketing::Recipient, type: :model do
  let(:user) { create(:user) }
  let(:contact) { create(:marketing_contact, user: user) }
  let(:campaign) { build(:marketing_campaign) }

  describe "Validations" do
    it { is_expected.to validate_presence_of(:campaign) }
    it { is_expected.to validate_presence_of(:contact) }
    it { is_expected.to validate_uniqueness_of(:contact_id).scoped_to(:campaign_id) }
  end

  describe "Associations" do
    it { is_expected.to belong_to(:campaign).class_name("Spree::Marketing::Campaign") }
    it { is_expected.to belong_to(:contact).class_name("Spree::Marketing::Contact") }
  end

  describe 'Scopes' do
    before { allow(campaign).to receive_messages(enqueue_update: nil) }

    context '.email_unopened' do
      let(:recipient_with_email_opened_at) { create(:marketing_recipient, campaign: campaign, email_opened_at: Time.current) }
      let(:recipient_without_email_opened_at) { create(:marketing_recipient, campaign: campaign, email_opened_at: nil) }

      it { expect(Spree::Marketing::Recipient.email_unopened).to include recipient_without_email_opened_at }
      it { expect(Spree::Marketing::Recipient.email_unopened).to_not include recipient_with_email_opened_at }
    end

    context '.with_emails' do
      let(:recipient_with_contact_email) { create(:marketing_recipient, campaign: campaign, contact: contact) }
      let(:recipient_without_contact_email) { create(:marketing_recipient, campaign: campaign) }
      let(:recipients_with_email) { Spree::Marketing::Recipient.with_emails([contact.email]) }

      it { expect(recipients_with_email).to include recipient_with_contact_email }
      it { expect(recipients_with_email).to_not include recipient_without_contact_email }
    end
  end

  describe 'Delegates' do
    it { is_expected.to delegate_method(:uid).to(:contact).with_prefix(true) }
    it { is_expected.to delegate_method(:email).to(:contact).with_prefix(true) }
    it { is_expected.to delegate_method(:user).to(:contact).with_prefix(true) }
  end

  describe '.update_email_opened_at' do
    let(:recipient) { create(:marketing_recipient, contact: contact, campaign: campaign) }
    let(:recipients_data) { [{ email_id: contact.uid, email_address: contact.email, last_open: Time.current }.with_indifferent_access] }

    before do
      allow(campaign).to receive_messages(enqueue_update: nil)
      campaign.recipients << recipient
      campaign.recipients.includes(:contact).update_email_opened_at(recipients_data)
    end

    it 'updates unopened recipients\'s email_opened_at' do
      expect(recipient.reload.email_opened_at).to eq(recipients_data[0][:last_open])
    end
  end



end
{'fraction_non_alphanumeric': 0.1117852546423975, 'fraction_numerical': 0.00036771465342893915, 'mean_word_length': 5.384976525821596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816126', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1684, 'n_words': 287}