github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/transactions/transactions_test.go (about) 1 // Copyright 2017 European Digital Reading Lab. All rights reserved. 2 // Licensed to the Readium Foundation under one or more contributor license agreements. 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file exposed on Github (readium) in the project repository. 5 6 package transactions 7 8 import ( 9 "database/sql" 10 "testing" 11 "time" 12 13 _ "github.com/mattn/go-sqlite3" 14 15 "github.com/readium/readium-lcp-server/config" 16 "github.com/readium/readium-lcp-server/status" 17 ) 18 19 func TestCRUD(t *testing.T) { 20 21 config.Config.LsdServer.Database = "sqlite3://:memory:" 22 driver, cnxn := config.GetDatabase(config.Config.LsdServer.Database) 23 db, err := sql.Open(driver, cnxn) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 evt, err := Open(db) 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 timestamp := time.Now().UTC().Truncate(time.Second) 34 35 // add 36 e := Event{DeviceName: "testdevice", Timestamp: timestamp, DeviceId: "deviceid1", LicenseStatusFk: 1} 37 err = evt.Add(e, status.STATUS_REVOKED_INT) 38 if err != nil { 39 t.Error(err) 40 } 41 42 // get 43 event, err := evt.Get(1) 44 if err != nil { 45 t.Error(err) 46 } 47 if event.ID != 1 { 48 t.Errorf("Failed getting item 1, got %d instead", event.ID) 49 } 50 if event.Type != "revoke" { 51 t.Errorf("Failed getting type revoke, got %s instead", event.Type) 52 } 53 54 // get by license status id 55 fn := evt.GetByLicenseStatusId(1) 56 eventList := make([]Event, 0) 57 for it, err := fn(); err == nil; it, err = fn() { 58 eventList = append(eventList, it) 59 } 60 if len(eventList) != 1 { 61 t.Errorf("Failed getting a list with one item, got %d instead", len(eventList)) 62 } 63 if eventList[0].Type != "revoke" { 64 t.Errorf("Failed getting type revoke, got %s instead", eventList[0].Type) 65 } 66 67 // add more 68 timestamp = time.Now().UTC().Truncate(time.Second) 69 e = Event{DeviceName: "testdevice", Timestamp: timestamp, DeviceId: "deviceid2", LicenseStatusFk: 1} 70 err = evt.Add(e, status.STATUS_ACTIVE_INT) 71 if err != nil { 72 t.Error(err) 73 } 74 e = Event{DeviceName: "testdevice", Timestamp: timestamp, DeviceId: "deviceid3", LicenseStatusFk: 1} 75 err = evt.Add(e, status.STATUS_ACTIVE_INT) 76 if err != nil { 77 t.Error(err) 78 } 79 80 // list registered devices 81 fnr := evt.ListRegisteredDevices(1) 82 deviceList := make([]Device, 0) 83 for it, err := fnr(); err == nil; it, err = fnr() { 84 deviceList = append(deviceList, it) 85 } 86 if len(deviceList) != 2 { 87 t.Errorf("Failed getting a list with two items, got %d instead", len(eventList)) 88 } 89 if deviceList[0].DeviceId != "deviceid2" { 90 t.Errorf("Failed getting a proper deviceid, got %s instead", deviceList[0].DeviceId) 91 } 92 93 // check device status 94 dstat, err := evt.CheckDeviceStatus(1, "deviceid2") 95 if err != nil { 96 t.Error(err) 97 } 98 if dstat != "register" { 99 t.Errorf("Failed getting a proper device status, got %s instead", dstat) 100 } 101 102 dstat, err = evt.CheckDeviceStatus(1, "deviceid1") 103 if err != nil { 104 t.Error(err) 105 } 106 if dstat != "revoke" { 107 t.Errorf("Failed getting a proper device status, got %s instead", dstat) 108 } 109 110 }