|
| 1 | +package pg |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParsePartitionBound(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input string |
| 12 | + wantStart time.Time |
| 13 | + wantEnd time.Time |
| 14 | + expectError bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "valid postgres UTC bound", |
| 18 | + input: "FOR VALUES FROM ('2026-02-17 00:00:00+00') TO ('2026-02-18 00:00:00+00')", |
| 19 | + wantStart: time.Date(2026, 2, 17, 0, 0, 0, 0, |
| 20 | + time.FixedZone("UTC", 0)), |
| 21 | + wantEnd: time.Date(2026, 2, 18, 0, 0, 0, 0, |
| 22 | + time.FixedZone("UTC", 0)), |
| 23 | + expectError: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "valid non-UTC offset", |
| 27 | + input: "FOR VALUES FROM ('2026-02-17 00:00:00+02') TO ('2026-02-18 00:00:00+02')", |
| 28 | + wantStart: time.Date(2026, 2, 17, 0, 0, 0, 0, |
| 29 | + time.FixedZone("", 2*3600)), |
| 30 | + wantEnd: time.Date(2026, 2, 18, 0, 0, 0, 0, |
| 31 | + time.FixedZone("", 2*3600)), |
| 32 | + expectError: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "invalid format string", |
| 36 | + input: "INVALID STRING", |
| 37 | + expectError: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "missing TO section", |
| 41 | + input: "FOR VALUES FROM ('2026-02-17 00:00:00+00')", |
| 42 | + expectError: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "invalid timestamp", |
| 46 | + input: "FOR VALUES FROM ('2026-02-17') TO ('2026-02-18')", |
| 47 | + expectError: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "postgres with timezone minutes", |
| 51 | + input: "FOR VALUES FROM ('2026-02-17 00:00:00+02:00') TO ('2026-02-18 00:00:00+02:00')", |
| 52 | + expectError: true, // attualmente non supportato dal layout |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + start, end, err := parsePartitionBound(tt.input) |
| 59 | + |
| 60 | + if tt.expectError { |
| 61 | + if err == nil { |
| 62 | + t.Fatalf("expected error but got nil") |
| 63 | + } |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("unexpected error: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + if !start.Equal(tt.wantStart) { |
| 72 | + t.Errorf("start mismatch\n got: %v\n want: %v", start, tt.wantStart) |
| 73 | + } |
| 74 | + |
| 75 | + if !end.Equal(tt.wantEnd) { |
| 76 | + t.Errorf("end mismatch\n got: %v\n want: %v", end, tt.wantEnd) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments