2019-01-27 09:46:44 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mts
|
|
|
|
|
2019-01-27 10:27:42 +03:00
|
|
|
import (
|
2019-01-27 10:59:45 +03:00
|
|
|
"errors"
|
2019-01-27 10:32:51 +03:00
|
|
|
"reflect"
|
2019-01-27 10:27:42 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-01-27 10:59:45 +03:00
|
|
|
const (
|
|
|
|
tstKey1 = "loc"
|
|
|
|
tstData1 = "a,b,c"
|
|
|
|
tstKey2 = "ts"
|
|
|
|
tstData2 = "12345678"
|
|
|
|
tstData3 = "d,e,f"
|
|
|
|
)
|
|
|
|
|
2019-01-27 10:27:42 +03:00
|
|
|
func TestAddAndGet(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
2019-01-27 10:59:45 +03:00
|
|
|
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 {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Could not get data for key: loc: %v", err.Error())
|
2019-01-27 10:59:45 +03:00
|
|
|
if data != tstData1 {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Did not get expected data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 10:59:45 +03:00
|
|
|
if data, err := meta.Get(tstKey2); err != nil {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Could not get data for key: ts: %v", err.Error())
|
2019-01-27 10:59:45 +03:00
|
|
|
if data != tstData2 {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Did not get expected data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
2019-01-27 10:59:45 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey1, tstData3)
|
2019-01-27 10:27:42 +03:00
|
|
|
|
2019-01-27 10:59:45 +03:00
|
|
|
if data, err := meta.Get(tstKey1); err != nil {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Did not expect err: %v", err.Error())
|
2019-01-27 10:59:45 +03:00
|
|
|
if data != tstData2 {
|
2019-01-27 10:27:42 +03:00
|
|
|
t.Errorf("Data did not correctly update for key \"loc\"")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 10:32:51 +03:00
|
|
|
func TestAll(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
|
|
|
tstMap := map[string]string{
|
2019-01-27 10:59:45 +03:00
|
|
|
tstKey1: tstData1,
|
|
|
|
tstKey2: tstData2,
|
2019-01-27 10:32:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-27 10:59:45 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
meta.Add(tstKey2, tstData2)
|
2019-01-27 10:32:51 +03:00
|
|
|
metaMap := meta.All()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(metaMap, tstMap) {
|
|
|
|
t.Errorf("Map not correct. Got: %v, want: %v", metaMap, tstMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 10:45:22 +03:00
|
|
|
func TestGetAbsentKey(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
|
|
|
|
2019-01-27 10:59:45 +03:00
|
|
|
if _, err := meta.Get(tstKey1); err != errKeyAbsent {
|
2019-01-27 10:51:49 +03:00
|
|
|
t.Errorf("Did not get expected err: %v", errKeyAbsent.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
2019-01-27 10:59:45 +03:00
|
|
|
meta.Add(tstKey1, tstData1)
|
|
|
|
if err := meta.Delete(tstKey1); err != nil {
|
2019-01-27 10:51:49 +03:00
|
|
|
t.Errorf("Did not expect error: %v", err.Error())
|
|
|
|
}
|
2019-01-27 10:59:45 +03:00
|
|
|
if _, err := meta.Get(tstKey1); err != errKeyAbsent {
|
2019-01-27 10:54:26 +03:00
|
|
|
t.Errorf("Did not get expected err: %v", errKeyAbsent.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteAbsentKey(t *testing.T) {
|
|
|
|
meta := NewMeta()
|
2019-01-27 10:59:45 +03:00
|
|
|
if err := meta.Delete(tstKey1); err != errKeyAbsent {
|
2019-01-27 10:54:26 +03:00
|
|
|
t.Errorf("Did not get expected err: %v", errKeyAbsent.Error())
|
2019-01-27 10:45:22 +03:00
|
|
|
}
|
|
|
|
}
|