Skip to content

Commit af75009

Browse files
authored
Merge pull request #60 from Jmainguy/sqliteTest
add golang tests for the sqlite database
2 parents a6d1b0e + 1ffd8ac commit af75009

File tree

10 files changed

+19115
-0
lines changed

10 files changed

+19115
-0
lines changed

.github/workflows/push.yml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
on:
2+
push:
3+
4+
name: push
5+
jobs:
6+
golangci:
7+
name: lint
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
with:
12+
fetch-depth: 1
13+
- name: golangci-lint
14+
uses: golangci/golangci-lint-action@v2
15+
with:
16+
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
17+
version: v1.29
18+
args: --timeout=5m
19+
working-directory: ./tests
20+
test:
21+
name: Test with Coverage
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Set up Go
25+
uses: actions/setup-go@v1
26+
with:
27+
go-version: '1.16.3'
28+
29+
- name: Get Build Tools
30+
run: |
31+
GO111MODULE=on go get github.com/ory/go-acc
32+
33+
- name: Add $GOPATH/bin to $PATH
34+
run: |
35+
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
36+
37+
- name: git checkout
38+
uses: actions/checkout@v2
39+
with:
40+
fetch-depth: 1
41+
42+
- name: Install dependencies
43+
working-directory: ./tests
44+
run: |
45+
go mod download
46+
47+
- name: Run Unit tests
48+
working-directory: ./tests
49+
run: |
50+
go-acc .
51+
52+
build:
53+
name: Lint and build
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: install go
57+
uses: actions/setup-go@v2
58+
with:
59+
go-version: '1.16.3'
60+
61+
- name: git checkout
62+
uses: actions/checkout@v2
63+
with:
64+
fetch-depth: 1
65+
66+
- name: install lint
67+
working-directory: ./tests
68+
run: GO111MODULE=off go get golang.org/x/lint/golint
69+
70+
- name: run golint and go fmt
71+
working-directory: ./tests
72+
run: ./lint.sh
73+
74+
- name: go build
75+
working-directory: ./tests
76+
run: go build

tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests

tests/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tests
2+
These are the files used to test the sqlite database for inaccuracies
3+
4+
They make assumptions, such as, all translations of the bible will have the same amount of chapters and verses in those chapters
5+
6+
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.
7+
8+
For the purpose of these tests however, we assume they should all match t_kjv King James Version.
9+
10+
If we use Crosswire https://crosswire.org/sword/modules/ModDisp.jsp?modType=Bibles as the source of the texts, we can meet these assumptions.
11+
12+
## Usage
13+
These tests can be run via the include .github/workflows/ actions, or you can run ```go test``` on the command line

