Skip to content

Add new data test#12

Merged
keenanjohnson merged 5 commits into
mainfrom
data-tests
May 13, 2026
Merged

Add new data test#12
keenanjohnson merged 5 commits into
mainfrom
data-tests

Conversation

@keenanjohnson
Copy link
Copy Markdown
Member

@keenanjohnson keenanjohnson commented May 13, 2026

Adds HTTP-level test coverage for the /data handler and the CSV/JSON formatting paths added in PR #8, plus targeted coverage for the reflect-based record-merging logic.

What changed

internal/data/data.go — two small testability seams, no behavior change:

  • New recordIterator interface (Next / Record / Err); the real *api.QueryTableResult already satisfies it.
  • New fetchPoints package-level function variable wrapping the DB call so Handle can be exercised end-to-end without a live InfluxDB.
  • Extracted collectPoints(res recordIterator) map[string]*Data from Handle so the iteration branches are independently testable.

internal/data/data_test.go — 10 new tests:

  • TestHandle_MethodNotAllowed — non-GET returns 405.
  • TestHandle_BadStart_Returns400WithBody — missing and unparseable start both return 400 with a non-empty body.
  • TestHandle_DefaultReturnsJSON — default response has Content-Type: application/json and decodes cleanly.
  • TestHandle_FormatCSV_ReturnsCSV?format=csv returns Content-Type: text/csv and a valid CSV body.
  • TestHandle_AcceptCSV_ReturnsCSVAccept: text/csv produces the same.
  • TestHandle_FetchError_Returns500 — error path from fetchPoints surfaces as 500.
  • TestWriteCSV_HeaderRowAndColumnOrder — first CSV row equals csvHeaders exactly.
  • TestCollectPoints_SkipsMissingHostTag — records without a host tag are dropped.
  • TestCollectPoints_SkipsNilValue — records with Value() == nil do not panic and do not overwrite the field.
  • TestCollectPoints_PopulatesKnownFields — mapped fields land in the right struct fields; unknown fields are ignored.

Notes / follow-up

The nil-Value test pins current behavior: when Value() is nil but the record has a valid host, a placeholder Data{Host: ...} entry is still created in the map (the write happens before the nil guard in Handle's loop). The test documents this rather than changing it — worth a separate decision on whether to move the placeholder write below the nil check.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds testability seams around the /data handler and expands unit/HTTP test coverage for JSON, CSV, error handling, and record-merging behavior.

Changes:

  • Introduces recordIterator, fetchPoints, and collectPoints to isolate DB querying from record collection.
  • Updates Handle to call the new fetchPoints seam before JSON/CSV response formatting.
  • Adds handler, CSV, and collectPoints-focused tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
internal/data/data.go Extracts data fetching/collection logic behind testable seams while preserving response formatting flow.
internal/data/data_test.go Adds HTTP handler tests, CSV output tests, and targeted collectPoints tests using fake Flux records.
Comments suppressed due to low confidence (1)

internal/data/data_test.go:223

  • This nil-value test only checks the zero-value field on a newly created placeholder, so it would not catch a regression that overwrites an existing nonzero CO2 value with zero when a later nil record for the same host/time arrives. Seed the iterator with a valid value before the nil record to verify the "does not overwrite" behavior described by the assertion.
	require.Len(t, points, 1, "a placeholder Data entry is created before the nil-value check")
	for _, d := range points {
		require.Equal(t, "frog-01", d.Host)
		require.Equal(t, 0.0, d.CO2, "nil Value() must not be written into the field")

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/data/data.go Outdated
Comment thread internal/data/data_test.go
Comment thread internal/data/data_test.go Outdated
Comment thread internal/data/data.go Outdated
keenanjohnson and others added 4 commits May 13, 2026 15:13
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@keenanjohnson keenanjohnson self-assigned this May 13, 2026
@keenanjohnson keenanjohnson added the enhancement New feature or request label May 13, 2026
@keenanjohnson keenanjohnson merged commit 73e0897 into main May 13, 2026
4 checks passed
@keenanjohnson keenanjohnson deleted the data-tests branch May 13, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants