move queryparams test to queryparams. handler now has no reference to allocators

This commit is contained in:
Emil Lerch 2026-01-07 09:31:50 -08:00
parent 03ca266e1d
commit 05d470871f
Signed by: lobo
GPG key ID: A7B62D657EF764F8
3 changed files with 29 additions and 29 deletions

View file

@ -149,3 +149,25 @@ test "parse transparency" {
const params_custom = try QueryParams.parse(allocator, "transparency=200");
try std.testing.expectEqual(@as(u8, 200), params_custom.transparency.?);
}
test "imperial units selection logic" {
// This test documents the priority order for unit selection:
// 1. Explicit ?u or ?m parameter (highest priority)
// 2. lang=us parameter
// 3. US IP detection
// 4. Default to metric
// The actual logic is tested through integration tests
// This test just verifies the QueryParams parsing works
const allocator = std.testing.allocator;
const params_u = try QueryParams.parse(allocator, "u");
try std.testing.expect(params_u.use_imperial.?);
const params_m = try QueryParams.parse(allocator, "m");
try std.testing.expect(!params_m.use_imperial.?);
const params_lang = try QueryParams.parse(allocator, "lang=us");
defer allocator.free(params_lang.lang.?);
try std.testing.expectEqualStrings("us", params_lang.lang.?);
}

View file

@ -136,7 +136,7 @@ fn handleWeatherInternal(
render_options.use_imperial = true; // this is a US IP
}
// Add coordinates header using response allocator
// Add coordinates header to response
const coords_header = try std.fmt.allocPrint(res.arena, "{d:.4},{d:.4}", .{ location.coords.latitude, location.coords.longitude });
res.headers.add("X-Location-Coordinates", coords_header);
@ -147,7 +147,7 @@ fn handleWeatherInternal(
res.content_type = .JSON; // reset to json
try Json.render(res.writer(), weather);
} else if (std.mem.eql(u8, fmt, "p1")) {
try Prometheus.render(res.writer(), weather, req_alloc);
try Prometheus.render(res.writer(), weather);
} else if (std.mem.eql(u8, fmt, "v2")) {
try V2.render(res.writer(), weather, render_options.use_imperial);
} else if (std.mem.startsWith(u8, fmt, "%")) {
@ -198,25 +198,3 @@ fn determineFormat(params: QueryParams, user_agent: ?[]const u8) Formatted.Forma
return .ansi;
return .html;
}
test "imperial units selection logic" {
// This test documents the priority order for unit selection:
// 1. Explicit ?u or ?m parameter (highest priority)
// 2. lang=us parameter
// 3. US IP detection
// 4. Default to metric
// The actual logic is tested through integration tests
// This test just verifies the QueryParams parsing works
const allocator = std.testing.allocator;
const params_u = try QueryParams.parse(allocator, "u");
try std.testing.expect(params_u.use_imperial.?);
const params_m = try QueryParams.parse(allocator, "m");
try std.testing.expect(!params_m.use_imperial.?);
const params_lang = try QueryParams.parse(allocator, "lang=us");
defer allocator.free(params_lang.lang.?);
try std.testing.expectEqualStrings("us", params_lang.lang.?);
}

View file

@ -3,7 +3,7 @@ const types = @import("../weather/types.zig");
const Moon = @import("../Moon.zig");
const utils = @import("utils.zig");
pub fn render(writer: *std.Io.Writer, weather: types.WeatherData, allocator: std.mem.Allocator) !void {
pub fn render(writer: *std.Io.Writer, weather: types.WeatherData) !void {
// Current conditions
try writer.print("# HELP temperature_feels_like_celsius Feels Like Temperature in Celsius\n", .{});
@ -62,8 +62,8 @@ pub fn render(writer: *std.Io.Writer, weather: types.WeatherData, allocator: std
// Forecast days
for (weather.forecast, 0..) |day, i| {
const forecast_label = try std.fmt.allocPrint(allocator, "{d}d", .{i});
defer allocator.free(forecast_label);
var buf: [16]u8 = undefined;
const forecast_label = try std.fmt.bufPrint(&buf, "{d}d", .{i});
try writer.print("uv_index{{forecast=\"{s}\"}} 0\n", .{forecast_label}); // Not in our data
@ -145,7 +145,7 @@ test "prometheus format includes required metrics" {
var output_buf: [8192]u8 = undefined;
var writer = std.Io.Writer.fixed(&output_buf);
try render(&writer, weather, allocator);
try render(&writer, weather);
const output = output_buf[0..writer.end];
@ -182,7 +182,7 @@ test "prometheus format has proper help comments" {
var output_buf: [4096]u8 = undefined;
var writer = std.Io.Writer.fixed(&output_buf);
try render(&writer, weather, allocator);
try render(&writer, weather);
const output = output_buf[0..writer.end];