stepci / liquidless-faker

Faker.js filter for liquidless templates
MIT License
3 stars 0 forks source link

Providing String as a argument causes crash #1

Open keinsell opened 6 months ago

keinsell commented 6 months ago

I've tried multiple ways to pipe options to faker but cannot succeeded, when I do things like {{ lorem.lines | fake: 1 }} where argument is privitive things ten to work - but only when argument is integer; as soon as I will add string there is a crash.

Example: ${{ phone.number | fake: "+48#########" }}

To be checked on https://stepci.com/ playground.

mishushakov commented 6 months ago

Playground was outdated. Feel free to reopen the issue if you're experiencing this on local

HKandulla commented 6 months ago

Hi @mishushakov , how can we pass objects to faker, as in faker.color.cmyk({ format: 'css' }) Thanks, Hannes

mishushakov commented 6 months ago

Like this?

{{color.cmyk | fake: {"format": "css"}}}

Keep in mind this is probably not possible to do in YAML, because of double quotes

keinsell commented 6 months ago

I would be interesting in knowing real example how I can pass the object to faker for example, like @HKandulla mentioned, faker.color.cmyk({ format: 'css' }) inside of the .yaml test file that is run in stepci.

keinsell commented 6 months ago

Playground was outdated. Feel free to reopen the issue if you're experiencing this on local

Problem is still persistent in playground and on my local machine, it's a real issue with stepci and how faker or liquidless is handled. And I cannot reopen the issue, can we cut the shit?

mishushakov commented 6 months ago

I have since added JSON5 support, you can now use single quotes for JSON objects:

version: "1.1"
name: Fake Data
tests:
  example:
    steps:
      - name: GET request
        http:
          url: "https://en.wikipedia.org/wiki/${{color.cmyk | fake: {'format': 'css'}}}"
          method: GET
          check:
            status: /^20/
keinsell commented 6 months ago

Okay, that seems to work - however when I try to reference phone number format it does not :/

version: "1.1"
name: Fake Data
tests:
  example:
    steps:
      - name: GET request
        http:
          url: "https://en.wikipedia.org/wiki/${{ phone.number | fake: '+48#########' }}"
          method: GET
          check:
            status: /^20/

At this point I have no clue it's issue with faker (where function works in faker library used in way it should be parsed), liquidless (it seems to pass things correctly) or liquidless-faker (where something on the road is wrong)

keinsell commented 6 months ago

Currently I've found workaround with exposing a endpoint on server that generates a random phone number that is later used in tests, but it would be good to more advanced fake data on the fly

HKandulla commented 5 months ago

Hi @mishushakov ,

thanks for looking into this! Adding JSON5 supports makes it possible to pass objects but faker still ignores the value.

Passing not format

version: "1.1"
    name: Fake Data
    tests:
      example:
        steps:
          - name: POST request
            http:
              url: "https://httpbin.org/post"
              method: POST
              json:
                name: default
                color: "${{color.cmyk | fake}}"
              check:
                status: /^20/

Request

POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{"name":"default","color":"0.5,0.56,0.52,0.96"}

Passing format: css

version: "1.1"
name: Fake Data
tests:
  example:
    steps:
      - name: POST request
        http:
          url: "https://httpbin.org/post"
          method: POST
          json:
            name: test_css
            color: "${{color.cmyk | fake: {'format': 'css'}}}"
          check:
            status: /^20/

Request

POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{"name":"test_css","color":"0.37,0.28,0.83,0.03}"}

Passing format: css should resolve to: faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%)

When using liquidless-faker v1.3.2 standalone passing objects as parameters work:

import { renderString } from 'liquidless'
import { fake } from '../src'

const string = renderString("{{ color.cmyk | fake: {'format': 'css'} }}", {}, {
  filters: {
    fake
  }
})

console.log(string)

Output:

➜  liquidless-faker git:(main) ✗ npm run test

> liquidless-faker@1.1.1 test
> ts-node ./tests/test.ts

cmyk(99%, 100%, 28%, 18%)
mishushakov commented 5 months ago

Have you properly re-installed stepci?

HKandulla commented 4 months ago

Hi @mishushakov , sorry for my late reply.. Yes, I have re-installed stepci and also tried the following example using the playground on https://stepci.com/

Example:

version: "1.1"
name: Fake Data with objects as parameter
tests:
  example:
    steps:
      - name: POST request
        http:
          url: "https://httpbin.org/post"
          method: POST
          json:
            name: test_css
            color: "${{color.cmyk | fake: {'format': 'css'}}}"
          check:
            status: /^20/

Request is:

POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{"name":"test_css","color":"0.02,0.92,0.41,0.9}"}

Request should be:

POST https://httpbin.org/post HTTP/1.1
Content-Type: application/json

{"name":"test_css","color":"cmyk(100%, 0%, 0%, 0%)}"}

See https://fakerjs.dev/api/color.html#cmyk