<commit_before>var Backbone = require('backbone');
var ConfigModel = require('../../../../javascripts/cartodb3/data/config-model');
var LayerDefinitionsCollection = require('../../../../javascripts/cartodb3/data/layer-definitions-collection');
var LayerDefinitionModel = require('../../../../javascripts/cartodb3/data/layer-definition-model');
var AnalysisDefinitionNodesCollection = require('../../../../javascripts/cartodb3/data/analysis-definition-nodes-collection');

describe('cartodb3/data/layer-definition-model', function () {
  beforeEach(function () {
    this.configModel = new ConfigModel({
      base_url: '/u/pepe'
    });

    this.analysisDefinitionNodesCollection = new AnalysisDefinitionNodesCollection(null, {
      configModel: this.configModel
    });

    this.collection = new LayerDefinitionsCollection(null, {
      configModel: this.configModel,
      analysisDefinitionNodesCollection: this.analysisDefinitionNodesCollection,
      mapId: 'm123'
    });
    this.model = new LayerDefinitionModel({
      id: 'abc-123',
      kind: 'carto',
      options: {
        type: 'CartoDB',
        color: '#FABADA',
        table_name: 'foo',
        query: 'SELECT * FROM foo',
        tile_style: 'asdasd',
        visible: true
      }
    }, {
      parse: true,
      configModel: this.configModel,
      collection: this.collection
    });
  });

  it('should transform some attrs to be compatible with cartodb.js', function () {
    expect(this.model.get('cartocss')).toEqual('asdasd');
    expect(this.model.get('tile_style')).toBeUndefined();

    expect(this.model.get('sql')).toContain('SELECT');
    expect(this.model.get('query')).toBeUndefined();
  });

  describe('.toJSON', function () {
    it('should return the original data', function () {
      expect(this.model.toJSON()).toEqual({
        id: 'abc-123',
        kind: 'carto',
        options: {
          type: 'CartoDB',
          visible: true,
          color: '#FABADA',
          table_name: 'foo',
          query: 'SELECT * FROM foo',
          tile_style: 'asdasd',
          style_properties: jasmine.any(Object),
          cartocss_history: jasmine.any(Array),
          sql_history: jasmine.any(Array)
        }
      });
    });
  });

  describe('styleModel', function () {
    it('should create style model if table name attribute exists', function () {
      expect(this.model.styleModel).toBeDefined();
    });

    it('should not add style model if table name doesn\'t exist', function () {
      var mdl = new LayerDefinitionModel({
        id: 'other',
        options: {
          type: 'tiled',
          tile_style: 'asdasd'
        }
      }, {
        parse: true,
        configModel: this.configModel
      });

      expect(mdl.styleModel).toBeUndefined();
    });<commit_msg>Added and fixed layer-definition-model tests<commit_after>var Backbone = require('backbone');
var ConfigModel = require('../../../../javascripts/cartodb3/data/config-model');
var LayerDefinitionsCollection = require('../../../../javascripts/cartodb3/data/layer-definitions-collection');
var LayerDefinitionModel = require('../../../../javascripts/cartodb3/data/layer-definition-model');
var AnalysisDefinitionNodesCollection = require('../../../../javascripts/cartodb3/data/analysis-definition-nodes-collection');

describe('cartodb3/data/layer-definition-model', function () {
  beforeEach(function () {
    this.configModel = new ConfigModel({
      base_url: '/u/pepe'
    });

    this.analysisDefinitionNodesCollection = new AnalysisDefinitionNodesCollection(null, {
      configModel: this.configModel
    });

    this.collection = new LayerDefinitionsCollection(null, {
      configModel: this.configModel,
      analysisDefinitionNodesCollection: this.analysisDefinitionNodesCollection,
      mapId: 'm123'
    });
    this.model = new LayerDefinitionModel({
      id: 'abc-123',
      kind: 'carto',
      options: {
        type: 'CartoDB',
        color: '#FABADA',
        table_name: 'foo',
        query: 'SELECT * FROM foo',
        tile_style: 'asdasd',
        visible: true
      }
    }, {
      parse: true,
      configModel: this.configModel,
      collection: this.collection
    });
  });

  it('should transform some attrs to be compatible with cartodb.js', function () {
    expect(this.model.get('cartocss')).toEqual('asdasd');
    expect(this.model.get('tile_style')).toBeUndefined();

    expect(this.model.get('sql')).toContain('SELECT');
    expect(this.model.get('query')).toBeUndefined();
  });

  it('should "remove" autoStyle attribute when model is saved', function () {
    this.model.sync = function () {};
    this.model.set('autoStyle', '8888-7777-6666-1111');
    this.model.save();
    expect(this.model.get('autoStyle')).toBeFalsy();
  });

  describe('.toJSON', function () {
    it('should return the original data', function () {
      expect(this.model.toJSON()).toEqual({
        id: 'abc-123',
        kind: 'carto',
        options: {
          type: 'CartoDB',
          visible: true,
          color: '#FABADA',
          table_name: 'foo',
          query: 'SELECT * FROM foo',
          tile_style: 'asdasd',
          style_properties: jasmine.any(Object),
          cartocss_history: jasmine.any(Array),
          sql_history: jasmine.any(Array)
        }
      });
    });

    it('should not include autoStyle info', function () {
      this.model.set('autoStyle', '8888-7777-6666-1111');
      var json = this.model.toJSON();
      expect(json).not.toContain({
        autoStyle: '8888-7777-6666-1111'
      });
    });
  });

  describe('styleModel', function () {
    it('should create style model if table name attribute exists', function () {
      expect(this.model.styleModel).toBeDefined();
    });

    it('should not add style model if table name doesn\'t exist', function () {
      var mdl = new LayerDefinitionModel({
        id: 'other',
        options: {
          type: 'tiled',
          tile_style: 'asdasd'
        }
      }, {
        parse: true,
        configModel: this.configModel
      });

      expect(mdl.styleModel).toBeUndefined();
    });
{'fraction_non_alphanumeric': 0.1550514117839073, 'fraction_numerical': 0.012404112942712583, 'mean_word_length': 2.7116898849182314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5396683', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1750, 'n_words': 339}