Merge branch '18024-no-docker-snap' into main
[arvados.git] / sdk / go / arvados / resource_list_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "bytes"
9         "encoding/json"
10         "testing"
11         "time"
12 )
13
14 func TestMarshalFiltersWithNanoseconds(t *testing.T) {
15         t0 := time.Now()
16         t0str := t0.Format(time.RFC3339Nano)
17         buf, err := json.Marshal([]Filter{
18                 {Attr: "modified_at", Operator: "=", Operand: t0}})
19         if err != nil {
20                 t.Fatal(err)
21         }
22         if expect := []byte(`[["modified_at","=","` + t0str + `"]]`); 0 != bytes.Compare(buf, expect) {
23                 t.Errorf("Encoded as %q, expected %q", buf, expect)
24         }
25 }
26
27 func TestMarshalFiltersWithNil(t *testing.T) {
28         buf, err := json.Marshal([]Filter{
29                 {Attr: "modified_at", Operator: "=", Operand: nil}})
30         if err != nil {
31                 t.Fatal(err)
32         }
33         if expect := []byte(`[["modified_at","=",null]]`); 0 != bytes.Compare(buf, expect) {
34                 t.Errorf("Encoded as %q, expected %q", buf, expect)
35         }
36 }
37
38 func TestUnmarshalFiltersWithNil(t *testing.T) {
39         buf := []byte(`["modified_at","=",null]`)
40         f := &Filter{}
41         err := f.UnmarshalJSON(buf)
42         if err != nil {
43                 t.Fatal(err)
44         }
45         expect := Filter{Attr: "modified_at", Operator: "=", Operand: nil}
46         if f.Attr != expect.Attr || f.Operator != expect.Operator || f.Operand != expect.Operand {
47                 t.Errorf("Decoded as %q, expected %q", f, expect)
48         }
49 }
50
51 func TestMarshalFiltersWithBoolean(t *testing.T) {
52         buf, err := json.Marshal([]Filter{
53                 {Attr: "is_active", Operator: "=", Operand: true}})
54         if err != nil {
55                 t.Fatal(err)
56         }
57         if expect := []byte(`[["is_active","=",true]]`); 0 != bytes.Compare(buf, expect) {
58                 t.Errorf("Encoded as %q, expected %q", buf, expect)
59         }
60 }
61
62 func TestUnmarshalFiltersWithBoolean(t *testing.T) {
63         buf := []byte(`["is_active","=",true]`)
64         f := &Filter{}
65         err := f.UnmarshalJSON(buf)
66         if err != nil {
67                 t.Fatal(err)
68         }
69         expect := Filter{Attr: "is_active", Operator: "=", Operand: true}
70         if f.Attr != expect.Attr || f.Operator != expect.Operator || f.Operand != expect.Operand {
71                 t.Errorf("Decoded as %q, expected %q", f, expect)
72         }
73 }