Skip to content

Commit

Permalink
test count commands (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo authored May 16, 2023
1 parent f1a047b commit 39fafe0
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 3 deletions.
29 changes: 29 additions & 0 deletions cmd/registry-experimental/cmd/count/count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package count

import (
"testing"

"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/server/registry"
)

// TestMain will set up a local RegistryServer and grpc.Server for all
// tests in this package if REGISTRY_ADDRESS env var is not set
// for the client.
func TestMain(m *testing.M) {
grpctest.TestMain(m, registry.Config{})
}
2 changes: 1 addition & 1 deletion cmd/registry-experimental/cmd/count/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
func deploymentsCommand() *cobra.Command {
var filter string
cmd := &cobra.Command{
Use: "deployments",
Use: "deployments [API PATTERN]",
Short: "Count the number of deployments of specified APIs",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
73 changes: 73 additions & 0 deletions cmd/registry-experimental/cmd/count/deployments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package count

import (
"context"
"strconv"
"testing"

"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/test/seeder"
)

func TestCountDeployments(t *testing.T) {
ctx := context.Background()
client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{
&rpc.ApiDeployment{
Name: "projects/count-test/locations/global/apis/1/deployments/1",
},
&rpc.ApiDeployment{
Name: "projects/count-test/locations/global/apis/2/deployments/1",
},
&rpc.ApiDeployment{
Name: "projects/count-test/locations/global/apis/2/deployments/2",
},
})

cmd := Command()
args := []string{"deployments", "projects/count-test/locations/global/apis/-"}
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
}

tests := []struct {
api string
count int
}{
{"projects/count-test/locations/global/apis/1", 1},
{"projects/count-test/locations/global/apis/2", 2},
}

for _, test := range tests {
t.Run(test.api, func(t *testing.T) {
api, err := client.GetApi(ctx, &rpc.GetApiRequest{
Name: test.api,
})
if err != nil {
t.Fatal("failed GetApi", err)
}
count, err := strconv.Atoi(api.Labels["deployments"])
if err != nil {
t.Fatal("failed strconv", err)
}
if count != test.count {
t.Errorf("expected %d, got %d", test.count, count)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/registry-experimental/cmd/count/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
func revisionsCommand() *cobra.Command {
var filter string
cmd := &cobra.Command{
Use: "revisions",
Use: "revisions [SPEC or DEPLOYMENT PATTERN]",
Short: "Count the number of revisions of specified resources",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
129 changes: 129 additions & 0 deletions cmd/registry-experimental/cmd/count/revisions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2023 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package count

import (
"context"
"strconv"
"testing"

"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/test/seeder"
)

func TestCountDeploymentRevisions(t *testing.T) {
ctx := context.Background()
client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{
&rpc.ApiDeployment{Name: "projects/count-test/locations/global/apis/1/deployments/1"},
&rpc.ApiDeployment{Name: "projects/count-test/locations/global/apis/1/deployments/2"},
})

updateDepReq := &rpc.UpdateApiDeploymentRequest{
ApiDeployment: &rpc.ApiDeployment{
Name: "projects/count-test/locations/global/apis/1/deployments/2",
ApiSpecRevision: "projects/p/apis/a/versions/v/specs/s@12345678",
},
}
_, err := client.UpdateApiDeployment(ctx, updateDepReq)
if err != nil {
t.Fatalf("Setup: UpdateApiDeployment(%+v) returned error: %s", updateDepReq, err)
}

cmd := Command()
args := []string{"revisions", "projects/count-test/locations/global/apis/1/deployments/-"}
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
}

tests := []struct {
deployment string
count int
}{
{"projects/count-test/locations/global/apis/1/deployments/1", 1},
{"projects/count-test/locations/global/apis/1/deployments/2", 2},
}

for _, test := range tests {
t.Run(test.deployment, func(t *testing.T) {
api, err := client.GetApiDeployment(ctx, &rpc.GetApiDeploymentRequest{
Name: test.deployment,
})
if err != nil {
t.Fatal("failed GetApiDeployment", err)
}
count, err := strconv.Atoi(api.Labels["revisions"])
if err != nil {
t.Fatal("failed strconv", err)
}
if count != test.count {
t.Errorf("expected %d, got %d", test.count, count)
}
})
}
}

func TestCountSpecRevisions(t *testing.T) {
ctx := context.Background()
client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{
&rpc.ApiSpec{Name: "projects/count-test/locations/global/apis/1/versions/1/specs/1"},
&rpc.ApiSpec{Name: "projects/count-test/locations/global/apis/1/versions/1/specs/2"},
})

updateSpecReq := &rpc.UpdateApiSpecRequest{
ApiSpec: &rpc.ApiSpec{
Name: "projects/count-test/locations/global/apis/1/versions/1/specs/2",
Contents: []byte(`whatever`),
},
}
_, err := client.UpdateApiSpec(ctx, updateSpecReq)
if err != nil {
t.Fatalf("Setup: UpdateApiSpec(%+v) returned error: %s", updateSpecReq, err)
}

cmd := Command()
args := []string{"revisions", "projects/count-test/locations/global/apis/1/versions/1/specs/-"}
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
}

tests := []struct {
spec string
count int
}{
{"projects/count-test/locations/global/apis/1/versions/1/specs/1", 1},
{"projects/count-test/locations/global/apis/1/versions/1/specs/2", 2},
}

for _, test := range tests {
t.Run(test.spec, func(t *testing.T) {
api, err := client.GetApiSpec(ctx, &rpc.GetApiSpecRequest{
Name: test.spec,
})
if err != nil {
t.Fatal("failed GetApiSpec", err)
}
count, err := strconv.Atoi(api.Labels["revisions"])
if err != nil {
t.Fatal("failed strconv", err)
}
if count != test.count {
t.Errorf("expected %d, got %d", test.count, count)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/registry-experimental/cmd/count/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
func versionsCommand() *cobra.Command {
var filter string
cmd := &cobra.Command{
Use: "versions",
Use: "versions [API PATTERN]",
Short: "Count the number of versions of specified APIs",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
73 changes: 73 additions & 0 deletions cmd/registry-experimental/cmd/count/versions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package count

import (
"context"
"strconv"
"testing"

"github.com/apigee/registry/pkg/connection/grpctest"
"github.com/apigee/registry/rpc"
"github.com/apigee/registry/server/registry/test/seeder"
)

func TestCountVersions(t *testing.T) {
ctx := context.Background()
client, _ := grpctest.SetupRegistry(ctx, t, "count-test", []seeder.RegistryResource{
&rpc.ApiVersion{
Name: "projects/count-test/locations/global/apis/1/versions/1",
},
&rpc.ApiVersion{
Name: "projects/count-test/locations/global/apis/2/versions/1",
},
&rpc.ApiVersion{
Name: "projects/count-test/locations/global/apis/2/versions/2",
},
})

cmd := Command()
args := []string{"versions", "projects/count-test/locations/global/apis/-"}
cmd.SetArgs(args)
if err := cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
}

tests := []struct {
api string
count int
}{
{"projects/count-test/locations/global/apis/1", 1},
{"projects/count-test/locations/global/apis/2", 2},
}

for _, test := range tests {
t.Run(test.api, func(t *testing.T) {
api, err := client.GetApi(ctx, &rpc.GetApiRequest{
Name: test.api,
})
if err != nil {
t.Fatal("failed GetApi", err)
}
count, err := strconv.Atoi(api.Labels["versions"])
if err != nil {
t.Fatal("failed strconv", err)
}
if count != test.count {
t.Errorf("expected %d, got %d", test.count, count)
}
})
}
}

0 comments on commit 39fafe0

Please sign in to comment.