3aeb8630e3
continuous-integration/drone/push Build is passing
- Introduced new functionality in `form_companies.go` to handle company selection from various input formats, including legacy `company_ids` and company summary objects. - Implemented `UnmarshalJSON` methods for `CreateCustomerForm`, `UpdateCustomerForm`, and `AssignCompaniesForm` to support flexible company data parsing. - Added unit tests in `form_companies_test.go` to validate the correct unmarshalling of company IDs and summaries, ensuring robust handling of different input scenarios. - Enhanced the `GetByCompanies` method in the customer repository to directly use string company IDs, improving data retrieval efficiency. This update significantly improves the handling of company data in customer forms, ensuring accurate processing and validation of company selections, while expanding test coverage for these functionalities.
93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package customer
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestCreateCustomerForm_UnmarshalJSON_CompanyIDs(t *testing.T) {
|
|
payload := `{
|
|
"type": "individual",
|
|
"role": "analyst",
|
|
"username": "testuser",
|
|
"email": "test@example.com",
|
|
"password": "Test!1234",
|
|
"company_ids": ["507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"]
|
|
}`
|
|
|
|
var form CreateCustomerForm
|
|
if err := json.Unmarshal([]byte(payload), &form); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
want := []string{"507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"}
|
|
if len(form.Companies) != len(want) {
|
|
t.Fatalf("Companies len = %d, want %d", len(form.Companies), len(want))
|
|
}
|
|
for i, id := range want {
|
|
if form.Companies[i] != id {
|
|
t.Fatalf("Companies[%d] = %q, want %q", i, form.Companies[i], id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUpdateCustomerForm_UnmarshalJSON_CompanySummaries(t *testing.T) {
|
|
payload := `{
|
|
"type": "individual",
|
|
"role": "analyst",
|
|
"username": "testuser",
|
|
"email": "test@example.com",
|
|
"companies": [
|
|
{"id": "507f1f77bcf86cd799439011", "name": "Acme"},
|
|
{"id": "507f1f77bcf86cd799439012", "name": "Globex"}
|
|
]
|
|
}`
|
|
|
|
var form UpdateCustomerForm
|
|
if err := json.Unmarshal([]byte(payload), &form); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if form.Companies == nil {
|
|
t.Fatal("Companies should be set")
|
|
}
|
|
|
|
want := []string{"507f1f77bcf86cd799439011", "507f1f77bcf86cd799439012"}
|
|
if len(*form.Companies) != len(want) {
|
|
t.Fatalf("Companies len = %d, want %d", len(*form.Companies), len(want))
|
|
}
|
|
for i, id := range want {
|
|
if (*form.Companies)[i] != id {
|
|
t.Fatalf("Companies[%d] = %q, want %q", i, (*form.Companies)[i], id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUpdateCustomerForm_UnmarshalJSON_OmittedCompanies(t *testing.T) {
|
|
payload := `{
|
|
"type": "individual",
|
|
"role": "analyst",
|
|
"username": "testuser",
|
|
"email": "test@example.com"
|
|
}`
|
|
|
|
var form UpdateCustomerForm
|
|
if err := json.Unmarshal([]byte(payload), &form); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if form.Companies != nil {
|
|
t.Fatalf("Companies = %v, want nil when omitted", form.Companies)
|
|
}
|
|
}
|
|
|
|
func TestAssignCompaniesForm_UnmarshalJSON_LegacyField(t *testing.T) {
|
|
payload := `{"company_ids": ["507f1f77bcf86cd799439011"]}`
|
|
|
|
var form AssignCompaniesForm
|
|
if err := json.Unmarshal([]byte(payload), &form); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(form.Companies) != 1 || form.Companies[0] != "507f1f77bcf86cd799439011" {
|
|
t.Fatalf("Companies = %v", form.Companies)
|
|
}
|
|
}
|