//nolint:lll package xmlw_test import ( "bytes" "context" "os" "testing" "github.com/stretchr/testify/require" "github.com/neilotoole/sq/cli/output" "github.com/neilotoole/sq/cli/output/xmlw" "github.com/neilotoole/sq/testh" "github.com/neilotoole/sq/testh/sakila" "github.com/neilotoole/sq/testh/testsrc" ) func TestRecordWriter_Actor(t *testing.T) { const ( want0 = ` ` want3Pretty = ` 1 PENELOPE GUINESS 2020-06-11T02:50:54Z 2 NICK WAHLBERG 2020-06-11T02:50:54Z 3 ED CHASE 2020-06-11T02:50:54Z ` want3NoPretty = ` 1PENELOPEGUINESS2020-06-11T02:50:54Z 2NICKWAHLBERG2020-06-11T02:50:54Z 3EDCHASE2020-06-11T02:50:54Z ` ) testCases := []struct { name string color bool pretty bool numRecs int want string }{ {name: "actor_0_pretty", color: false, pretty: true, numRecs: 0, want: want0}, {name: "actor_0_no_pretty", color: false, pretty: false, numRecs: 0, want: want0}, {name: "actor_3_pretty", color: false, pretty: true, numRecs: 3, want: want3Pretty}, {name: "actor_3_no_pretty", color: false, pretty: false, numRecs: 3, want: want3NoPretty}, } for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { ctx := context.Background() pr := output.NewPrinting() pr.EnableColor(tc.color) pr.Compact = !tc.pretty recMeta, recs := testh.RecordsFromTbl(t, sakila.SL3, sakila.TblActor) recs = recs[0:tc.numRecs] buf := &bytes.Buffer{} w := xmlw.NewRecordWriter(buf, pr) require.NoError(t, w.Open(ctx, recMeta)) require.NoError(t, w.WriteRecords(ctx, recs)) require.NoError(t, w.Close(ctx)) require.Equal(t, tc.want, buf.String()) }) } } func TestRecordWriter_TblTypes(t *testing.T) { ctx := context.Background() pr := output.NewPrinting() pr.EnableColor(false) recMeta, recs := testh.RecordsFromTbl(t, testsrc.MiscDB, testsrc.TblTypes) buf := &bytes.Buffer{} w := xmlw.NewRecordWriter(buf, pr) require.NoError(t, w.Open(ctx, recMeta)) require.NoError(t, w.WriteRecords(ctx, recs)) require.NoError(t, w.Close(ctx)) want, err := os.ReadFile("testdata/tbl_types.xml") require.NoError(t, err) require.Equal(t, string(want), buf.String()) }