Skip to content

Commit

Permalink
fix(mysql): Correcting where we calculate the time since (#123)
Browse files Browse the repository at this point in the history
* Correcting where we calculate the time since

* Import sort
  • Loading branch information
Jacobbrewer1 authored Apr 25, 2024
1 parent 7fbcd3d commit 0150b1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
5 changes: 4 additions & 1 deletion pkg/dataaccess/db_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (m *mysqlImpl) GetHistory(ctx context.Context, environment ...summary.Envir
}

// Parse the date.
dt, err := time.Parse(time.DateOnly, d)
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
slog.Error("Error parsing date", slog.String(logging.KeyError, err.Error()))
continue
Expand Down Expand Up @@ -319,6 +319,9 @@ ORDER by executed_at DESC;
&report.Failed, &report.Changed, &report.Total, &report.YamlFile); err != nil {
return nil, fmt.Errorf("error scanning rows: %w", err)
}

report.CalculateTimeSince()

reports = append(reports, report)
}

Expand Down
32 changes: 21 additions & 11 deletions pkg/dataaccess/db_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Jacobbrewer1/puppet-summary/pkg/codegen/apis/summary"
"github.com/Jacobbrewer1/puppet-summary/pkg/entities"
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -123,9 +124,9 @@ func (s *mysqlSuite) TestGetHistoryAllEnvs() {
s.mockDB.ExpectPrepare(expSql1)

rows1 := sqlmock.NewRows([]string{"DATE(executed_at)"}).
AddRow("2023-02-21").
AddRow("2023-02-22").
AddRow("2023-02-23")
AddRow("2023-02-21T00:00:00Z").
AddRow("2023-02-22T00:00:00Z").
AddRow("2023-02-23T00:00:00Z")

s.mockDB.ExpectQuery(expSql1).
WillReturnRows(rows1)
Expand Down Expand Up @@ -220,9 +221,9 @@ func (s *mysqlSuite) TestGetHistoryMultipleEnv() {
s.mockDB.ExpectPrepare(expSql1)

rows1 := sqlmock.NewRows([]string{"DATE(executed_at)"}).
AddRow("2023-02-21").
AddRow("2023-02-22").
AddRow("2023-02-23")
AddRow("2023-02-21T00:00:00Z").
AddRow("2023-02-22T00:00:00Z").
AddRow("2023-02-23T00:00:00Z")

s.mockDB.ExpectQuery(expSql1).
WillReturnRows(rows1)
Expand Down Expand Up @@ -317,9 +318,9 @@ func (s *mysqlSuite) TestGetHistorySingleEnv() {
s.mockDB.ExpectPrepare(expSql1)

rows1 := sqlmock.NewRows([]string{"DATE(executed_at)"}).
AddRow("2023-02-21").
AddRow("2023-02-22").
AddRow("2023-02-23")
AddRow("2023-02-21T00:00:00Z").
AddRow("2023-02-22T00:00:00Z").
AddRow("2023-02-23T00:00:00Z")

s.mockDB.ExpectQuery(expSql1).
WillReturnRows(rows1)
Expand Down Expand Up @@ -496,7 +497,7 @@ ORDER by executed_at DESC;
report, err := s.dbObject.GetReports(ctx, "fqdn")
s.Require().NoError(err)

s.Require().Equal([]*entities.PuppetReportSummary{
reps := []*entities.PuppetReportSummary{
{
ID: id1,
Fqdn: "fqdn",
Expand All @@ -521,7 +522,16 @@ ORDER by executed_at DESC;
Total: 3,
YamlFile: "yaml_file1",
},
}, report)
}

// Ensure that the time since field is set.
for i := range report {
r := report[i]
require.NotEqual(s.T(), 0, r.TimeSince.Time())
r.TimeSince = entities.Duration(0)
}

s.Require().Equal(reps, report)
}

func (s *mysqlSuite) TestGetRunsByStateSingleState() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/dataaccess/db_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ func (s *sqliteImpl) GetReports(ctx context.Context, fqdn string) ([]*entities.P
&report.Failed, &report.Changed, &report.Total, &report.YamlFile); err != nil {
return nil, fmt.Errorf("error scanning row: %w", err)
}

report.CalculateTimeSince()

reports = append(reports, report)
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/dataaccess/db_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/Jacobbrewer1/puppet-summary/pkg/codegen/apis/summary"
"github.com/Jacobbrewer1/puppet-summary/pkg/entities"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -497,7 +498,7 @@ func (s *sqliteSuite) TestGetReports() {
report, err := s.dbObject.GetReports(ctx, "fqdn")
s.Require().NoError(err)

s.Require().Equal([]*entities.PuppetReportSummary{
reps := []*entities.PuppetReportSummary{
{
ID: id1,
Fqdn: "fqdn",
Expand All @@ -522,7 +523,16 @@ func (s *sqliteSuite) TestGetReports() {
Total: 3,
YamlFile: "yaml_file1",
},
}, report)
}

// Ensure that the time since field is set.
for i := range report {
r := report[i]
require.NotEqual(s.T(), 0, r.TimeSince.Time())
r.TimeSince = entities.Duration(0)
}

s.Require().Equal(reps, report)
}

func (s *sqliteSuite) TestGetRunsByStateSingleState() {
Expand Down

0 comments on commit 0150b1f

Please sign in to comment.