Git Product home page Git Product logo

pg-stories's Introduction

pg-stories

The purpose of this package is to create readable scenarios that will run against backend implementation of the postgres protocol version 3.0. It is heavily dependent on the great jackc/pgx package.

Main Concepts

Step

A Step can be one of:

  • *Command
    Consists of FrontendMessage and will cause the test to send this message to backend.
    Example:
    &Command{&pgproto3.Query{String: "SELECT 1;"}}
  • *Response
    Consists of BackendMessage and will cause the test to wait for message from backend and compare between the provided and received messages.
    Example:
    &Response{&pgproto3.ReadyForQuery{}}

*Story

Story contains a sequence of Step and requires a Frontend to run the steps upon.
Each step will be either sent to backend or be compared to received message from backend, according to it's type.

Example
func TestExample(t *testing.T) {
    story := &Story{
        Steps: []Step{
            &Command{&pgproto3.Query{String: "SELECT 1;"}},
            &Response{&pgproto3.RowDescription{}},
            &Response{&pgproto3.DataRow{}},
            &Response{&pgproto3.CommandComplete{}},
            &Response{&pgproto3.ReadyForQuery{}},
        },
        Frontend: f,
    }
    sigKill := make(chan interface{})
    timer := time.NewTimer(time.Second * 2)
    go func() {
        <-timer.C
        sigKill <- fmt.Errorf("timeout")
    }()
    err = story.Run(t, sigKill)
    if err != nil {
    	timer.Stop()
        t.Fatal(err)
    }
}

Story Transcript (WIP)

You can also define stories using simple text files. Currently all backend and frontend messages that don't require parameters are working and some are supporting parameters.

Notes
  • Each line of the file can contain only one step, start order or end order

DSL

Story
  • === $1
    Signals start of story.
    Params
    1. The name / description of the story. Will be returned from the parser.
  • ===
    Signals end of story.
Step

Each line that define a step must start with either -> for command (frontend message) or <- for response (backend message)
Responses not yet accepting values so you can define them as expected response and they will be evaluated without checking returned values.

Commands:

  • -> Q "$1" - (Query)
    Params
    1. Query string.
      Example
      Q "SELECT 1;"
  • -> P "$1" "$2" [$3] - (Parse)
    Params
    1. Destination prepared statement name. Empty string defines unnamed statement.
    2. Query string.
    3. Comma separated parameter OIDs
      Example
      -> P "stmt1" "SELECT * FROM (VALUES($1),($2)) t;" [0,2]
  • -> B "$1" "$2" [$3] - (Bind)
    Params
    1. Destination portal name. Empty string defines unnamed portal.
    2. Source prepared statement. Empty string targets unnamed statement.
    3. Comma separated parameter values
      Example
      -> B "portal1" "stmt1" [1,foo]
  • -> D $1 "$2" - (Describe)
    Params
    1. Object type. Can be either S for statement or P for portal.
    2. Name of the Object
      Example -> D S "stmt1"
  • -> E "$1" $2 - (Execute)
    Params
    1. Portal name. Empty string targets unnamed portal.
    2. Max rows. 0 for unlimited.
  • -> S - (Sync)
  • -> H - (Flush)

Responses:

  • <- 1 - (ParseComplete)
  • <- 2 - (BindComplete)
  • <- C - (CommandComplete)
  • <- T - (RowDescription)
  • <- t - (ParameterDescription)
  • <- D - (DataRow)
  • <- E - (ErrorResponse)
  • <- Z - (ReadyForQuery)

Full Example:

=== execute named portal
-> P "stmt_name" "SELECT * FROM (VALUES($1))" [0]
-> B "portal_name" "stmt_name" [foo]
-> E "portal_name" 0
-> S
<- 1
<- 2
<- D
<- C
<- Z
===

pg-stories's People

Contributors

avivklas avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

avivklas

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.