config_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package config
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. )
  7. const missingVersion = `{
  8. }`
  9. const missingPackages = `{
  10. "version": "1"
  11. }`
  12. const unknownVersion = `{
  13. "version": "foo"
  14. }`
  15. const unknownFields = `{
  16. "version": "1",
  17. "foo": "bar"
  18. }`
  19. func TestBadConfigs(t *testing.T) {
  20. for _, test := range []struct {
  21. name string
  22. err string
  23. json string
  24. }{
  25. {
  26. "missing version",
  27. "no version number",
  28. missingVersion,
  29. },
  30. {
  31. "missing packages",
  32. "no packages",
  33. missingPackages,
  34. },
  35. {
  36. "unknown version",
  37. "invalid version number",
  38. unknownVersion,
  39. },
  40. {
  41. "unknown fields",
  42. `yaml: unmarshal errors:
  43. line 3: field foo not found in type config.V1GenerateSettings`,
  44. unknownFields,
  45. },
  46. } {
  47. tt := test
  48. t.Run(tt.name, func(t *testing.T) {
  49. _, err := ParseConfig(strings.NewReader(tt.json))
  50. if err == nil {
  51. t.Fatalf("expected err; got nil")
  52. }
  53. if diff := cmp.Diff(err.Error(), tt.err); diff != "" {
  54. t.Errorf("differed (-want +got):\n%s", diff)
  55. }
  56. })
  57. }
  58. }
  59. const validConfigOne = `{
  60. "version": "1"
  61. "packages": []
  62. }`
  63. func FuzzConfig(f *testing.F) {
  64. f.Add(validConfigOne)
  65. f.Fuzz(func(t *testing.T, orig string) {
  66. ParseConfig(strings.NewReader(orig))
  67. })
  68. }
  69. func TestInvalidConfig(t *testing.T) {
  70. err := Validate(&Config{
  71. SQL: []SQL{{
  72. Database: &Database{
  73. URI: "",
  74. Managed: false,
  75. },
  76. }},
  77. })
  78. if err == nil {
  79. t.Errorf("expected err; got nil")
  80. }
  81. }