mirror of https://bitbucket.org/ausocean/av.git
177 lines
4.4 KiB
Go
177 lines
4.4 KiB
Go
/*
|
|
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.
|
|
*/
|
|
|
|
package meta
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
tstKey1 = "loc"
|
|
tstData1 = "a,b,c"
|
|
tstKey2 = "ts"
|
|
tstData2 = "12345678"
|
|
tstData3 = "d,e,f"
|
|
)
|
|
|
|
var (
|
|
errNotExpectedOut = "Did not get expected out. \nGot : %v, \nwant: %v\n"
|
|
errUnexpectedErr = "Unexpected err: %v\n"
|
|
errNotExpectedErr = "Not expected err: %v\n"
|
|
)
|
|
|
|
// TestAddAndGet ensures that we can add metadata and then successfully get it.
|
|
func TestAddAndGet(t *testing.T) {
|
|
meta := New()
|
|
meta.Add(tstKey1, tstData1)
|
|
meta.Add(tstKey2, tstData2)
|
|
errors.New("Trying to delete map entry that doesn't exist")
|
|
if data, err := meta.Get(tstKey1); err != nil {
|
|
t.Errorf("Could not get data for key: loc: %v", err.Error())
|
|
if data != tstData1 {
|
|
t.Errorf("Did not get expected data")
|
|
}
|
|
}
|
|
|
|
if data, err := meta.Get(tstKey2); err != nil {
|
|
t.Errorf("Could not get data for key: ts: %v", err.Error())
|
|
if data != tstData2 {
|
|
t.Errorf("Did not get expected data")
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
meta := New()
|
|
meta.Add(tstKey1, tstData1)
|
|
meta.Add(tstKey1, tstData3)
|
|
|
|
if data, err := meta.Get(tstKey1); err != nil {
|
|
t.Errorf(errUnexpectedErr, err.Error())
|
|
if data != tstData2 {
|
|
t.Errorf("Data did not correctly update for key \"loc\"")
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestAll ensures we can get a correct map using Meta.All() after adding some data
|
|
func TestAll(t *testing.T) {
|
|
meta := New()
|
|
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) {
|
|
meta := New()
|
|
|
|
if _, err := meta.Get(tstKey1); err != errKeyAbsent {
|
|
t.Errorf(errNotExpectedErr, errKeyAbsent.Error())
|
|
}
|
|
}
|
|
|
|
// TestDelete ensures we can remove a data entry in the Meta map.
|
|
func TestDelete(t *testing.T) {
|
|
meta := New()
|
|
meta.Add(tstKey1, tstData1)
|
|
if err := meta.Delete(tstKey1); err != nil {
|
|
t.Errorf(errUnexpectedErr, err.Error())
|
|
}
|
|
if _, err := meta.Get(tstKey1); err != errKeyAbsent {
|
|
t.Errorf(errNotExpectedErr, errKeyAbsent.Error())
|
|
}
|
|
}
|
|
|
|
// TestDeleteAbsentKey checks that we get an expected error when we try to delete
|
|
// an entry in the Meta map that doesn't exist.
|
|
func TestDeleteAbsentKey(t *testing.T) {
|
|
meta := New()
|
|
if err := meta.Delete(tstKey1); err != errKeyAbsent {
|
|
t.Errorf(errNotExpectedErr, errKeyAbsent.Error())
|
|
}
|
|
}
|
|
|
|
// TestEncode checks that we're getting the correct byte slice from Meta.Encode().
|
|
func TestEncode(t *testing.T) {
|
|
meta := New()
|
|
meta.Add(tstKey1, tstData1)
|
|
meta.Add(tstKey2, tstData2)
|
|
|
|
dataLen := len(tstKey1+tstData1+tstKey2+tstData2) + 3
|
|
expectedOut := []byte{
|
|
0x00,
|
|
0x10,
|
|
byte(dataLen >> 8),
|
|
byte(dataLen),
|
|
}
|
|
expectedOut = append(expectedOut, []byte(
|
|
tstKey1+"="+tstData1+"\t"+
|
|
tstKey2+"="+tstData2)...)
|
|
|
|
got := meta.Encode()
|
|
if !bytes.Equal(expectedOut, got) {
|
|
t.Errorf(errNotExpectedOut, got, expectedOut)
|
|
}
|
|
}
|
|
|
|
func TestReadFrom(t *testing.T) {
|
|
tstMeta := append([]byte{0x00, 0x10, 0x00, 0x12}, "loc=a,b,c\tts=12345"...)
|
|
got, err := ReadFrom([]byte(tstMeta), "loc")
|
|
if err != nil {
|
|
t.Errorf(errUnexpectedErr, err.Error())
|
|
}
|
|
want := "a,b,c"
|
|
if got != want {
|
|
t.Errorf(errNotExpectedOut, got, want)
|
|
}
|
|
|
|
if got, err = ReadFrom([]byte(tstMeta), "ts"); err != nil {
|
|
t.Errorf(errUnexpectedErr, err.Error())
|
|
}
|
|
want = "12345"
|
|
if got != want {
|
|
t.Errorf(errNotExpectedOut, got, want)
|
|
}
|
|
}
|