๐Ÿ“ฆ obafemitayor / newsletter-subscription-application

๐Ÿ“„ subscription_service_spec.rb ยท 204 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204# frozen_string_literal: true

require 'rails_helper'

RSpec.describe SubscriptionService do
  let(:customer) { create(:customer) }
  let(:category1) { create(:category) }
  let(:category2) { create(:category) }

  describe '.create_subscription' do
    it 'should create new subscription successfully when customer does not exist' do
      SubscriptionService.create_subscription(
        work_email: 'new_customer@example.com',
        first_name: 'New',
        last_name: 'Customer',
        category_guids: [category1.guid, category2.guid]
      )
      new_customer = Customer.last
      expect(new_customer.work_email).to eq('new_customer@example.com')
      expect(new_customer.first_name).to eq('New')
      expect(new_customer.last_name).to eq('Customer')
      expect(new_customer.guid).to be_present

      subscriptions = Subscription.where(customer_id: new_customer.id)
      expect(subscriptions.pluck(:category_id)).to contain_exactly(category1.id, category2.id)
    end

    it 'should create new subscription successfully when customer exists' do
      SubscriptionService.create_subscription(
        work_email: customer.work_email,
        first_name: customer.first_name,
        last_name: customer.last_name,
        category_guids: [category1.guid, category2.guid]
      )

      expect(Customer.last).to eq(customer)
      subscriptions = Subscription.where(customer_id: customer.id)
      expect(subscriptions.pluck(:category_id)).to contain_exactly(category1.id, category2.id)
    end

    it 'should create new subscription successfully when category_guids contains existing subscriptions' do
      create(:subscription, customer: customer, category: category1)

      SubscriptionService.create_subscription(
        work_email: customer.work_email,
        first_name: customer.first_name,
        last_name: customer.last_name,
        category_guids: [category1.guid, category2.guid]
      )

      subscriptions = Subscription.where(customer_id: customer.id)
      expect(subscriptions.pluck(:category_id)).to contain_exactly(category1.id, category2.id)
    end
  end

  describe '.get_subscriptions' do
    let(:customer1) { create(:customer) }
    let(:customer2) { create(:customer) }
    let(:customer3) { create(:customer) }
    let!(:subscription1) { create(:subscription, customer: customer1, category: category1) }
    let!(:subscription2) { create(:subscription, customer: customer2, category: category2) }
    let!(:subscription3) { create(:subscription, customer: customer3, category: category1) }
    let!(:deleted_subscription) { create(:subscription, deleted_at: Time.current) }

    it 'returns paginated record when category_guids is not provided and data is less than limit' do
      result = SubscriptionService.get_subscriptions(limit: 5)

      expect(result[:subscriptions].length).to eq(3)
      expect(result[:next_cursor]).to eq(subscription3.id)
      expect(result[:previous_cursor]).to eq(subscription1.id)
      
      result[:subscriptions].each do |subscription|
        expect(subscription).to include(
          'work_email' => be_a(String),
          'first_name' => be_a(String),
          'last_name' => be_a(String),
          'category_name' => be_a(String)
        )
      end
    end

    it 'returns paginated record when category_guids is not provided and data is more than limit' do
      result1 = SubscriptionService.get_subscriptions(limit: 2)
      expect(result1[:subscriptions].length).to eq(2)
      expect(result1[:next_cursor]).to eq(subscription2.id)
      expect(result1[:previous_cursor]).to eq(subscription1.id)

      result2 = SubscriptionService.get_subscriptions(pagination_id: result1[:next_cursor], limit: 2)
      expect(result2[:subscriptions].length).to eq(1)
      expect(result2[:next_cursor]).to eq(subscription3.id)
      expect(result2[:previous_cursor]).to eq(subscription3.id)
      expect(result2[:has_more]).to be_falsey

      [result1, result2].each do |result|
        result[:subscriptions].each do |subscription|
          expect(subscription).to include(
            'work_email' => be_a(String),
            'first_name' => be_a(String),
            'last_name' => be_a(String),
            'category_name' => be_a(String)
          )
        end
      end
    end

    it 'returns paginated record when category_guids is provided and data is less than limit' do
      result = SubscriptionService.get_subscriptions(
        category_guids: [category1.guid],
        limit: 5
      )

      expect(result[:subscriptions].length).to eq(2)
      expect(result[:next_cursor]).to eq(subscription3.id)
      expect(result[:previous_cursor]).to eq(subscription1.id)
      expect(result[:has_more]).to be_falsey
      
      result[:subscriptions].each do |subscription|
        expect(subscription).to include(
          'work_email' => be_a(String),
          'first_name' => be_a(String),
          'last_name' => be_a(String),
          'category_name' => eq(category1.name)
        )
      end
    end

    it 'returns paginated record when category_guids is provided and data is more than limit' do
      # Create additional customers and subscriptions for category1
      customer4 = create(:customer)
      customer5 = create(:customer)
      customer6 = create(:customer)
      customer7 = create(:customer)
      subscription4 = create(:subscription, customer: customer4, category: category1)
      subscription5 = create(:subscription, customer: customer5, category: category1)
      subscription6 = create(:subscription, customer: customer6, category: category1)
      subscription7 = create(:subscription, customer: customer7, category: category1)

      result1 = SubscriptionService.get_subscriptions(category_guids: [category1.guid], limit: 3)
      expect(result1[:subscriptions].length).to eq(3)
      expect(result1[:previous_cursor]).to eq(subscription1.id)
      expect(result1[:next_cursor]).to eq(subscription4.id)
      expect(result1[:has_more]).to be_truthy

      result2 = SubscriptionService.get_subscriptions(
        category_guids: [category1.guid],
        pagination_id: result1[:next_cursor],
        limit: 3
      )
      expect(result2[:subscriptions].length).to eq(3)
      expect(result2[:previous_cursor]).to eq(subscription5.id)
      expect(result2[:next_cursor]).to eq(subscription7.id)
      expect(result2[:has_more]).to be_falsey

      [result1, result2].each do |result|
        result[:subscriptions].each do |subscription|
          expect(subscription).to include(
            'work_email' => be_a(String),
            'first_name' => be_a(String),
            'last_name' => be_a(String),
            'category_name' => eq(category1.name)
          )
        end
      end
    end

    it 'should be able to navigate forward and backward' do
      # Create additional customers and subscriptions for category1
      customer4 = create(:customer)
      customer5 = create(:customer)
      customer6 = create(:customer)
      customer7 = create(:customer)
      subscription4 = create(:subscription, customer: customer4, category: category1)
      subscription5 = create(:subscription, customer: customer5, category: category1)
      subscription6 = create(:subscription, customer: customer6, category: category1)
      subscription7 = create(:subscription, customer: customer7, category: category1)

      result1 = SubscriptionService.get_subscriptions(category_guids: [category1.guid], limit: 3)
      expect(result1[:subscriptions].length).to eq(3)
      expect(result1[:previous_cursor]).to eq(subscription1.id)
      expect(result1[:next_cursor]).to eq(subscription4.id)

      result2 = SubscriptionService.get_subscriptions(
        category_guids: [category1.guid],
        pagination_id: result1[:next_cursor],
        limit: 3
      )
      expect(result2[:subscriptions].length).to eq(3)
      expect(result2[:previous_cursor]).to eq(subscription5.id)
      expect(result2[:next_cursor]).to eq(subscription7.id)

      result3 = SubscriptionService.get_subscriptions(
        category_guids: [category1.guid],
        pagination_id: result2[:previous_cursor],
        is_forward: false,
        limit: 3
      )

      expect(result3[:subscriptions].length).to eq(3)
      expect(result3[:previous_cursor]).to eq(subscription1.id)
      expect(result3[:next_cursor]).to eq(subscription4.id)
    end
  end
end