Skip to content

Commit

Permalink
update build tags to fix test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent 3e07b26 commit 8dc5889
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test and Coverage
name: Test, Vet, and Coverage

on:
push:
Expand Down Expand Up @@ -29,6 +29,14 @@ jobs:
- name: Run go mod download
run: go mod download

- name: Lint Go Code
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Run go vet
run: make vet

- name: Run Tests and Generate Coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# Coverage file
coverage.txt
coverage.out

# Go workspace file
go.work
go.work.sum
Expand Down
46 changes: 22 additions & 24 deletions uuid_test.go → uuidkey_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
//go:build test
// +build test
//go:build !integration
// +build !integration

package uuidkey_test
package uuidkey

import (
"testing"

"github.com/agentstation/uuidkey"

frsuuid "github.com/gofrs/uuid" // test-only dependency
guuid "github.com/google/uuid" // test-only dependency
)

func TestValid(t *testing.T) {
validKeys := []uuidkey.Key{
validKeys := []Key{
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X",
"0000000-0000000-0000000-0000000",
"ZZZZZZZ-ZZZZZZZ-ZZZZZZZ-ZZZZZZZ",
}
invalidKeys := []uuidkey.Key{
invalidKeys := []Key{
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0", // Too short
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0XX", // Too long
"38qarv0-1ET0G6Z-2CJD9VA-2ZZAR0X", // Lowercase
Expand All @@ -42,23 +40,33 @@ func TestValid(t *testing.T) {

func TestFromString(t *testing.T) {
validKey := "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X"
k, err := uuidkey.FromString(validKey)
k, err := FromString(validKey)
if err != nil {
t.Errorf("FromString() returned an error for valid key: %v", err)
}
if k != uuidkey.Key(validKey) {
if k != Key(validKey) {
t.Errorf("FromString() returned incorrect key. Got %s, want %s", k, validKey)
}

invalidKey := "invalid-key"
_, err = uuidkey.FromString(invalidKey)
_, err = FromString(invalidKey)
if err == nil {
t.Errorf("FromString() did not return an error for invalid key")
}
}

func TestEncodeDecode(t *testing.T) {
uuidStr := "d1756360-5da0-40df-9926-a76abff5601d"
key := Encode(uuidStr)
decodedUUID := key.Decode()

if decodedUUID != uuidStr {
t.Errorf("Encode/Decode roundtrip failed. Got %s, want %s", decodedUUID, uuidStr)
}
}

func TestUUIDString(t *testing.T) {
validKey := uuidkey.Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
validKey := Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
expectedUUID := "d1756360-5da0-40df-9926-a76abff5601d"

uuidStr, err := validKey.UUIDString()
Expand All @@ -69,31 +77,21 @@ func TestUUIDString(t *testing.T) {
t.Errorf("UUIDString() returned incorrect UUID string. Got %s, want %s", uuidStr, expectedUUID)
}

invalidKey := uuidkey.Key("invalid-key")
invalidKey := Key("invalid-key")
_, err = invalidKey.UUIDString()
if err == nil {
t.Errorf("UUIDString() did not return an error for invalid key")
}
}

func TestEncodeDecode(t *testing.T) {
uuidStr := "d1756360-5da0-40df-9926-a76abff5601d"
key := uuidkey.Encode(uuidStr)
decodedUUID := key.Decode()

if decodedUUID != uuidStr {
t.Errorf("Encode/Decode roundtrip failed. Got %s, want %s", decodedUUID, uuidStr)
}
}

func TestGoogleUUIDRoundtrip(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using Google's library
originalUUID := guuid.New()
uuidString := originalUUID.String()

// Encode the UUID to our custom key format
key := uuidkey.Encode(uuidString)
key := Encode(uuidString)

// Ensure the key is valid
if !key.Valid() {
Expand Down Expand Up @@ -132,7 +130,7 @@ func TestGofrsUUIDRoundtrip(t *testing.T) {
uuidString := originalUUID.String()

// Encode the UUID to our custom key format
key := uuidkey.Encode(uuidString)
key := Encode(uuidString)

// Ensure the key is valid
if !key.Valid() {
Expand Down

0 comments on commit 8dc5889

Please sign in to comment.