| 1 |
// Copyright (c) 2022 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package dbutil |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
) |
| 15 |
|
| 16 |
type dialectFilterTest struct { |
| 17 |
name string |
| 18 |
line string |
| 19 |
dialect Dialect |
| 20 |
count int |
| 21 |
uncomment bool |
| 22 |
} |
| 23 |
|
| 24 |
func TestParseDialectFilter(t *testing.T) { |
| 25 |
db := &Database{Dialect: SQLite} |
| 26 |
tests := []dialectFilterTest{ |
| 27 |
{"Own dialect: single line", `-- only: sqlite`, SQLite, 1, false}, |
| 28 |
{"Own dialect: multiple lines", `-- only: sqlite for next 5 lines`, SQLite, 5, false}, |
| 29 |
{"Own dialect: fenced", `-- only: sqlite until "end only"`, SQLite, -1, false}, |
| 30 |
{"Own dialect: single line, commented", `-- only: sqlite (line commented)`, SQLite, 1, true}, |
| 31 |
{"Own dialect: multiple lines, commented", `-- only: sqlite for next 5 lines (lines commented)`, SQLite, 5, true}, |
| 32 |
{"Own dialect: fenced, commented", `-- only: sqlite until "end only" (lines commented)`, SQLite, -1, true}, |
| 33 |
|
| 34 |
{"Other dialect: single line", `-- only: postgres`, Postgres, 1, false}, |
| 35 |
{"Other dialect: multiple lines", `-- only: postgres for next 5 lines`, Postgres, 5, false}, |
| 36 |
{"Other dialect: fenced", `-- only: postgres until "end only"`, Postgres, -1, false}, |
| 37 |
{"Other dialect: single line, commented", `-- only: postgres (line commented)`, Postgres, 1, true}, |
| 38 |
{"Other dialect: multiple lines, commented", `-- only: postgres for next 5 lines (lines commented)`, Postgres, 5, true}, |
| 39 |
{"Other dialect: fenced, commented", `-- only: postgres until "end only" (lines commented)`, Postgres, -1, true}, |
| 40 |
} |
| 41 |
for _, test := range tests { |
| 42 |
t.Run(test.name, func(t *testing.T) { |
| 43 |
dialect, lines, uncomment, err := db.parseDialectFilter([]byte(test.line)) |
| 44 |
assert.NoError(t, err) |
| 45 |
assert.Equal(t, test.dialect, dialect) |
| 46 |
assert.Equal(t, test.count, lines) |
| 47 |
assert.Equal(t, test.uncomment, uncomment) |
| 48 |
}) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
type filterOutputTest struct { |
| 53 |
name string |
| 54 |
input string |
| 55 |
outputPostgres string |
| 56 |
outputSQLite string |
| 57 |
} |
| 58 |
|
| 59 |
func TestFilterSQLUpgrade(t *testing.T) { |
| 60 |
pg := &Database{Dialect: Postgres} |
| 61 |
lite := &Database{Dialect: SQLite} |
| 62 |
tests := []filterOutputTest{ |
| 63 |
{"Single line, commented", ` |
| 64 |
-- only: postgres |
| 65 |
meowgres |
| 66 |
-- only: sqlite (lines commented) |
| 67 |
-- meowlite |
| 68 |
`, ` |
| 69 |
meowgres |
| 70 |
`, ` |
| 71 |
meowlite |
| 72 |
`, |
| 73 |
}, |
| 74 |
{"Fenced, commented", ` |
| 75 |
-- only: postgres until "end only" |
| 76 |
meowgres |
| 77 |
line 2 |
| 78 |
-- end only postgres |
| 79 |
shared line |
| 80 |
-- only: sqlite until "end only" (lines commented) |
| 81 |
-- meowlite |
| 82 |
-- line 2.5 |
| 83 |
-- end only sqlite |
| 84 |
shared line 2 |
| 85 |
`, ` |
| 86 |
meowgres |
| 87 |
line 2 |
| 88 |
shared line |
| 89 |
shared line 2 |
| 90 |
`, ` |
| 91 |
shared line |
| 92 |
meowlite |
| 93 |
line 2.5 |
| 94 |
shared line 2 |
| 95 |
`, |
| 96 |
}, |
| 97 |
} |
| 98 |
for _, test := range tests { |
| 99 |
t.Run(test.name, func(t *testing.T) { |
| 100 |
out, err := pg.filterSQLUpgrade(bytes.Split([]byte(test.input), []byte("\n"))) |
| 101 |
assert.NoError(t, err) |
| 102 |
assert.Equal(t, test.outputPostgres, out) |
| 103 |
out, err = lite.filterSQLUpgrade(bytes.Split([]byte(test.input), []byte("\n"))) |
| 104 |
assert.NoError(t, err) |
| 105 |
assert.Equal(t, test.outputSQLite, out) |
| 106 |
}) |
| 107 |
} |
| 108 |
} |