|
| 1 | +package legacy |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/grafana/grafana/pkg/storage/unified/sql/sqltemplate" |
| 15 | +) |
| 16 | + |
| 17 | +//go:embed testdata/* |
| 18 | +var testdataFS embed.FS |
| 19 | + |
| 20 | +func testdata(t *testing.T, filename string) []byte { |
| 21 | + t.Helper() |
| 22 | + b, err := testdataFS.ReadFile(`testdata/` + filename) |
| 23 | + if err != nil { |
| 24 | + writeTestData(filename, "<empty>") |
| 25 | + assert.Fail(t, "missing test file") |
| 26 | + } |
| 27 | + return b |
| 28 | +} |
| 29 | + |
| 30 | +func writeTestData(filename, value string) { |
| 31 | + _ = os.WriteFile(filepath.Join("testdata", filename), []byte(value), 0777) |
| 32 | +} |
| 33 | + |
| 34 | +func TestQueries(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + // Check each dialect |
| 38 | + dialects := []sqltemplate.Dialect{ |
| 39 | + sqltemplate.MySQL, |
| 40 | + sqltemplate.SQLite, |
| 41 | + sqltemplate.PostgreSQL, |
| 42 | + } |
| 43 | + |
| 44 | + // Each template has one or more test cases, each identified with a |
| 45 | + // descriptive name (e.g. "happy path", "error twiddling the frobb"). Each |
| 46 | + // of them will test that for the same input data they must produce a result |
| 47 | + // that will depend on the Dialect. Expected queries should be defined in |
| 48 | + // separate files in the testdata directory. This improves the testing |
| 49 | + // experience by separating test data from test code, since mixing both |
| 50 | + // tends to make it more difficult to reason about what is being done, |
| 51 | + // especially as we want testing code to scale and make it easy to add |
| 52 | + // tests. |
| 53 | + type ( |
| 54 | + testCase = struct { |
| 55 | + Name string |
| 56 | + |
| 57 | + // Data should be the struct passed to the template. |
| 58 | + Data sqltemplate.SQLTemplateIface |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + // Define tests cases. Most templates are trivial and testing that they |
| 63 | + // generate correct code for a single Dialect is fine, since the one thing |
| 64 | + // that always changes is how SQL placeholder arguments are passed (most |
| 65 | + // Dialects use `?` while PostgreSQL uses `$1`, `$2`, etc.), and that is |
| 66 | + // something that should be tested in the Dialect implementation instead of |
| 67 | + // here. We will ask to have at least one test per SQL template, and we will |
| 68 | + // lean to test MySQL. Templates containing branching (conditionals, loops, |
| 69 | + // etc.) should be exercised at least once in each of their branches. |
| 70 | + // |
| 71 | + // NOTE: in the Data field, make sure to have pointers populated to simulate |
| 72 | + // data is set as it would be in a real request. The data being correctly |
| 73 | + // populated in each case should be tested in integration tests, where the |
| 74 | + // data will actually flow to and from a real database. In this tests we |
| 75 | + // only care about producing the correct SQL. |
| 76 | + testCases := map[*template.Template][]*testCase{ |
| 77 | + sqlQueryDashboards: { |
| 78 | + { |
| 79 | + Name: "history_uid", |
| 80 | + Data: &sqlQuery{ |
| 81 | + SQLTemplate: new(sqltemplate.SQLTemplate), |
| 82 | + Query: &DashboardQuery{ |
| 83 | + OrgID: 2, |
| 84 | + UID: "UUU", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + Name: "history_uid_at_version", |
| 90 | + Data: &sqlQuery{ |
| 91 | + SQLTemplate: new(sqltemplate.SQLTemplate), |
| 92 | + Query: &DashboardQuery{ |
| 93 | + OrgID: 2, |
| 94 | + UID: "UUU", |
| 95 | + Version: 3, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + Name: "history_uid_second_page", |
| 101 | + Data: &sqlQuery{ |
| 102 | + SQLTemplate: new(sqltemplate.SQLTemplate), |
| 103 | + Query: &DashboardQuery{ |
| 104 | + OrgID: 2, |
| 105 | + UID: "UUU", |
| 106 | + LastID: 7, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + Name: "dashboard", |
| 112 | + Data: &sqlQuery{ |
| 113 | + SQLTemplate: new(sqltemplate.SQLTemplate), |
| 114 | + Query: &DashboardQuery{ |
| 115 | + OrgID: 2, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + Name: "dashboard_next_page", |
| 121 | + Data: &sqlQuery{ |
| 122 | + SQLTemplate: new(sqltemplate.SQLTemplate), |
| 123 | + Query: &DashboardQuery{ |
| 124 | + OrgID: 2, |
| 125 | + LastID: 22, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + // Execute test cases |
| 133 | + for tmpl, tcs := range testCases { |
| 134 | + t.Run(tmpl.Name(), func(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + |
| 137 | + for _, tc := range tcs { |
| 138 | + t.Run(tc.Name, func(t *testing.T) { |
| 139 | + t.Parallel() |
| 140 | + |
| 141 | + for _, dialect := range dialects { |
| 142 | + filename := dialect.DialectName() + "__" + tc.Name + ".sql" |
| 143 | + t.Run(filename, func(t *testing.T) { |
| 144 | + // not parallel because we're sharing tc.Data, not |
| 145 | + // worth it deep cloning |
| 146 | + |
| 147 | + expectedQuery := string(testdata(t, filename)) |
| 148 | + //expectedQuery := sqltemplate.FormatSQL(rawQuery) |
| 149 | + |
| 150 | + tc.Data.SetDialect(dialect) |
| 151 | + err := tc.Data.Validate() |
| 152 | + require.NoError(t, err) |
| 153 | + got, err := sqltemplate.Execute(tmpl, tc.Data) |
| 154 | + require.NoError(t, err) |
| 155 | + |
| 156 | + got = sqltemplate.RemoveEmptyLines(got) |
| 157 | + if diff := cmp.Diff(expectedQuery, got); diff != "" { |
| 158 | + writeTestData(filename, got) |
| 159 | + t.Errorf("%s: %s", tc.Name, diff) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments