github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/stateConfig_test.go (about)

     1  package repository
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	. "gopkg.in/check.v1"
     8  )
     9  
    10  type StateConfigTests struct {
    11  	dir string
    12  }
    13  
    14  var _ = Suite(&StateConfigTests{})
    15  
    16  func (t *StateConfigTests) SetUpTest(c *C) {
    17  	t.dir = c.MkDir()
    18  }
    19  
    20  func (t *StateConfigTests) getStateConfigPath() string {
    21  	return filepath.Join(t.dir, "config.json")
    22  }
    23  
    24  func (t *StateConfigTests) getStateConfig() *StateConfig {
    25  	stateConfig := NewStateConfig(t.getStateConfigPath())
    26  	stateConfig.DefaultServer = &ServerStateConfig{
    27  		URL:                 "default_server",
    28  		Fingerprint:         "fp",
    29  		RemoteTransactionID: 1,
    30  		LocalTransactionID:  64,
    31  	}
    32  	return stateConfig
    33  }
    34  
    35  func (t *StateConfigTests) TestSave(c *C) {
    36  	stateConfig := t.getStateConfig()
    37  	err := stateConfig.Save()
    38  	c.Assert(err, IsNil)
    39  }
    40  
    41  func (t *StateConfigTests) TestSaveNotExists(c *C) {
    42  	os.RemoveAll(t.dir)
    43  	stateConfig := t.getStateConfig()
    44  	err := stateConfig.Save()
    45  	c.Assert(err, NotNil)
    46  }
    47  
    48  func (t *StateConfigTests) storeStateConfig(c *C) {
    49  	stateConfig := t.getStateConfig()
    50  	err := stateConfig.Save()
    51  	c.Assert(err, IsNil)
    52  }
    53  
    54  func (t *StateConfigTests) TestLoad(c *C) {
    55  	t.storeStateConfig(c)
    56  
    57  	stateConfig := &StateConfig{
    58  		Path: t.getStateConfigPath(),
    59  	}
    60  	err := stateConfig.Load()
    61  	c.Assert(err, IsNil)
    62  
    63  	defaultServer := stateConfig.DefaultServer
    64  	c.Assert(defaultServer.URL, Equals, "default_server")
    65  	c.Assert(defaultServer.Fingerprint, Equals, "fp")
    66  	c.Assert(defaultServer.LocalTransactionID, Equals, int64(64))
    67  	c.Assert(defaultServer.RemoteTransactionID, Equals, int64(1))
    68  }
    69  
    70  func (t *StateConfigTests) TestLoadNotExists(c *C) {
    71  	stateConfig := t.getStateConfig()
    72  	err := stateConfig.Load()
    73  	c.Assert(err, NotNil)
    74  }