Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,48 @@

Just some fun stuff with golang

## Running Tests

To run all tests:

```bash
go test ./...
```

To run tests with verbose output:

```bash
go test -v ./...
```

To run benchmarks:

```bash
go test -bench=. ./tinkering
```

### Token Package Integration Tests

The token package includes integration tests that require a PostgreSQL database. To run these tests:

1. Start the database:
```bash
cd token
docker-compose up -d
```

2. Remove the skip statement in `token/token_test.go` (line 10: `t.Skip()`)

3. Run the tests:
```bash
go test ./token -v
```

4. Stop the database when done:
```bash
cd token
docker-compose down
```

## Running

47 changes: 47 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,55 @@ package token

import (
"testing"
"time"
)

// TestTokenHelpers tests helper functions that don't require database
func TestTokenHelpers(t *testing.T) {
t.Run("tokenToArgs and createToken roundtrip", func(t *testing.T) {
token := &Token{limit: 2}

// Create test data
testTime := time.Date(2024, 1, 15, 12, 30, 45, 123456789, time.UTC)
testFoos := []Foo{
{ID: 1, Data: "first", UpdatedAt: testTime},
{ID: 2, Data: "second", UpdatedAt: testTime},
{ID: 3, Data: "third", UpdatedAt: testTime},
}

// Create token from foos
tokenStr := token.createToken(testFoos)

// Parse it back
id, parsedTime := token.tokenToArgs(tokenStr)

// Verify the roundtrip
if id != 3 {
t.Errorf("expected id 3, got %d", id)
}
if !parsedTime.Equal(testTime) {
t.Errorf("expected time %v, got %v", testTime, parsedTime)
}
})

t.Run("toJSON creates valid json", func(t *testing.T) {
testTime := time.Date(2024, 1, 15, 12, 30, 45, 0, time.UTC)
testFoos := []Foo{
{ID: 1, Data: "test", CreatedAt: testTime, UpdatedAt: testTime},
}

result := toJSON(testFoos, "old_token", "new_token")

// Basic validation that we got JSON back
if len(result) == 0 {
t.Error("expected non-empty JSON result")
}
if result[0] != '{' {
t.Error("expected JSON to start with {")
}
})
}

// TestToken tests some implementations of selecting records with a token
// each record should be returned to client at least once (dups are ok)
func TestToken(t *testing.T) {
Expand Down