Files
readeck/pkg/csvstruct/csvstruct_test.go
Olivier Meunier 2df5dd48c9 Refactored CSV import adapters
This adds a csvBaseAdapter that takes care of the basic process of
reading CSV entries from one or several files (as in the pocket export
file where we read all .csv files from its zip file).

csvAdapter, readwiseAdapter and pocketFileAdapter are now all based
on csvBaseAdapter with their own types and functions.

Tests were refactored to make them easier to write and to cover the
ImportWorker process.

This adds a pkg/csvstruct package that can generate a scanner to read
CSV lines into a struct.
2025-06-26 12:11:10 +02:00

75 lines
1.6 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2020 Artyom Pervukhin
//
// SPDX-License-Identifier: MIT
package csvstruct
import (
"strings"
"testing"
)
var sink testDst
type testDst struct {
Name string `csv:"name"`
Age uint8 `csv:"age"`
Ignore bool
}
func TestScanner(t *testing.T) {
var dst testDst
scan, err := NewScanner([]string{"name", "age"}, &dst)
if err != nil {
t.Fatal(err)
}
if dst != (testDst{}) {
t.Fatalf("NewScanner modified destination: %+v", dst)
}
if err := scan([]string{"John", "42"}, &dst); err != nil {
t.Fatal(err)
}
want := testDst{Name: "John", Age: 42}
if dst != want {
t.Fatalf("got: %+v, want: %+v", dst, want)
}
}
func TestScanner_DifferentType(t *testing.T) {
scan, err := NewScanner([]string{"name", "age"}, &testDst{})
if err != nil {
t.Fatal(err)
}
type testDst2 struct {
Name string `csv:"name"`
Age uint8 `csv:"age"`
Ignore bool
_ byte // <- differs from testDst type
}
var dst testDst2
defer func() {
p := recover()
if p == nil {
t.Fatal("using different types in NewScanner and Scanner should have panicked, but it's not")
}
if s, ok := p.(string); !ok || !strings.Contains(s, "different type") {
t.Fatalf("unexpected panic value: %v", p)
}
}()
t.Fatalf("Scanner returned on different types: %v", scan([]string{"John", "42"}, &dst))
}
func BenchmarkScanner(b *testing.B) {
scan, err := NewScanner([]string{"name", "age"}, &sink)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
if err := scan([]string{"John", "42"}, &sink); err != nil {
b.Fatal(err)
}
}
}