-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Jmainguy/sqliteTest
add golang tests for the sqlite database
- Loading branch information
Showing
10 changed files
with
19,115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
on: | ||
push: | ||
|
||
name: push | ||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 1 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v2 | ||
with: | ||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. | ||
version: v1.29 | ||
args: --timeout=5m | ||
working-directory: ./tests | ||
test: | ||
name: Test with Coverage | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up Go | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: '1.16.3' | ||
|
||
- name: Get Build Tools | ||
run: | | ||
GO111MODULE=on go get github.com/ory/go-acc | ||
- name: Add $GOPATH/bin to $PATH | ||
run: | | ||
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH" | ||
- name: git checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 1 | ||
|
||
- name: Install dependencies | ||
working-directory: ./tests | ||
run: | | ||
go mod download | ||
- name: Run Unit tests | ||
working-directory: ./tests | ||
run: | | ||
go-acc . | ||
build: | ||
name: Lint and build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: install go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: '1.16.3' | ||
|
||
- name: git checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 1 | ||
|
||
- name: install lint | ||
working-directory: ./tests | ||
run: GO111MODULE=off go get golang.org/x/lint/golint | ||
|
||
- name: run golint and go fmt | ||
working-directory: ./tests | ||
run: ./lint.sh | ||
|
||
- name: go build | ||
working-directory: ./tests | ||
run: go build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Tests | ||
These are the files used to test the sqlite database for inaccuracies | ||
|
||
They make assumptions, such as, all translations of the bible will have the same amount of chapters and verses in those chapters | ||
|
||
This assumption is not true, as chapters / verses are not in the original text, and each translation is free to decide how to split it up. | ||
|
||
For the purpose of these tests however, we assume they should all match t_kjv King James Version. | ||
|
||
If we use Crosswire https://crosswire.org/sword/modules/ModDisp.jsp?modType=Bibles as the source of the texts, we can meet these assumptions. | ||
|
||
## Usage | ||
These tests can be run via the include .github/workflows/ actions, or you can run ```go test``` on the command line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/divan/num2words" | ||
"github.com/iancoleman/strcase" | ||
_ "github.com/mattn/go-sqlite3" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func rowQuery(query string, db *sql.DB) *sql.Row { | ||
row := db.QueryRow(query) | ||
return row | ||
} | ||
|
||
func rowsQuery(query string, db *sql.DB) (*sql.Rows, error) { | ||
rows, err := db.Query(query) | ||
return rows, err | ||
} | ||
|
||
func check(e error) { | ||
if e != nil { | ||
fmt.Println(e) | ||
} | ||
} | ||
|
||
func mapIDToBook(db *sql.DB) map[int]string { | ||
bookMap := make(map[int]string) | ||
query := `SELECT b,n FROM key_english;` | ||
rows, err := db.Query(query) | ||
dbCheck(err) | ||
keyEnglish := KeyEnglish{} | ||
for rows.Next() { | ||
err = rows.Scan(&keyEnglish.ID, &keyEnglish.Book) | ||
check(err) | ||
bookMap[keyEnglish.ID] = keyEnglish.Book | ||
} | ||
return bookMap | ||
} | ||
|
||
func dbCheck(e error) { | ||
if e != nil { | ||
if strings.Contains(e.Error(), "no such table:") { | ||
fmt.Println(e) | ||
fmt.Println("This usually means your database doesnt exist, or is corrupt") | ||
os.Exit(1) | ||
} | ||
} | ||
} | ||
|
||
func returnSingleInt(query string) (int, error) { | ||
var result int | ||
db, err := sql.Open("sqlite3", "../bible-sqlite.db?cache=shared&mode=memory") | ||
if err != nil { | ||
return result, err | ||
} | ||
row := rowQuery(query, db) | ||
err = row.Scan(&result) | ||
if err != nil { | ||
return result, err | ||
} | ||
db.Close() | ||
return result, err | ||
} | ||
|
||
func longNames(book string) string { | ||
book = strings.ReplaceAll(book, "1 ", "First") | ||
book = strings.ReplaceAll(book, "2 ", "Second") | ||
book = strings.ReplaceAll(book, "3 ", "Third") | ||
book = strings.ReplaceAll(book, " of ", "Of") | ||
return book | ||
} | ||
|
||
func generateTests(db *sql.DB) { | ||
i := 1 | ||
booksInBible := 66 | ||
bookMap := mapIDToBook(db) | ||
for i <= booksInBible { | ||
bookName := longNames(bookMap[i]) | ||
query := fmt.Sprintf(`select count(distinct c) from t_kjv where b = %d;`, i) | ||
chapters, err := returnSingleInt(query) | ||
check(err) | ||
test := fmt.Sprintf(` | ||
func Test%sChapterCount(t *testing.T) { | ||
expectedCount := %d | ||
query := `+`"`+"select `table` "+`from bible_version_key;"`+` | ||
result := returnArray(t, query) | ||
bookID := %d | ||
for _, bibleVersion := range result { | ||
query := fmt.Sprintf("select count(distinct c) from %%s where b = %%d;", bibleVersion, bookID) | ||
actualCount, err := returnSingleInt(query) | ||
assert.Nil(t, err) | ||
msg := fmt.Sprintf("Should only be %%d chapters in %%s book in %%s translation", expectedCount, bookMap[bookID], bibleVersion) | ||
assert.Equal(t, expectedCount, actualCount, msg) | ||
} | ||
}`, bookName, chapters, i) | ||
fmt.Println(test) | ||
// Test amount of verses | ||
currentChapter := 1 | ||
for currentChapter <= chapters { | ||
prettyChapterName := num2words.Convert(currentChapter) | ||
prettyChapterName = strings.ReplaceAll(prettyChapterName, " ", "") | ||
prettyChapterName = strcase.ToCamel(prettyChapterName) | ||
query := fmt.Sprintf(`select count(distinct v) from t_kjv where b = %d and c = %d;`, i, currentChapter) | ||
expectedVerseCount, err := returnSingleInt(query) | ||
check(err) | ||
verseTest := fmt.Sprintf(` | ||
func Test%sChapter%sTotalVerseCount(t *testing.T) { | ||
expectedCount := %d | ||
query := `+`"`+"select `table` "+`from bible_version_key;"`+` | ||
result := returnArray(t, query) | ||
bookID := %d | ||
chapterID := %d | ||
for _, bibleVersion := range result { | ||
query := fmt.Sprintf("select count(distinct v) from %%s where b = %%d and c = %%d;", bibleVersion, bookID, chapterID) | ||
actualCount, err := returnSingleInt(query) | ||
assert.Nil(t, err) | ||
msg := fmt.Sprintf("Should only be %%d verses in chapter %%d of %%s book in %%s translation", expectedCount, chapterID, bookMap[bookID], bibleVersion) | ||
assert.Equal(t, expectedCount, actualCount, msg) | ||
} | ||
}`, bookName, prettyChapterName, expectedVerseCount, i, currentChapter) | ||
fmt.Println(verseTest) | ||
currentChapter++ | ||
} | ||
i++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/jmainguy/bible_databases/tests | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 // indirect | ||
github.com/iancoleman/strcase v0.1.3 // indirect | ||
github.com/mattn/go-sqlite3 v1.14.7 // indirect | ||
github.com/stretchr/testify v1.7.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 h1:fJ8/Lid8fF4i7Bwl7vWKvG2KeZzr3yU4qG6h/DPdXLU= | ||
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942/go.mod h1:K88GQWK1aAiPMo9q2LZwyKBfEGnge7kmVVTUcZ61HSc= | ||
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw= | ||
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE= | ||
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= | ||
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
FormatCheck=$(gofmt -l *.go | wc -l) | ||
if [ $FormatCheck -gt 0 ]; then | ||
gofmt -l *.go | ||
echo "gofmt -w *.go your code please." | ||
exit 1 | ||
fi | ||
## Run golint | ||
golint -set_exit_status | ||
if [ $? -gt 0 ]; then | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
_ "github.com/mattn/go-sqlite3" | ||
"os" | ||
) | ||
|
||
func main() { | ||
// Open sqlite3 database containing the bibles | ||
db, err := sql.Open("sqlite3", "../bible-sqlite.db") | ||
if err != nil { | ||
fmt.Println("Had trouble opening database") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
generateTests(db) | ||
} |
Oops, something went wrong.