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

Unit tests - scorer/registry #260

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nathannaveen
Copy link
Contributor

  • Added unit tests for scorer/registry

Signed-off-by: nathannaveen 42319948+nathannaveen@users.noreply.github.com

- Added unit tests for scorer/registry

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
internal/scorer/algorithm/registry_test.go Outdated Show resolved Hide resolved
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.valuesForRegistry {
Register(k, v)
Copy link
Contributor

Choose a reason for hiding this comment

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

The global registry shouldn't be used as the state is not reset between tests.

Instead a new registry should be created for each test iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I re-set GlobalRegistry to NewRegistry() every test so we don’t run into this problem.

I can't create a new registry for every test because I am calling NewAlgorithm(), and NewAlgorithm()'s return is based on GlobalRegistry. If I create a new registry for every test, GlobalRegistry will always be empty when calling NewAlgorithm().

}

func TestNewAlgorithm(t *testing.T) {
type args struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

A better approach to this test method might be to:

  1. populate the registry with one or two entries before the test loop (I suggest having a factory that always works and stores inputs, and a factory that always returns an error).
  2. define tests that call NewAlgorithm on the registry.

i.e:

func TestNewAlgorithm(t *testing.T) {
  r := NewRegistry()
  r.Register("test", func(...) { ... })

  tests := []struct{
    // ...
  }{
    // ...
  }
  for _, test := range tests {
    t.Run(...)
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am registering GlobalRegistry at the top. I am also resetting GlobalRegistry using t.Cleanup() so as to not pollute the global variable.

If there is a better way to do this, could you please explain it to me with code examples? I don't mind you adding commits to this PR if that makes it easier.

Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

Using a custom registry is a good idea as it avoids side-effect and removes the need to do cleanups.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I don't understand what you mean by "custom registry", could you please explain and give some examples? It's been a while since I worked on this PR :)

Copy link
Contributor

Choose a reason for hiding this comment

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

When I say custom registry I mean the one returned by r := NewRegistry(), rather than using the GlobalRegistry.

By creating a new Registry in each test function, you avoid the need to run a cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't use NewRegistry() because Register() and NewAlgorithm() are based on GlobalRegistry meaning that they won't run properly without setting GlobalRegistry.

For example, if we used r := NewRegistry() and then registered r using r.Register(). GlobalRegistry would be empty when calling NewAlgorithm().

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
@nathannaveen nathannaveen force-pushed the nathan/feat/scorer/registry/unittests branch from 7cd674a to 8b0ffa1 Compare December 10, 2022 00:25
@nathannaveen
Copy link
Contributor Author

@calebbrown, a friendly ping!

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
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.

None yet

2 participants