README ¶
= Exercise 7.9 // Refs: :chapter: 7 :exercise: 9 :url-base: https://github.com/fenegroni/TGPL-exercise-solutions :workflow: workflows/Exercise {chapter}.{exercise} :action: actions/workflows/ch{chapter}ex{exercise}.yml :url-workflow: {url-base}/{workflow} :url-action: {url-base}/{action} :badge-exercise: image:{url-workflow}/badge.svg?branch=main[link={url-action}] {badge-exercise} Use the `html/template` package (Section 4.6) to replace `printTracks` with a function that displays the tracks as an HTML table. Use the solution to the previous exercise to arrange that each click on a column head makes an HTTP request to sort the table. == Test The exercise requires us to implement the integration of two separate pieces of functionality: . the HTML output in table form with hyperlinks in the headers . the ordering of the table contents to use the ordering from exercise 7.8 The integration of those two separate requirements also requires us to validate the hyperlinks in the HTML output can be used to make a valid request to obtain the desired output. == Strategy My TDD strategy for this exercise is to validate the integration of all the requirements. The test does not require us run a real web server since the `httptest` package offers us the opportunity to create a `Request` from a link in the HTML output which we can use to validate the link itself. My intention is not to implement Unit Tests for all elements. Rather than focusing on the detail of the individual parts I am focusing on the end result. == Front end I am not a front end web developer. I expect the HTML output to be the bare minimum that satisfies the requirements: a basic table with no borders and headers that contain fixed hyperlinks. == Elements by ID The integration test will extract elements from the table by id so that we know the order is correct and will then extract the URL from a known element to then make an HTTP call and get the next result. === Leverage exercise 5.8 In exercise 5.8 we wrote a function `ElementByID` to extract the correct elements by ID. This will be the first part of the integration test: given the HTML output extract the elements of the table and the links from the headers. Not only HTML elements will have an ID, but their IDs will be generated so that we can pick individual items and validate their position, order, attributes, etc. == Http Request To avoid the need for a whole web server just to validate the link in the table's headers, we will instead simply assume a Request with the link and its parameters was sent to us and we must now satisfy it. We use the `httptest` package to create a Web Request using the link in the header of each column. We then get the `sort` attribute from the header, sort the table by that value, and validate the resulting html. == Validating the sorted HTML In this integration test, I assume that the HTML produced by `PrintTracksAsHTML` is the same whether this is the result of printing it after ordering it by a column or another. A more robust test would compare the position within the table of the elements with the same ID. I'll leave that for a future improvement.
Click to show internal directories.
Click to hide internal directories.