hey guys can anyone check this repo and tell me why this .py script is written? and if its possible to test this code without that py file? https://github.com/dreamsofcode-io/golang-microservice-course-nn/blob/main/scripts/publish-orders.py
#i want to test this code. im confused about this repo. can anyone help me?
16 messages · Page 1 of 1 (latest)
i have written a go code alternative to this py file. ```package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/google/uuid"
)
type LineItem struct {
ItemID string json:"item_id"
Quantity int json:"quantity"
Price int json:"price"
}
type Order struct {
CustomerID string json:"customer_id"
LineItems []LineItem json:"line_items"
}
func main() {
rand.New(rand.NewSource(time.Now().UnixNano()))
itemIDs := make([]string, 1000)
for i := 0; i < 1000; i++ {
itemIDs[i] = uuid.New().String()
}
customers := make([]string, 100)
for i := 0; i < 100; i++ {
customers[i] = uuid.New().String()
}
for i := 0; i < 120; i++ {
customer := customers[rand.Intn(len(customers))]
numLineItems := rand.Intn(10) + 1
lineItems := make([]LineItem, numLineItems)
for j := 0; j < numLineItems; j++ {
itemID := itemIDs[rand.Intn(len(itemIDs))]
lineItems[j] = LineItem{
ItemID: itemID,
Quantity: rand.Intn(10) + 1,
Price: rand.Intn(10000) + 1,
}
}
order := Order{
CustomerID: customer,
LineItems: lineItems,
}
orderJSON, err := json.Marshal(order)
if err != nil {
fmt.Println("failed to marshal order:", err)
return
}
resp, err := http.Post("http://localhost:3000/orders", "application/json", bytes.NewBuffer(orderJSON))
if err != nil {
fmt.Println("failed to send request:", err)
return
}
defer resp.Body.Close()
fmt.Println("posted order", i+1)
}
}
can you tell me what file name and folder name you give here
it looks like it's just generating 100 customers and 1000 products as test data
it's likely written in python for simplicitys sake, no need to compile as it's not part of the application
ok thanks for clarifying. i wrote go code alternative to that python code above. also is there any other way to test this other than this method?
I don't think it's so much as a test as just putting some values into the database so you can actually see things in the app
yeah is this sometihng you include with your repo or just use it only for our testing?
personally, probably yeah
that means just for testing and not for repo right?
thanks bro!!
yeah it's literally only for use while developing the application - you need some data to show it's working
ok thanks!
I'd include it
just as example scripts
they don't have to work in the future necessarily