Skip to content

Commit

Permalink
Merge pull request #85 from s12chung/read_korean
Browse files Browse the repository at this point in the history
Read korean and fix slices queries/responses
  • Loading branch information
s12chung authored Apr 9, 2024
2 parents a76b5a6 + 8bafd5f commit 55fbfc8
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 25 deletions.
8 changes: 8 additions & 0 deletions db/pkg/db/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ func (q *Queries) ClearAll(ctx context.Context) error {
return nil
}

// ClearAllTable clears data from a table
func (q *Queries) ClearAllTable(ctx context.Context, tableName string) error {
if _, err := q.db.ExecContext(ctx, fmt.Sprintf("DELETE FROM %v; ", tableName)); err != nil {
return err
}
return nil
}

var dbStorage storage.DBStorage

// SetDBStorage sets the storage.DBStorage used in model JSON marshall/unmarshall
Expand Down
3 changes: 3 additions & 0 deletions db/pkg/db/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ func (q *Queries) SourceStructuredIndex(ctx context.Context) ([]SourceStructured
return nil, err
}

if sources == nil {
return nil, nil
}
sourceStructureds := make([]SourceStructured, len(sources))
for i, source := range sources {
sourceStructureds[i] = source.ToSourceStructured()
Expand Down
34 changes: 34 additions & 0 deletions db/pkg/db/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,37 @@ func TestQueries_SourceUpdate(t *testing.T) {
testRecentTimestamps(t, source.UpdatedAt)
require.NotEqual(newSource.UpdatedAt, source.UpdatedAt)
}

func TestQueries_SourceStructuredIndex(t *testing.T) {
testName := "TestQueries_SourceStructuredIndex"

testCases := []struct {
name string
}{
{name: "basic"},
{name: "clear"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)

txQs := TxQsT(t, nil)
if tc.name == "clear" {
txQs = TxQsT(t, WriteOpts())
require.NoError(txQs.ClearAllTable(txQs.Ctx(), "sources"))
}

sourceStructureds, err := txQs.SourceStructuredIndex(txQs.Ctx())
require.NoError(err)

var staticCopy []SourceStructured
if len(sourceStructureds) > 0 {
staticCopy = make([]SourceStructured, len(sourceStructureds))
for i := range sourceStructureds {
staticCopy[i] = sourceStructureds[i].StaticCopy()
}
}
fixture.CompareReadOrUpdateJSON(t, path.Join(testName, tc.name), staticCopy)
})
}
}
233 changes: 233 additions & 0 deletions db/pkg/db/testdata/TestQueries_SourceStructuredIndex/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
[
{
"name": "split",
"reference": "split.txt",
"parts": [
{
"tokenized_texts": [
{
"text": "내가 가는 이길이",
"translation": "The road that I’m taking",
"tokens": [
{
"text": "",
"part_of_speech": "Pronoun",
"start_index": 0,
"length": 1
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 1,
"length": 1
},
{
"text": "",
"part_of_speech": "Verb",
"start_index": 3,
"length": 1
},
{
"text": "",
"part_of_speech": "Ending",
"start_index": 4,
"length": 1
},
{
"text": "이길",
"part_of_speech": "Noun",
"start_index": 6,
"length": 2
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 8,
"length": 1
}
]
},
{
"text": "어디로 가는지",
"translation": "Where it’s leading me to, where it’s taking me",
"tokens": [
{
"text": "어디",
"part_of_speech": "Pronoun",
"start_index": 0,
"length": 2
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 2,
"length": 1
},
{
"text": "",
"part_of_speech": "Verb",
"start_index": 4,
"length": 1
},
{
"text": "는지",
"part_of_speech": "Ending",
"start_index": 5,
"length": 2
}
]
}
]
}
],
"updated_at": "0001-01-01T00:00:00Z",
"created_at": "0001-01-01T00:00:00Z"
},
{
"name": "weave",
"reference": "weave.txt",
"parts": [
{
"media": {
"image_key": "testdb.SourcePartMediaImageKey.png"
},
"tokenized_texts": [
{
"text": "이것은 샘플 파일입니다. tmp/in.txt에 자신의 텍스트를 입력합니다.",
"translation": "This is a sample file. Put your own text at: tmp/in.txt.",
"tokens": [
{
"text": "이것",
"part_of_speech": "Pronoun",
"start_index": 0,
"length": 2
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 2,
"length": 1
},
{
"text": "샘플",
"part_of_speech": "Noun",
"start_index": 4,
"length": 2
},
{
"text": "파일",
"part_of_speech": "Noun",
"start_index": 7,
"length": 2
},
{
"text": "",
"part_of_speech": "Copula",
"start_index": 9,
"length": 1
},
{
"text": "ㅂ니다",
"part_of_speech": "Ending",
"start_index": 10,
"length": 3
},
{
"text": ".",
"part_of_speech": "Punctuation",
"start_index": 13,
"length": 1
},
{
"text": "tmp",
"part_of_speech": "OtherLanguage",
"start_index": 15,
"length": 3
},
{
"text": "/",
"part_of_speech": "Punctuation",
"start_index": 18,
"length": 1
},
{
"text": "in",
"part_of_speech": "OtherLanguage",
"start_index": 19,
"length": 2
},
{
"text": ".",
"part_of_speech": "Punctuation",
"start_index": 21,
"length": 1
},
{
"text": "txt",
"part_of_speech": "OtherLanguage",
"start_index": 22,
"length": 3
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 25,
"length": 1
},
{
"text": "자신",
"part_of_speech": "Noun",
"start_index": 27,
"length": 2
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 29,
"length": 1
},
{
"text": "텍스트",
"part_of_speech": "Noun",
"start_index": 31,
"length": 3
},
{
"text": "",
"part_of_speech": "Postposition",
"start_index": 34,
"length": 1
},
{
"text": "입력",
"part_of_speech": "Noun",
"start_index": 36,
"length": 2
},
{
"text": "",
"part_of_speech": "Suffix",
"start_index": 38,
"length": 1
},
{
"text": "ㅂ니다",
"part_of_speech": "Ending",
"start_index": 39,
"length": 3
},
{
"text": ".",
"part_of_speech": "Punctuation",
"start_index": 42,
"length": 1
}
]
}
]
}
],
"updated_at": "0001-01-01T00:00:00Z",
"created_at": "0001-01-01T00:00:00Z"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
31 changes: 27 additions & 4 deletions pkg/api/apihelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/s12chung/text2anki/db/pkg/db"
"github.com/s12chung/text2anki/db/pkg/db/testdb"
"github.com/s12chung/text2anki/pkg/api/config"
Expand All @@ -22,6 +24,28 @@ func joinPath(elem ...any) string {
return fmt.Sprintf(strings.Repeat("/%v", len(elem)), elem...)
}

func testIndex[T test.StaticCopyable[T]](t *testing.T, s txServer, testName, tableName string) {
testCases := []struct {
name string
}{
{name: "basic"},
{name: "clear"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)

txQs := testdb.TxQs(t, nil)
if tc.name == "clear" {
txQs = testdb.TxQs(t, db.WriteOpts())
require.NoError(txQs.ClearAllTable(txQs.Ctx(), tableName))
}
resp := test.HTTPDo(t, s.NewTxRequestWithMode(t, txQs, txReadOnly, http.MethodGet, "", nil))
testModelsResponse[T](t, resp, testName, tc.name, nil)
})
}
}

