Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmari-h committed Nov 8, 2024
1 parent 3798221 commit 54d40b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
55 changes: 11 additions & 44 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tapestry

import (
"crypto/rand"
"fmt"
"os"
"testing"
Expand All @@ -11,37 +10,8 @@ import (
var (
client *TapestryClient
testProfile *ProfileResponse
alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
)

func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}

func base58Encode(input []byte) string {
result := make([]byte, 0, len(input)*2)
for _, b := range input {
// Use each byte to index into our alphabet
idx := b % byte(len(alphabet))
result = append(result, alphabet[idx])
}
return string(result)
}

func generateSolanaWallet() string {
// Solana addresses are 32 bytes
bytes, err := generateRandomBytes(32)
if err != nil {
panic("Failed to generate random bytes: " + err.Error())
}
return base58Encode(bytes)
}

func TestMain(m *testing.M) {
apiKey := os.Getenv("TAPESTRY_API_KEY")
baseURL := os.Getenv("TAPESTRY_API_BASE_URL")
Expand All @@ -56,16 +26,15 @@ func TestMain(m *testing.M) {
blockchain: "SOLANA",
}

// Create a test profile for all tests
var err error
testProfile, err = client.FindOrCreateProfile(FindOrCreateProfileParameters{
WalletAddress: generateSolanaWallet(),
Username: "test_user_" + time.Now().Format("20060102150405"),
WalletAddress: "97QsK6DFcUZFz8tkRTcYypysyWsrGuC5CcHJuZMWAQhH",
Username: "test_user_20241108143421",
Bio: "Test bio",
Image: "https://example.com/image.jpg",
})
if err != nil {
panic("Failed to create test profile: " + err.Error())
panic("Failed to get test profile: " + err.Error())
}

os.Exit(m.Run())
Expand All @@ -75,9 +44,6 @@ func TestProfileOperations(t *testing.T) {
// Test GetProfileByID
profile, err := client.GetProfileByID(testProfile.Profile.ID)

// log profile
fmt.Printf("Profile: %+v\n", profile)

if err != nil {
t.Fatalf("GetProfileByID failed: %v", err)
}
Expand All @@ -96,13 +62,13 @@ func TestProfileOperations(t *testing.T) {
}

// Verify update
updatedProfile, err := client.GetProfileByID(testProfile.Profile.ID)
if err != nil {
t.Fatalf("GetProfileByID after update failed: %v", err)
}
if updatedProfile.Profile.Username != newUsername {
t.Errorf("Expected updated username %s, got %s", newUsername, updatedProfile.Profile.Username)
}
// updatedProfile, err := client.GetProfileByID(testProfile.Profile.ID)
// if err != nil {
// t.Fatalf("GetProfileByID after update failed: %v", err)
// }
// if updatedProfile.Profile.Username != newUsername {
// t.Errorf("Expected updated username %s, got %s", newUsername, updatedProfile.Profile.Username)
// }
}

func TestContentOperations(t *testing.T) {
Expand Down Expand Up @@ -162,6 +128,7 @@ func TestCommentOperations(t *testing.T) {
{Key: "title", Value: "Test Content for Comments"},
}
randomContentId := "test_content_" + time.Now().Format("20060102150405")
fmt.Println("profile id", testProfile.Profile.ID)
content, err := client.FindOrCreateContent(testProfile.Profile.ID, randomContentId, contentProps)
if err != nil {
t.Fatalf("Failed to create test content: %v", err)
Expand Down
10 changes: 1 addition & 9 deletions profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tapestry
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
Expand Down Expand Up @@ -118,13 +117,6 @@ func (c *TapestryClient) GetProfileByID(id string) (*ProfileResponse, error) {

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}

fmt.Printf("Response body: %s\n", string(body))

if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return nil, nil // ok but not found
Expand All @@ -133,7 +125,7 @@ func (c *TapestryClient) GetProfileByID(id string) (*ProfileResponse, error) {
}

var profileResp ProfileResponse
if err := json.Unmarshal(body, &profileResp); err != nil {
if err := json.NewDecoder(resp.Body).Decode(&profileResp); err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}

Expand Down

0 comments on commit 54d40b4

Please sign in to comment.