tests/generateTests.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"github.com/divan/num2words"
7+
"github.com/iancoleman/strcase"
8+
_ "github.com/mattn/go-sqlite3"
9+
"os"
10+
"strings"
11+
)
12+
13+
func rowQuery(query string, db *sql.DB) *sql.Row {
14+
row := db.QueryRow(query)
15+
return row
16+
}
17+
18+
func rowsQuery(query string, db *sql.DB) (*sql.Rows, error) {
19+
rows, err := db.Query(query)
20+
return rows, err
21+
}
22+
23+
func check(e error) {
24+
if e != nil {
25+
fmt.Println(e)
26+
}
27+
}
28+
29+
func mapIDToBook(db *sql.DB) map[int]string {
30+
bookMap := make(map[int]string)
31+
query := `SELECT b,n FROM key_english;`
32+
rows, err := db.Query(query)
33+
dbCheck(err)
34+
keyEnglish := KeyEnglish{}
35+
for rows.Next() {
36+
err = rows.Scan(&keyEnglish.ID, &keyEnglish.Book)
37+
check(err)
38+
bookMap[keyEnglish.ID] = keyEnglish.Book
39+
}
40+
return bookMap
41+
}
42+
43+
func dbCheck(e error) {
44+
if e != nil {
45+
if strings.Contains(e.Error(), "no such table:") {
46+
fmt.Println(e)
47+
fmt.Println("This usually means your database doesnt exist, or is corrupt")
48+
os.Exit(1)
49+
}
50+
}
51+
}
52+
53+
func returnSingleInt(query string) (int, error) {
54+
var result int
55+
db, err := sql.Open("sqlite3", "../bible-sqlite.db?cache=shared&mode=memory")
56+
if err != nil {
57+
return result, err
58+
}
59+
row := rowQuery(query, db)
60+
err = row.Scan(&result)
61+
if err != nil {
62+
return result, err
63+
}
64+
db.Close()
65+
return result, err
66+
}
67+
68+
func longNames(book string) string {
69+
book = strings.ReplaceAll(book, "1 ", "First")
70+
book = strings.ReplaceAll(book, "2 ", "Second")
71+
book = strings.ReplaceAll(book, "3 ", "Third")
72+
book = strings.ReplaceAll(book, " of ", "Of")
73+
return book
74+
}
75+
76+
func generateTests(db *sql.DB) {
77+
i := 1
78+
booksInBible := 66
79+
bookMap := mapIDToBook(db)
80+
for i <= booksInBible {
81+
bookName := longNames(bookMap[i])
82+
query := fmt.Sprintf(`select count(distinct c) from t_kjv where b = %d;`, i)
83+
chapters, err := returnSingleInt(query)
84+
check(err)
85+
test := fmt.Sprintf(`
86+
func Test%sChapterCount(t *testing.T) {
87+
expectedCount := %d
88+
query := `+`"`+"select `table` "+`from bible_version_key;"`+`
89+
result := returnArray(t, query)
90+
bookID := %d
91+
for _, bibleVersion := range result {
92+
query := fmt.Sprintf("select count(distinct c) from %%s where b = %%d;", bibleVersion, bookID)
93+
actualCount, err := returnSingleInt(query)
94+
assert.Nil(t, err)
95+
msg := fmt.Sprintf("Should only be %%d chapters in %%s book in %%s translation", expectedCount, bookMap[bookID], bibleVersion)
96+
assert.Equal(t, expectedCount, actualCount, msg)
97+
}
98+
}`, bookName, chapters, i)
99+
fmt.Println(test)
100+
// Test amount of verses
101+
currentChapter := 1
102+
for currentChapter <= chapters {
103+
prettyChapterName := num2words.Convert(currentChapter)
104+
prettyChapterName = strings.ReplaceAll(prettyChapterName, " ", "")
105+
prettyChapterName = strcase.ToCamel(prettyChapterName)
106+
query := fmt.Sprintf(`select count(distinct v) from t_kjv where b = %d and c = %d;`, i, currentChapter)
107+
expectedVerseCount, err := returnSingleInt(query)
108+
check(err)
109+
verseTest := fmt.Sprintf(`
110+
func Test%sChapter%sTotalVerseCount(t *testing.T) {
111+
expectedCount := %d
112+
query := `+`"`+"select `table` "+`from bible_version_key;"`+`
113+
result := returnArray(t, query)
114+
bookID := %d
115+
chapterID := %d
116+
for _, bibleVersion := range result {
117+
query := fmt.Sprintf("select count(distinct v) from %%s where b = %%d and c = %%d;", bibleVersion, bookID, chapterID)
118+
actualCount, err := returnSingleInt(query)
119+
assert.Nil(t, err)
120+
msg := fmt.Sprintf("Should only be %%d verses in chapter %%d of %%s book in %%s translation", expectedCount, chapterID, bookMap[bookID], bibleVersion)
121+
assert.Equal(t, expectedCount, actualCount, msg)
122+
}
123+
}`, bookName, prettyChapterName, expectedVerseCount, i, currentChapter)
124+
fmt.Println(verseTest)
125+
currentChapter++
126+
}
127+
i++
128+
}
129+
}

tests/go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/jmainguy/bible_databases/tests
2+
3+
go 1.16
4+
5+
require (
6+
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 // indirect
7+
github.com/iancoleman/strcase v0.1.3 // indirect
8+
github.com/mattn/go-sqlite3 v1.14.7 // indirect
9+
github.com/stretchr/testify v1.7.0 // indirect
10+
)

tests/go.sum

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 h1:fJ8/Lid8fF4i7Bwl7vWKvG2KeZzr3yU4qG6h/DPdXLU=
4+
github.com/divan/num2words v0.0.0-20170904212200-57dba452f942/go.mod h1:K88GQWK1aAiPMo9q2LZwyKBfEGnge7kmVVTUcZ61HSc=
5+
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
6+
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
7+
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
8+
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tests/lint.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
FormatCheck=$(gofmt -l *.go | wc -l)
3+
if [ $FormatCheck -gt 0 ]; then
4+
gofmt -l *.go
5+
echo "gofmt -w *.go your code please."
6+
exit 1
7+
fi
8+
## Run golint
9+
golint -set_exit_status
10+
if [ $? -gt 0 ]; then
11+
exit 1
12+
fi

tests/main.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
_ "github.com/mattn/go-sqlite3"
7+
"os"
8+
)
9+
10+
func main() {
11+
// Open sqlite3 database containing the bibles
12+
db, err := sql.Open("sqlite3", "../bible-sqlite.db")
13+
if err != nil {
14+
fmt.Println("Had trouble opening database")
15+
fmt.Println(err)
16+
os.Exit(1)
17+
}
18+
19+
generateTests(db)
20+
}

0 commit comments

Comments
 (0)