Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing Test coverage for ReadMigrations function #3397

Closed

Conversation

Bharadwajshivam28
Copy link

What type of PR is this?

I am creating Test Coverage for the function ReadMigrations

Which issue(s) this PR fixes:

refs: #3390

@Bharadwajshivam28
Copy link
Author

@richscott I am working on creating Test coverage for the function ReadMigrations . While seeing the function and what it does I have created few tests:-

  1. Checks file format which will be read (currently the file name is just for test we need to change it as per our req)
  2. Checks slice format
  3. Checks the ID length (adjust the maximum ID length as needed)

There are many changes required like we can add/remove conditions which will make things more better ....

Copy link
Member

@richscott richscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few suggestions for now - this is a good start.

)

func TestReadMigrations(t *testing.T) {
mockFiles := []string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start - using a mocked filesystem with io/fs is good. Try adding some more files in here, with varying differences to match (or not) the expected patterns) - see the existing migrations in internal/scheduler/database/migrations/*.sql and also in internal/lookoutv2/schema/migrations/*.sql.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will look into it

}
assert.Len(t, migrations, 1, "Unexpected slice length")

// Slice first value should be int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments for these assertions are nice, but probably not necessary - the code is very clear.

}

/*
func createMockFileSystem(files []string) fs.FS {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you just need to declare a new struct type, and then have it implement the same method(s) that are defined for fs.FS - see https://pkg.go.dev/io/[email protected]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be just an instance of creating a classic Go interface implementation. You'd create a struct (for example, type struct testFS { ... }) and then create method(s) that implement the fs.FS interface - in this case, there's just one method, so the new function definition would be
func (myFS *testFS) Open(name string) (File, error) { /* return a fake File object */ }.

If you search for golang interfaces, there are several good articles on the basics of implementing interfaces in Go, like https://golangbot.com/interfaces-part-1/ .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created struct testFS for Mockfiles handling

@Bharadwajshivam28
Copy link
Author

Hello @richscott I have modified tests. I aligned on to match the expected pattern which we want from the ReadMigrations function. I compared with .sql files in the location internal/scheduler/database/migrations/*.sql and internal/lookoutv2/schema/migrations/*.sql

Currently the Tests includes-

  1. Checks file format
  2. Checks file is sorted or not
  3. Checks for extracted ID for each file
  4. Checks for ID length
  5. Checks for slice formatting and types
  6. checks if the sql (content not empty)

btw.... it needs modifcation as currently mockfiles part is unfinished and also the current tests needs changes

@Bharadwajshivam28
Copy link
Author

can you please @richscott review the current tests which i have pushed

@@ -98,7 +98,7 @@ func ReadMigrations(fsys fs.FS, basePath string) ([]Migration, error) {

var migrations []Migration
for _, f := range files {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay...


assert.Equal(t, migrations[0].id, 1, "Incorrect ID for 001_initial_schema.sql")
assert.Equal(t, migrations[1].id, 2, "Incorrect ID for 002_cancel_reason.sql")
assert.Equal(t, migrations[2].id, 3, "Incorrect ID for 003_run_leased_column.sql")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to exhaustively test for ID matching for every existing SQL file - just two or three sample tests should be enough.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks...

@Bharadwajshivam28
Copy link
Author

Hey @richscott I have made the changes..

package database

import (
"testing"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run the goimports tool on this file? It will sort the imports, and separate out third-party/non-standard-library modules, automatically.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay let me do it

Copy link
Author

@Bharadwajshivam28 Bharadwajshivam28 Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @richscott When i run goimports -w migrations_test.go It says command not found but I have installed it.

I am running from the path /home/shivam/armada/internal/common/database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants