{"size": 2432, "ext": "js", "max_stars_repo_path": "test/unit/database/practice.test.js", "max_stars_repo_name": "agileworks-tw/node-tdd-sample", "max_stars_repo_stars_event_min_datetime": "2017-06-22T17:10:48.000Z", "max_stars_repo_stars_event_max_datetime": "2017-06-22T17:10:48.000Z", "max_issues_repo_path": "test/unit/database/practice.test.js", "max_issues_repo_name": "agileworks-tw/node-tdd-sample", "max_issues_repo_issues_event_min_datetime": "2016-07-10T07:21:38.000Z", "max_issues_repo_issues_event_max_datetime": "2016-07-31T07:30:45.000Z", "max_forks_repo_path": "test/unit/database/practice.test.js", "max_forks_repo_name": "agileworks-tw/node-tdd-sample", "max_forks_repo_forks_event_min_datetime": "2016-07-07T10:17:50.000Z", "max_forks_repo_forks_event_max_datetime": "2016-08-23T14:48:44.000Z"}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": 38, "avg_line_length": 20.2666666667, "max_line_length": 78, "alphanum_fraction": 0.4699835526}
import task1_initModel from '../../../src/database/task1';

describe('practice', () => {
  let models = null;
  beforeEach(async (done) => {
    try {
      models = await task1_initModel();
      done();
    } catch (e) {
      done(e);
    }
  });

  it('在資料庫中 Post table 中新增一個 price 欄位 data types 為 INTEGER', async (done) => {
    try {
      const data = {
        title: 'post a',
        desc: 'post desc',
        price: 100,
      };
      const result = await models.Post.create(data);
      result.toJSON().should.has.keys(
        'id',
        'title',
        'desc',
        'price',
        'createdAt',
        'updatedAt'
      );
      result.price.should.be.eq(data.price);
      done();
    } catch (e) {
      done(e);
    }
  });

  it('使用 sequelize 新增一個 post 使測試 pass', async (done) => {
    try {
      const input = {
        title: 'AAA',
        desc: 'BBB',
        price: 100,
      };

      let result;

      result.title.should.be.eq('AAA');
      result.desc.should.be.eq('BBB');
      result.price.should.be.eq(100);
      done();
    } catch (e) {
      done(e);
    }
  });

  describe('practice find', () => {
    let models = null;
    let targetPost = null
    beforeEach(async (done) => {
      try {
        models = await task1_initModel();
        const data = {
          title: 'post a',
          desc: 'post desc',
          price: 100,
        };
        targetPost = await models.Post.create(data);
        done();
      } catch (e) {
        done(e);
      }
    });

    it('使用 sequelize 尋找 targetPost ', async (done) => {
      try {

        let findTarget;

        findTarget.id.should.be.eq(targetPost.id);
        done();
      } catch (e) {
        done(e);
      }
    });

    it('使用 sequelize 更新 targetPost 的資料', async (done) => {
      try {
        const input = {
          title: '123',
          desc: '456',
          price: 999,
        }

        let findTarget;
        let result;

        result.title.should.be.eq(input.title);
        result.desc.should.be.eq(input.desc);
        result.price.should.be.eq(input.price);
        done();
      } catch (e) {
        done(e);
      }
    });

    it('使用 sequelize 刪除 targetPost', async (done) => {
      try {

        let findTarget;

        let check = await models.Post.findAll();
        (check.length === 0).should.be.true;
        done();
      } catch (e) {
        done(e);
      }
    });
  });
});
