launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/annotator_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 gc "launchpad.net/gocheck" 8 9 "launchpad.net/juju-core/state" 10 ) 11 12 var annotatorTests = []struct { 13 about string 14 initial map[string]string 15 input map[string]string 16 expected map[string]string 17 err string 18 }{ 19 { 20 about: "test setting an annotation", 21 input: map[string]string{"mykey": "myvalue"}, 22 expected: map[string]string{"mykey": "myvalue"}, 23 }, 24 { 25 about: "test setting multiple annotations", 26 input: map[string]string{"key1": "value1", "key2": "value2"}, 27 expected: map[string]string{"key1": "value1", "key2": "value2"}, 28 }, 29 { 30 about: "test overriding annotations", 31 initial: map[string]string{"mykey": "myvalue"}, 32 input: map[string]string{"mykey": "another-value"}, 33 expected: map[string]string{"mykey": "another-value"}, 34 }, 35 { 36 about: "test setting an invalid annotation", 37 input: map[string]string{"invalid.key": "myvalue"}, 38 err: `cannot update annotations on .*: invalid key "invalid.key"`, 39 }, 40 { 41 about: "test returning a non existent annotation", 42 expected: map[string]string{}, 43 }, 44 { 45 about: "test removing an annotation", 46 initial: map[string]string{"mykey": "myvalue"}, 47 input: map[string]string{"mykey": ""}, 48 expected: map[string]string{}, 49 }, 50 { 51 about: "test removing multiple annotations", 52 initial: map[string]string{"key1": "v1", "key2": "v2", "key3": "v3"}, 53 input: map[string]string{"key1": "", "key3": ""}, 54 expected: map[string]string{"key2": "v2"}, 55 }, 56 { 57 about: "test removing/adding annotations in the same transaction", 58 initial: map[string]string{"key1": "value1"}, 59 input: map[string]string{"key1": "", "key2": "value2"}, 60 expected: map[string]string{"key2": "value2"}, 61 }, 62 { 63 about: "test removing a non existent annotation", 64 input: map[string]string{"mykey": ""}, 65 expected: map[string]string{}, 66 }, 67 { 68 about: "test passing an empty map", 69 input: map[string]string{}, 70 expected: map[string]string{}, 71 }, 72 } 73 74 func testAnnotator(c *gc.C, getEntity func() (state.Annotator, error)) { 75 for i, t := range annotatorTests { 76 c.Logf("test %d. %s", i, t.about) 77 entity, err := getEntity() 78 c.Assert(err, gc.IsNil) 79 err = entity.SetAnnotations(t.initial) 80 c.Assert(err, gc.IsNil) 81 err = entity.SetAnnotations(t.input) 82 if t.err != "" { 83 c.Assert(err, gc.ErrorMatches, t.err) 84 continue 85 } 86 // Retrieving single values works as expected. 87 for key, value := range t.input { 88 v, err := entity.Annotation(key) 89 c.Assert(err, gc.IsNil) 90 c.Assert(v, gc.Equals, value) 91 } 92 // The value stored in MongoDB changed. 93 ann, err := entity.Annotations() 94 c.Assert(err, gc.IsNil) 95 c.Assert(ann, gc.DeepEquals, t.expected) 96 // Clean up existing annotations. 97 cleanup := make(map[string]string) 98 for key := range t.expected { 99 cleanup[key] = "" 100 } 101 err = entity.SetAnnotations(cleanup) 102 c.Assert(err, gc.IsNil) 103 } 104 }