Skip to content

Instantly share code, notes, and snippets.

@caalberts
caalberts / app.js
Last active February 16, 2019 15:51
Attempt to handle errors in JS
// specifically creates a closure to handle specific error class with a given error handler
// it rethrows all other errors
const specifically = (errorClass) => (errHandler) => (err) => {
if (err instanceof errorClass) {
return errHandler(err)
}
throw err
}
@caalberts
caalberts / brew-sync.sh
Created January 27, 2018 14:38 — forked from jamiew/brew-sync.sh
Sync Homebrew installations between Macs via Dropbox (including casks)
#!/bin/bash
# Sync Homebrew installations between Macs via Dropbox
#
BREW="/usr/local/bin/brew"
# first get local settings
echo "Reading local settings ..."
rm -f /tmp/brew-sync.*
func (r *AccountRepository) UpdateAccount(id string, data map[string]interface{}) (*Account, error) {
_, err := r.proxy.HMSet(id, data)
if err != nil {
return nil, err
} else {
return r.FetchAccount(id)
}
}
func TestUpdateAccountUpdatesAccountWithNewData(t *testing.T) {
repository := createRepositoryWithData(fakeData)
newBalance := "200"
updateValue := map[string]interface{}{"Balance": newBalance}
account, err := repository.UpdateAccount(id, updateValue)
assert.Equal(t, id, account.Id)
assert.Equal(t, "John Doe", account.Name)
func (r *AccountRepository) FetchAccount(id string) (*Account, error) {
data, err := r.client.HGetAll(id)
if err != nil {
return nil, err
}
return toAccount(data)
}
type Account struct {
package main
import (
"strconv"
"errors"
)
type AccountRepository struct {
client RedisProxy
}
package main
import "github.com/go-redis/redis"
type Redis struct {
*redis.Client
}
func (r *Redis) HGetAll(key string) (map[string]string, error) {
return r.Client.HGetAll(key).Result()
func TestFetchAccountWhenAccountExists(t *testing.T) {
repository := createRepositoryWithData(fakeData)
account, err := repository.FetchAccount(id)
assert.Equal(t, id, account.Id)
assert.Equal(t, "John Doe", account.Name)
assert.Equal(t, 100, account.Balance)
assert.Nil(t, err)
}
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
type stubRedis struct {
data map[string]string
err error
type RedisProxy interface {
HGetAll(string) (map[string]string, error)
HMSet(string, map[string]interface{}) (string, error)
}