|
| 1 | +package blueprint_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/pitabwire/frame/blueprint" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMerge_Additive(t *testing.T) { |
| 10 | + base := blueprint.Blueprint{ |
| 11 | + Service: "users", |
| 12 | + HTTP: []blueprint.HTTPRoute{ |
| 13 | + {Name: "list", Method: "GET", Route: "/users", Handler: "List"}, |
| 14 | + }, |
| 15 | + Plugins: []blueprint.Plugin{{Name: "logging"}}, |
| 16 | + } |
| 17 | + overlay := blueprint.Blueprint{ |
| 18 | + HTTP: []blueprint.HTTPRoute{ |
| 19 | + {Name: "create", Method: "POST", Route: "/users", Handler: "Create"}, |
| 20 | + }, |
| 21 | + Plugins: []blueprint.Plugin{{Name: "metrics"}}, |
| 22 | + } |
| 23 | + |
| 24 | + out, err := blueprint.Merge(base, overlay) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("merge: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + if len(out.HTTP) != 2 { |
| 30 | + t.Fatalf("expected 2 routes, got %d", len(out.HTTP)) |
| 31 | + } |
| 32 | + if len(out.Plugins) != 2 { |
| 33 | + t.Fatalf("expected 2 plugins, got %d", len(out.Plugins)) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestMerge_Override(t *testing.T) { |
| 38 | + base := blueprint.Blueprint{ |
| 39 | + HTTP: []blueprint.HTTPRoute{ |
| 40 | + {Name: "list", Method: "GET", Route: "/users", Handler: "List"}, |
| 41 | + }, |
| 42 | + } |
| 43 | + overlay := blueprint.Blueprint{ |
| 44 | + HTTP: []blueprint.HTTPRoute{ |
| 45 | + {Name: "list", Method: "GET", Route: "/users", Handler: "ListV2", Override: true}, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + out, err := blueprint.Merge(base, overlay) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("merge: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + if out.HTTP[0].Handler != "ListV2" { |
| 55 | + t.Fatalf("expected override to apply") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestMerge_Remove(t *testing.T) { |
| 60 | + base := blueprint.Blueprint{ |
| 61 | + HTTP: []blueprint.HTTPRoute{ |
| 62 | + {Name: "list", Method: "GET", Route: "/users", Handler: "List"}, |
| 63 | + }, |
| 64 | + } |
| 65 | + overlay := blueprint.Blueprint{ |
| 66 | + HTTP: []blueprint.HTTPRoute{ |
| 67 | + {Name: "list", Remove: true}, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + out, err := blueprint.Merge(base, overlay) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("merge: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if len(out.HTTP) != 0 { |
| 77 | + t.Fatalf("expected route removed, got %d", len(out.HTTP)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestMerge_ServiceMismatch(t *testing.T) { |
| 82 | + base := blueprint.Blueprint{Service: "users"} |
| 83 | + overlay := blueprint.Blueprint{Service: "billing"} |
| 84 | + |
| 85 | + _, err := blueprint.Merge(base, overlay) |
| 86 | + if err == nil { |
| 87 | + t.Fatalf("expected mismatch error") |
| 88 | + } |
| 89 | +} |
0 commit comments