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

Make myradio-go more mockable #22

Merged
merged 4 commits into from
Oct 24, 2016
Merged

Make myradio-go more mockable #22

merged 4 commits into from
Oct 24, 2016

Conversation

MattWindsor91
Copy link
Member

This PRQ:

  • Splits Session into two things, a Session object that handles API requests from clients, and an apiRequester interface that actually does the API request;
  • Moves the old Session behaviour into authedRequester (which implements apiRequester);
  • Adds a new mockRequester that pretends to do an API request but instead returns a stock json.RawMessage;
  • Allows Sessions to be created with mockRequesters using the new MockSession constructor;
  • Adds a (rather pointless) test to show how this can be used.

Thus, any of myradio-go's existing methods can now be sent a JSON file directly instead of having to make an API call, which makes testing the myradio-go specific logic much easier!

This might seem a little overengineered, but it's the easiest way I could think of to make this possible.

The reason Session still exists, instead of using apiRequester directly, is backwards compatibility; allowing the myradio-go API to remain method-based; allowing helper methods like apiRequest to be defined without changing the requesters; and allowing future expansion of what lives in Session.

Things that would be nice:

  • Move show_test's JSON into a separate file;
  • Make it so myradio-go can dump out the JSON from calling an API method, so that it can be used for future testing. This might be better done in an external shell script wrapping curl though.

This allows mock requests to be created.
This is a pretty useless test, but at least it can now be run.
Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

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

It's rather... verbose for what's essentially just checking that json is being marshalled correctly. There's not even any guarantee that it follows what MyRadio outputs either, unless someone keeps that up to date...

Of course, I have no idea how you might improve on that...

}

if !reflect.DeepEqual(showMeta, expected) {
t.Errorf("expected:\n%v\n\ngot:\n%v", expected, showMeta)
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this could get a little difficult to decipher in the case that they differ. Perhaps make it a more specific error? Can't say I'm sure how you'd do that though..

Copy link
Member Author

Choose a reason for hiding this comment

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

Without actually enumerating through all of the fields of showMeta, not sure how. This is verbose as it is :/


// NewSession constructs a new Session with the given API key.
func NewSession(apikey string) (*Session, error) {
url, err := url.Parse(`https://ury.york.ac.uk/api/v2`)
Copy link
Member

Choose a reason for hiding this comment

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

wait, why was this ury.york.ac.uk in the original? Also, why isn't configurable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel as if making this configurable would be an entirely new prq :p Also I have no idea.

Copy link
Member

Choose a reason for hiding this comment

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

Moved to #23

baseurl: *url,
}, nil
// AuthedRequester answers API requests by making an authed API call.
type authedRequester struct {
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth bothering to call it an "authedRequester" ? Why not just "Requester"?

Copy link
Member Author

Choose a reason for hiding this comment

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

First, it's not meant to be public, so it shouldn't be capitalised :p

Second, idk, it seems weird to call a specific instance of a given interface (in this case apiRequester something more generic-sounding than its interface. Maybe the interface should be requester and this apiRequester or something.

@@ -0,0 +1,119 @@
package myradio
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. I'm more of a fan of black box testing with go modules tbh, so this should be myradio_test - can you fiddle it so that's possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@MattWindsor91
Copy link
Member Author

@LordAro short of having the myradio-go tests spin up an entire implementation of the MyRadio API, I'm not sure how these tests can do anything other than just checking the interpretation of JSON. (Some API methods do more than just unmarshal the JSON, the test here was just a quick proof of context).

I'd argue that even making sure the JSON marshalling works can be useful. First, until I did the lint branch the track marshalling was completely broken and nobody noticed. Second, it ensures that if we change GetSearchMeta or the ShowMeta struct there's a bit of code exercising it (that isn't an external client) that'll break enough to make us sure we've covered all our bases.

If anyone can find a way of making it less verbose, then sure. I picked a bad example in hindsight: show metadata is inherently very verbose!

@LordAro
Copy link
Member

LordAro commented Oct 24, 2016

probably.

@LordAro LordAro merged commit 011d875 into master Oct 24, 2016
@LordAro LordAro deleted the mw-mockable branch October 24, 2016 19:57
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.

3 participants