Skip to content

Commit

Permalink
Merge pull request #201 from vsimakhin/fix/unit-tests
Browse files Browse the repository at this point in the history
Fix/unit tests
  • Loading branch information
vsimakhin authored Mar 8, 2024
2 parents 5d7fa76 + 5bd1efb commit 4e1d203
Show file tree
Hide file tree
Showing 24 changed files with 239 additions and 426 deletions.
4 changes: 2 additions & 2 deletions cmd/web/handlers_aircraft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestHandlerAircrafts(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetAircraftsAll")
models.AddMock(mock, "GetAircraftsLast")
models.InitMock(mock, "GetAircraftsAll")
models.InitMock(mock, "GetAircraftsLast")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
8 changes: 4 additions & 4 deletions cmd/web/handlers_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestAuth(t *testing.T) {
srv := httptest.NewServer(app.routes())
defer srv.Close()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

// auth enabled
app.isAuthEnabled = true
Expand All @@ -43,7 +43,7 @@ func TestHandlerLogin(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -60,7 +60,7 @@ func TestHandlerLogout(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/handlers_aux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestHandlerNotFound(t *testing.T) {

app, mock := initTestApplication()
models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
6 changes: 3 additions & 3 deletions cmd/web/handlers_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestHandlerExport(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -37,8 +37,8 @@ func TestHandlerExportLogbook(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetFlightRecords")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetFlightRecords")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
14 changes: 7 additions & 7 deletions cmd/web/handlers_flightrecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestHandlerFlightRecordNew(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetAircraftsLast")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetAircraftsLast")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -37,10 +37,10 @@ func TestHandlerFlightRecordByID(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetAircraftsLast")
models.AddMock(mock, "GetFlightRecordByID")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetAircraftsLast")
models.InitMock(mock, "GetFlightRecordByID")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
18 changes: 9 additions & 9 deletions cmd/web/handlers_licensing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestHandlerLicensing(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetLicenses")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetLicenses")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -37,7 +37,7 @@ func TestHandlerLicensingRecordsData(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetLicenses")
models.InitMock(mock, "GetLicenses")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -53,9 +53,9 @@ func TestHandlerLicensingRecordByID(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetLicenseRecordByID")
models.AddMock(mock, "GetLicensesCategory")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetLicenseRecordByID")
models.InitMock(mock, "GetLicensesCategory")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -75,7 +75,7 @@ func TestHandlerLicensingDownload(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetLicenseRecordByID")
models.InitMock(mock, "GetLicenseRecordByID")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -93,8 +93,8 @@ func TestHandlerLicensingRecordNew(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetLicensesCategory")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetLicensesCategory")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
6 changes: 3 additions & 3 deletions cmd/web/handlers_logbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestHandlerLogbook(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetFlightRecords")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetFlightRecords")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -36,7 +36,7 @@ func TestHandlerFlightRecordsData(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetFlightRecords")
models.InitMock(mock, "GetFlightRecords")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
14 changes: 7 additions & 7 deletions cmd/web/handlers_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func TestHandlerMap(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetAircraftsModels")
models.AddMock(mock, "GetAircraftsRegs")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetAircraftsModels")
models.InitMock(mock, "GetAircraftsRegs")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -37,9 +37,9 @@ func TestHandlerMapGetData(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetFlightRecords")
models.AddMock(mock, "GetAirports")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetFlightRecords")
models.InitMock(mock, "GetAirports")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
6 changes: 3 additions & 3 deletions cmd/web/handlers_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestHandlerSettings(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetAirportCount")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetAirportCount")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -38,7 +38,7 @@ func TestHandlerSettingsAircraftClasses(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
38 changes: 19 additions & 19 deletions cmd/web/handlers_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ func TestHandlerStats(t *testing.T) {

// first tab
// totals
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// last 28 days
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// last 90 days
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// this months
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// this year
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")

// second tab
models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetTotalsClassType")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetTotalsClassType")

// third tab
models.AddMock(mock, "GetTotalsClassType")
models.InitMock(mock, "GetTotalsClassType")

// fourth tab
models.AddMock(mock, "GetTotalsYear")
models.InitMock(mock, "GetTotalsYear")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -61,19 +61,19 @@ func TestHandlerStatsTotals(t *testing.T) {
app, mock := initTestApplication()

// totals
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// last 28 days
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// last 90 days
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// this month
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// last 12 months
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")
// this year
models.AddMock(mock, "GetTotals")
models.InitMock(mock, "GetTotals")

models.AddMock(mock, "GetSettings")
models.InitMock(mock, "GetSettings")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -88,8 +88,8 @@ func TestHandlerStatsByClass(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetSettings")
models.AddMock(mock, "GetTotalsClassType")
models.InitMock(mock, "GetSettings")
models.InitMock(mock, "GetTotalsClassType")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand All @@ -104,7 +104,7 @@ func TestHandlerStatsByType(t *testing.T) {

app, mock := initTestApplication()

models.AddMock(mock, "GetTotalsClassType")
models.InitMock(mock, "GetTotalsClassType")

srv := httptest.NewServer(app.routes())
defer srv.Close()
Expand Down
2 changes: 0 additions & 2 deletions cmd/web/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func initTestApplication() (*application, sqlmock.Sqlmock) {
errorLog.Panicf("an error '%s' was not expected when opening a stub database connection", err)
}

models.InitSQLMockValues()

// any order of the queries
mock.MatchExpectationsInOrder(false)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/vsimakhin/web-logbook
go 1.20

require (
github.com/DATA-DOG/go-sqlmock v1.5.1
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/alexedwards/scs/v2 v2.7.0
github.com/go-chi/chi/v5 v5.0.11
github.com/go-pdf/fpdf v0.9.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/DATA-DOG/go-sqlmock v1.5.1 h1:FK6RCIUSfmbnI/imIICmboyQBkOckutaa6R5YYlLZyo=
github.com/DATA-DOG/go-sqlmock v1.5.1/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/alexedwards/scs/v2 v2.7.0 h1:DY4rqLCM7UIR9iwxFS0++z1NhTzQlKV30aMHkJCDWKw=
github.com/alexedwards/scs/v2 v2.7.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -116,6 +118,7 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
6 changes: 3 additions & 3 deletions internal/models/aircrafts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func TestGetAircrafts(t *testing.T) {
db, mock := initDBModel(t)

AddMock(mock, "GetAircraftsLast")
AddMock(mock, "GetAircraftsAll")
InitMock(mock, "GetAircraftsLast")
InitMock(mock, "GetAircraftsAll")

// All aircrafts
aircrafts, err := db.GetAircrafts(AllAircrafts)
Expand All @@ -32,7 +32,7 @@ func TestGetAircrafts(t *testing.T) {
func TestGetAircraftClasses(t *testing.T) {
db, mock := initDBModel(t)

AddMock(mock, "GetSettings")
InitMock(mock, "GetSettings")

classes, err := db.GetAircraftClasses()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/models/airports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestGetAirportByID(t *testing.T) {
db, mock := initDBModel(t)

AddMock(mock, "GetAirportByID")
InitMock(mock, "GetAirportByID")

ap, err := db.GetAirportByID("XXXX")
if err != nil {
Expand All @@ -23,7 +23,7 @@ func TestGetAirportByID(t *testing.T) {
func TestGetAirports(t *testing.T) {
db, mock := initDBModel(t)

AddMock(mock, "GetAirports")
InitMock(mock, "GetAirports")

aps, err := db.GetAirports()
if err != nil {
Expand All @@ -39,7 +39,7 @@ func TestGetAirports(t *testing.T) {
func TestGetAirportCount(t *testing.T) {
db, mock := initDBModel(t)

AddMock(mock, "GetAirportCount")
InitMock(mock, "GetAirportCount")

c, err := db.GetAirportCount()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/models/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (m *DBModel) DeleteAttachmentsForFlightRecord(uuid string) error {
for rows.Next() {

err = rows.Scan(&id)

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4e1d203

Please sign in to comment.