Skip to content

Commit

Permalink
Fix generate
Browse files Browse the repository at this point in the history
  • Loading branch information
myc2h6o committed Feb 14, 2023
1 parent 187c9c2 commit c153f18
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/services/mysql/parse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ServerID(input string) (*ServerId, error) {
return &resourceId, nil
}

// ServerIDInsensitively parses a Server ID into an ServerId struct, insensitively
// ServerIDInsensitively parses an Server ID into an ServerId struct, insensitively
// This should only be used to parse an ID for rewriting, the ServerID
// method should be used instead for validation etc.
//
Expand Down
117 changes: 117 additions & 0 deletions internal/services/mysql/parse/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,120 @@ func TestServerID(t *testing.T) {
}
}
}

func TestServerIDInsensitively(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ServerId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/",
Error: true,
},

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/servers/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/servers/server1",
Expected: &ServerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "server1",
},
},

{
// lower-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/servers/server1",
Expected: &ServerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "server1",
},
},

{
// upper-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/SERVERS/server1",
Expected: &ServerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "server1",
},
},

{
// mixed-cased segment names
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DBforMySQL/SeRvErS/server1",
Expected: &ServerId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Name: "server1",
},
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := ServerIDInsensitively(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}

0 comments on commit c153f18

Please sign in to comment.