github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/index/index_test.go (about) 1 // Copyright 2020 Readium Foundation. All rights reserved. 2 // Use of this source code is governed by a BSD-style license 3 // that can be found in the LICENSE file exposed on Github (readium) in the project repository. 4 5 package index 6 7 import ( 8 "database/sql" 9 "testing" 10 11 _ "github.com/mattn/go-sqlite3" 12 13 "github.com/readium/readium-lcp-server/config" 14 ) 15 16 func TestCRUD(t *testing.T) { 17 18 config.Config.LcpServer.Database = "sqlite3://:memory:" 19 driver, cnxn := config.GetDatabase(config.Config.LcpServer.Database) 20 db, err := sql.Open(driver, cnxn) 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 err = db.Ping() 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 idx, err := Open(db) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 c := Content{ID: "test20", 36 EncryptionKey: []byte("1234"), 37 Location: "test.epub", 38 Length: 1000, 39 Sha256: "xxxx", 40 Type: "epub"} 41 42 err = idx.Add(c) 43 if err != nil { 44 t.Fatal(err) 45 } 46 cbis, err := idx.Get(c.ID) 47 if err != nil { 48 t.Fatal(err) 49 } 50 if c.ID != cbis.ID { 51 t.Fatal("Failed to Get back the record") 52 } 53 54 c.Location = "test.epub" 55 err = idx.Update(c) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 c2 := Content{ID: "test21", 61 EncryptionKey: []byte("1234"), 62 Location: "test2.epub", 63 Length: 2000, 64 Sha256: "xxxx", 65 Type: "epub"} 66 67 err = idx.Add(c2) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 fn := idx.List() 73 contents := make([]Content, 0) 74 75 for it, err := fn(); err == nil; it, err = fn() { 76 contents = append(contents, it) 77 } 78 if len(contents) != 2 { 79 t.Fatal("Failed to List two rows") 80 } 81 82 }