|
| 1 | +/* |
| 2 | + Copyright 2016 GitHub Inc. |
| 3 | + See https://github.com/github/gh-osc/blob/master/LICENSE |
| 4 | +*/ |
| 5 | + |
| 6 | +package sql |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/outbrain/golib/log" |
| 15 | + test "github.com/outbrain/golib/tests" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + spacesRegexp = regexp.MustCompile(`[ \t\n\r]+`) |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + log.SetLevel(log.ERROR) |
| 24 | +} |
| 25 | + |
| 26 | +func normalizeQuery(name string) string { |
| 27 | + name = strings.Replace(name, "`", "", -1) |
| 28 | + name = spacesRegexp.ReplaceAllString(name, " ") |
| 29 | + name = strings.TrimSpace(name) |
| 30 | + return name |
| 31 | +} |
| 32 | + |
| 33 | +func TestEscapeName(t *testing.T) { |
| 34 | + names := []string{"my_table", `"my_table"`, "`my_table`"} |
| 35 | + for _, name := range names { |
| 36 | + escaped := EscapeName(name) |
| 37 | + test.S(t).ExpectEquals(escaped, "`my_table`") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestBuildEqualsComparison(t *testing.T) { |
| 42 | + { |
| 43 | + columns := []string{"c1"} |
| 44 | + values := []string{"@v1"} |
| 45 | + comparison, err := BuildEqualsComparison(columns, values) |
| 46 | + test.S(t).ExpectNil(err) |
| 47 | + test.S(t).ExpectEquals(comparison, "((`c1` = @v1))") |
| 48 | + } |
| 49 | + { |
| 50 | + columns := []string{"c1", "c2"} |
| 51 | + values := []string{"@v1", "@v2"} |
| 52 | + comparison, err := BuildEqualsComparison(columns, values) |
| 53 | + test.S(t).ExpectNil(err) |
| 54 | + test.S(t).ExpectEquals(comparison, "((`c1` = @v1) and (`c2` = @v2))") |
| 55 | + } |
| 56 | + { |
| 57 | + columns := []string{"c1"} |
| 58 | + values := []string{"@v1", "@v2"} |
| 59 | + _, err := BuildEqualsComparison(columns, values) |
| 60 | + test.S(t).ExpectNotNil(err) |
| 61 | + } |
| 62 | + { |
| 63 | + columns := []string{} |
| 64 | + values := []string{} |
| 65 | + _, err := BuildEqualsComparison(columns, values) |
| 66 | + test.S(t).ExpectNotNil(err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestBuildRangeComparison(t *testing.T) { |
| 71 | + { |
| 72 | + columns := []string{"c1"} |
| 73 | + values := []string{"@v1"} |
| 74 | + comparison, err := BuildRangeComparison(columns, values, LessThanComparisonSign) |
| 75 | + test.S(t).ExpectNil(err) |
| 76 | + test.S(t).ExpectEquals(comparison, "((`c1` < @v1))") |
| 77 | + } |
| 78 | + { |
| 79 | + columns := []string{"c1"} |
| 80 | + values := []string{"@v1"} |
| 81 | + comparison, err := BuildRangeComparison(columns, values, LessThanOrEqualsComparisonSign) |
| 82 | + test.S(t).ExpectNil(err) |
| 83 | + test.S(t).ExpectEquals(comparison, "((`c1` < @v1) or ((`c1` = @v1)))") |
| 84 | + } |
| 85 | + { |
| 86 | + columns := []string{"c1", "c2"} |
| 87 | + values := []string{"@v1", "@v2"} |
| 88 | + comparison, err := BuildRangeComparison(columns, values, LessThanComparisonSign) |
| 89 | + test.S(t).ExpectNil(err) |
| 90 | + test.S(t).ExpectEquals(comparison, "((`c1` < @v1) or (((`c1` = @v1)) AND (`c2` < @v2)))") |
| 91 | + } |
| 92 | + { |
| 93 | + columns := []string{"c1", "c2"} |
| 94 | + values := []string{"@v1", "@v2"} |
| 95 | + comparison, err := BuildRangeComparison(columns, values, LessThanOrEqualsComparisonSign) |
| 96 | + test.S(t).ExpectNil(err) |
| 97 | + test.S(t).ExpectEquals(comparison, "((`c1` < @v1) or (((`c1` = @v1)) AND (`c2` < @v2)) or ((`c1` = @v1) and (`c2` = @v2)))") |
| 98 | + } |
| 99 | + { |
| 100 | + columns := []string{"c1", "c2", "c3"} |
| 101 | + values := []string{"@v1", "@v2", "@v3"} |
| 102 | + comparison, err := BuildRangeComparison(columns, values, LessThanOrEqualsComparisonSign) |
| 103 | + test.S(t).ExpectNil(err) |
| 104 | + test.S(t).ExpectEquals(comparison, "((`c1` < @v1) or (((`c1` = @v1)) AND (`c2` < @v2)) or (((`c1` = @v1) and (`c2` = @v2)) AND (`c3` < @v3)) or ((`c1` = @v1) and (`c2` = @v2) and (`c3` = @v3)))") |
| 105 | + } |
| 106 | + { |
| 107 | + columns := []string{"c1"} |
| 108 | + values := []string{"@v1", "@v2"} |
| 109 | + _, err := BuildRangeComparison(columns, values, LessThanOrEqualsComparisonSign) |
| 110 | + test.S(t).ExpectNotNil(err) |
| 111 | + } |
| 112 | + { |
| 113 | + columns := []string{} |
| 114 | + values := []string{} |
| 115 | + _, err := BuildRangeComparison(columns, values, LessThanOrEqualsComparisonSign) |
| 116 | + test.S(t).ExpectNotNil(err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestBuildRangeInsertQuery(t *testing.T) { |
| 121 | + databaseName := "mydb" |
| 122 | + originalTableName := "tbl" |
| 123 | + ghostTableName := "ghost" |
| 124 | + sharedColumns := []string{"id", "name", "position"} |
| 125 | + { |
| 126 | + uniqueKey := "PRIMARY" |
| 127 | + uniqueKeyColumns := []string{"id"} |
| 128 | + rangeStartValues := []string{"@v1s"} |
| 129 | + rangeEndValues := []string{"@v1e"} |
| 130 | + |
| 131 | + query, err := BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName, sharedColumns, uniqueKey, uniqueKeyColumns, rangeStartValues, rangeEndValues) |
| 132 | + test.S(t).ExpectNil(err) |
| 133 | + expected := ` |
| 134 | + insert /* gh-osc mydb.tbl */ ignore into mydb.ghost (id, name, position) |
| 135 | + (select id, name, position from mydb.tbl force index (PRIMARY) |
| 136 | + where (((id > @v1s) or ((id = @v1s))) and ((id < @v1e) or ((id = @v1e)))) |
| 137 | + ) |
| 138 | + ` |
| 139 | + test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected)) |
| 140 | + } |
| 141 | + { |
| 142 | + uniqueKey := "name_position_uidx" |
| 143 | + uniqueKeyColumns := []string{"name", "position"} |
| 144 | + rangeStartValues := []string{"@v1s", "@v2s"} |
| 145 | + rangeEndValues := []string{"@v1e", "@v2e"} |
| 146 | + |
| 147 | + query, err := BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName, sharedColumns, uniqueKey, uniqueKeyColumns, rangeStartValues, rangeEndValues) |
| 148 | + test.S(t).ExpectNil(err) |
| 149 | + expected := ` |
| 150 | + insert /* gh-osc mydb.tbl */ ignore into mydb.ghost (id, name, position) |
| 151 | + (select id, name, position from mydb.tbl force index (name_position_uidx) |
| 152 | + where (((name > @v1s) or (((name = @v1s)) AND (position > @v2s)) or ((name = @v1s) and (position = @v2s))) and ((name < @v1e) or (((name = @v1e)) AND (position < @v2e)) or ((name = @v1e) and (position = @v2e)))) |
| 153 | + ) |
| 154 | + ` |
| 155 | + test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected)) |
| 156 | + } |
| 157 | +} |
0 commit comments