2022-04-15 09:42:07 +02:00
|
|
|
package clean
|
2022-03-24 18:30:59 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-28 17:36:59 +02:00
|
|
|
func TestSqlSpecial(t *testing.T) {
|
|
|
|
t.Run("Special", func(t *testing.T) {
|
|
|
|
if s, o := SqlSpecial(1); !s {
|
|
|
|
t.Error("char is special")
|
|
|
|
} else if !o {
|
|
|
|
t.Error("\" must be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial(31); !s {
|
|
|
|
t.Error("char is special")
|
|
|
|
} else if !o {
|
|
|
|
t.Error("\" must be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('\\'); !s {
|
|
|
|
t.Error("\\ is special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("\\ must not be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('\''); !s {
|
|
|
|
t.Error("' is special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("' must not be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('"'); !s {
|
|
|
|
t.Error("\" is special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("\" must not be omitted")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("NotSpecial", func(t *testing.T) {
|
|
|
|
if s, o := SqlSpecial(32); s {
|
|
|
|
t.Error("space is not special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("space must not be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('A'); s {
|
|
|
|
t.Error("A is not special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("A must not be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('a'); s {
|
|
|
|
t.Error("a is not special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("a must not be omitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s, o := SqlSpecial('_'); s {
|
|
|
|
t.Error("_ is not special")
|
|
|
|
} else if o {
|
|
|
|
t.Error("_ must not be omitted")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-24 18:30:59 +01:00
|
|
|
func TestSqlString(t *testing.T) {
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
|
|
|
assert.Equal(t, "", SqlString(""))
|
|
|
|
})
|
|
|
|
t.Run("Special", func(t *testing.T) {
|
|
|
|
s := "' \" \t \n %_''"
|
2022-03-28 17:36:59 +02:00
|
|
|
exp := "'' \"\" %_''''"
|
2022-03-24 18:30:59 +01:00
|
|
|
result := SqlString(s)
|
|
|
|
t.Logf("String..: %s", s)
|
|
|
|
t.Logf("Expected: %s", exp)
|
|
|
|
t.Logf("Result..: %s", result)
|
|
|
|
assert.Equal(t, exp, result)
|
|
|
|
})
|
|
|
|
t.Run("Alnum", func(t *testing.T) {
|
|
|
|
assert.Equal(t, "123ABCabc", SqlString("123ABCabc"))
|
|
|
|
})
|
|
|
|
}
|