apopt.comCBE 493R Data-Driven Athletics

Course / Tutorials / Week 12 / Q-35

Quick tutorial Q-35 · Week 12 · about 15 min · Foundation

Three tests: normal, missing, invalid

Write the three input tests every app must pass, with assert, for a Python copy of the book's resting-heart app, then feed it a real-sized file in the right and the wrong units.

  1. 1Gather
  2. 2Statistics
  3. 3Visualize
  4. 4Cleanse
  5. 5Features
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy (used)
  1. Ask
  2. Collect
  3. Check (this week)
  4. Interpret
  5. Coach (this week)
  6. Reassess

Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-34 A calculation in a web page.

The decision

Before an app shows anyone a number, decide what it must do with a good file, an empty file and a file in the wrong units, and write a test for each. An app that shows a wrong number in a large font is worse than no app.

The raw data

Try it

Users will feed your app the wrong file, an empty file, or a file in the wrong units. Section 12.4.4 asks every app to pass three tests before anything else:

The functions are a Python copy of the book's student app, Resting heart check: read the rr column, refuse what cannot be used, and compute RMSSD.

The functions under test

PythonRead, check, compute

The three tests

Each test states the expected behavior first, exactly as the hand answer was written before the known-answer test. assert stops with an error if the expectation fails; silence means it passed. The normal case is the book's hand calculation: 800, 820, 790, 810 and 800 ms give an RMSSD of √450 = 21.21 ms.

PythonNormal, missing, invalid

Type a prediction to enable Run.

Show the expected result without running
ok - test_normal
ok - test_missing
ok - test_invalid
normal case: RMSSD 21.21 ms

The invalid test is the one that earned its place. Before the check existed, four intervals in seconds passed the only guard ("at least three intervals"), the cleaning rule then removed all four, and the page printed NaN where the number belongs (Section 12.4.4).

The same three tests in the app's own language, for node --test, from the book's student app (test/inputs.test.js):

test('normal: the five hand-worked beats pass and give the hand answer', () => {
  const rr = parseRR('rr\n800\n820\n790\n810\n800\n');
  assert.equal(checkInput(rr), null);
  assert.ok(Math.abs(analyze(rr).rmssd - Math.sqrt(450)) <= 0.01); // 21.21 ms
});
test('missing: an empty file, a header alone, no rr column', () => {
  assert.match(checkInput(parseRR('')), /Fewer than three RR/);
  assert.match(checkInput(parseRR('time,rr\n')), /Fewer than three RR/);
  assert.throws(() => parseRR('time,hr\n1,60\n'), /No "rr"/);
});
test('invalid: intervals in seconds are refused, never shown as NaN', () => {
  const rr = parseRR('rr\n0.80\n0.82\n0.79\n0.81\n');
  assert.equal(rr.length, 4); // read, but not usable
  assert.match(checkInput(rr), /in milliseconds\?/);
});

A real-sized file, right and wrong

Now the book's synthetic example file, 331 intervals, as it is and as it would arrive if a logger had written seconds.

PythonThe example file in milliseconds and in seconds

Show the expected result without running
milliseconds 331 intervals -> ok, analyze
seconds      331 intervals -> Fewer than three intervals between 300 and 2000 ms. Are the values in milliseconds?
Two panels of RR interval against beat number. Left, the example file in milliseconds sits inside the shaded 300 to 2000 ms band. Right, the same file in seconds lies along the bottom, far below the band.

The first file passes and goes on to the analysis; the second is refused with a message the user can act on, and no number is shown. "It does not crash" was never the test.

For the athlete: "If the app says your file is not in milliseconds, export it again from the strap's app; it will never show you a number it could not check."

Check yourself

  1. An RR file holds 0.80, 0.82, 0.79, 0.81. What should the app do?

  2. Why is "it does not crash" not enough for the missing and invalid tests?

0 of 2 answered · 0 right the first time

Acknowledgements

The functions and the JavaScript tests follow the book's student app (book/ch12-app/code/student-app, MIT License).

Built 2026-11-30 with Python packages numpy 2.5.5, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.