Giter Site home page Giter Site logo

Comments (3)

simonexmachina avatar simonexmachina commented on September 25, 2024 2

I think the reason the first example throws is that you're not returning the attrs in the initializer.

The second example is the right approach. The following works:

const STEP_FREQ = 1
factory.define('packet', Object, {
  time: factory.seq('Packet.time', i => Math.floor( Date.now() / 1000 ) + ( STEP_FREQ * i )),
}, {
  afterBuild: model => {
    model.time_to = model.time
    return model
  }
})
factory.build('packet').then(console.log) // => { time: 1507078473, time_to: 1507078473 }

from factory-girl.

4Z4T4R avatar 4Z4T4R commented on September 25, 2024

I have plenty of test cases that dont require be to create or build or persist my models... in fact I don't have models at all. I just need to pump out sequential JSON objects with attribute duplicates. I tried the afterBuild approach but this doesn't get me what I need.

Why is this closed?
Is there a way to duplicate attributes so the values within a JSON object can be replicated?

from factory-girl.

4Z4T4R avatar 4Z4T4R commented on September 25, 2024

For example ... I am trying to replicate the time field in the time_to field. For whatever irrelevant reason, the two have to have the same exact values.... I am trying to accomplish this by trying the buildOptions function which results in an unhandled promise rejection...

factory.define('packet', Object, buildOptions => {
  let attrs = {
    "curr_hour" : 0,
    "device_id" : "ULP00037",
    "last_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
    "latitude" : factory.chance('latitude', { 'fixed': 13 }),
    "longitude" : factory.chance('longitude', { 'fixed': 13 }),
    "time": factory.seq( 'Packet.time',
      (i) =>
      {
        let g = new Date();
        return Math.floor( g.getTime()/1000 ) + ( STEP_FREQ * i );
      }
    ),
    "step_count": factory.seq( 'Packet.step_count',
      (i) =>
      {
        return i * STEP_INTERVAL;
      }
    ),
    "packet_id" : factory.seq( 'Packet.packet_id',
      ( i ) =>
      {
        return i;
      }
    ),
    "prev_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
    "proximity" : 0
  };
  attrs['time_to'] = attrs['time'];
});

let packet = factory.attrs('packet', {}, {})

...and the afterBuild which results in a missing attribute inside the JSON...

factory.define('packet', Object, {
 	"curr_hour" : 0,
	"device_id" : "ULP00037",
	"last_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
	"latitude" : factory.chance('latitude', { 'fixed': 13 }),
	"longitude" : factory.chance('longitude', { 'fixed': 13 }),
  "time": factory.seq( 'Packet.time',
    (i) =>
    {
      let g = new Date();
      return Math.floor( g.getTime()/1000 ) + ( STEP_FREQ * i );
    }
  ),
  "step_count": factory.seq( 'Packet.step_count',
    (i) =>
    {
      return i * STEP_INTERVAL;
    }
  ),
	"packet_id" : factory.seq( 'Packet.packet_id',
    ( i ) =>
    {
      return i;
    }
  ),
	"prev_hour" : () => { return 3600 / STEP_FREQ * STEP_INTERVAL; },
	"proximity" : 0
}, {
  afterBuild: model => {
    model.time_to = model.time;
    return model;
  }
});
let packet = factory.attrs('packet')

You cover so many use cases in your docs... and issues. But this one?

from factory-girl.

Related Issues (20)

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.