test_customer_usage.rb

 1# frozen_string_literal: true
 2
 3require "test_helper"
 4
 5require "customer_usage"
 6
 7CustomerUsage::REDIS = Minitest::Mock.new
 8
 9class CustomerUsageTest < Minitest::Test
10	def setup
11		@customer_usage = CustomerUsage.new("customer")
12	end
13
14	def test_incr_message_usage_counts_ten_recipients_as_one_message
15		CustomerUsage::REDIS.expect(:zremrangebylex, []) do |*args|
16			cust_key, dash, * = args
17			assert_equal "jmp_customer_outbound_messages-customer", cust_key
18			assert_equal "-", dash
19		end
20		CustomerUsage::REDIS.expect(:zincrby, 1) do |*args|
21			cust_key, amount, * = args
22			assert_equal "jmp_customer_outbound_messages-customer", cust_key
23			assert_equal 1, amount
24		end
25
26		result = @customer_usage.incr_message_usage(
27			recipients: Array.new(10) { |i| "#{i}@example.com" }
28		).sync
29
30		assert_equal result, { today: 1, body: 0 }
31		CustomerUsage::REDIS.verify
32	end
33	em :test_incr_message_usage_counts_ten_recipients_as_one_message
34
35	def test_incr_message_usage_counts_nine_recipients_as_one_message
36		CustomerUsage::REDIS.expect(:zremrangebylex, []) do |*args|
37			cust_key, dash, * = args
38			assert_equal "jmp_customer_outbound_messages-customer", cust_key
39			assert_equal "-", dash
40		end
41		CustomerUsage::REDIS.expect(:zincrby, 1) do |*args|
42			cust_key, amount, * = args
43			assert_equal "jmp_customer_outbound_messages-customer", cust_key
44			assert_equal 1, amount
45		end
46
47		result = @customer_usage.incr_message_usage(
48			recipients: Array.new(9) { |i| "#{i}@example.com" }
49		).sync
50
51		assert_equal result, { today: 1, body: 0 }
52		CustomerUsage::REDIS.verify
53	end
54	em :test_incr_message_usage_counts_nine_recipients_as_one_message
55
56	def test_incr_message_usage
57		CustomerUsage::REDIS.expect(:zremrangebylex, []) do |*args|
58			cust_key, dash, * = args
59			assert_equal "jmp_customer_outbound_messages-customer", cust_key
60			assert_equal "-", dash
61		end
62		CustomerUsage::REDIS.expect(:zincrby, 2) do |*args|
63			cust_key, amount, * = args
64			assert_equal "jmp_customer_outbound_messages-customer", cust_key
65			assert_equal 2, amount
66		end
67
68		# Literally no way to make this fit
69		# rubocop:disable Metrics/LineLength
70		expected_key = "jmp_outbound_body-dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"
71		# rubocop:enable Metrics/LineLength
72
73		CustomerUsage::REDIS.expect(:incrby, 2) do |*args|
74			body_key, amount = args
75			assert_equal body_key, expected_key
76			assert_equal amount, 2
77		end
78
79		CustomerUsage::REDIS.expect(:expire, 2) do |*args|
80			body_key, timeout = args
81
82			assert_equal body_key, expected_key
83			assert_equal timeout, 180
84		end
85
86		result = @customer_usage.incr_message_usage(
87			recipients: Array.new(11) { |i| "#{i}@example.com" },
88			body: "Hello, World!"
89		).sync
90
91		assert_equal result, { today: 2, body: 2 }
92		CustomerUsage::REDIS.verify
93	end
94	em :test_incr_message_usage
95end