use testify asserts
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type IDTestCase struct {
|
||||
name string
|
||||
event Event
|
||||
expectedID string
|
||||
name string
|
||||
event Event
|
||||
expected string
|
||||
}
|
||||
|
||||
var idTestCases = []IDTestCase{
|
||||
@@ -20,7 +21,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "13a55672a600398894592f4cb338652d4936caffe5d3718d11597582bb030c39",
|
||||
expected: "13a55672a600398894592f4cb338652d4936caffe5d3718d11597582bb030c39",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -32,7 +33,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "hello world",
|
||||
},
|
||||
expectedID: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
expected: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -44,7 +45,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "hello world 😀",
|
||||
},
|
||||
expectedID: "e42083fafbf9a39f97914fd9a27cedb38c429ac3ca8814288414eaad1f472fe8",
|
||||
expected: "e42083fafbf9a39f97914fd9a27cedb38c429ac3ca8814288414eaad1f472fe8",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -56,7 +57,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "\"You say yes.\"\\n\\t\"I say no.\"",
|
||||
},
|
||||
expectedID: "343de133996a766bf00561945b6f2b2717d4905275976ca75c1d7096b7d1900c",
|
||||
expected: "343de133996a766bf00561945b6f2b2717d4905275976ca75c1d7096b7d1900c",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -68,7 +69,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "{\"field\": [\"value\",\"value\"],\"numeral\": 123}",
|
||||
},
|
||||
expectedID: "c6140190453ee947efb790e70541a9d37c41604d1f29e4185da4325621ed5270",
|
||||
expected: "c6140190453ee947efb790e70541a9d37c41604d1f29e4185da4325621ed5270",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -82,7 +83,7 @@ var idTestCases = []IDTestCase{
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "7d3e394c75916362436f11c603b1a89b40b50817550cfe522a90d769655007a4",
|
||||
expected: "7d3e394c75916362436f11c603b1a89b40b50817550cfe522a90d769655007a4",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -96,7 +97,7 @@ var idTestCases = []IDTestCase{
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "7db394e274fb893edbd9f4aa9ff189d4f3264bf1a29cef8f614e83ebf6fa19fe",
|
||||
expected: "7db394e274fb893edbd9f4aa9ff189d4f3264bf1a29cef8f614e83ebf6fa19fe",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -110,7 +111,7 @@ var idTestCases = []IDTestCase{
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "656b47884200959e0c03054292c453cfc4beea00b592d92c0f557bff765e9d34",
|
||||
expected: "656b47884200959e0c03054292c453cfc4beea00b592d92c0f557bff765e9d34",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -126,7 +127,7 @@ var idTestCases = []IDTestCase{
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "f7c27f2eacda7ece5123a4f82db56145ba59f7c9e6c5eeb88552763664506b06",
|
||||
expected: "f7c27f2eacda7ece5123a4f82db56145ba59f7c9e6c5eeb88552763664506b06",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -140,7 +141,7 @@ var idTestCases = []IDTestCase{
|
||||
},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "fd2798d165d9bf46acbe817735dc8cedacd4c42dfd9380792487d4902539e986",
|
||||
expected: "fd2798d165d9bf46acbe817735dc8cedacd4c42dfd9380792487d4902539e986",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -152,7 +153,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "9ca742f2e2eea72ad6e0277a6287e2bb16a3e47d64b8468bc98474e266cf0ec2",
|
||||
expected: "9ca742f2e2eea72ad6e0277a6287e2bb16a3e47d64b8468bc98474e266cf0ec2",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -164,7 +165,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "4740b027040bb4d0ee8e885f567a80277097da70cddd143d8a6dadf97f6faaa3",
|
||||
expected: "4740b027040bb4d0ee8e885f567a80277097da70cddd143d8a6dadf97f6faaa3",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -176,7 +177,7 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "b28cdd44496acb49e36c25859f0f819122829a12dc57c07612d5f44cb121d2a7",
|
||||
expected: "b28cdd44496acb49e36c25859f0f819122829a12dc57c07612d5f44cb121d2a7",
|
||||
},
|
||||
|
||||
{
|
||||
@@ -188,16 +189,16 @@ var idTestCases = []IDTestCase{
|
||||
Tags: [][]string{},
|
||||
Content: "",
|
||||
},
|
||||
expectedID: "995c4894c264e6b9558cb94b7b34008768d53801b99960b47298d4e3e23fadd3",
|
||||
expected: "995c4894c264e6b9558cb94b7b34008768d53801b99960b47298d4e3e23fadd3",
|
||||
},
|
||||
}
|
||||
|
||||
func TestEventGetId(t *testing.T) {
|
||||
for _, tc := range idTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
computed, err := tc.event.GetID()
|
||||
expectOk(t, err)
|
||||
expectEqualStrings(t, computed, tc.expectedID)
|
||||
actual, err := tc.event.GetID()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package roots
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func TestUnmarshalEventJSON(t *testing.T) {
|
||||
|
||||
func TestMarshalEventJSON(t *testing.T) {
|
||||
eventJSONBytes, err := json.Marshal(testEvent)
|
||||
expectOk(t, err)
|
||||
expectEqualStrings(t, string(eventJSONBytes), testEventJSON)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testEventJSON, string(eventJSONBytes))
|
||||
}
|
||||
|
||||
func TestEventJSONRoundTrip(t *testing.T) {
|
||||
@@ -40,8 +41,8 @@ func TestEventJSONRoundTrip(t *testing.T) {
|
||||
t.Error("test event is invalid")
|
||||
}
|
||||
eventJSON, err := json.Marshal(event)
|
||||
expectOk(t, err)
|
||||
expectEqualStrings(t, string(eventJSON), expectedJSON)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedJSON, string(eventJSON))
|
||||
|
||||
unmarshalledEvent := Event{}
|
||||
json.Unmarshal(eventJSON, &unmarshalledEvent)
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSignEvent(t *testing.T) {
|
||||
eventID := testEvent.ID
|
||||
expectedSig := testEvent.Sig
|
||||
computedSig, err := SignEvent(eventID, testSK)
|
||||
actualSig, err := SignEvent(eventID, testSK)
|
||||
|
||||
expectOk(t, err)
|
||||
expectEqualStrings(t, computedSig, expectedSig)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, actualSig, expectedSig)
|
||||
}
|
||||
|
||||
func TestSignInvalidEventID(t *testing.T) {
|
||||
@@ -19,8 +20,7 @@ func TestSignInvalidEventID(t *testing.T) {
|
||||
|
||||
_, err := SignEvent(badEventID, testSK)
|
||||
|
||||
expectError(t, err)
|
||||
expectErrorSubstring(t, err, expectedError)
|
||||
assert.ErrorContains(t, err, expectedError)
|
||||
}
|
||||
|
||||
func TestSignInvalidPrivateKey(t *testing.T) {
|
||||
@@ -30,6 +30,5 @@ func TestSignInvalidPrivateKey(t *testing.T) {
|
||||
|
||||
_, err := SignEvent(eventID, badSK)
|
||||
|
||||
expectError(t, err)
|
||||
expectErrorSubstring(t, err, expectedError)
|
||||
assert.ErrorContains(t, err, expectedError)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testSK = "f43a0435f69529f310bbd1d6263d2fbf0977f54bfe2310cc37ae5904b83bb167"
|
||||
const testPK = "cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef"
|
||||
|
||||
@@ -15,3 +20,13 @@ var testEvent = Event{
|
||||
|
||||
var testEventJSON = `{"id":"c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad","pubkey":"cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef","created_at":1760740551,"kind":1,"tags":[],"content":"hello world","sig":"83b71e15649c9e9da362c175f988c36404cabf357a976d869102a74451cfb8af486f6088b5631033b4927bd46cad7a0d90d7f624aefc0ac260364aa65c36071a"}`
|
||||
var testEventJSONBytes = []byte(testEventJSON)
|
||||
|
||||
func expectEqualEvents(t *testing.T, got, want Event) {
|
||||
assert.Equal(t, want.ID, got.ID)
|
||||
assert.Equal(t, want.PubKey, got.PubKey)
|
||||
assert.Equal(t, want.CreatedAt, got.CreatedAt)
|
||||
assert.Equal(t, want.Kind, got.Kind)
|
||||
assert.Equal(t, want.Content, got.Content)
|
||||
assert.Equal(t, want.Sig, got.Sig)
|
||||
assert.Equal(t, want.Tags, got.Tags)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -184,10 +185,7 @@ func TestValidateEventStructure(t *testing.T) {
|
||||
for _, tc := range structureTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.event.ValidateStructure()
|
||||
if err == nil {
|
||||
t.Error("expected invalid event structure")
|
||||
}
|
||||
expectErrorSubstring(t, err, tc.expectedError)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -204,7 +202,7 @@ func TestValidateEventIDFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
err := event.ValidateID()
|
||||
expectErrorSubstring(t, err, "does not match computed id")
|
||||
assert.ErrorContains(t, err, "does not match computed id")
|
||||
}
|
||||
|
||||
func TestValidateSignature(t *testing.T) {
|
||||
@@ -213,7 +211,7 @@ func TestValidateSignature(t *testing.T) {
|
||||
publicKey := testEvent.PubKey
|
||||
err := ValidateSignature(eventID, eventSig, publicKey)
|
||||
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateInvalidSignature(t *testing.T) {
|
||||
@@ -222,7 +220,7 @@ func TestValidateInvalidSignature(t *testing.T) {
|
||||
publicKey := testEvent.PubKey
|
||||
err := ValidateSignature(eventID, eventSig, publicKey)
|
||||
|
||||
expectErrorSubstring(t, err, "event signature is invalid")
|
||||
assert.ErrorContains(t, err, "event signature is invalid")
|
||||
}
|
||||
|
||||
type ValidateSignatureTestCase struct {
|
||||
@@ -279,9 +277,7 @@ func TestValidateSignatureInvalidEventSignature(t *testing.T) {
|
||||
for _, tc := range validateSignatureTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := ValidateSignature(tc.id, tc.sig, tc.pubkey)
|
||||
|
||||
expectError(t, err)
|
||||
expectErrorSubstring(t, err, tc.expectedError)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -301,5 +297,5 @@ func TestValidateEvent(t *testing.T) {
|
||||
}
|
||||
|
||||
err := event.Validate()
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@ package roots
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
//"fmt"
|
||||
"reflect"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -586,17 +585,15 @@ func TestFilterMarshalJSON(t *testing.T) {
|
||||
for _, tc := range marshalTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := tc.filter.MarshalJSON()
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var expectedMap, resultMap map[string]interface{}
|
||||
var expectedMap, actualMap map[string]interface{}
|
||||
err = json.Unmarshal([]byte(tc.expected), &expectedMap)
|
||||
expectOk(t, err)
|
||||
err = json.Unmarshal(result, &resultMap)
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
err = json.Unmarshal(result, &actualMap)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if !reflect.DeepEqual(expectedMap, resultMap) {
|
||||
t.Errorf("marshal mismatch: got %s, want %s", result, tc.expected)
|
||||
}
|
||||
assert.Equal(t, expectedMap, actualMap)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -606,7 +603,7 @@ func TestFilterUnmarshalJSON(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var result Filter
|
||||
err := result.UnmarshalJSON([]byte(tc.input))
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expectEqualFilters(t, result, tc.expected)
|
||||
})
|
||||
@@ -617,11 +614,11 @@ func TestFilterRoundTrip(t *testing.T) {
|
||||
for _, tc := range roundTripTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
jsonBytes, err := tc.filter.MarshalJSON()
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var result Filter
|
||||
err = result.UnmarshalJSON(jsonBytes)
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expectEqualFilters(t, result, tc.filter)
|
||||
})
|
||||
@@ -632,120 +629,29 @@ func TestFilterRoundTrip(t *testing.T) {
|
||||
// Helpers
|
||||
|
||||
func expectEqualFilters(t *testing.T, got, want Filter) {
|
||||
// Compare IDs
|
||||
if got.IDs == nil && want.IDs == nil {
|
||||
// pass
|
||||
} else if got.IDs == nil || want.IDs == nil {
|
||||
t.Errorf("mismatched ids: got %v, want %v", got.IDs, want.IDs)
|
||||
} else {
|
||||
expectEqualStringSlices(t, got.IDs, want.IDs)
|
||||
}
|
||||
assert.Equal(t, want.IDs, got.IDs)
|
||||
assert.Equal(t, want.Authors, got.Authors)
|
||||
assert.Equal(t, want.Kinds, got.Kinds)
|
||||
assert.Equal(t, want.Since, got.Since)
|
||||
assert.Equal(t, want.Until, got.Until)
|
||||
assert.Equal(t, want.Limit, got.Limit)
|
||||
assert.Equal(t, want.Tags, got.Tags)
|
||||
|
||||
// Compare Authors
|
||||
if got.Authors == nil && want.Authors == nil {
|
||||
// pass
|
||||
} else if got.Authors == nil || want.Authors == nil {
|
||||
t.Errorf("mismatched authors: got %v, want %v", got.Authors, want.Authors)
|
||||
} else {
|
||||
expectEqualStringSlices(t, got.Authors, want.Authors)
|
||||
if want.Extensions == nil && got.Extensions == nil {
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, got.Extensions)
|
||||
assert.NotNil(t, want.Extensions)
|
||||
|
||||
// Compare Kinds
|
||||
if got.Kinds == nil && want.Kinds == nil {
|
||||
// pass
|
||||
} else if got.Kinds == nil || want.Kinds == nil {
|
||||
t.Errorf("mismatched kinds: got %v, want %v", got.Kinds, want.Kinds)
|
||||
} else {
|
||||
expectEqualIntSlices(t, got.Kinds, want.Kinds)
|
||||
}
|
||||
assert.Equal(t, len(want.Extensions), len(got.Extensions))
|
||||
for key, wantValue := range want.Extensions {
|
||||
gotValue, ok := got.Extensions[key]
|
||||
assert.True(t, ok, "expected key %s", key)
|
||||
|
||||
// Compare Timestamps
|
||||
if got.Since == nil && want.Since == nil {
|
||||
// pass
|
||||
} else if got.Since == nil || want.Since == nil {
|
||||
t.Errorf("mismatched since pointers: got %v, want %v", got.Since, want.Since)
|
||||
} else {
|
||||
expectEqualIntPointers(t, got.Since, want.Since)
|
||||
}
|
||||
|
||||
if got.Until == nil && want.Until == nil {
|
||||
// pass
|
||||
} else if got.Until == nil || want.Until == nil {
|
||||
t.Errorf("mismatched until pointers: got %v, want %v", got.Until, want.Until)
|
||||
} else {
|
||||
expectEqualIntPointers(t, got.Until, want.Until)
|
||||
}
|
||||
|
||||
// Compare Limit
|
||||
if got.Limit == nil && want.Limit == nil {
|
||||
// pass
|
||||
} else if got.Limit == nil || want.Limit == nil {
|
||||
t.Errorf("mismatched limit pointers: got %v, want %v", got.Limit, want.Limit)
|
||||
} else {
|
||||
expectEqualIntPointers(t, got.Limit, want.Limit)
|
||||
}
|
||||
|
||||
// Compare Tags
|
||||
if got.Tags == nil && want.Tags == nil {
|
||||
// pass
|
||||
} else if got.Tags == nil || want.Tags == nil {
|
||||
t.Errorf("mismatched tags: got %v, want %v", got.Tags, want.Tags)
|
||||
} else {
|
||||
expectEqualTags(t, got.Tags, want.Tags)
|
||||
}
|
||||
|
||||
// Compare Extensions
|
||||
if got.Extensions == nil && want.Extensions == nil {
|
||||
// pass
|
||||
} else if got.Extensions == nil || want.Extensions == nil {
|
||||
t.Errorf("mismatched extensions: got %v, want %v", got.Extensions, want.Extensions)
|
||||
} else {
|
||||
expectEqualExtensions(t, got.Extensions, want.Extensions)
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualTags(t *testing.T, got, want map[string][]string) {
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("length mismatch: got %d, want %d", len(got), len(want))
|
||||
}
|
||||
for key, wantValues := range want {
|
||||
gotValues, exists := got[key]
|
||||
if !exists {
|
||||
t.Fatalf("expected key %q to exist", key)
|
||||
}
|
||||
if len(wantValues) != len(gotValues) {
|
||||
t.Errorf(
|
||||
"key %q: length mismatch: got %d, want %d",
|
||||
key, len(gotValues), len(wantValues))
|
||||
}
|
||||
for i := range wantValues {
|
||||
if gotValues[i] != wantValues[i] {
|
||||
t.Errorf(
|
||||
"key %q: index %d: got %s, want %s",
|
||||
key, i, gotValues[i], wantValues[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualExtensions(t *testing.T, got, want map[string]json.RawMessage) {
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("length mismatch: got %d, want %d", len(got), len(want))
|
||||
}
|
||||
for key, wantValue := range want {
|
||||
gotValue, ok := got[key]
|
||||
if !ok {
|
||||
t.Errorf("expected key %s, got nil", key)
|
||||
}
|
||||
var gotJSON, wantJSON interface{}
|
||||
if err := json.Unmarshal(wantValue, &wantJSON); err != nil {
|
||||
t.Errorf("key %q: failed to unmarshal 'want' value: %s", key, wantValue)
|
||||
}
|
||||
if err := json.Unmarshal(gotValue, &gotJSON); err != nil {
|
||||
t.Errorf("key %q: failed to unmarshal 'got' value: %s", key, wantValue)
|
||||
}
|
||||
if !reflect.DeepEqual(gotJSON, wantJSON) {
|
||||
t.Errorf("key %q: got %s, want %s", key, gotValue, wantValue)
|
||||
}
|
||||
assert.NoError(t, json.Unmarshal(wantValue, &wantJSON))
|
||||
assert.NoError(t, json.Unmarshal(gotValue, &gotJSON))
|
||||
assert.Equal(t, wantJSON, gotJSON)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package roots
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -30,14 +31,14 @@ var (
|
||||
type FilterTestCase struct {
|
||||
name string
|
||||
filter Filter
|
||||
matchingIDs []string
|
||||
expectedIDs []string
|
||||
}
|
||||
|
||||
var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "empty filter",
|
||||
filter: Filter{},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -53,7 +54,7 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "empty id",
|
||||
filter: Filter{IDs: []string{}},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -69,31 +70,31 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "single id prefix",
|
||||
filter: Filter{IDs: []string{"e751d41f"}},
|
||||
matchingIDs: []string{"e751d41f"},
|
||||
expectedIDs: []string{"e751d41f"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "single full id",
|
||||
filter: Filter{IDs: []string{"e67fa7b84df6b0bb4c57f8719149de77f58955d7849da1be10b2267c72daad8b"}},
|
||||
matchingIDs: []string{"e67fa7b8"},
|
||||
expectedIDs: []string{"e67fa7b8"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "multiple id prefixes",
|
||||
filter: Filter{IDs: []string{"562bc378", "5e4c64f1"}},
|
||||
matchingIDs: []string{"562bc378", "5e4c64f1"},
|
||||
expectedIDs: []string{"562bc378", "5e4c64f1"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "no id match",
|
||||
filter: Filter{IDs: []string{"ffff"}},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty author",
|
||||
filter: Filter{Authors: []string{}},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -109,13 +110,13 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "single author prefix",
|
||||
filter: Filter{Authors: []string{"d877e187"}},
|
||||
matchingIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
|
||||
expectedIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "multiple author prefixex",
|
||||
filter: Filter{Authors: []string{"d877e187", "9e4b726a"}},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -128,19 +129,19 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "single author full",
|
||||
filter: Filter{Authors: []string{"d877e187934bd942a71221b50ff2b426bd0777991b41b6c749119805dc40bcbe"}},
|
||||
matchingIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
|
||||
expectedIDs: []string{"e751d41f", "562bc378", "e67fa7b8"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "no author match",
|
||||
filter: Filter{Authors: []string{"ffff"}},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty kind",
|
||||
filter: Filter{Kinds: []int{}},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -156,13 +157,13 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "single kind",
|
||||
filter: Filter{Kinds: []int{1}},
|
||||
matchingIDs: []string{"562bc378", "7a5d83d4", "4b03b69a"},
|
||||
expectedIDs: []string{"562bc378", "7a5d83d4", "4b03b69a"},
|
||||
},
|
||||
|
||||
{
|
||||
name: "multiple kinds",
|
||||
filter: Filter{Kinds: []int{0, 2}},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"e67fa7b8",
|
||||
"5e4c64f1",
|
||||
@@ -175,13 +176,13 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "no kind match",
|
||||
filter: Filter{Kinds: []int{99}},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
name: "since only",
|
||||
filter: Filter{Since: intPtr(5000)},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"7a5d83d4",
|
||||
"3a122100",
|
||||
"4a15d963",
|
||||
@@ -193,7 +194,7 @@ var filterTestCases = []FilterTestCase{
|
||||
{
|
||||
name: "until only",
|
||||
filter: Filter{Until: intPtr(3000)},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -206,7 +207,7 @@ var filterTestCases = []FilterTestCase{
|
||||
Since: intPtr(4000),
|
||||
Until: intPtr(6000),
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"5e4c64f1",
|
||||
"7a5d83d4",
|
||||
"3a122100",
|
||||
@@ -218,7 +219,7 @@ var filterTestCases = []FilterTestCase{
|
||||
filter: Filter{
|
||||
Since: intPtr(10000),
|
||||
},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -228,7 +229,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"e": {},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"e751d41f",
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
@@ -248,7 +249,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"e": {"5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"562bc378"},
|
||||
expectedIDs: []string{"562bc378"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -261,7 +262,7 @@ var filterTestCases = []FilterTestCase{
|
||||
},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"562bc378", "3a122100"},
|
||||
expectedIDs: []string{"562bc378", "3a122100"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -274,7 +275,7 @@ var filterTestCases = []FilterTestCase{
|
||||
},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"562bc378"},
|
||||
expectedIDs: []string{"562bc378"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -284,7 +285,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"p": {"91cf9b32f3735070f46c0a86a820a47efa08a5be6c9f4f8cf68e5b5b75c92d60"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"e67fa7b8"},
|
||||
expectedIDs: []string{"e67fa7b8"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -294,7 +295,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"emoji": {"🌊"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"e67fa7b8"},
|
||||
expectedIDs: []string{"e67fa7b8"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -305,7 +306,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"p": {"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{"3a122100"},
|
||||
expectedIDs: []string{"3a122100"},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -315,7 +316,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"p": {"ae3f2a91"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -325,7 +326,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"z": {"anything"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{},
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
|
||||
{
|
||||
@@ -334,7 +335,7 @@ var filterTestCases = []FilterTestCase{
|
||||
Authors: []string{"d877e187"},
|
||||
Kinds: []int{1, 2},
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"562bc378",
|
||||
"e67fa7b8",
|
||||
},
|
||||
@@ -347,7 +348,7 @@ var filterTestCases = []FilterTestCase{
|
||||
Since: intPtr(2000),
|
||||
Until: intPtr(7000),
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"5e4c64f1",
|
||||
"4a15d963",
|
||||
},
|
||||
@@ -361,7 +362,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"power": {"fire"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"4a15d963",
|
||||
},
|
||||
},
|
||||
@@ -377,7 +378,7 @@ var filterTestCases = []FilterTestCase{
|
||||
"power": {"fire"},
|
||||
},
|
||||
},
|
||||
matchingIDs: []string{
|
||||
expectedIDs: []string{
|
||||
"4a15d963",
|
||||
},
|
||||
},
|
||||
@@ -386,14 +387,14 @@ var filterTestCases = []FilterTestCase{
|
||||
func TestEventFilterMatching(t *testing.T) {
|
||||
for _, tc := range filterTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
matchedIDs := []string{}
|
||||
actualIDs := []string{}
|
||||
for _, event := range testEvents {
|
||||
if tc.filter.Matches(&event) {
|
||||
matchedIDs = append(matchedIDs, event.ID[:8])
|
||||
actualIDs = append(actualIDs, event.ID[:8])
|
||||
}
|
||||
}
|
||||
|
||||
expectEqualStringSlices(t, matchedIDs, tc.matchingIDs)
|
||||
assert.Equal(t, tc.expectedIDs, actualIDs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
4
go.mod
4
go.mod
@@ -5,9 +5,13 @@ go 1.23.5
|
||||
require (
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.5
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0
|
||||
github.com/stretchr/testify v1.8.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/decred/dcrd/crypto/blake256 v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
13
go.sum
13
go.sum
@@ -2,9 +2,22 @@ github.com/btcsuite/btcd/btcec/v2 v2.3.5 h1:dpAlnAwmT1yIBm3exhT1/8iUSD98RDJM5vqJ
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.5/go.mod h1:m22FrOAiuxl/tht9wIqAoGHcbnCCaPWyauO8y2LGGtQ=
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U0x++OzVrdms8=
|
||||
github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvwDRwnI3hwNaAHRnc=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
@@ -10,7 +11,7 @@ var hexPattern = regexp.MustCompile("^[a-f0-9]{64}$")
|
||||
func TestGeneratePrivateKey(t *testing.T) {
|
||||
sk, err := GeneratePrivateKey()
|
||||
|
||||
expectOk(t, err)
|
||||
assert.NoError(t, err)
|
||||
if !hexPattern.MatchString(sk) {
|
||||
t.Errorf("invalid private key format: %s", sk)
|
||||
}
|
||||
@@ -19,11 +20,11 @@ func TestGeneratePrivateKey(t *testing.T) {
|
||||
func TestGetPublicKey(t *testing.T) {
|
||||
pk, err := GetPublicKey(testSK)
|
||||
|
||||
expectOk(t, err)
|
||||
expectEqualStrings(t, pk, testPK)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testPK, pk)
|
||||
}
|
||||
|
||||
func TestGetPublicKeyInvalidPrivateKey(t *testing.T) {
|
||||
_, err := GetPublicKey("abc123")
|
||||
expectErrorSubstring(t, err, "private key must be 64 hex characters")
|
||||
assert.ErrorContains(t, err, "private key must be 64 hex characters")
|
||||
}
|
||||
|
||||
89
util_test.go
89
util_test.go
@@ -1,94 +1,5 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func intPtr(i int) *int {
|
||||
return &i
|
||||
}
|
||||
|
||||
func expectOk(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
t.Errorf("got error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func expectError(t *testing.T, err error) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func expectErrorSubstring(t *testing.T, err error, expected string) {
|
||||
if !strings.Contains(err.Error(), expected) {
|
||||
t.Errorf("error = %q, want substring %q", err.Error(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualStrings(t *testing.T, got, want string) {
|
||||
if got != want {
|
||||
t.Errorf("got %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualIntPointers(t *testing.T, got, want *int) {
|
||||
if *got != *want {
|
||||
t.Errorf("got %d, want %d", *got, *want)
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualStringSlices(t *testing.T, got, want []string) {
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("length mismatch: got %d, want %d", len(got), len(want))
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("index %d: got %s, want %s", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func expectEqualIntSlices(t *testing.T, got, want []int) {
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("length mismatch: got %d, want %d", len(got), len(want))
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("index %d: got %d, want %d", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func expectEqualEvents(t *testing.T, got, want Event) {
|
||||
if got.ID != want.ID ||
|
||||
got.PubKey != want.PubKey ||
|
||||
got.CreatedAt != want.CreatedAt ||
|
||||
got.Kind != want.Kind ||
|
||||
got.Content != want.Content ||
|
||||
got.Sig != want.Sig ||
|
||||
!equalTags(got.Tags, want.Tags) {
|
||||
t.Errorf("got %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func equalTags(a, b [][]string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if len(a[i]) != len(b[i]) {
|
||||
return false
|
||||
}
|
||||
for j := range a[i] {
|
||||
if a[i][j] != b[i][j] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user