<commit_before>package model

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"testing"
	"time"

	"github.com/flimzy/diff"
	"github.com/flimzy/kivik"
	"github.com/flimzy/testy"
)

func TestDeckList(t *testing.T) {
	tests := []struct {
		name     string
		repo     *Repo
		expected []*Deck
		err      string
	}{
		{
			name: "not logged in",
			repo: &Repo{},
			err:  "not logged in",
		},
		{
			name: "reduce query fail",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`invalid json`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						err: errors.New("foo error"),
					},
				},
			},
			err: "foo error",
		},
		{
			name: "fleshen error",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`invalid json`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						rows: []*mockRows{{
							rows:   []string{"", ""},
							values: []string{"[234,6]", "[234,6]"},
							keys: []string{
								`["new",""]`,
								`["new","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
							},
						}},
					},
				},
			},
			err: "invalid character 'i' looking for beginning of value",
		},
		{
			name: "success",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`{"name":"Test Deck"}`),
							"deck-foo":                         mockRow(`{"name":"Foo"}`),
							"deck-bar":                         mockRow(`{"name":"Bar"}`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						options: []kivik.Options{
							{"group_level": 2},
							{"startkey": []interface{}{"old", "deck-Brm5eFOpF0553VTksh7hlySt6M8"}, "reduce": false},
							{"startkey": []interface{}{"old", "deck-foo"}, "reduce": false},
							{"startkey": []interface{}{"old", ""}, "reduce": false},
						},
						rows: []*mockRows{
							{
								rows: []string{"", "", "", "", "", "", "", "", "", ""},
								values: []string{
									"[384,6]", "[1911,76]", "[57,10]",
									"[234,6]", "[1811,56]", "[52,9]",
									"[100,0]", "[100,20]", "[5,1]",
									"[50,0]",
								},
								keys: []string{
									`["new",""]`,
									`["old",""]`,
									`["suspended",""]`,
									`["new","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["old","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["suspended","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["new","deck-foo"]`,
									`["old","deck-foo"]`,
									`["suspended","deck-foo"]`,
									`["new","deck-bar"]`,
								},
							},
							{rows: []string{"", "", "", ""}, values: []string{"{}", "{}", "{}", "{}"}},
							{rows: []string{"", ""}, values: []string{"{}", "{}"}},
							{rows: []string{"", "", "", "", "", ""}, values: []string{"{}", "{}", "{}", "{}", "{}", "{}"}},
						},
					},
				},
			},
			expected: []*Deck{
				{
					Name:           "All",
					TotalCards:     2352,
					DueCards:       6,
					LearningCards:  76,
					MatureCards:    1835,
					NewCards:       384,
					SuspendedCards: 57,
				},
				{
					Name:       "Bar",
					ID:         "deck-bar",
					TotalCards: 50,
					NewCards:   50,
				},
				{
					Name:           "Foo",
					ID:             "deck-foo",
					TotalCards:     205,
					DueCards:       2,
					LearningCards:  20,
					MatureCards:    80,
					NewCards:       100,
					SuspendedCards: 5,
				},
				{
					Name:           "Test Deck",
					ID:             "deck-Brm5eFOpF0553VTksh7hlySt6M8",
					TotalCards:     2097,
					DueCards:       4,
					LearningCards:  56,
					MatureCards:    1755,
					NewCards:       234,
					SuspendedCards: 52,
				},
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result, err := test.repo.DeckList(context.Background())
			var errMsg string
			if err != nil {
				errMsg = err.Error()
			}<commit_msg>Add a test<commit_after>package model

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"testing"
	"time"

	"github.com/flimzy/diff"
	"github.com/flimzy/kivik"
	"github.com/flimzy/testy"
)

