PageRenderTime 223ms CodeModel.GetById 131ms app.highlight 9ms RepoModel.GetById 81ms app.codeStats 1ms

/util_test.go

http://github.com/sdegutis/GoRM
Go | 101 lines | 88 code | 13 blank | 0 comment | 22 complexity | b90722b4277b4d1f1f90e2f7a3383f35 MD5 | raw file
  1package gorm
  2
  3import (
  4	"testing"
  5	"reflect"
  6)
  7
  8func TestTypeName(t *testing.T) {
  9	type PersonStruct struct {
 10		age  int
 11		name string
 12	}
 13
 14	var pete PersonStruct
 15	tname := getTypeName(pete)
 16	if tname != "PersonStruct" {
 17		t.Errorf("Expected type %T to be PersonStruct, got %v\n", pete, tname)
 18	}
 19}
 20
 21func TestSnakeCasing(t *testing.T) {
 22	names := map[string]string{
 23		"ThisThat": "this_that",
 24		"WhatIAm":  "what_i_am",
 25		"IAmNot":   "i_am_not",
 26		"Shop":     "shop",
 27	}
 28	for key, val := range names {
 29		if name := snakeCasedName(key); name != val {
 30			t.Errorf("Expected [%v] to translate to [%v], got [%v]\n", key, val, name)
 31		}
 32	}
 33}
 34
 35func TestTitleCasing(t *testing.T) {
 36	names := map[string]string{
 37		"this_that": "ThisThat",
 38		"what_i_am": "WhatIAm",
 39		"i_am_not":  "IAmNot",
 40		"shop":      "Shop",
 41	}
 42	for key, val := range names {
 43		if name := titleCasedName(key); name != val {
 44			t.Errorf("Expected [%v] to translate to [%v], got [%v]\n", key, val, name)
 45		}
 46	}
 47}
 48
 49func TestPluralizeString(t *testing.T) {
 50	names := map[string]string{
 51		"person": "persons",
 52		"yak":    "yaks",
 53		"ghost":  "ghosts",
 54		"party":  "parties",
 55	}
 56	for key, val := range names {
 57		if name := pluralizeString(key); name != val {
 58			t.Errorf("Expected [%v] to translate to [%v], got [%v]\n", key, val, name)
 59		}
 60	}
 61}
 62
 63func TestScanStructIntoMap(t *testing.T) {
 64	var pete Person
 65	pete.Name = "bob"
 66	pete.Age = 32
 67	pete.Id = 7
 68
 69	peteMap, err := scanStructIntoMap(&pete)
 70	if err != nil {
 71		t.Error(err)
 72	}
 73
 74	peteComparableMap := map[string]interface{}{
 75		"name": "bob",
 76		"age":  32,
 77		"id":   7,
 78	}
 79
 80	if !reflect.DeepEqual(peteMap, peteComparableMap) {
 81		t.Errorf("pete's map was not filled out properly. have %v, want %v", peteMap, peteComparableMap)
 82	}
 83}
 84
 85func TestScanMapIntoStruct(t *testing.T) {
 86	personMap := map[string][]byte{
 87		"name": []byte("bob"),
 88		"id":   []byte("2"),
 89		"age":  []byte("42"),
 90	}
 91
 92	bob := Person{}
 93
 94	err := scanMapIntoStruct(&bob, personMap)
 95	if err != nil {
 96		t.Error(err)
 97	}
 98	if bob.Name != "bob" || bob.Age != 42 || bob.Id != 2 {
 99		t.Errorf("struct was not filled out right; got %v with error %v", bob, err)
100	}
101}