forked from viant/bqwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_test.go
142 lines (118 loc) · 3.92 KB
/
meta_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package bqwt
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestMeta_Update(t *testing.T) {
inThePast := time.Now().Add(-time.Hour)
now := time.Now()
inTheFuture := time.Now().Add(time.Hour)
from := inThePast.UnixNano() / int64(time.Millisecond)
to := now.UnixNano() / int64(time.Millisecond)
toFuture := inTheFuture.UnixNano() / int64(time.Millisecond)
useCase2Meta := NewMeta("gs://bucket/meta.json", "dataset")
useCase2Meta.Update(NewTableInfo("p", "dataset", "tableX", inThePast, now), now)
var useCases = []struct {
description string
tableInfo *TableInfo
meta *Meta
expected *WindowedTable
}{
{
description: "new table",
meta: NewMeta("gs://bucket/meta.json", "dataset"),
tableInfo: NewTableInfo("p", "dataset", "tableX", inThePast, now),
expected: &WindowedTable{
ID: "dataset.tableX",
ProjectID: "p",
Name: "tableX",
Dataset: "dataset",
Expression: fmt.Sprintf("[dataset.tableX@%v-%v]", from, to),
AbsoluteExpression: fmt.Sprintf("[p:dataset.tableX@%v-%v]", from, to),
Window: &TimeWindow{
From: inThePast,
To: now,
},
LastChanged: now,
Changed: true,
},
},
{
description: "existing table no update",
meta: useCase2Meta,
tableInfo: NewTableInfo("p", "dataset", "tableX", inThePast, now),
expected: &WindowedTable{
ID: "dataset.tableX",
ProjectID: "p",
Name: "tableX",
Dataset: "dataset",
Expression: fmt.Sprintf("[dataset.tableX@%v-%v]", from, to),
AbsoluteExpression: fmt.Sprintf("[p:dataset.tableX@%v-%v]", from, to),
Window: &TimeWindow{
From: inThePast,
To: now,
},
LastChanged: now,
Changed: false,
},
},
{
description: "existing table changing update",
meta: useCase2Meta,
tableInfo: NewTableInfo("p", "dataset", "tableX", now, inTheFuture),
expected: &WindowedTable{
ID: "dataset.tableX",
ProjectID: "p",
Name: "tableX",
Dataset: "dataset",
Expression: fmt.Sprintf("[dataset.tableX@%v-%v]", to+1, toFuture),
AbsoluteExpression: fmt.Sprintf("[p:dataset.tableX@%v-%v]", to+1, toFuture),
Window: &TimeWindow{
From: now.Add(time.Millisecond),
To: inTheFuture,
},
LastChanged: now,
Changed: true,
},
},
}
for _, useCase := range useCases {
updated := useCase.meta.Update(useCase.tableInfo, now)
assert.Equal(t, useCase.expected, updated, useCase.description)
assert.True(t, updated.Expression != "", useCase.description)
}
}
func TestMeta_Prune(t *testing.T) {
agesAgo := time.Now().Add(-time.Hour * 120)
inThePast := time.Now().Add(-24 * time.Hour)
now := time.Now()
meta := NewMeta("gs://bucket/meta.json", "dataset")
meta.Update(NewTableInfo("p", "dataset", "tableX", inThePast, now), now)
meta.Update(NewTableInfo("p", "dataset", "tableY", agesAgo, inThePast), inThePast)
{ //no prune
meta.Prune(0, now)
assert.Equal(t, 2, len(meta.Tables))
}
{ //no prune
meta.Prune(time.Hour, now)
assert.Equal(t, 1, len(meta.Tables))
assert.Equal(t, "tableX", meta.Tables[0].Name)
}
}
func TestMeta_SortLastModifiedDesc(t *testing.T) {
agesAgo := time.Now().Add(-time.Hour * 120)
t0 := time.Now().Add(-3 * time.Hour)
t1 := time.Now().Add(-2 * time.Hour)
t2 := time.Now().Add(-1 * time.Hour)
t3 := time.Now()
meta := NewMeta("gs://bucket/meta.json", "dataset")
meta.Update(NewTableInfo("p", "dataset", "table2", agesAgo, t2), t2)
meta.Update(NewTableInfo("p", "dataset", "table0", agesAgo, t0), t0)
meta.Update(NewTableInfo("p", "dataset", "table3", agesAgo, t3), t3)
meta.Update(NewTableInfo("p", "dataset", "table1", agesAgo, t1), t1)
assert.Equal(t, meta.Tables[0].Name, "table2")
meta.SortLastModifiedDesc()
assert.Equal(t, meta.Tables[0].Name, "table3")
}