|
| 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 | +} |
0 commit comments