Framework

Go Test Data Generation with MockHero

The Script

package main

import (
  "bytes"
  "encoding/json"
  "net/http"
  "os"
)

type Field struct {
  Name string `json:"name"`
  Type string `json:"type"`
  Ref  string `json:"ref,omitempty"`
}
type Table struct {
  Name   string  `json:"name"`
  Count  int     `json:"count"`
  Fields []Field `json:"fields"`
}
type Body struct {
  Tables []Table `json:"tables"`
  Seed   int     `json:"seed,omitempty"`
}

func main() {
  body := Body{
    Tables: []Table{
      {Name: "users", Count: 30, Fields: []Field{
        {Name: "id", Type: "uuid"},
        {Name: "email", Type: "email"},
      }},
      {Name: "orders", Count: 120, Fields: []Field{
        {Name: "id", Type: "uuid"},
        {Name: "user_id", Type: "ref", Ref: "users.id"},
        {Name: "total", Type: "price"},
      }},
    },
  }
  buf, _ := json.Marshal(body)
  req, _ := http.NewRequest("POST",
    "https://api.mockhero.dev/api/v1/generate", bytes.NewReader(buf))
  req.Header.Set("x-api-key", os.Getenv("MOCKHERO_API_KEY"))
  req.Header.Set("Content-Type", "application/json")
  res, _ := http.DefaultClient.Do(req)
  var data map[string][]map[string]any
  json.NewDecoder(res.Body).Decode(&data)
  // insert with pgx.CopyFrom ...
}

Get Started

Free API key.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles