github.com/mymmsc/gox@v1.3.33/util/uuid/sql_test.go (about)

     1  // Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining
     4  // a copy of this software and associated documentation files (the
     5  // "Software"), to deal in the Software without restriction, including
     6  // without limitation the rights to use, copy, modify, merge, publish,
     7  // distribute, sublicense, and/or sell copies of the Software, and to
     8  // permit persons to whom the Software is furnished to do so, subject to
     9  // the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be
    12  // included in all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    15  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    16  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    17  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    18  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    19  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    20  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    21  
    22  package uuid
    23  
    24  import (
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  type sqlTestSuite struct{}
    29  
    30  var _ = Suite(&sqlTestSuite{})
    31  
    32  func (s *sqlTestSuite) TestValue(c *C) {
    33  	u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
    34  	c.Assert(err, IsNil)
    35  
    36  	val, err := u.Value()
    37  	c.Assert(err, IsNil)
    38  	c.Assert(val, Equals, u.String())
    39  }
    40  
    41  func (s *sqlTestSuite) TestValueNil(c *C) {
    42  	u := UUID{}
    43  
    44  	val, err := u.Value()
    45  	c.Assert(err, IsNil)
    46  	c.Assert(val, Equals, Nil.String())
    47  }
    48  
    49  func (s *sqlTestSuite) TestNullUUIDValueNil(c *C) {
    50  	u := NullUUID{}
    51  
    52  	val, err := u.Value()
    53  	c.Assert(err, IsNil)
    54  	c.Assert(val, IsNil)
    55  }
    56  
    57  func (s *sqlTestSuite) TestScanBinary(c *C) {
    58  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    59  	b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    60  
    61  	u1 := UUID{}
    62  	err := u1.Scan(b1)
    63  	c.Assert(err, IsNil)
    64  	c.Assert(u, Equals, u1)
    65  
    66  	b2 := []byte{}
    67  	u2 := UUID{}
    68  
    69  	err = u2.Scan(b2)
    70  	c.Assert(err, NotNil)
    71  }
    72  
    73  func (s *sqlTestSuite) TestScanString(c *C) {
    74  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    75  	s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
    76  
    77  	u1 := UUID{}
    78  	err := u1.Scan(s1)
    79  	c.Assert(err, IsNil)
    80  	c.Assert(u, Equals, u1)
    81  
    82  	s2 := ""
    83  	u2 := UUID{}
    84  
    85  	err = u2.Scan(s2)
    86  	c.Assert(err, NotNil)
    87  }
    88  
    89  func (s *sqlTestSuite) TestScanText(c *C) {
    90  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
    91  	b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
    92  
    93  	u1 := UUID{}
    94  	err := u1.Scan(b1)
    95  	c.Assert(err, IsNil)
    96  	c.Assert(u, Equals, u1)
    97  
    98  	b2 := []byte("")
    99  	u2 := UUID{}
   100  	err = u2.Scan(b2)
   101  	c.Assert(err, NotNil)
   102  }
   103  
   104  func (s *sqlTestSuite) TestScanUnsupported(c *C) {
   105  	u := UUID{}
   106  
   107  	err := u.Scan(true)
   108  	c.Assert(err, NotNil)
   109  }
   110  
   111  func (s *sqlTestSuite) TestScanNil(c *C) {
   112  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
   113  
   114  	err := u.Scan(nil)
   115  	c.Assert(err, NotNil)
   116  }
   117  
   118  func (s *sqlTestSuite) TestNullUUIDScanValid(c *C) {
   119  	u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
   120  	s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
   121  
   122  	u1 := NullUUID{}
   123  	err := u1.Scan(s1)
   124  	c.Assert(err, IsNil)
   125  	c.Assert(u1.Valid, Equals, true)
   126  	c.Assert(u1.UUID, Equals, u)
   127  }
   128  
   129  func (s *sqlTestSuite) TestNullUUIDScanNil(c *C) {
   130  	u := NullUUID{UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, true}
   131  
   132  	err := u.Scan(nil)
   133  	c.Assert(err, IsNil)
   134  	c.Assert(u.Valid, Equals, false)
   135  	c.Assert(u.UUID, Equals, Nil)
   136  }