<commit_before>import "mocha";
import * as _ from "lodash";
import * as supertest from "supertest";
import * as should from "should";
import db from '../../src/server/database';
import { makeApp } from '../../app';

const app = makeApp(true);

interface API {
  type: string;
  collection: string;
  makeResource: () => any;
}

export function makeSpec(api: API) {
  describe(`${api.collection} API`, () => {
    let resourceId;

    it(`should return 404 on GET /api/${api.type}/0`, (done) => {
      supertest(app)
        .get(`/api/${api.type}/0`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`should return 404 on GET /api/${api.type}/z`, (done) => {
      supertest(app)
        .get(`/api/${api.type}/z`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/POST /api/${api.collection}`, (done) => {
      let resource = api.makeResource();
      supertest(app)
        .post(`/api/${api.collection}`)
        .send(resource)
        .end((err, res) => {
          if (err) throw err;
          resource = res.body;
          should(resource.id).not.be.null;
          should(resource.id).not.be.empty;
          resourceId = resource.id;
          done();
        });
    });

    it(`/GET /api/${api.type}/:id`, (done) => {
      supertest(app)
        .get(`/api/${api.type}/${resourceId}`)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).not.be.null;
          done();
        });
    });

    it(`/GET /api/${api.collection}`, (done) => {
      supertest(app)
        .get(`/api/${api.collection}`)
        .end((err, res) => {
          if (err) throw err;
          const list: any[] = res.body || [];
          should(list).not.be.empty;
          const it = list.find(u => u.id === resourceId);
          should(it).be.Object();
          done();
        });
    });

    it(`/DELETE /api/${api.type}/0 not found`, (done) => {
      supertest(app)
        .del(`/api/${api.type}/0`)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/DELETE /api/${api.type}/z not found`, (done) => {
      supertest(app)
        .del(`/api/${api.type}/z`)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/DELETE /api/${api.type}/:id`, (done) => {
      const url = `/api/${api.type}/${resourceId}`;
      supertest(app)
        .del(url)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;

          supertest(app).get(url)
            .expect(404)
            .end((err, res) => {
              if (err) throw err;
              should(res.body).be.empty;
              done();
            });
        });
    });
  });
}

makeSpec({
  type: 'user',
  collection: 'users',
  makeResource: () => {
    return {
      id: "1",
      name: "bob"
    };
  }
});

makeSpec({
  type: 'team',
  collection: 'teams',
  makeResource: () => {
    return {
      id: "1",
      name: "super"
    };
  }
});

makeSpec({
  type: 'calendar',
  collection: 'calendars',
  makeResource: () => {
    return {
      name: 'team calendar',
      type: 'leaves',
      description: 'team leaves',
    };
  }
});

makeSpec({
  type: 'event',
  collection: 'events',
  makeResource: () => {
    return {
      type: 'status',
      comment: 'nothing',
    };
  },
});<commit_msg>expect 200 and test for update operation<commit_after>import "mocha";
import * as _ from "lodash";
import * as supertest from "supertest";
import * as should from "should";
import db from '../../src/server/database';
import { makeApp } from '../../app';

const app = makeApp(true);

interface API {
  type: string;
  collection: string;
  makeResource: () => any;
  update: any;
}

export function makeSpec(api: API) {
  describe(`${api.collection} API`, () => {
    let resourceId;

    it(`should return 404 on GET /api/${api.type}/0`, (done) => {
      supertest(app)
        .get(`/api/${api.type}/0`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`should return 404 on GET /api/${api.type}/z`, (done) => {
      supertest(app)
        .get(`/api/${api.type}/z`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/POST /api/${api.collection}`, (done) => {
      let resource = api.makeResource();
      supertest(app).post(`/api/${api.collection}`)
        .send(resource)
        .expect(200)
        .end((err, res) => {
          if (err) throw err;
          resource = res.body;
          should(resource.id).not.be.null;
          should(resource.id).not.be.empty;
          resourceId = resource.id;
          done();
        });
    });

    it(`/GET and /PUT /api/${api.type}/:id`, (done) => {
      const url = `/api/${api.type}/${resourceId}`;
      supertest(app).get(url)
        .expect(200)
        .end((err, res) => {
          if (err) throw err;

          const resource = res.body;

          should(res.body).not.be.null;
          should(res.body).not.be.empty;

          const updatedResource = Object.assign({}, resource, api.update);

          supertest(app).put(url)
            .send(updatedResource)
            .expect(200)
            .end((err, res) => {
              if (err) throw err;
              // TODO assert body is changed correctly, compare with updatedResource
              should(res.body).not.be.null;
              should(res.body).not.be.empty;
              done();
            });
        });
    });

    it(`/GET /api/${api.collection}`, (done) => {
      supertest(app).get(`/api/${api.collection}`)
        .expect(200)
        .end((err, res) => {
          if (err) throw err;
          const list: any[] = res.body || [];
          should(list).not.be.empty;
          const it = list.find(u => u.id === resourceId);
          should(it).be.Object();
          done();
        });
    });

    it(`/DELETE /api/${api.type}/0 not found`, (done) => {
      supertest(app).del(`/api/${api.type}/0`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/DELETE /api/${api.type}/z not found`, (done) => {
      supertest(app).del(`/api/${api.type}/z`)
        .expect(404)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;
          done();
        });
    });

    it(`/DELETE /api/${api.type}/:id`, (done) => {
      const url = `/api/${api.type}/${resourceId}`;
      supertest(app).del(url)
        .end((err, res) => {
          if (err) throw err;
          should(res.body).be.empty;

          supertest(app).get(url)
            .expect(404)
            .end((err, res) => {
              if (err) throw err;
              should(res.body).be.empty;
              done();
            });
        });
    });
  });
}

makeSpec({
  type: 'user',
  collection: 'users',
  makeResource: () => {
    return {
      id: "1",
      name: "bob"
    };
  },
  update: {name: "rob"},
});

makeSpec({
  type: 'team',
  collection: 'teams',
  makeResource: () => {
    return {
      id: "1",
      name: "super"
    };
  },
  update: {name: "bulls"},
});

makeSpec({
  type: 'calendar',
  collection: 'calendars',
  makeResource: () => {
    return {
      name: 'team calendar',
      type: 'leaves',
      description: 'team leaves',
    };
  },
  update: {type: "absences"},
});

makeSpec({
  type: 'event',
  collection: 'events',
  makeResource: () => {
    return {
      type: 'status',
      comment: 'nothing',
    };
  },
  update: {type: "presence"},
});
{'fraction_non_alphanumeric': 0.19987608426270137, 'fraction_numerical': 0.007806691449814126, 'mean_word_length': 1.7285327924273157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6018946', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2673, 'n_words': 533}