func TestDeckList(t *testing.T) {
	tests := []struct {
		name     string
		repo     *Repo
		expected []*Deck
		err      string
	}{
		{
			name: "not logged in",
			repo: &Repo{},
			err:  "not logged in",
		},
		{
			name: "reduce query fail",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`invalid json`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						err: errors.New("foo error"),
					},
				},
			},
			err: "foo error",
		},
		{
			name: "fleshen error",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`invalid json`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						rows: []*mockRows{{
							rows:   []string{"", ""},
							values: []string{"[234,6]", "[234,6]"},
							keys: []string{
								`["new",""]`,
								`["new","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
							},
						}},
					},
				},
			},
			err: "invalid character 'i' looking for beginning of value",
		},
		{
			name: "success",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockQuerier{
						kivikDB: &mockMultiGetter{rows: map[string]kivikRow{
							"deck-Brm5eFOpF0553VTksh7hlySt6M8": mockRow(`{"name":"Test Deck"}`),
							"deck-foo":                         mockRow(`{"name":"Foo"}`),
							"deck-bar":                         mockRow(`{"name":"Bar"}`),
							UserDDocID:                         mockRow(fmt.Sprintf(`{"version":%d}`, UserDDocVersion)),
						}},
						options: []kivik.Options{
							{"group_level": 2},
							{"startkey": []interface{}{"old", "deck-Brm5eFOpF0553VTksh7hlySt6M8"}, "reduce": false},
							{"startkey": []interface{}{"old", "deck-foo"}, "reduce": false},
							{"startkey": []interface{}{"old", ""}, "reduce": false},
						},
						rows: []*mockRows{
							{
								rows: []string{"", "", "", "", "", "", "", "", "", ""},
								values: []string{
									"[384,6]", "[1911,76]", "[57,10]",
									"[234,6]", "[1811,56]", "[52,9]",
									"[100,0]", "[100,20]", "[5,1]",
									"[50,0]",
								},
								keys: []string{
									`["new",""]`,
									`["old",""]`,
									`["suspended",""]`,
									`["new","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["old","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["suspended","deck-Brm5eFOpF0553VTksh7hlySt6M8"]`,
									`["new","deck-foo"]`,
									`["old","deck-foo"]`,
									`["suspended","deck-foo"]`,
									`["new","deck-bar"]`,
								},
							},
							{rows: []string{"", "", "", ""}, values: []string{"{}", "{}", "{}", "{}"}},
							{rows: []string{"", ""}, values: []string{"{}", "{}"}},
							{rows: []string{"", "", "", "", "", ""}, values: []string{"{}", "{}", "{}", "{}", "{}", "{}"}},
						},
					},
				},
			},
			expected: []*Deck{
				{
					Name:           "All",
					TotalCards:     2352,
					DueCards:       6,
					LearningCards:  76,
					MatureCards:    1835,
					NewCards:       384,
					SuspendedCards: 57,
				},
				{
					Name:       "Bar",
					ID:         "deck-bar",
					TotalCards: 50,
					NewCards:   50,
				},
				{
					Name:           "Foo",
					ID:             "deck-foo",
					TotalCards:     205,
					DueCards:       2,
					LearningCards:  20,
					MatureCards:    80,
					NewCards:       100,
					SuspendedCards: 5,
				},
				{
					Name:           "Test Deck",
					ID:             "deck-Brm5eFOpF0553VTksh7hlySt6M8",
					TotalCards:     2097,
					DueCards:       4,
					LearningCards:  56,
					MatureCards:    1755,
					NewCards:       234,
					SuspendedCards: 52,
				},
			},
		},
		{
			name: "wrong DDoc version",
			repo: &Repo{
				user: "bob",
				local: &mockClient{
					db: &mockMultiGetter{rows: map[string]kivikRow{
						UserDDocID: mockRow(fmt.Sprintf(`{"version":%d}`, 0)),
					}},
				},
			},
			err: "current ddoc not found",
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result, err := test.repo.DeckList(context.Background())
			var errMsg string
			if err != nil {
				errMsg = err.Error()
			}
{'fraction_non_alphanumeric': 0.21468990493435944, 'fraction_numerical': 0.038592123132639204, 'mean_word_length': 2.094187675070028, 'pattern_counts': {'":': 37, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087132', 'n_tokens_mistral': 4715, 'n_tokens_neox': 3312, 'n_words': 539}