Skip to content

Commit da45421

Browse files
committed
chore: add ToJSON FromJSON test
1 parent 52d573b commit da45421

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

internal/plan/plan_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package plan
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
47
"strings"
58
"testing"
69

@@ -56,6 +59,53 @@ func TestPlanSummary(t *testing.T) {
5659
}
5760
}
5861

62+
func TestPlanJSONRoundTrip(t *testing.T) {
63+
testDataDir := "../../testdata/migrate"
64+
versions := []string{"v1", "v2", "v3", "v4", "v5"}
65+
66+
for _, version := range versions {
67+
t.Run(fmt.Sprintf("version_%s", version), func(t *testing.T) {
68+
planFilePath := filepath.Join(testDataDir, version, "plan.json")
69+
70+
// Read the original plan.json file
71+
originalJSON, err := os.ReadFile(planFilePath)
72+
if err != nil {
73+
t.Fatalf("Failed to read %s: %v", planFilePath, err)
74+
}
75+
76+
// First FromJSON: Load plan from JSON
77+
plan1, err := FromJSON(originalJSON, "public")
78+
if err != nil {
79+
t.Fatalf("Failed to parse JSON from %s: %v", planFilePath, err)
80+
}
81+
82+
// First ToJSON: Convert plan back to JSON
83+
json1, err := plan1.ToJSON()
84+
if err != nil {
85+
t.Fatalf("Failed to convert plan to JSON (first): %v", err)
86+
}
87+
88+
// Compare original JSON with first round-trip JSON
89+
if string(originalJSON) != json1 {
90+
t.Errorf("JSON round-trip failed for %s: original and round-trip JSON differ", version)
91+
t.Logf("Original JSON length: %d", len(originalJSON))
92+
t.Logf("Round-trip JSON length: %d", len(json1))
93+
// For debugging, show first difference
94+
minLen := len(originalJSON)
95+
if len(json1) < minLen {
96+
minLen = len(json1)
97+
}
98+
for i := 0; i < minLen; i++ {
99+
if originalJSON[i] != json1[i] {
100+
t.Logf("First difference at position %d: original=%c, round-trip=%c", i, originalJSON[i], json1[i])
101+
break
102+
}
103+
}
104+
}
105+
})
106+
}
107+
}
108+
59109
func TestPlanToJSON(t *testing.T) {
60110
oldSQL := `CREATE TABLE users (
61111
id integer NOT NULL

0 commit comments

Comments
 (0)