-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Unit tests - scorer/registry #260
base: main
Are you sure you want to change the base?
Unit tests - scorer/registry #260
Conversation
- Added unit tests for scorer/registry Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
for k, v := range tt.valuesForRegistry { | ||
Register(k, v) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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:
- 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). - 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(...)
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
7cd674a
to
8b0ffa1
Compare
@calebbrown, a friendly ping! |
Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
Signed-off-by: nathannaveen 42319948+nathannaveen@users.noreply.github.com