1

I need to write tests for that function:

func Parse(f *multipart.Form) ([]Person, error)

It's important to say, that multipart.Form.Value is not just key:value, thats more complicated json, simple example:

[
    {
        "value": {
            "person_name__0": "Mike",
            "person_name__1": "John"
        },
        "question": {
            "id": 52681363,
            "slug": "person_name"
        }
    },
    {
        "value": {
            "created_date__0": "2024-06-26",
            "created_date__1": "2024-06-24"
        },
        "question": {
            "id": 52681362,
            "slug": "created_date"
        }
    },
    {
        "value": "Germany",
        "question": {
            "id": 52681360,
            "slug": "country"
        }
    }
]

What is the best way to test that func? Should I create json files(or just json strings in code) manually, make from them multipart.Form objects or should I create such files automatically(I believe this is a hard one)? Or I can somehow create mock objects? But how can I create mock multipart.Form objects, that will have the same .Value structure(I mean same json structure, like in my example)?

Thats my first try to test something, may be there's stupid questions, but I hope you can tell me whats wrong and help me out=)

3
  • What would be your approach? What have you tried thus far? How would you imagine an ideal solution? Commented Jun 30 at 20:48
  • @Cerise Limón should I write jsons manually? (I mean write, for example, 10 json strings manually and use them) Or it will be better if I will create them automatically?
    – kirin
    Commented Jun 30 at 20:59
  • @Markus W Mahlberg I Just want to understand what is the perfect way to test that function. My ideal solution is if I will somehow generate(automatically) plenty of jsons and use them
    – kirin
    Commented Jun 30 at 21:00

1 Answer 1

3

Use text. Keep it simple.

func TestExample(t *testing.T) {

    data := `--XXX
Content-Disposition: form-data; name="text"

a text value
--XXX
Content-Disposition: form-data; name="file"; filename="a.txt"
Content-Type: text/json

[
    {
        "value": {
            "person_name__0": "Mike",
            "person_name__1": "John"
        },
        "question": {
            "id": 52681363,
            "slug": "person_name"
        }
    },
    {
        "value": {
            "created_date__0": "2024-06-26",
            "created_date__1": "2024-06-24"
        },
        "question": {
            "id": 52681362,
            "slug": "created_date"
        }
    },
    {
        "value": "Germany",
        "question": {
            "id": 52681360,
            "slug": "country"
        }
    }
]
--XXX--`

    form, err := multipart.NewReader(strings.NewReader(data), "XXX").ReadForm(1024 * 1024)
    if err != nil {
        t.Fatal(err)
    }
    people, err := Parse(form)
    // Check for expected values here.

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.