func testIndent(t *testing.T, resp test.Response, testName, name string) {
jsonBody := test.IndentJSON(t, resp.Body.Bytes())
fixture.CompareReadOrUpdate(t, fixtureFileName(testName, name), jsonBody)
Expand Down Expand Up @@ -53,13 +77,12 @@ type txServer struct {
}

func (s txServer) NewRequest(t *testing.T, method, path string, body io.Reader) *http.Request {
return s.newTxRequest(t, testdb.TxQs(t, nil), txReadOnly, method, path, body)
return s.NewTxRequestWithMode(t, testdb.TxQs(t, nil), txReadOnly, method, path, body)
}
func (s txServer) NewTxRequest(t *testing.T, tx db.TxQs, method, path string, body io.Reader) *http.Request {
return s.newTxRequest(t, tx, txWritable, method, path, body)
return s.NewTxRequestWithMode(t, tx, txWritable, method, path, body)
}

func (s txServer) newTxRequest(t *testing.T, tx db.TxQs, mode config.TxMode, method, path string, body io.Reader) *http.Request {
func (s txServer) NewTxRequestWithMode(t *testing.T, tx db.TxQs, mode config.TxMode, method, path string, body io.Reader) *http.Request {
req := s.Server.NewRequest(t, tx.Ctx(), method, path, body)
s.pool.SetTx(t, req, tx, mode)
return req
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {

// NotesIndex shows lists all the notes
func (rs Routes) NotesIndex(_ *http.Request, txQs db.TxQs) (any, *jhttp.HTTPError) {
return jhttp.ReturnModelOr500(func() (any, error) {
return jhttp.ReturnSliceOr500(func() ([]db.Note, error) {
return txQs.NotesIndex(txQs.Ctx())
})
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func init() {

func TestRoutes_NotesIndex(t *testing.T) {
testName := "TestRoutes_NotesIndex"
resp := test.HTTPDo(t, notesServer.NewRequest(t, http.MethodGet, "", nil))
testModelsResponse[db.Note](t, resp, testName, "", nil)
testIndex[db.Note](t, notesServer, testName, "notes")
}

func TestRoutes_NoteCreate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {

// SourcesIndex returns a list of sources
func (rs Routes) SourcesIndex(_ *http.Request, txQs db.TxQs) (any, *jhttp.HTTPError) {
return jhttp.ReturnModelOr500(func() (any, error) {
return jhttp.ReturnSliceOr500(func() ([]db.SourceStructured, error) {
return txQs.SourceStructuredIndex(txQs.Ctx())
})
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func createdSource(t *testing.T, txQs db.TxQs) db.Source {

func TestRoutes_SourcesIndex(t *testing.T) {
testName := "TestRoutes_SourcesIndex"
resp := test.HTTPDo(t, sourcesServer.NewRequest(t, http.MethodGet, "", nil))
testModelsResponse[db.SourceStructured](t, resp, testName, "", nil)
testIndex[db.SourceStructured](t, sourcesServer, testName, "sources")
}

func TestRoutes_SourceGet(t *testing.T) {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions pkg/api/testdata/TestRoutes_NotesIndex/clear_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading

0 comments on commit 55fbfc8

Please sign in to comment.