|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + config "github.com/thirdweb-dev/indexer/configs" |
| 9 | +) |
| 10 | + |
| 11 | +func TestApplyDefaultTimeRange(t *testing.T) { |
| 12 | + // Save original config |
| 13 | + originalConfig := config.Cfg |
| 14 | + |
| 15 | + // Test with different default values |
| 16 | + testCases := []struct { |
| 17 | + name string |
| 18 | + defaultDays int |
| 19 | + inputParams map[string]string |
| 20 | + expectedParams map[string]string |
| 21 | + shouldApply bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "no timestamp filters - should apply defaults (7 days)", |
| 25 | + defaultDays: 7, |
| 26 | + inputParams: map[string]string{"other_filter": "value"}, |
| 27 | + shouldApply: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "no timestamp filters - should apply defaults (3 days)", |
| 31 | + defaultDays: 3, |
| 32 | + inputParams: map[string]string{"other_filter": "value"}, |
| 33 | + shouldApply: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "has block_timestamp_gte - should not apply defaults", |
| 37 | + defaultDays: 7, |
| 38 | + inputParams: map[string]string{"block_timestamp_gte": "1234567890"}, |
| 39 | + shouldApply: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "has block_timestamp_lte - should not apply defaults", |
| 43 | + defaultDays: 7, |
| 44 | + inputParams: map[string]string{"block_timestamp_lte": "1234567890"}, |
| 45 | + shouldApply: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "has other block_timestamp filter - should not apply defaults", |
| 49 | + defaultDays: 7, |
| 50 | + inputParams: map[string]string{"block_timestamp_other": "1234567890"}, |
| 51 | + shouldApply: false, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range testCases { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + // Set test config |
| 58 | + config.Cfg.API.DefaultTimeRangeDays = tt.defaultDays |
| 59 | + |
| 60 | + // Create a copy of input params |
| 61 | + params := make(map[string]string) |
| 62 | + for k, v := range tt.inputParams { |
| 63 | + params[k] = v |
| 64 | + } |
| 65 | + |
| 66 | + // Apply default time range |
| 67 | + ApplyDefaultTimeRange(params) |
| 68 | + |
| 69 | + // Check if defaults were applied |
| 70 | + hadGteBefore := false |
| 71 | + hadLteBefore := false |
| 72 | + hasGteAfter := false |
| 73 | + hasLteAfter := false |
| 74 | + |
| 75 | + // Check what was in input |
| 76 | + for key := range tt.inputParams { |
| 77 | + if key == "block_timestamp_gte" { |
| 78 | + hadGteBefore = true |
| 79 | + } |
| 80 | + if key == "block_timestamp_lte" { |
| 81 | + hadLteBefore = true |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Check what's in output |
| 86 | + for key := range params { |
| 87 | + if key == "block_timestamp_gte" { |
| 88 | + hasGteAfter = true |
| 89 | + } |
| 90 | + if key == "block_timestamp_lte" { |
| 91 | + hasLteAfter = true |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if tt.shouldApply { |
| 96 | + // Should have added both gte and lte |
| 97 | + if !hasGteAfter || !hasLteAfter { |
| 98 | + t.Errorf("Expected default time range to be applied, but gte=%v, lte=%v", hasGteAfter, hasLteAfter) |
| 99 | + } |
| 100 | + } else { |
| 101 | + // Should not have added any new defaults |
| 102 | + if (!hadGteBefore && hasGteAfter) || (!hadLteBefore && hasLteAfter) { |
| 103 | + t.Errorf("Expected no new defaults to be applied, but added gte=%v, lte=%v", |
| 104 | + !hadGteBefore && hasGteAfter, !hadLteBefore && hasLteAfter) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Verify the time range is reasonable |
| 109 | + if hasGteAfter && hasLteAfter { |
| 110 | + now := time.Now() |
| 111 | + maxDaysAgo := now.AddDate(0, 0, -(tt.defaultDays + 1)) |
| 112 | + |
| 113 | + // Parse the timestamps |
| 114 | + if gteStr, exists := params["block_timestamp_gte"]; exists { |
| 115 | + if gteUnix, err := strconv.ParseInt(gteStr, 10, 64); err == nil { |
| 116 | + gte := time.Unix(gteUnix, 0) |
| 117 | + if gte.Before(maxDaysAgo) { |
| 118 | + t.Errorf("block_timestamp_gte is too old: %v (expected within %d days)", gte, tt.defaultDays) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if lteStr, exists := params["block_timestamp_lte"]; exists { |
| 124 | + if lteUnix, err := strconv.ParseInt(lteStr, 10, 64); err == nil { |
| 125 | + lte := time.Unix(lteUnix, 0) |
| 126 | + if lte.After(now) { |
| 127 | + t.Errorf("block_timestamp_lte is in the future: %v", lte) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | + |
| 135 | + // Restore original config |
| 136 | + config.Cfg = originalConfig |
| 137 | +} |
0 commit comments