2019-01-31 13:53:06 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
meta_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
meta_test.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
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.
|
|
|
|
|
|
|
|
It 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 revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-01-31 14:00:08 +03:00
|
|
|
package meta
|
2019-01-31 13:53:06 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-05 16:27:53 +03:00
|
|
|
"encoding/binary"
|
2019-01-31 13:53:06 +03:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tstKey1 = "loc"
|
|
|
|
tstData1 = "a,b,c"
|
|
|
|
tstKey2 = "ts"
|
|
|
|
tstData2 = "12345678"
|
|
|
|
tstData3 = "d,e,f"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestAddAndGet ensures that we can add metadata and then successfully get it.
|
|
|
|
func TestAddAndGet(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey2, tstData2)
|
2019-02-08 02:55:57 +03:00
|
|
|
if data, ok := meta.Get(tstKey1); !ok {
|
|
|
|
t.Errorf("Could not get data for key: %v\n", tstKey1)
|
2019-01-31 13:53:06 +03:00
|
|
|
if data != tstData1 {
|
2019-02-07 01:31:40 +03:00
|
|
|
t.Error("Did not get expected data")
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 02:55:57 +03:00
|
|
|
if data, ok := meta.Get(tstKey2); !ok {
|
|
|
|
t.Errorf("Could not get data for key: %v", tstKey2)
|
2019-01-31 13:53:06 +03:00
|
|
|
if data != tstData2 {
|
2019-02-07 01:31:40 +03:00
|
|
|
t.Error("Did not get expected data")
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUpdate checks that we can use Meta.Add to actually update metadata
|
|
|
|
// if it already exists in the Meta map.
|
|
|
|
func TestUpdate(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey1, tstData3)
|
|
|
|
|
2019-02-08 02:55:57 +03:00
|
|
|
if data, ok := meta.Get(tstKey1); !ok {
|
|
|
|
t.Errorf("Could not get data for key: %v\n", tstKey1)
|
2019-01-31 13:53:06 +03:00
|
|
|
if data != tstData2 {
|
2019-02-07 01:31:40 +03:00
|
|
|
t.Error(`Data did not correctly update for key "loc"`)
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAll ensures we can get a correct map using Meta.All() after adding some data
|
|
|
|
func TestAll(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
tstMap := map[string]string{
|
|
|
|
tstKey1: tstData1,
|
|
|
|
tstKey2: tstData2,
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey2, tstData2)
|
|
|
|
metaMap := meta.All()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(metaMap, tstMap) {
|
|
|
|
t.Errorf("Map not correct. Got: %v, want: %v", metaMap, tstMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGetAbsentKey ensures that we get the expected error when we try to get with
|
|
|
|
// key that does not yet exist in the Meta map.
|
|
|
|
func TestGetAbsentKey(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
|
2019-02-08 02:55:57 +03:00
|
|
|
if _, ok := meta.Get(tstKey1); ok {
|
|
|
|
t.Error("Get for absent key incorrectly returned'ok'")
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDelete ensures we can remove a data entry in the Meta map.
|
|
|
|
func TestDelete(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
2019-02-08 03:26:19 +03:00
|
|
|
meta.Delete(tstKey1)
|
2019-02-08 02:55:57 +03:00
|
|
|
if _, ok := meta.Get(tstKey1); ok {
|
|
|
|
t.Error("Get incorrectly returned okay for absent key")
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestEncode checks that we're getting the correct byte slice from Meta.Encode().
|
|
|
|
func TestEncode(t *testing.T) {
|
2019-01-31 14:00:08 +03:00
|
|
|
meta := New()
|
2019-01-31 13:53:06 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey2, tstData2)
|
|
|
|
|
|
|
|
dataLen := len(tstKey1+tstData1+tstKey2+tstData2) + 3
|
2019-02-05 15:48:05 +03:00
|
|
|
header := [4]byte{
|
2019-01-31 13:53:06 +03:00
|
|
|
0x00,
|
|
|
|
0x10,
|
|
|
|
}
|
2019-02-05 16:27:53 +03:00
|
|
|
binary.BigEndian.PutUint16(header[2:4], uint16(dataLen))
|
2019-02-05 15:48:05 +03:00
|
|
|
expectedOut := append(header[:], []byte(
|
2019-01-31 13:53:06 +03:00
|
|
|
tstKey1+"="+tstData1+"\t"+
|
|
|
|
tstKey2+"="+tstData2)...)
|
|
|
|
|
|
|
|
got := meta.Encode()
|
|
|
|
if !bytes.Equal(expectedOut, got) {
|
2019-02-05 16:27:53 +03:00
|
|
|
t.Errorf("Did not get expected out. \nGot : %v, \nwant: %v\n", got, expectedOut)
|
2019-02-01 01:57:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:31:17 +03:00
|
|
|
// TestReadFrom checks that we can correctly obtain a value for a partiular key
|
|
|
|
// from a string of metadata using the ReadFrom func.
|
2019-02-09 04:46:57 +03:00
|
|
|
func TestGetFrom(t *testing.T) {
|
2019-02-04 15:18:51 +03:00
|
|
|
tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...)
|
2019-02-01 01:57:43 +03:00
|
|
|
|
2019-02-07 03:12:01 +03:00
|
|
|
tests := []struct {
|
|
|
|
key string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"loc",
|
|
|
|
"a,b,c",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ts",
|
|
|
|
"12345",
|
|
|
|
},
|
2019-02-01 01:57:43 +03:00
|
|
|
}
|
2019-02-07 03:12:01 +03:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-02-09 04:44:32 +03:00
|
|
|
got, err := Get(test.key, []byte(tstMeta))
|
2019-02-07 03:12:01 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected err: %v\n", err)
|
|
|
|
}
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("Did not get expected out. \nGot : %v, \nwant: %v\n", got, test.want)
|
|
|
|
}
|
2019-01-31 13:53:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 14:01:00 +03:00
|
|
|
|
2019-02-09 04:46:57 +03:00
|
|
|
// TestGetAll checks that meta.GetAll can correctly get all metadata
|
2019-02-08 14:01:00 +03:00
|
|
|
// from descriptor data.
|
2019-02-09 04:46:57 +03:00
|
|
|
func TestGetAll(t *testing.T) {
|
2019-02-08 14:01:00 +03:00
|
|
|
tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...)
|
|
|
|
want := [][2]string{
|
|
|
|
{
|
|
|
|
"loc",
|
|
|
|
"a,b,c",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ts",
|
|
|
|
"12345",
|
|
|
|
},
|
|
|
|
}
|
2019-02-09 04:44:32 +03:00
|
|
|
got, err := GetAll(tstMeta)
|
2019-02-08 14:01:00 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v\n", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Did not get expected out. \nGot : %v, \nWant: %v\n", got, want)
|
|
|
|
}
|
|
|
|
}
|