You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 line
1.2KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/gob"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/stretchr/testify/assert"
  7. "os"
  8. "testing"
  9. )
  10. type ABC struct {
  11. TEST string
  12. ABC int
  13. AB []byte
  14. }
  15. func TestMain(m *testing.M) {
  16. err := config.readConfig() //wechat API config
  17. if err != nil {
  18. log.Println(err)
  19. log.Fatalf("unable to read %s, program quit\n", configFile)
  20. return
  21. }
  22. loan.SetDSN(config.DSN)
  23. runTests := m.Run()
  24. os.Exit(runTests)
  25. }
  26. func TestSession_SaveOtherType(t *testing.T) {
  27. gob.Register(ABC{})
  28. se := loan.Session{}
  29. se.FakeNew()
  30. e := se.Write()
  31. assert.Equal(t, e, nil, "dbWrite should be ok")
  32. se1 := loan.Session{}
  33. e = se1.Read(se.Id)
  34. assert.Equal(t, e, nil, "dbRead should success")
  35. assert.Equal(t, se, se1, "two structure should be equal")
  36. //random struct
  37. p := ABC{}
  38. p.TEST = "this is TEST"
  39. p.ABC = 123
  40. p.AB = []byte("000111222333444555666777888999")
  41. //save to db
  42. se.Add("people", p)
  43. e = se.Write()
  44. assert.Equal(t, e, nil, "dbWrite Bin struct")
  45. //read it back
  46. e = se1.Read(se.Id)
  47. assert.Equal(t, e, nil, "dbRead read Bin")
  48. //unpack Bin
  49. p1 := se1.Get("people")
  50. assert.Equal(t, e, nil, "UnMarshal bin")
  51. //the Bin should be the same
  52. assert.Equal(t, p, p1, "two structure should be the same")
  53. }