예제 #1
0
func Test_sumOfSquaresOfDigits(t *testing.T) {
	assert.Equal(t, 49, sumOfSquaresOfDigits("7"))
	assert.Equal(t, 82, sumOfSquaresOfDigits("19"))
	assert.Equal(t, 68, sumOfSquaresOfDigits("82"))
	assert.Equal(t, 100, sumOfSquaresOfDigits("68"))
	assert.Equal(t, 1, sumOfSquaresOfDigits("100"))
}
예제 #2
0
func TestCreateExpense_Success(t *testing.T) {
	tx := dbtest.BeginTx()
	defer tx.Rollback()

	expense, err := service.CreateExpense(logger.Discard, tx, service.CreateExpenseParams{
		Description: "Peter Lustig",
		Amount:      42,
	})

	assert.MustBeNil(t, err)
	assert.Equal(t, "Peter Lustig", expense.Description)
	assert.Equal(t, 42, expense.Amount)

	cnt, err := db.CountExpenses(tx)
	assert.MustBeNil(t, err)
	assert.Equal(t, 1, cnt)
}
예제 #3
0
// http://de.scribd.com/doc/142257204/Function-Kata-Fro%CC%88hliche-Zahlen
func TestIsHappyNumber(t *testing.T) {
	assert.Equal(t, false, IsHappyNumber(-7))
	assert.Equal(t, false, IsHappyNumber(0))
	assert.Equal(t, false, IsHappyNumber(21))
	assert.Equal(t, false, IsHappyNumber(2))
	assert.Equal(t, true, IsHappyNumber(7))
	assert.Equal(t, true, IsHappyNumber(964))
	assert.Equal(t, false, IsHappyNumber(999))
}
예제 #4
0
func TestQueryFromOpts(t *testing.T) {
	query, args := FormatOpts(
		"SELECT name FROM expenses",
		OrderBy("name ASC"),
		Where("ignore = '?' AND name = ? AND age > ? AND created_at < ?", "harald", 12, "2015"),
		Limit(12),
	)

	assert.Equal(t, "SELECT name FROM expenses WHERE ignore = '?' AND name = $1 AND age > $2 AND created_at < $3 ORDER BY name ASC LIMIT 12", query)
	assert.DeepEqual(t, []interface{}{"harald", 12, "2015"}, args)
}
예제 #5
0
func TestListExpenses(t *testing.T) {
	tx := dbtest.BeginTx()
	defer tx.Rollback()

	_, err := fabricate.Expense(tx).Description("Important Expense").Exec()
	assert.MustBeNil(t, err)

	expenses, err := service.ListExpenses(logger.Discard, tx)
	assert.MustBeNil(t, err)
	assert.MustBeEqual(t, 1, len(expenses))
	assert.Equal(t, "Important Expense", expenses[0].Description)
}
예제 #6
0
func TestStripLines(t *testing.T) {
	unstripped := `first
  second
     further in
third`

	stripped := `first
second
further in
third`

	assert.Equal(t, stripped, stripLines(unstripped))
}
예제 #7
0
func TestAPIExpenses(t *testing.T) {
	txManager := dbtest.NewTxManager()
	defer txManager.Rollback()

	router := web.NewRouter(logger.Discard, txManager)
	s := testclient.New(router)

	{
		status, body, err := s.PostForm("/api/v1/expenses", "description=asd&amount=12")
		assert.MustBeNil(t, err)
		defer body.Close()

		assert.Equal(t, 201, status)
		AssertJSONSchema(t,
			schema.Map{
				"amount":      12,
				"description": "asd",
			},
			body,
		)
	}

	{
		status, body, err := s.Get("/api/v1/expenses")
		assert.MustBeNil(t, err)
		defer body.Close()

		assert.Equal(t, 200, status)
		AssertJSONSchema(t,
			schema.Array(
				schema.Map{
					"amount":      12,
					"description": "asd",
				}),
			body,
		)
	}
}
예제 #8
0
func TestCreateExpense_Validation(t *testing.T) {
	tx := dbtest.BeginTx()
	defer tx.Rollback()

	_, err := service.CreateExpense(logger.Discard, tx, service.CreateExpenseParams{
		Description: "",
		Amount:      0,
	})

	assert.MustBeEqual(t, "ValidationError: Description: must be present, Amount: must be greater than 0", err.Error())

	cnt, err := db.CountExpenses(tx)
	assert.MustBeNil(t, err)
	assert.Equal(t, 0, cnt)
}
예제 #9
0
// http://de.scribd.com/doc/142096464/Function-Kata-ROT
func TestROT13(t *testing.T) {
	assert.Equal(t, "URYYB, JBEYQ", ROT13("Hello, World"))
	assert.Equal(t, "NR", ROT13("ä"))
}
예제 #10
0
// http://de.scribd.com/doc/140817312/Function-Kata-FizzBuzz
func TestFizzBuzz_Length(t *testing.T) {
	result := FizzBuzz()

	assert.Equal(t, 100, len(result))
}
예제 #11
0
// http://de.scribd.com/doc/140927641/Function-Kata-From-Roman-Numerals
func TestFromRomanNumerals_Addition(t *testing.T) {
	assert.Equal(t, 1, FromRomanNumerals("I"))
	assert.Equal(t, 2, FromRomanNumerals("II"))
	assert.Equal(t, 5, FromRomanNumerals("V"))
	assert.Equal(t, 2013, FromRomanNumerals("MMXIII"))
}
예제 #12
0
func TestFromRomanNumerals_Subtraction(t *testing.T) {
	assert.Equal(t, 4, FromRomanNumerals("IV"))
	assert.Equal(t, 9, FromRomanNumerals("IX"))
	assert.Equal(t, 42, FromRomanNumerals("XLII"))
	assert.Equal(t, 99, FromRomanNumerals("XCIX"))
}