[add] tests, [add] refactor epub feat

This commit is contained in:
2023-10-23 20:18:16 -04:00
parent bf6ac96376
commit b5d5e4bd64
17 changed files with 429 additions and 353 deletions

View File

@@ -3,8 +3,6 @@ package graph
import (
"fmt"
"math"
"reichard.io/bbank/database"
)
type SVGGraphPoint struct {
@@ -28,12 +26,12 @@ type SVGBezierOpposedLine struct {
Angle int
}
func GetSVGGraphData(inputData []database.GetDailyReadStatsRow, svgWidth int, svgHeight int) SVGGraphData {
func GetSVGGraphData(inputData []int64, svgWidth int, svgHeight int) SVGGraphData {
// Derive Height
var maxHeight int = 0
for _, item := range inputData {
if int(item.MinutesRead) > maxHeight {
maxHeight = int(item.MinutesRead)
if int(item) > maxHeight {
maxHeight = int(item)
}
}
@@ -55,7 +53,7 @@ func GetSVGGraphData(inputData []database.GetDailyReadStatsRow, svgWidth int, sv
var maxBY int = 0
var minBX int = 0
for idx, item := range inputData {
itemSize := int(float32(item.MinutesRead) * sizeRatio)
itemSize := int(float32(item) * sizeRatio)
itemY := svgHeight - itemSize
lineX := (idx + 1) * blockOffset
barPoints = append(barPoints, SVGGraphPoint{

33
graph/graph_test.go Normal file
View File

@@ -0,0 +1,33 @@
package graph
import (
"testing"
)
func TestGetSVGGraphData(t *testing.T) {
inputPoints := []int64{10, 90, 50, 5, 10, 5, 70, 60, 50, 90}
svgData := GetSVGGraphData(inputPoints, 500, 100)
expect := "M 50,95 C63,95 80,50 100,50 C120,50 128,73 150,73 C172,73 180,98 200,98 C220,98 230,95 250,95 C270,95 279,98 300,98 C321,98 330,62 350,62 C370,62 380,67 400,67 C420,67 430,73 450,73 C470,73 489,50 500,50"
if svgData.BezierPath != expect {
t.Fatalf(`Expected: %v, Got: %v`, expect, svgData.BezierPath)
}
expect = "L 500,98 L 50,98 Z"
if svgData.BezierFill != expect {
t.Fatalf(`Expected: %v, Got: %v`, expect, svgData.BezierFill)
}
if svgData.Width != 500 {
t.Fatalf(`Expected: %v, Got: %v`, 500, svgData.Width)
}
if svgData.Height != 100 {
t.Fatalf(`Expected: %v, Got: %v`, 100, svgData.Height)
}
if svgData.Offset != 50 {
t.Fatalf(`Expected: %v, Got: %v`, 50, svgData.Offset)
}
}