-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
111 lines (98 loc) · 2.1 KB
/
update_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// update_test.go
//
// Copyright (c) 2023 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package main
import (
"encoding/hex"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/go-openapi/strfmt"
"github.com/zeebo/blake3"
)
func Test_fileHash(t *testing.T) {
target := "README.md"
f, err := os.Open(target)
if err != nil {
t.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
t.Fatal(err)
}
}()
h := blake3.New()
if _, err = io.Copy(h, f); err != nil {
t.Fatal(err)
}
expect := hex.EncodeToString(h.Sum(nil))
res, err := fileHash(target)
if err != nil {
t.Fatal(err)
}
if res != expect {
t.Errorf("expect %v, got %v", expect, res)
}
}
func Test_modelVersionList(t *testing.T) {
list := modelVersionList{
{
ModelID: 3,
CreatedAt: strfmt.DateTime(time.Now().Add(3 * time.Hour)),
},
{
ModelID: 1,
CreatedAt: strfmt.DateTime(time.Now().Add(1 * time.Hour)),
},
{
ModelID: 2,
CreatedAt: strfmt.DateTime(time.Now().Add(2 * time.Hour)),
},
}
t.Run("Len", func(t *testing.T) {
res := list.Len()
if res != len(list) {
t.Errorf("expect %v, got %v", len(list), res)
}
})
t.Run("Less", func(t *testing.T) {
cases := []struct {
i int
j int
expect bool
}{
{i: 0, j: 0, expect: false},
{i: 0, j: 1, expect: false},
{i: 0, j: 2, expect: false},
{i: 1, j: 0, expect: true},
{i: 1, j: 1, expect: false},
{i: 1, j: 2, expect: true},
{i: 2, j: 0, expect: true},
{i: 2, j: 1, expect: false},
{i: 2, j: 2, expect: false},
}
for _, c := range cases {
t.Run(fmt.Sprintf("i:%v, j:%v", c.i, c.j), func(t *testing.T) {
if res := list.Less(c.i, c.j); res != c.expect {
t.Errorf("expect %v, got %v", c.expect, res)
}
})
}
})
t.Run("Swap", func(t *testing.T) {
l := make(modelVersionList, len(list))
for i, v := range list {
l[i] = v
}
l.Swap(0, 2)
if l[0].ModelID != list[2].ModelID || l[1].ModelID != list[1].ModelID || l[2].ModelID != list[0].ModelID {
t.Error("swapped list doesn't match")
}
})
}