Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - returnUserMap #2352

Closed
tomsmith8 opened this issue Jan 7, 2025 · 3 comments
Closed

[Unit Tests] - returnUserMap #2352

tomsmith8 opened this issue Jan 7, 2025 · 3 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "returnUserMap"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/handlers/auth.go


package auth

import (
  "strings"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stakwork/sphinx-tribes/config"
  "your_project/db" // Replace with the actual import path for the db package
)

// Mock configuration for testing
var testConfig = struct {
  Host string
}{
  Host: "http://localhost",
}

func TestReturnUserMap(t *testing.T) {
  config.Host = testConfig.Host

  tests := []struct {
  	name     string
  	person   db.Person
  	expected map[string]interface{}
  }{
  	{
  		name: "Standard Input Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            "123e4567-e89b-12d3-a456-426614174000",
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     "owner_pubkey_value",
  			OwnerAlias:      "owner_alias_value",
  			OwnerContactKey: "contact_key_value",
  			Img:             "img_url",
  			Description:     "A sample description",
  			Tags:            "tag1,tag2",
  			UniqueName:      "unique_name_value",
  			Extras:          map[string]interface{}{"key": "value"},
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     100,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          "123e4567-e89b-12d3-a456-426614174000",
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  "owner_pubkey_value",
  			"owner_alias":   "owner_alias_value",
  			"contact_key":   "contact_key_value",
  			"img":           "img_url",
  			"description":   "A sample description",
  			"tags":          "tag1,tag2",
  			"unique_name":   "unique_name_value",
  			"pubkey":        "owner_pubkey_value",
  			"extras":        map[string]interface{}{"key": "value"},
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": 100,
  			"alias":         "owner_alias_value",
  			"url":           config.Host,
  		},
  	},
  	{
  		name:   "Empty Fields Test",
  		person: db.Person{},
  		expected: map[string]interface{}{
  			"id":            0,
  			"uuid":          "",
  			"created":       "",
  			"owner_pubkey":  "",
  			"owner_alias":   "",
  			"contact_key":   "",
  			"img":           "",
  			"description":   "",
  			"tags":          "",
  			"unique_name":   "",
  			"pubkey":        "",
  			"extras":        nil,
  			"last_login":    "",
  			"price_to_meet": 0,
  			"alias":         "",
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Maximum Length Strings Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            strings.Repeat("a", 36),
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     strings.Repeat("b", 255),
  			OwnerAlias:      strings.Repeat("c", 255),
  			OwnerContactKey: strings.Repeat("d", 255),
  			Img:             strings.Repeat("e", 255),
  			Description:     strings.Repeat("f", 1024),
  			Tags:            strings.Repeat("g", 255),
  			UniqueName:      strings.Repeat("h", 255),
  			Extras:          map[string]interface{}{"key": strings.Repeat("i", 255)},
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     100,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          strings.Repeat("a", 36),
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  strings.Repeat("b", 255),
  			"owner_alias":   strings.Repeat("c", 255),
  			"contact_key":   strings.Repeat("d", 255),
  			"img":           strings.Repeat("e", 255),
  			"description":   strings.Repeat("f", 1024),
  			"tags":          strings.Repeat("g", 255),
  			"unique_name":   strings.Repeat("h", 255),
  			"pubkey":        strings.Repeat("b", 255),
  			"extras":        map[string]interface{}{"key": strings.Repeat("i", 255)},
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": 100,
  			"alias":         strings.Repeat("c", 255),
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Null Values Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            nil,
  			Created:         nil,
  			OwnerPubKey:     nil,
  			OwnerAlias:      nil,
  			OwnerContactKey: nil,
  			Img:             nil,
  			Description:     nil,
  			Tags:            nil,
  			UniqueName:      nil,
  			Extras:          nil,
  			LastLogin:       nil,
  			PriceToMeet:     0,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          nil,
  			"created":       nil,
  			"owner_pubkey":  nil,
  			"owner_alias":   nil,
  			"contact_key":   nil,
  			"img":           nil,
  			"description":   nil,
  			"tags":          nil,
  			"unique_name":   nil,
  			"pubkey":        nil,
  			"extras":        nil,
  			"last_login":    nil,
  			"price_to_meet": 0,
  			"alias":         nil,
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Large Extras Map Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            "123e4567-e89b-12d3-a456-426614174000",
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     "owner_pubkey_value",
  			OwnerAlias:      "owner_alias_value",
  			OwnerContactKey: "contact_key_value",
  			Img:             "img_url",
  			Description:     "A sample description",
  			Tags:            "tag1,tag2",
  			UniqueName:      "unique_name_value",
  			Extras:          generateLargeMap(10000), // Assume this function generates a large map
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     100,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          "123e4567-e89b-12d3-a456-426614174000",
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  "owner_pubkey_value",
  			"owner_alias":   "owner_alias_value",
  			"contact_key":   "contact_key_value",
  			"img":           "img_url",
  			"description":   "A sample description",
  			"tags":          "tag1,tag2",
  			"unique_name":   "unique_name_value",
  			"pubkey":        "owner_pubkey_value",
  			"extras":        generateLargeMap(10000),
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": 100,
  			"alias":         "owner_alias_value",
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Duplicate Field Values Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            "123e4567-e89b-12d3-a456-426614174000",
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     "owner_pubkey_value",
  			OwnerAlias:      "owner_alias_value",
  			OwnerContactKey: "contact_key_value",
  			Img:             "img_url",
  			Description:     "A sample description",
  			Tags:            "tag1,tag2",
  			UniqueName:      "unique_name_value",
  			Extras:          map[string]interface{}{"key": "value"},
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     100,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          "123e4567-e89b-12d3-a456-426614174000",
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  "owner_pubkey_value",
  			"owner_alias":   "owner_alias_value",
  			"contact_key":   "contact_key_value",
  			"img":           "img_url",
  			"description":   "A sample description",
  			"tags":          "tag1,tag2",
  			"unique_name":   "unique_name_value",
  			"pubkey":        "owner_pubkey_value",
  			"extras":        map[string]interface{}{"key": "value"},
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": 100,
  			"alias":         "owner_alias_value",
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Invalid UUID Format Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            "invalid-uuid-format",
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     "owner_pubkey_value",
  			OwnerAlias:      "owner_alias_value",
  			OwnerContactKey: "contact_key_value",
  			Img:             "img_url",
  			Description:     "A sample description",
  			Tags:            "tag1,tag2",
  			UniqueName:      "unique_name_value",
  			Extras:          map[string]interface{}{"key": "value"},
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     100,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          "invalid-uuid-format",
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  "owner_pubkey_value",
  			"owner_alias":   "owner_alias_value",
  			"contact_key":   "contact_key_value",
  			"img":           "img_url",
  			"description":   "A sample description",
  			"tags":          "tag1,tag2",
  			"unique_name":   "unique_name_value",
  			"pubkey":        "owner_pubkey_value",
  			"extras":        map[string]interface{}{"key": "value"},
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": 100,
  			"alias":         "owner_alias_value",
  			"url":           config.Host,
  		},
  	},
  	{
  		name: "Negative Price Test",
  		person: db.Person{
  			ID:              1,
  			Uuid:            "123e4567-e89b-12d3-a456-426614174000",
  			Created:         "2023-10-01T12:00:00Z",
  			OwnerPubKey:     "owner_pubkey_value",
  			OwnerAlias:      "owner_alias_value",
  			OwnerContactKey: "contact_key_value",
  			Img:             "img_url",
  			Description:     "A sample description",
  			Tags:            "tag1,tag2",
  			UniqueName:      "unique_name_value",
  			Extras:          map[string]interface{}{"key": "value"},
  			LastLogin:       "2023-10-02T12:00:00Z",
  			PriceToMeet:     -50,
  		},
  		expected: map[string]interface{}{
  			"id":            1,
  			"uuid":          "123e4567-e89b-12d3-a456-426614174000",
  			"created":       "2023-10-01T12:00:00Z",
  			"owner_pubkey":  "owner_pubkey_value",
  			"owner_alias":   "owner_alias_value",
  			"contact_key":   "contact_key_value",
  			"img":           "img_url",
  			"description":   "A sample description",
  			"tags":          "tag1,tag2",
  			"unique_name":   "unique_name_value",
  			"pubkey":        "owner_pubkey_value",
  			"extras":        map[string]interface{}{"key": "value"},
  			"last_login":    "2023-10-02T12:00:00Z",
  			"price_to_meet": -50,
  			"alias":         "owner_alias_value",
  			"url":           config.Host,
  		},
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		result := returnUserMap(tt.person)
  		assert.Equal(t, tt.expected, result)
  	})
  }
}

// Helper function to generate a large map for testing
func generateLargeMap(size int) map[string]interface{} {
  largeMap := make(map[string]interface{}, size)
  for i := 0; i < size; i++ {
  	largeMap[string(i)] = i
  }
  return largeMap
}
@MirzaHanan
Copy link
Contributor

MirzaHanan commented Jan 7, 2025

@tomsmith8 assign me?

@AhsanFarooqDev
Copy link
Contributor

@tomsmith8 please assign

@humansinstitute
Copy link
Contributor

duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants