Skip to content

Commit

Permalink
iter12 features
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0rcist committed Aug 5, 2024
1 parent 522468a commit 00a8547
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
45 changes: 45 additions & 0 deletions internal/storage/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,51 @@ func TestDatabaseStorage_Get(t *testing.T) {
mockRow.AssertExpectations(t)
}

func TestDatabaseStorage_List(t *testing.T) {
mockPool := NewPGXPoolMock()
storage := DatabaseStorage{Pool: mockPool}

ctx := context.Background()
expectedRecords := []Record{
{Name: "name1", Value: metrics.Counter(123)},
{Name: "name2", Value: metrics.Gauge(456)},
}

mockRows := new(PGXRowsMock)
mockPool.On("Query", ctx, mock.AnythingOfType("string"), []interface{}(nil)).Return(mockRows, nil)
mockRows.On("Next").Return(true).Twice()
mockRows.On("Next").Return(false)

counter := 0
mockRows.On("Scan", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
rec := expectedRecords[counter]
*args.Get(0).(*string) = rec.Name
*args.Get(1).(*string) = rec.Value.Kind()

switch expectedRecords[counter].Value.Kind() {
case metrics.KindCounter:
value, _ := rec.Value.(metrics.Counter)
*args.Get(2).(*float64) = float64(value)
case metrics.KindGauge:
value, _ := rec.Value.(metrics.Gauge)
*args.Get(2).(*float64) = float64(value)
}

counter++
}).Twice().Return(nil)
mockRows.On("Err").Return(nil)
mockRows.On("Close").Return(nil)
mockRows.On("CommandTag").Return(pgconn.NewCommandTag("select"))

records, err := storage.List(ctx)

assert.NoError(t, err)
assert.Equal(t, expectedRecords, records)

mockPool.AssertExpectations(t)
mockRows.AssertExpectations(t)
}

func TestDatabaseStorage_Ping(t *testing.T) {
mockPool := NewPGXPoolMock()
storage := DatabaseStorage{Pool: mockPool}
Expand Down
5 changes: 2 additions & 3 deletions internal/storage/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestAsyncDumping(t *testing.T) {
ctx := context.Background()

storePath := "test_store.json"
defer removeFile(t, storePath)

fs, err := NewFileStorage(storePath, 1, false)
checkNoError(t, err, "failed to create new FileStorage")
Expand All @@ -105,7 +106,7 @@ func TestAsyncDumping(t *testing.T) {
err = fs.Push(ctx, record.CalculateRecordID(), record)
checkNoError(t, err, "failed to push record")

time.Sleep(1000 * time.Millisecond)
time.Sleep(1200 * time.Millisecond)

err = fs.Close(ctx)
checkNoError(t, err, "failed to close fs")
Expand All @@ -117,8 +118,6 @@ func TestAsyncDumping(t *testing.T) {
err = json.Unmarshal(data, &ms)
checkNoError(t, err, "failed to unmarshal storage file")

defer removeFile(t, storePath)

if restored, err := ms.Get(ctx, record.CalculateRecordID()); err != nil || restored != record {
t.Errorf("expected record %v, got %v", record, restored)
}
Expand Down

0 comments on commit 00a8547

Please sign in to comment.