Skip to content

Commit

Permalink
using testify instead of testing.T methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 8, 2024
1 parent b2692a2 commit 8ec7f41
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 659 deletions.
15 changes: 5 additions & 10 deletions count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ package nostr
import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCount(t *testing.T) {
const RELAY = "wss://relay.nostr.band"

rl := mustRelayConnect(RELAY)
rl := mustRelayConnect(t, RELAY)
defer rl.Close()

count, err := rl.Count(context.Background(), Filters{
{Kinds: []int{KindContactList}, Tags: TagMap{"p": []string{"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"}}},
})
if err != nil {
t.Errorf("count request failed: %v", err)
return
}

if count <= 0 {
t.Errorf("count result wrong: %v", count)
return
}
assert.NoError(t, err)
assert.Greater(t, count, int64(0))
}
151 changes: 66 additions & 85 deletions envelopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package nostr
import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEventEnvelopeEncodingAndDecoding(t *testing.T) {
Expand All @@ -13,67 +15,53 @@ func TestEventEnvelopeEncodingAndDecoding(t *testing.T) {

for _, raw := range eventEnvelopes {
var env EventEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse event envelope json: %v", err)
}

if env.GetID() != env.ID {
t.Errorf("error serializing event id: %s != %s", env.GetID(), env.ID)
}

if ok, _ := env.CheckSignature(); !ok {
t.Error("signature verification failed when it should have succeeded")
}

asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal event as json: %v", err)
}

if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
err := json.Unmarshal([]byte(raw), &env)
assert.NoError(t, err)
assert.Equal(t, env.GetID(), env.ID)

ok, _ := env.CheckSignature()
assert.True(t, ok)

asJSON, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, raw, string(asJSON))
}
}

func TestNoticeEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["NOTICE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
noticeEnv := `["NOTICE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
var env NoticeEnvelope
json.Unmarshal([]byte(src), &env)
if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" {
t.Error("failed to decode NOTICE")
}
err := json.Unmarshal([]byte(noticeEnv), &env)
assert.NoError(t, err)
assert.Equal(t, "kjasbdlasvdluiasvd\"kjasbdksab\\d", env)

if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode NOTICE: expected '%s', got '%s'", src, string(res))
}
res, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, noticeEnv, string(res))
}

func TestEoseEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["EOSE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
eoseEnv := `["EOSE","kjasbdlasvdluiasvd\"kjasbdksab\\d"]`
var env EOSEEnvelope
json.Unmarshal([]byte(src), &env)
if env != "kjasbdlasvdluiasvd\"kjasbdksab\\d" {
t.Error("failed to decode EOSE")
}
err := json.Unmarshal([]byte(eoseEnv), &env)
assert.NoError(t, err)
assert.Equal(t, "kjasbdlasvdluiasvd\"kjasbdksab\\d", env)

if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode EOSE: expected '%s', got '%s'", src, string(res))
}
res, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, eoseEnv, string(res))
}

func TestCountEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["COUNT","z",{"count":12}]`
countEnv := `["COUNT","z",{"count":12}]`
var env CountEnvelope
json.Unmarshal([]byte(src), &env)
if *env.Count != 12 {
t.Error("failed to decode COUNT")
}
err := json.Unmarshal([]byte(countEnv), &env)
assert.NoError(t, err)
assert.Equal(t, 12, *env.Count)

if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode COUNT: expected '%s', got '%s'", src, string(res))
}
res, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, countEnv, string(res))
}

func TestOKEnvelopeEncodingAndDecoding(t *testing.T) {
Expand All @@ -84,35 +72,35 @@ func TestOKEnvelopeEncodingAndDecoding(t *testing.T) {

for _, raw := range okEnvelopes {
var env OKEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse ok envelope json: %v", err)
}

asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal ok as json: %v", err)
}

if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
err := json.Unmarshal([]byte(raw), &env)
assert.NoError(t, err)

asJSON, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, raw, string(asJSON))
}
}

