lure/internal/db/db_test.go

251 lines
5.6 KiB
Go
Raw Normal View History

/*
* LURE - Linux User REpository
2023-09-20 22:38:22 +00:00
* Copyright (C) 2023 Elara Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-12-27 19:11:19 +00:00
package db_test
import (
"reflect"
"strings"
"testing"
"github.com/jmoiron/sqlx"
2023-09-22 22:21:34 +00:00
"go.elara.ws/lure/internal/db"
2022-12-27 19:11:19 +00:00
)
var testPkg = db.Package{
2023-01-11 23:26:12 +00:00
Name: "test",
Version: "0.0.1",
Release: 1,
Epoch: 2,
Description: db.NewJSON(map[string]string{
"en": "Test package",
"ru": "Проверочный пакет",
}),
Homepage: db.NewJSON(map[string]string{
2023-09-19 21:28:05 +00:00
"en": "https://lure.elara.ws/",
2023-01-11 23:26:12 +00:00
}),
Maintainer: db.NewJSON(map[string]string{
2023-09-19 21:28:05 +00:00
"en": "Elara Musayelyan <elara@elara.ws>",
"ru": "Элара Мусаелян <arsen@arsenm.dev>",
2023-01-11 23:26:12 +00:00
}),
2022-12-27 19:11:19 +00:00
Architectures: db.NewJSON([]string{"arm64", "amd64"}),
Licenses: db.NewJSON([]string{"GPL-3.0-or-later"}),
Provides: db.NewJSON([]string{"test"}),
Conflicts: db.NewJSON([]string{"test"}),
Replaces: db.NewJSON([]string{"test-old"}),
Depends: db.NewJSON(map[string][]string{
"": {"sudo"},
}),
BuildDepends: db.NewJSON(map[string][]string{
"": {"golang"},
"arch": {"go"},
}),
Repository: "default",
}
func TestInit(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
2023-09-19 21:28:05 +00:00
_, err = db.DB().Exec("SELECT * FROM pkgs")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
ver, ok := db.GetVersion()
if !ok {
t.Errorf("Expected version to be present")
} else if ver != db.CurrentVersion {
t.Errorf("Expected version %d, got %d", db.CurrentVersion, ver)
}
2022-12-27 19:11:19 +00:00
}
func TestInsertPackage(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(testPkg)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
dbPkg := db.Package{}
2023-09-19 21:28:05 +00:00
err = sqlx.Get(db.DB(), &dbPkg, "SELECT * FROM pkgs WHERE name = 'test' AND repository = 'default'")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
if !reflect.DeepEqual(testPkg, dbPkg) {
t.Errorf("Expected test package to be the same as database package")
}
}
func TestGetPkgs(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
x1 := testPkg
x1.Name = "x1"
x2 := testPkg
x2.Name = "x2"
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x1)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x2)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
result, err := db.GetPkgs("name LIKE 'x%'")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
for result.Next() {
var dbPkg db.Package
err = result.StructScan(&dbPkg)
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if !strings.HasPrefix(dbPkg.Name, "x") {
t.Errorf("Expected package name to start with 'x', got %s", dbPkg.Name)
}
}
}
func TestGetPkg(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
x1 := testPkg
x1.Name = "x1"
x2 := testPkg
x2.Name = "x2"
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x1)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x2)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
pkg, err := db.GetPkg("name LIKE 'x%' ORDER BY name")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
if pkg.Name != "x1" {
t.Errorf("Expected x1 package, got %s", pkg.Name)
}
if !reflect.DeepEqual(*pkg, x1) {
t.Errorf("Expected x1 to be %v, got %v", x1, *pkg)
}
}
func TestDeletePkgs(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
x1 := testPkg
x1.Name = "x1"
x2 := testPkg
x2.Name = "x2"
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x1)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x2)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = db.DeletePkgs("name = 'x1'")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
var dbPkg db.Package
2023-09-19 21:28:05 +00:00
err = db.DB().Get(&dbPkg, "SELECT * FROM pkgs WHERE name LIKE 'x%' ORDER BY name LIMIT 1;")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if dbPkg.Name != "x2" {
t.Errorf("Expected x2 package, got %s", dbPkg.Name)
}
}
func TestJsonArrayContains(t *testing.T) {
2023-09-19 21:28:05 +00:00
_, err := db.Open(":memory:")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
defer db.Close()
2022-12-27 19:11:19 +00:00
x1 := testPkg
x1.Name = "x1"
x2 := testPkg
x2.Name = "x2"
x2.Provides.Val = append(x2.Provides.Val, "x")
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x1)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
2023-09-19 21:28:05 +00:00
err = db.InsertPackage(x2)
2022-12-27 19:11:19 +00:00
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
var dbPkg db.Package
2023-09-19 21:28:05 +00:00
err = db.DB().Get(&dbPkg, "SELECT * FROM pkgs WHERE json_array_contains(provides, 'x');")
2022-12-27 19:11:19 +00:00
if err != nil {
t.Fatalf("Expected no error, got %s", err)
}
if dbPkg.Name != "x2" {
t.Errorf("Expected x2 package, got %s", dbPkg.Name)
}
}