PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/random_data_test.go

https://gitlab.com/ezeql/go-randomdata
Go | 257 lines | 197 code | 60 blank | 0 comment | 62 complexity | 403fc29794ff0661c03079ec77e134a0 MD5 | raw file
  1. package randomdata
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. )
  7. func TestRandomStringDigits(t *testing.T) {
  8. t.Log("TestRandomStringDigits")
  9. if len(StringNumber(2, "-")) != 5 {
  10. t.Fatal("Wrong length returned")
  11. }
  12. if len(StringNumber(2, "")) != 4 {
  13. t.Fatal("Wrong length returned")
  14. }
  15. if len(StringNumberExt(3, "/", 3)) != 11 {
  16. t.Fatal("Wrong length returned")
  17. }
  18. if len(StringNumberExt(3, "", 3)) != 9 {
  19. t.Fatal("Wrong length returned")
  20. }
  21. }
  22. func TestFirstName(t *testing.T) {
  23. t.Log("TestFirstName")
  24. firstNameMale := FirstName(Male)
  25. firstNameFemale := FirstName(Female)
  26. randomName := FirstName(RandomGender)
  27. if !findInSlice(jsonData.FirstNamesMale, firstNameMale) {
  28. t.Error("firstNameMale empty or not in male names")
  29. }
  30. if !findInSlice(jsonData.FirstNamesFemale, firstNameFemale) {
  31. t.Error("firstNameFemale empty or not in female names")
  32. }
  33. if randomName == "" {
  34. t.Error("randomName empty")
  35. }
  36. }
  37. func TestLastName(t *testing.T) {
  38. t.Log("TestLastName")
  39. lastName := LastName()
  40. if !findInSlice(jsonData.LastNames, lastName) {
  41. t.Error("lastName empty or not in slice")
  42. }
  43. }
  44. func TestFullName(t *testing.T) {
  45. t.Log("TestFullName")
  46. fullNameMale := FullName(Male)
  47. fullNameFemale := FullName(Female)
  48. fullNameRandom := FullName(RandomGender)
  49. maleSplit := strings.Fields(fullNameMale)
  50. femaleSplit := strings.Fields(fullNameFemale)
  51. randomSplit := strings.Fields(fullNameRandom)
  52. if len(maleSplit) == 0 {
  53. t.Error("Failed on full name male")
  54. }
  55. if !findInSlice(jsonData.FirstNamesMale, maleSplit[0]) {
  56. t.Error("Couldnt find maleSplit first name in firstNamesMale")
  57. }
  58. if !findInSlice(jsonData.LastNames, maleSplit[1]) {
  59. t.Error("Couldnt find maleSplit last name in lastNames")
  60. }
  61. if len(femaleSplit) == 0 {
  62. t.Error("Failed on full name female")
  63. }
  64. if !findInSlice(jsonData.FirstNamesFemale, femaleSplit[0]) {
  65. t.Error("Couldnt find femaleSplit first name in firstNamesFemale")
  66. }
  67. if !findInSlice(jsonData.LastNames, femaleSplit[1]) {
  68. t.Error("Couldnt find femaleSplit last name in lastNames")
  69. }
  70. if len(randomSplit) == 0 {
  71. t.Error("Failed on full name random")
  72. }
  73. if !findInSlice(jsonData.FirstNamesMale, randomSplit[0]) && !findInSlice(jsonData.FirstNamesFemale, randomSplit[0]) {
  74. t.Error("Couldnt find randomSplit first name in either firstNamesMale or firstNamesFemale")
  75. }
  76. }
  77. func TestEmail(t *testing.T) {
  78. t.Log("TestEmail")
  79. email := Email()
  80. if email == "" {
  81. t.Error("Failed to generate email with content")
  82. }
  83. }
  84. func TestCountry(t *testing.T) {
  85. t.Log("TestCountry")
  86. countryFull := Country(FullCountry)
  87. countryTwo := Country(TwoCharCountry)
  88. countryThree := Country(ThreeCharCountry)
  89. if len(countryThree) < 3 {
  90. t.Error("countryThree < 3 chars")
  91. }
  92. if !findInSlice(jsonData.Countries, countryFull) {
  93. t.Error("Couldnt find country in countries")
  94. }
  95. if !findInSlice(jsonData.CountriesTwoChars, countryTwo) {
  96. t.Error("Couldnt find country with two chars in countriesTwoChars")
  97. }
  98. if !findInSlice(jsonData.CountriesThreeChars, countryThree) {
  99. t.Error("Couldnt find country with three chars in countriesThreeChars")
  100. }
  101. }
  102. func TestCurrency(t *testing.T) {
  103. t.Log("TestCurrency")
  104. if !findInSlice(jsonData.Currencies, Currency()) {
  105. t.Error("Could not find currency in currencies")
  106. }
  107. }
  108. func TestCity(t *testing.T) {
  109. t.Log("TestCity")
  110. city := City()
  111. if !findInSlice(jsonData.Cities, city) {
  112. t.Error("Couldnt find city in cities")
  113. }
  114. }
  115. func TestParagraph(t *testing.T) {
  116. t.Log("TestParagraph")
  117. paragraph := Paragraph()
  118. if !findInSlice(jsonData.Paragraphs, paragraph) {
  119. t.Error("Couldnt find paragraph in paragraphs")
  120. }
  121. }
  122. func TestBool(t *testing.T) {
  123. t.Log("TestBool")
  124. booleanVal := Boolean()
  125. if booleanVal != true && booleanVal != false {
  126. t.Error("Bool was wrong format")
  127. }
  128. }
  129. func TestState(t *testing.T) {
  130. t.Log("TestState")
  131. stateValSmall := State(Small)
  132. stateValLarge := State(Large)
  133. if !findInSlice(jsonData.StatesSmall, stateValSmall) {
  134. t.Error("Couldnt find small state name in states")
  135. }
  136. if !findInSlice(jsonData.States, stateValLarge) {
  137. t.Error("Couldnt find state name in states")
  138. }
  139. }
  140. func TestNoun(t *testing.T) {
  141. if len(jsonData.Nouns) == 0 {
  142. t.Error("Nouns is empty")
  143. }
  144. noun := Noun()
  145. if !findInSlice(jsonData.Nouns, noun) {
  146. t.Error("Couldnt find noun in json data")
  147. }
  148. }
  149. func TestAdjective(t *testing.T) {
  150. if len(jsonData.Adjectives) == 0 {
  151. t.Error("Adjectives array is empty")
  152. }
  153. adjective := Adjective()
  154. if !findInSlice(jsonData.Adjectives, adjective) {
  155. t.Error("Couldnt find noun in json data")
  156. }
  157. }
  158. func TestSillyName(t *testing.T) {
  159. sillyName := SillyName()
  160. if len(sillyName) == 0 {
  161. t.Error("Couldnt generate a silly name")
  162. }
  163. }
  164. func TestIpV4Address(t *testing.T) {
  165. ipAddress := IpV4Address()
  166. ipBlocks := strings.Split(ipAddress, ".")
  167. if len(ipBlocks) < 0 || len(ipBlocks) > 4 {
  168. t.Error("Invalid generated IP address")
  169. }
  170. for _, blockString := range ipBlocks {
  171. blockNumber, err := strconv.Atoi(blockString)
  172. if err != nil {
  173. t.Error("Error while testing IpV4Address(): " + err.Error())
  174. }
  175. if blockNumber < 0 || blockNumber > 255 {
  176. t.Error("Invalid generated IP address")
  177. }
  178. }
  179. }
  180. func TestDecimal(t *testing.T) {
  181. d := Decimal(2, 4, 3)
  182. if !(d >= 2 && d <= 4) {
  183. t.Error("Invalid generate range")
  184. }
  185. ds := strings.Split(strconv.FormatFloat(d, 'f', 3, 64), ".")
  186. if len(ds[1]) != 3 {
  187. t.Error("Invalid floating point")
  188. }
  189. }
  190. func findInSlice(source []string, toFind string) bool {
  191. for _, text := range source {
  192. if text == toFind {
  193. return true
  194. }
  195. }
  196. return false
  197. }