Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
horgh committed Mar 25, 2024
1 parent 54897d4 commit d33cbdb
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 125 deletions.
52 changes: 26 additions & 26 deletions client/download_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package client

import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -10,8 +15,7 @@ import (
"github.com/stretchr/testify/require"
)

// TestRead checks the database download functionality.
func TestRead(t *testing.T) {
func TestDownload(t *testing.T) {
edition := metadata{
EditionID: "edition-1",
Date: "2024-02-02",
Expand Down Expand Up @@ -40,7 +44,7 @@ func TestRead(t *testing.T) {
description string
preserveFileTime bool
server func(t *testing.T) *httptest.Server
checkResult func(t *testing.T, resp *ReadResult, err error)
checkResult func(t *testing.T, res DownloadResponse, err error)
}{
{
description: "successful download",
Expand Down Expand Up @@ -82,16 +86,13 @@ func TestRead(t *testing.T) {

return server
},
checkResult: func(t *testing.T, resp *ReadResult, err error) {
checkResult: func(t *testing.T, res DownloadResponse, err error) {
require.NoError(t, err)
c, rerr := io.ReadAll(resp.reader)
c, rerr := io.ReadAll(res.Reader)
require.NoError(t, rerr)
require.Equal(t, dbContent, string(c))
require.Equal(t, edition.EditionID, resp.EditionID)
require.Equal(t, edition.MD5, resp.OldHash)
require.Equal(t, "618dd27a10de24809ec160d6807f363f", resp.NewHash)

require.Equal(t, lastModified, resp.ModifiedAt)
require.Equal(t, "618dd27a10de24809ec160d6807f363f", res.MD5)
require.Equal(t, lastModified, res.LastModified)
},
},
{
Expand All @@ -108,8 +109,7 @@ func TestRead(t *testing.T) {
}))
return server
},
checkResult: func(t *testing.T, resp *ReadResult, err error) {
require.Nil(t, resp)
checkResult: func(t *testing.T, _ DownloadResponse, err error) {
require.Error(t, err)
require.Regexp(t, "^unexpected HTTP status code", err.Error())
},
Expand All @@ -132,8 +132,7 @@ func TestRead(t *testing.T) {
}))
return server
},
checkResult: func(t *testing.T, resp *ReadResult, err error) {
require.Nil(t, resp)
checkResult: func(t *testing.T, _ DownloadResponse, err error) {
require.Error(t, err)
require.Regexp(t, "^encountered an error creating GZIP reader", err.Error())
},
Expand Down Expand Up @@ -166,8 +165,7 @@ func TestRead(t *testing.T) {

return server
},
checkResult: func(t *testing.T, resp *ReadResult, err error) {
require.Nil(t, resp)
checkResult: func(t *testing.T, _ DownloadResponse, err error) {
require.Error(t, err)
require.Regexp(t, "^tar archive does not contain an mmdb file", err.Error())
},
Expand Down Expand Up @@ -211,30 +209,32 @@ func TestRead(t *testing.T) {

return server
},
checkResult: func(t *testing.T, resp *ReadResult, err error) {
require.Nil(t, resp)
checkResult: func(t *testing.T, _ DownloadResponse, err error) {
require.Error(t, err)
require.Regexp(t, "^tar archive does not contain an mmdb file", err.Error())
},
},
}

ctx := context.Background()

accountID := 10
licenseKey := "license"

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
server := test.server(t)
defer server.Close()

r := NewHTTPReader(
server.URL, // fixed, as the server is mocked above.
10, // fixed, as it's not valuable for the purpose of the test.
"license", // fixed, as it's not valuable for the purpose of the test.
false, // verbose
http.DefaultClient,
c, err := New(
accountID,
licenseKey,
WithEndpoint(server.URL),
)
require.NoError(t, err)

reader, err := r.get(ctx, edition.EditionID, edition.MD5)
test.checkResult(t, reader, err)
res, err := c.Download(ctx, edition.EditionID, edition.MD5)
test.checkResult(t, res, err)
})
}
}
16 changes: 9 additions & 7 deletions client/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,22 @@ func TestGetMetadata(t *testing.T) {

ctx := context.Background()

accountID := 10
licenseKey := "license"

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
server := test.server(t)
defer server.Close()

r := NewHTTPReader(
server.URL, // fixed, as the server is mocked above.
10, // fixed, as it's not valuable for the purpose of the test.
"license", // fixed, as it's not valuable for the purpose of the test.
false, // verbose
http.DefaultClient,
c, err := New(
accountID,
licenseKey,
WithEndpoint(server.URL),
)
require.NoError(t, err)

result, err := r.getMetadata(ctx, "edition-1")
result, err := c.getMetadata(ctx, "edition-1")
test.checkResult(t, result, err)
})
}
Expand Down
80 changes: 35 additions & 45 deletions internal/geoipupdate/database/local_file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,67 @@ func TestLocalFileWriterWrite(t *testing.T) {
checkErr func(require.TestingT, error, ...interface{})
preserveFileTime bool
//nolint:revive // support older versions
checkTime func(require.TestingT, interface{}, interface{}, ...interface{})
result *ReadResult
checkTime func(require.TestingT, interface{}, interface{}, ...interface{})
editionID string
reader io.ReadCloser
newMD5 string
lastModified time.Time
}{
{
description: "success",
checkErr: require.NoError,
preserveFileTime: true,
checkTime: require.Equal,
result: &ReadResult{
reader: io.NopCloser(strings.NewReader("database content")),
EditionID: "GeoIP2-City",
OldHash: "",
NewHash: "cfa36ddc8279b5483a5aa25e9a6151f4",
ModifiedAt: testTime,
},
editionID: "GeoIP2-City",
reader: io.NopCloser(strings.NewReader("database content")),
newMD5: "cfa36ddc8279b5483a5aa25e9a6151f4",
lastModified: testTime,
}, {
description: "hash does not match",
checkErr: require.Error,
preserveFileTime: true,
checkTime: require.Equal,
result: &ReadResult{
reader: io.NopCloser(strings.NewReader("database content")),
EditionID: "GeoIP2-City",
OldHash: "",
NewHash: "badhash",
ModifiedAt: testTime,
},
editionID: "GeoIP2-City",
reader: io.NopCloser(strings.NewReader("database content")),
newMD5: "badhash",
lastModified: testTime,
}, {
description: "hash case does not matter",
checkErr: require.NoError,
preserveFileTime: true,
checkTime: require.Equal,
result: &ReadResult{
reader: io.NopCloser(strings.NewReader("database content")),
EditionID: "GeoIP2-City",
OldHash: "",
NewHash: "cfa36ddc8279b5483a5aa25e9a6151f4",
ModifiedAt: testTime,
},
editionID: "GeoIP2-City",
reader: io.NopCloser(strings.NewReader("database content")),
newMD5: "cfa36ddc8279b5483a5aa25e9a6151f4",
lastModified: testTime,
}, {
description: "do not preserve file modification time",
checkErr: require.NoError,
preserveFileTime: false,
checkTime: require.NotEqual,
result: &ReadResult{
reader: io.NopCloser(strings.NewReader("database content")),
EditionID: "GeoIP2-City",
OldHash: "",
NewHash: "CFA36DDC8279B5483A5AA25E9A6151F4",
ModifiedAt: testTime,
},
editionID: "GeoIP2-City",
reader: io.NopCloser(strings.NewReader("database content")),
newMD5: "CFA36DDC8279B5483A5AA25E9A6151F4",
lastModified: testTime,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
tempDir := t.TempDir()
defer test.result.reader.Close()

fw, err := NewLocalFileWriter(tempDir, test.preserveFileTime, false)
require.NoError(t, err)

err = fw.Write(test.result)
err = fw.Write(
test.editionID,
test.reader,
test.newMD5,
test.lastModified,
)
test.checkErr(t, err)
if err == nil {
database, err := os.Stat(fw.getFilePath(test.result.EditionID))
database, err := os.Stat(fw.getFilePath(test.editionID))
require.NoError(t, err)

test.checkTime(t, database.ModTime().UTC(), testTime)
Expand All @@ -96,28 +91,23 @@ func TestLocalFileWriterWrite(t *testing.T) {

// TestLocalFileWriterGetHash tests functionality of the LocalFileWriter.GetHash method.
func TestLocalFileWriterGetHash(t *testing.T) {
result := &ReadResult{
reader: io.NopCloser(strings.NewReader("database content")),
EditionID: "GeoIP2-City",
OldHash: "",
NewHash: "cfa36ddc8279b5483a5aa25e9a6151f4",
ModifiedAt: time.Time{},
}
editionID := "GeoIP2-City"
reader := io.NopCloser(strings.NewReader("database content"))
newMD5 := "cfa36ddc8279b5483a5aa25e9a6151f4"
lastModified := time.Time{}

tempDir := t.TempDir()

defer result.reader.Close()

fw, err := NewLocalFileWriter(tempDir, false, false)
require.NoError(t, err)

err = fw.Write(result)
err = fw.Write(editionID, reader, newMD5, lastModified)
require.NoError(t, err)

// returns the correct hash for an existing database.
hash, err := fw.GetHash(result.EditionID)
hash, err := fw.GetHash(editionID)
require.NoError(t, err)
require.Equal(t, hash, result.NewHash)
require.Equal(t, hash, newMD5)

// returns a zero hash for a non existing edition.
hash, err = fw.GetHash("NewEdition")
Expand Down
Loading

0 comments on commit d33cbdb

Please sign in to comment.