Giter Club home page Giter Club logo

ent-factory's Introduction

Ent Factory

a golang factory library for ent.

Function

Simplify the process of creating mock model objects for ent schema.

  • Auto reading the struct of ent model schemas to create factory functions.
  • Auto fake data for each field as default.
  • Allow self define the value of any field independently.

Struct

-- cmd
    -- root.go
    -- cmd.go
-- constants
    -- constants.go
    -- errors.go
-- main.go
-- README.md

How to install

Install

go install github.com/zaihui/ent-factory@latest

Setup

Flags

  • schemaPath
    • path of your entschema, normally is "gen/entschema"
    • required
  • outputPath
    • path of your factories
    • the endpoint must be "factories"
    • required
  • projectPath
    • the relative path of this project
    • eg. github.com/zaihui/ent-factory
    • required
  • factoriesPath
    • the relative path of these factories located in this project
    • eg. factories, means projectPath/factories
    • the default value is factories
  • appPath
    • the relative path of app client
    • eg. service/app means github.com/zaihui/ent-factory/service/app
    • the default value is service/app
  • entClientName
    • the name of ent client, means appPath.entClientName
    • the default value is EntClient

Makefile Command

all_factory:
  go run ent-factory generate --schemaPath {your ent schema path} --outputPath {path of your factories} ----projectPath {your project module path}

Sample

all_factory:
  go run ent-factory generate --schemaPath gen/entschema --outputPath  /Users/lvxinyan/zaihui/ent-factory/factories --projectPath github.com/zaihui/ent-factory

Sample

The sample of a factory

package booktableorderfactory

import (
	"github.com/bxcodec/faker"
	"github/zaihui/ent-factory/factories"
	"github/zaihui/ent-factory/gen/entschema"
	"github/zaihui/ent-factory/service/app"
)

type BookTableOrderFieldSetter func(*entschema.BookTableOrder)


func SetUID(uIDGen string) BookTableOrderFieldSetter {
	return func(bookTableOrderGen *entschema.BookTableOrder) {
		bookTableOrderGen.UID = uIDGen
	}
}

func SetUserUID(userUIDGen string) BookTableOrderFieldSetter {
	return func(bookTableOrderGen *entschema.BookTableOrder) {
		bookTableOrderGen.UserUID = userUIDGen
	}
}
func SetOrderNo(orderNoGen string) BookTableOrderFieldSetter {
	return func(bookTableOrderGen *entschema.BookTableOrder) {
		bookTableOrderGen.OrderNo = orderNoGen
	}
}
func SetType(typeGen string) BookTableOrderFieldSetter {
	return func(bookTableOrderGen *entschema.BookTableOrder) {
		bookTableOrderGen.Type = typeGen
	}
}

func New(s factories.TestSuite, opts ...BookTableOrderFieldSetter) *entschema.BookTableOrder {
	data := entschema.BookTableOrder{}
	s.NoError(faker.FakeData(&data))
	for _, opt := range opts {
		opt(&data)
	}
	return app.EntClient.BookTableOrder.Create().
		SetUID(data.UID).
		SetUserUID(data.UserUID).
		SetOrderNo(data.OrderNo).
		SetType(data.Type).
		SaveX(s.Context())
}

how to use this factory

package main
import (
	your_location_of_factories/booktableorderfactory
)
// s is an instance of test suite
booktableorderfactory.new(s) 
// if you want to customize the value of a field, eg. UID
booktableorderfactory.new(s, booktableorderfactory.SetUID("your uid"))

Todo

  • Support Time Optional Function
  • Support Json Field Optional Function
  • Lint
  • Unit Test Coverage
  • Version Compatibility Test

ent-factory's People

Contributors

lxy1992 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.