func TestClosedEnvelopeEncodingAndDecoding(t *testing.T) {
for _, src := range []string{
closeEnvelopes := []string{
`["CLOSED","_","error: something went wrong"]`,
`["CLOSED",":1","auth-required: take a selfie and send it to the CIA"]`,
} {
}

for _, raw := range closeEnvelopes {
var env ClosedEnvelope
json.Unmarshal([]byte(src), &env)
if env.SubscriptionID != "_" && env.SubscriptionID != ":1" {
t.Error("failed to decode CLOSED")
}
if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode CLOSED: expected '%s', got '%s'", src, string(res))
}
err := json.Unmarshal([]byte(raw), &env)
assert.NoError(t, err)
assert.Condition(t, func() (success bool) {
if env.SubscriptionID != "_" && env.SubscriptionID != ":1" {
return false
}
return true
})

res, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, raw, string(res))
}
}

Expand All @@ -124,19 +112,12 @@ func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) {

for _, raw := range authEnvelopes {
var env AuthEnvelope
if err := json.Unmarshal([]byte(raw), &env); err != nil {
t.Errorf("failed to parse auth envelope json: %v", err)
}

asjson, err := json.Marshal(env)
if err != nil {
t.Errorf("failed to re marshal auth as json: %v", err)
}

if string(asjson) != raw {
t.Log(string(asjson))
t.Error("json serialization broken")
}
err := json.Unmarshal([]byte(raw), &env)
assert.NoError(t, err)

asJSON, err := json.Marshal(env)
assert.NoError(t, err)
assert.Equal(t, raw, string(asJSON))
}
}

Expand Down Expand Up @@ -184,12 +165,12 @@ func TestParseMessage(t *testing.T) {
if testCase.ExpectedEnvelope == nil && envelope == nil {
return
}
if testCase.ExpectedEnvelope == nil && envelope != nil {
t.Fatalf("expected nil but got %v\n", envelope)
}
if testCase.ExpectedEnvelope.String() != envelope.String() {
t.Fatalf("unexpected output:\n %s\n != %s", testCase.ExpectedEnvelope, envelope)

if testCase.ExpectedEnvelope == nil {
assert.NotNil(t, envelope, "expected nil but got %v\n", envelope)
}

assert.Equal(t, testCase.ExpectedEnvelope.String(), envelope.String())
})
}
}
Expand Down
25 changes: 8 additions & 17 deletions eose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestEOSEMadness(t *testing.T) {
rl := mustRelayConnect(RELAY)
rl := mustRelayConnect(t, RELAY)
defer rl.Close()

sub, err := rl.Subscribe(context.Background(), Filters{
{Kinds: []int{KindTextNote}, Limit: 2},
})
if err != nil {
t.Errorf("subscription failed: %v", err)
return
}
assert.NoError(t, err)

timeout := time.After(3 * time.Second)
n := 0
Expand All @@ -25,15 +24,11 @@ func TestEOSEMadness(t *testing.T) {
for {
select {
case event := <-sub.Events:
if event == nil {
t.Fatalf("event is nil: %v", event)
}
assert.NotNil(t, event)
n++
case <-sub.EndOfStoredEvents:
e++
if e > 1 {
t.Fatalf("eose infinite loop")
}
assert.Less(t, e, 1, "eose infinite loop")
continue
case <-rl.Context().Done():
t.Fatalf("connection closed: %v", rl.Context().Err())
Expand All @@ -43,10 +38,6 @@ func TestEOSEMadness(t *testing.T) {
}

end:
if e != 1 {
t.Fatalf("didn't get an eose")
}
if n < 2 {
t.Fatalf("didn't get events")
}
assert.Equal(t, 1, e)
assert.Greater(t, n, 2)
}
Loading

0 comments on commit 8ec7f41

Please sign in to comment.