{"size": 3087, "ext": "js", "max_stars_repo_path": "test/express-cassandra-swagger.spec.js", "max_stars_repo_name": "dekelev/express-cassandra-swagger", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/express-cassandra-swagger.spec.js", "max_issues_repo_name": "dekelev/express-cassandra-swagger", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/express-cassandra-swagger.spec.js", "max_forks_repo_name": "dekelev/express-cassandra-swagger", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.1927710843, "max_line_length": 133, "alphanum_fraction": 0.6699060577}
const assert = require('chai').assert;
const sinon = require('sinon');
const yaml = require('js-yaml');

const yamlWriter = require('../lib/utils/yaml.writer');
const expressCassandraSwagger = require('../lib/express-cassandra-swagger');

const SimpleModel = require('./models/SimpleModel');
const SimpleModelRequire = require('./models/SimpleModelRequire');

const SIMPLE_MODEL_SCHEMA =
  'title: SimpleModel\ntype: object\nrequired: []\nproperties:\n  intAttr:\n    type: integer\n    ' +
  'format: int32\n  stringAttr:\n    type: string\n  dateTimeAttr:\n    type: integer\n    format: int32\n';

const SIMPLE_MODEL_SCHEMA_OPEN_API_2 =
  'title: SimpleModel\ntype: object\nrequired: []\nproperties:\n  intAttr:\n    type: integer\n    ' +
  'format: int32\n  stringAttr:\n    type: string\n  dateTimeAttr:\n    type: integer\n    format: int32\n';

const SIMPLE_MODEL_SCHEMA_REQUIRE =
  'title: SimpleModel\ntype: object\nrequired:\n  - stringAttr\n  - dateTimeAttr\nproperties:\n  intAttr:\n    type: integer\n    ' +
  'format: int32\n  stringAttr:\n    type: string\n  dateTimeAttr:\n    type: integer\n    format: int32\n';

describe('express-cassandra-swagger', () => {
  beforeEach(() => {
    global.sinon = sinon.createSandbox();
  });

  afterEach(() => {
    global.sinon.restore();
  });

  describe('generateSchema', () => {
    it('generates model schema yaml from single model', () => {
      const result = expressCassandraSwagger.generateSchema(SimpleModel);

      assert.lengthOf(result, 1);
      assert.equal(result[0].name, 'SimpleModel');
      assert.equal(result[0].schema, SIMPLE_MODEL_SCHEMA);
    });

    it('generates model schema yaml from array of models', () => {
      const result = expressCassandraSwagger.generateSchema([SimpleModel]);

      assert.lengthOf(result, 1);
      assert.equal(result[0].name, 'SimpleModel');
      assert.equal(result[0].schema, SIMPLE_MODEL_SCHEMA);
    });

    it('generates model schema yaml with required fields', () => {
      const result = expressCassandraSwagger.generateSchema([SimpleModelRequire]);

      assert.lengthOf(result, 1);
      assert.equal(result[0].name, 'SimpleModel');
      assert.equal(result[0].schema, SIMPLE_MODEL_SCHEMA_REQUIRE);
    });

    it('saves model schema yaml', () => {
      let writeResult;
      const writeStub = global.sinon
        .stub(yamlWriter, 'writeYamlsToFs')
        .callsFake((writeParams) => {
          writeResult = writeParams;
        });

      const result = expressCassandraSwagger.saveSchema(SimpleModel, 'dummyDir');

      assert.lengthOf(writeResult, 1);
      assert.equal(writeResult[0].name, 'SimpleModel');
      assert.equal(writeResult[0].schema, SIMPLE_MODEL_SCHEMA);
    });
  });

  describe('generateSchemaRaw', () => {
    it('generates model schema from single model', () => {
      const result = expressCassandraSwagger.generateSchemaRaw(SimpleModel);

      assert.lengthOf(result, 1);
      assert.equal(result[0].name, 'SimpleModel');
      assert.deepEqual(result[0].schema, yaml.load(SIMPLE_MODEL_SCHEMA));
    });
  });
});
