Skip to content

Increase coverage by adding more tests #1446

@ggyuchive

Description

@ggyuchive

What would you like to be added
Currently, Yorkie coverage is 38.31%. Some lines such as Client.Attach, remain uncovered even though the tests execute Client.Attach multiple times. But it seems some functionality is missing in testscript.

Why is this needed
Test enhancement can assure backward compatibility, no performance regression in case of new feature development or refactoring.

Good to add

  1. Add RPC test related to schema API (CreateSchema, GetSchema, ...) (link)
  2. Add test scenario calls cluster API (link)
  3. Add test cases which expects error
  4. ...

How to measure coverage enhancement in local

  1. Run go test -tags integration -race -coverprofile=coverage_main.out -covermode=atomic ./... in latest yorkie change
  2. Add test - I newly added AdminRPC CreateSchema test with below.
  • ./server/rpc/testcases/testcases.go
func RunAdminCreateSchemaTest(
	t *testing.T,
	testClient v1connect.YorkieServiceClient,
	testAdminClient v1connect.AdminServiceClient,
	testAdminAuthInterceptor *admin.AuthInterceptor,
) {
	resp, err := testAdminClient.LogIn(
		context.Background(),
		connect.NewRequest(&api.LogInRequest{
			Username: helper.AdminUser,
			Password: helper.AdminPassword,
		},
		))
	assert.NoError(t, err)

	testAdminAuthInterceptor.SetToken(resp.Msg.Token)

	_, err = testClient.ActivateClient(
		context.Background(),
		connect.NewRequest(&api.ActivateClientRequest{ClientKey: t.Name()}))
	assert.NoError(t, err)

	schemaRule1 := []types.Rule{
		{
			Path: "$.title",
			Type: "string",
		},
	}
	rule1 := converter.ToRules(schemaRule1)

	_, err = testAdminClient.CreateSchema(
		context.Background(),
		connect.NewRequest(&api.CreateSchemaRequest{
			ProjectName:   defaultProjectName,
			SchemaName:    "test-schema",
			SchemaVersion: 1,
			SchemaBody:    "type Document = {title: string;};",
			Rules:         rule1,
		},
		))
	assert.NoError(t, err)
}
  • ./server/rpc/server_test.go
	t.Run("admin create schema test", func(t *testing.T) {
		testcases.RunAdminCreateSchemaTest(t, testClient, testAdminClient, testAdminAuthInterceptor)
	})
  1. Run go test -tags integration -race -coverprofile=coverage_add.out -covermode=atomic ./…
  2. Add compare_coverage.sh in repository
#!/bin/bash
set -e

BASE=$1
HEAD=$2

if [ ! -f "$BASE" ]; then
  echo "Base file '$BASE' not found."
  exit 1
fi

if [ ! -f "$HEAD" ]; then
  echo "Head file '$HEAD' not found."
  exit 1
fi

sort "$BASE" > base.sorted
sort "$HEAD" > head.sorted

echo "------- Covered in BASE but not in HEAD -------"
join base.sorted head.sorted | awk '$3 >= 1 && $5 == 0 { print $1, $3 " -> " $5 }'

echo "------- Covered in HEAD but not in BASE -------"
join base.sorted head.sorted | awk '$3 == 0 && $5 >= 1 { print $1, $3 " -> " $5 }'

rm base.sorted head.sorted
  1. chmod 777 compare_coverage.sh
  2. ./compare_coverage.sh coverage_main.out coverage_add.out
    We can see 18 lines are additionally covers.
------- Covered in BASE but not in HEAD -------
------- Covered in HEAD but not in BASE -------
[[github.com/yorkie-team/yorkie/server/rpc/admin_server.go:648.56,650.42](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:648.56,650.42)](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:648.56,650.42) 0 -> 1
[[github.com/yorkie-team/yorkie/server/rpc/admin_server.go:654.2,655.16](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:654.2,655.16)](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:654.2,655.16) 0 -> 1
[[github.com/yorkie-team/yorkie/server/rpc/admin_server.go:659.2,668.16](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:659.2,668.16)](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:659.2,668.16) 0 -> 1
[[github.com/yorkie-team/yorkie/server/rpc/admin_server.go:672.2,674.9](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:672.2,674.9)](http://github.com/yorkie-team/yorkie/server/rpc/admin_server.go:672.2,674.9) 0 -> 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions