/util_test.go

http://github.com/sdegutis/GoRM · Go · 101 lines · 88 code · 13 blank · 0 comment · 22 complexity · b90722b4277b4d1f1f90e2f7a3383f35 MD5 · raw file

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