11# frozen_string_literal: true
22
33require_relative 'test_helper'
4+ require_relative '../lib/analytics/trend_analyzer'
45
56class TestAnalytics < Minitest ::Test
67 def setup
78 @sample_views = [ 100 , 110 , 120 , 115 , 130 , 125 , 140 , 135 , 150 , 145 ]
89 @sample_dates = ( 1 ..10 ) . map { |i | Date . today - i }
10+ @analyzer = Analytics ::TrendAnalyzer . new ( @sample_views , @sample_dates )
911 end
1012
1113 def test_total_calculation
12- total = @sample_views . sum
13- assert_equal 1270 , total
14+ assert_equal 1270 , @analyzer . total
1415 end
1516
1617 def test_average_calculation
17- avg = @sample_views . sum / @sample_views . size . to_f
18- assert_in_delta 127.0 , avg , 0.1
18+ assert_equal 127 , @analyzer . average
1919 end
2020
21- def test_wow_growth
22- # Simulate 14+ days of data
23- views = [ 100 , 110 , 120 , 130 , 140 , 150 , 160 , # week 1
24- 110 , 115 , 125 , 135 , 145 , 155 , 165 ] # week 2
25-
26- current_week = views . last ( 7 ) . sum
27- previous_week = views [ -14 ..-8 ] . sum
28- growth = ( ( current_week - previous_week ) * 100 / previous_week ) . round ( 1 )
29-
30- assert_in_delta 3.7 , growth , 0.1
21+ def test_empty_values_total
22+ analyzer = Analytics ::TrendAnalyzer . new ( [ ] )
23+ assert_equal 0 , analyzer . total
3124 end
3225
33- def test_empty_data_handling
34- assert_equal 0 , [ ] . sum
35- assert_equal 0 , [ ] . size
26+ def test_empty_values_average
27+ analyzer = Analytics ::TrendAnalyzer . new ( [ ] )
28+ assert_equal 0 , analyzer . average
29+ end
30+
31+ def test_wow_growth_with_sufficient_data
32+ # 14+ days of data: week1 = [100,110,120,130,140,150,160] sum = 910
33+ # week2 = [110,115,125,135,145,155,165] sum = 950
34+ # growth = (950 - 910) / 910 * 100 = 4.4%
35+ views = [ 100 , 110 , 120 , 130 , 140 , 150 , 160 ,
36+ 110 , 115 , 125 , 135 , 145 , 155 , 165 ]
37+ analyzer = Analytics ::TrendAnalyzer . new ( views )
38+ assert_in_delta 4.4 , analyzer . wow_growth , 0.1
39+ end
40+
41+ def test_wow_growth_with_insufficient_data
42+ analyzer = Analytics ::TrendAnalyzer . new ( [ 100 , 200 , 300 ] )
43+ assert_equal 0 , analyzer . wow_growth
44+ end
45+
46+ def test_mom_growth_with_sufficient_data
47+ # 60+ days of data (simplified)
48+ views = [ 10 ] * 30 + [ 15 ] * 30
49+ analyzer = Analytics ::TrendAnalyzer . new ( views )
50+ assert_in_delta 50.0 , analyzer . mom_growth , 0.1
51+ end
52+
53+ def test_growth_rate
54+ views = [ 100 , 110 , 121 ] # 10% growth each period
55+ analyzer = Analytics ::TrendAnalyzer . new ( views )
56+ # (121/100)^(1/2) - 1 = 0.1 = 10%
57+ assert_in_delta 10.0 , analyzer . growth_rate , 0.5
58+ end
59+
60+ def test_peak_day
61+ peak = @analyzer . peak_day
62+ assert_equal 150 , peak [ :value ]
63+ assert_equal 8 , peak [ :index ]
64+ end
65+
66+ def test_forecast_returns_array
67+ forecast = @analyzer . forecast ( 5 )
68+ assert_equal 5 , forecast . size
69+ assert forecast . all? { |v | v . is_a? ( Numeric ) }
70+ end
71+
72+ def test_forecast_with_insufficient_data
73+ analyzer = Analytics ::TrendAnalyzer . new ( [ 1 , 2 , 3 ] )
74+ assert_equal [ ] , analyzer . forecast ( 5 )
75+ end
76+
77+ def test_moving_average
78+ ma = @analyzer . moving_average ( 3 )
79+ # First MA: (100+110+120)/3 = 110
80+ # Second: (110+120+115)/3 = 115
81+ assert_in_delta 110.0 , ma [ 0 ] , 0.1
82+ assert_in_delta 115.0 , ma [ 1 ] , 0.1
3683 end
3784end
0 commit comments