// Ensure that an account can retrieve all associated funnels. func TestAccountFunnels(t *testing.T) { withDB(func(db *DB) { db.Do(func(tx *Tx) error { a1 := &Account{} a2 := &Account{} assert.NoError(t, tx.CreateAccount(a1)) assert.NoError(t, tx.CreateAccount(a2)) // Add funnels to first account. assert.NoError(t, a1.CreateFunnel(&Funnel{Name: "Funnel B", Steps: []*FunnelStep{{Condition: "action == 'foo'"}}})) assert.NoError(t, a1.CreateFunnel(&Funnel{Name: "Funnel A", Steps: []*FunnelStep{{Condition: "action == 'foo'"}}})) // Add funnels to second account. assert.NoError(t, a2.CreateFunnel(&Funnel{Name: "Funnel C", Steps: []*FunnelStep{{Condition: "action == 'foo'"}}})) return nil }) // Check first account. db.With(func(tx *Tx) error { a, _ := tx.Account(1) funnels, err := a.Funnels() if assert.NoError(t, err) && assert.Equal(t, len(funnels), 2) { assert.Equal(t, funnels[0].Tx, tx) assert.Equal(t, funnels[0].ID(), 2) assert.Equal(t, funnels[0].AccountID, 1) assert.Equal(t, funnels[0].Name, "Funnel A") assert.Equal(t, funnels[1].Tx, tx) assert.Equal(t, funnels[1].ID(), 1) assert.Equal(t, funnels[1].AccountID, 1) assert.Equal(t, funnels[1].Name, "Funnel B") } // Make sure we can only get a1 funnels. f, err := a.Funnel(1) assert.NoError(t, err) assert.NotNil(t, f) f, err = a.Funnel(3) assert.Equal(t, err, ErrFunnelNotFound) assert.Nil(t, f) return nil }) // Check second account's funnels. db.With(func(tx *Tx) error { a, _ := tx.Account(2) funnels, err := a.Funnels() if assert.NoError(t, err) && assert.Equal(t, len(funnels), 1) { assert.Equal(t, funnels[0].Tx, tx) assert.Equal(t, funnels[0].ID(), 3) assert.Equal(t, funnels[0].AccountID, 2) assert.Equal(t, funnels[0].Name, "Funnel C") } return nil }) }) }
// Ensure that an account can retrieve all associated users. func TestAccountUsers(t *testing.T) { withDB(func(db *DB) { // Create two accounts. db.Do(func(tx *Tx) error { a1 := &Account{} assert.NoError(t, tx.CreateAccount(a1)) a2 := &Account{} assert.NoError(t, tx.CreateAccount(a2)) return nil }) // Add users to first account. db.Do(func(tx *Tx) error { a1, _ := tx.Account(1) assert.NoError(t, a1.CreateUser(&User{Email: "*****@*****.**", Password: "******"})) assert.NoError(t, a1.CreateUser(&User{Email: "*****@*****.**", Password: "******"})) return nil }) // Add users to second account. db.Do(func(tx *Tx) error { a2, _ := tx.Account(2) assert.NoError(t, a2.CreateUser(&User{Email: "*****@*****.**", Password: "******"})) return nil }) // Check first account users. db.With(func(tx *Tx) error { a1, _ := tx.Account(1) users, err := a1.Users() if assert.NoError(t, err) && assert.Equal(t, len(users), 2) { assert.Equal(t, users[0].Tx, tx) assert.Equal(t, users[0].ID(), 2) assert.Equal(t, users[0].AccountID, 1) assert.Equal(t, users[0].Email, "*****@*****.**") assert.Equal(t, users[1].Tx, tx) assert.Equal(t, users[1].ID(), 1) assert.Equal(t, users[1].AccountID, 1) assert.Equal(t, users[1].Email, "*****@*****.**") } return nil }) // Check second account users. db.With(func(tx *Tx) error { a2, _ := tx.Account(2) users, err := a2.Users() if assert.NoError(t, err) && assert.Equal(t, len(users), 1) { assert.Equal(t, users[0].Tx, tx) assert.Equal(t, users[0].ID(), 3) assert.Equal(t, users[0].AccountID, 2) assert.Equal(t, users[0].Email, "*****@*****.**") } return nil }) }) }
// Ensure that an account can be deleted. func TestAccountDelete(t *testing.T) { withDB(func(db *DB) { // Create account. err := db.Do(func(tx *Tx) error { return tx.CreateAccount(&Account{}) }) assert.NoError(t, err) // Retrieve and delete account. err = db.Do(func(tx *Tx) error { a, _ := tx.Account(1) return a.Delete() }) assert.NoError(t, err) // Retrieve the account again. err = db.With(func(tx *Tx) error { _, err := tx.Account(1) return err }) assert.Equal(t, err, ErrAccountNotFound) }) }