File: meta_test.go

package info (click to toggle)
golang-github-ncw-swift 1.0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 412 kB
  • sloc: python: 29; sh: 18; makefile: 4
file content (213 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download | duplicates (7)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Tests for swift metadata
package swift

import (
	"testing"
	"time"
)

func TestHeadersToMetadata(t *testing.T) {
}

func TestHeadersToAccountMetadata(t *testing.T) {
}

func TestHeadersToContainerMetadata(t *testing.T) {
}

func TestHeadersToObjectMetadata(t *testing.T) {
}

func TestMetadataToHeaders(t *testing.T) {
}

func TestMetadataToAccountHeaders(t *testing.T) {
}

func TestMetadataToContainerHeaders(t *testing.T) {
}

func TestMetadataToObjectHeaders(t *testing.T) {
}

func TestNsToFloatString(t *testing.T) {
	for _, d := range []struct {
		ns int64
		fs string
	}{
		{0, "0"},
		{1, "0.000000001"},
		{1000, "0.000001"},
		{1000000, "0.001"},
		{100000000, "0.1"},
		{1000000000, "1"},
		{10000000000, "10"},
		{12345678912, "12.345678912"},
		{12345678910, "12.34567891"},
		{12345678900, "12.3456789"},
		{12345678000, "12.345678"},
		{12345670000, "12.34567"},
		{12345600000, "12.3456"},
		{12345000000, "12.345"},
		{12340000000, "12.34"},
		{12300000000, "12.3"},
		{12000000000, "12"},
		{10000000000, "10"},
		{1347717491123123123, "1347717491.123123123"},
	} {
		if nsToFloatString(d.ns) != d.fs {
			t.Error("Failed", d.ns, "!=", d.fs)
		}
		if d.ns > 0 && nsToFloatString(-d.ns) != "-"+d.fs {
			t.Error("Failed on negative", d.ns, "!=", d.fs)
		}
	}
}

func TestFloatStringToNs(t *testing.T) {
	for _, d := range []struct {
		ns int64
		fs string
	}{
		{0, "0"},
		{0, "0."},
		{0, ".0"},
		{0, "0.0"},
		{0, "0.0000000001"},
		{1, "0.000000001"},
		{1000, "0.000001"},
		{1000000, "0.001"},
		{100000000, "0.1"},
		{100000000, "0.10"},
		{100000000, "0.1000000001"},
		{1000000000, "1"},
		{1000000000, "1."},
		{1000000000, "1.0"},
		{10000000000, "10"},
		{12345678912, "12.345678912"},
		{12345678912, "12.3456789129"},
		{12345678912, "12.34567891299"},
		{12345678910, "12.34567891"},
		{12345678900, "12.3456789"},
		{12345678000, "12.345678"},
		{12345670000, "12.34567"},
		{12345600000, "12.3456"},
		{12345000000, "12.345"},
		{12340000000, "12.34"},
		{12300000000, "12.3"},
		{12000000000, "12"},
		{10000000000, "10"},
		// This is a typical value which has more bits in than a float64
		{1347717491123123123, "1347717491.123123123"},
	} {
		ns, err := floatStringToNs(d.fs)
		if err != nil {
			t.Error("Failed conversion", err)
		}
		if ns != d.ns {
			t.Error("Failed", d.fs, "!=", d.ns, "was", ns)
		}
		if d.ns > 0 {
			ns, err := floatStringToNs("-" + d.fs)
			if err != nil {
				t.Error("Failed conversion", err)
			}
			if ns != -d.ns {
				t.Error("Failed on negative", -d.ns, "!=", "-"+d.fs)
			}
		}
	}

	// These are expected to produce errors
	for _, fs := range []string{
		"",
		" 1",
		"- 1",
		"- 1",
		"1.-1",
		"1.0.0",
		"1x0",
	} {
		ns, err := floatStringToNs(fs)
		if err == nil {
			t.Error("Didn't produce expected error", fs, ns)
		}
	}

}

func TestGetModTime(t *testing.T) {
	for _, d := range []struct {
		ns string
		t  string
	}{
		{"1354040105", "2012-11-27T18:15:05Z"},
		{"1354040105.", "2012-11-27T18:15:05Z"},
		{"1354040105.0", "2012-11-27T18:15:05Z"},
		{"1354040105.000000000000", "2012-11-27T18:15:05Z"},
		{"1354040105.123", "2012-11-27T18:15:05.123Z"},
		{"1354040105.123456", "2012-11-27T18:15:05.123456Z"},
		{"1354040105.123456789", "2012-11-27T18:15:05.123456789Z"},
		{"1354040105.123456789123", "2012-11-27T18:15:05.123456789Z"},
		{"0", "1970-01-01T00:00:00.000000000Z"},
	} {
		expected, err := time.Parse(time.RFC3339, d.t)
		if err != nil {
			t.Error("Bad test", err)
		}
		m := Metadata{"mtime": d.ns}
		actual, err := m.GetModTime()
		if err != nil {
			t.Error("Parse error", err)
		}
		if !actual.Equal(expected) {
			t.Error("Expecting", expected, expected.UnixNano(), "got", actual, actual.UnixNano())
		}
	}
	for _, ns := range []string{
		"EMPTY",
		"",
		" 1",
		"- 1",
		"- 1",
		"1.-1",
		"1.0.0",
		"1x0",
	} {
		m := Metadata{}
		if ns != "EMPTY" {
			m["mtime"] = ns
		}
		actual, err := m.GetModTime()
		if err == nil {
			t.Error("Expected error not produced")
		}
		if !actual.IsZero() {
			t.Error("Expected output to be zero")
		}
	}
}

func TestSetModTime(t *testing.T) {
	for _, d := range []struct {
		ns string
		t  string
	}{
		{"1354040105", "2012-11-27T18:15:05Z"},
		{"1354040105", "2012-11-27T18:15:05.000000Z"},
		{"1354040105.123", "2012-11-27T18:15:05.123Z"},
		{"1354040105.123456", "2012-11-27T18:15:05.123456Z"},
		{"1354040105.123456789", "2012-11-27T18:15:05.123456789Z"},
		{"0", "1970-01-01T00:00:00.000000000Z"},
	} {
		time, err := time.Parse(time.RFC3339, d.t)
		if err != nil {
			t.Error("Bad test", err)
		}
		m := Metadata{}
		m.SetModTime(time)
		if m["mtime"] != d.ns {
			t.Error("mtime wrong", m, "should be", d.ns)
		}
	}
}