github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/mongo/utils/validfield_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package utils_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/mongo/utils"
    11  )
    12  
    13  type ValidFieldSuite struct{}
    14  
    15  var _ = gc.Suite(&ValidFieldSuite{})
    16  
    17  func (s *ValidFieldSuite) TestOk(c *gc.C) {
    18  	c.Check(utils.IsValidFieldName("foo"), jc.IsTrue)
    19  }
    20  
    21  func (s *ValidFieldSuite) TestEmpty(c *gc.C) {
    22  	c.Check(utils.IsValidFieldName(""), jc.IsFalse)
    23  }
    24  
    25  func (s *ValidFieldSuite) TestDollarPrefix(c *gc.C) {
    26  	c.Check(utils.IsValidFieldName("$foo"), jc.IsFalse)
    27  }
    28  
    29  func (s *ValidFieldSuite) TestEmbeddedDollar(c *gc.C) {
    30  	c.Check(utils.IsValidFieldName("foo$bar"), jc.IsTrue)
    31  }
    32  
    33  func (s *ValidFieldSuite) TestDot(c *gc.C) {
    34  	c.Check(utils.IsValidFieldName(".foo"), jc.IsFalse)
    35  	c.Check(utils.IsValidFieldName("foo.bar"), jc.IsFalse)
    36  	c.Check(utils.IsValidFieldName("bar."), jc.IsFalse)
    37  }
    38  
    39  func (s *ValidFieldSuite) TestCheckStorableOk(c *gc.C) {
    40  	type Doc struct {
    41  		A string            `bson:"a"`
    42  		B map[string]string `bson:"b"`
    43  	}
    44  
    45  	c.Check(utils.CheckStorable(Doc{
    46  		A: "hi",
    47  		B: map[string]string{
    48  			"some":  "thing",
    49  			"other": "thing",
    50  		},
    51  	}), jc.ErrorIsNil)
    52  }
    53  
    54  func (s *ValidFieldSuite) TestCheckStorableBad(c *gc.C) {
    55  	type Doc struct {
    56  		A string `bson:"$a"`
    57  	}
    58  	c.Check(utils.CheckStorable(Doc{
    59  		A: "hi",
    60  	}), gc.ErrorMatches, `"\$a" is not a valid field name`)
    61  }
    62  
    63  func (s *ValidFieldSuite) TestCheckStorableBadNested(c *gc.C) {
    64  	type Doc struct {
    65  		A map[string]string `bson:"a"`
    66  	}
    67  
    68  	c.Check(utils.CheckStorable(Doc{
    69  		A: map[string]string{
    70  			"some": "thing",
    71  			"$foo": "thing",
    72  		},
    73  	}), gc.ErrorMatches, `"\$foo" is not a valid field name`)
    74  }
    75  
    76  func (s *ValidFieldSuite) TestCheckStorableBadDeepNested(c *gc.C) {
    77  	type SubDoc struct {
    78  		A int               `bson:"a"`
    79  		B map[string]string `bson:"b"`
    80  	}
    81  	type Doc struct {
    82  		A map[string]map[string]SubDoc `bson:"a"`
    83  	}
    84  
    85  	c.Check(utils.CheckStorable(Doc{
    86  		A: map[string]map[string]SubDoc{
    87  			"foo": {
    88  				"foo": {
    89  					A: 99,
    90  					B: map[string]string{
    91  						"some":    "thing",
    92  						"foo.bar": "thing",
    93  					},
    94  				},
    95  			},
    96  		},
    97  	}), gc.ErrorMatches, `"foo.bar" is not a valid field name`)
    98  }