require_relative '../../spec_helper'
require 'rspec/core'
require 'ostruct'
require_relative '../../../app/models/layer/presenter'

include CartoDB

describe CartoDB::LayerModule::Presenter do
  describe '#to_vizjson_v2' do
    it 'wraps the sql if query_wrapper available' do
      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        tooltip: { 'template' => 'blahblah' },
        kind:           'carto',
        options: {
          'query'         => '',
          'tile_style'    => '',
          'interactivity' => '',
          'style_version' => '',
          'table_name'    => '000cd294-b124-4f82-b569-0f7fe41d2db8',
          'query_wrapper' => 'bogus tpl <%= sql %>'
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson.fetch(:options).fetch(:sql).should eq 'bogus tpl select * from "000cd294-b124-4f82-b569-0f7fe41d2db8"'

      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        tooltip: { 'template' => 'blahblah' },
        kind:           'carto',
        options: {
          'query'         => '',
          'tile_style'    => '',
          'interactivity' => '',
          'style_version' => '',
          'table_name'    => 'bogus_table',
          'query_wrapper' => 'bogus template <%= sql %>'
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson.fetch(:options).fetch(:sql).should eq 'bogus template select * from bogus_table'

      layer = OpenStruct.new(
        public_values: { 'options' => {'visible' => nil} },
        tooltip: { 'template' => 'blahblah' },
        kind:           'carto',
        options: {
          'query'         => 'select the_geom from bogus_table',
          'tile_style'    => '',
          'interactivity' => '',
          'style_version' => '',
          'table_name'    => 'bogus_table',
          'query_wrapper' => 'bogus template <%= sql %>'
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson.fetch(:options).fetch(:sql)
        .should == 'bogus template select the_geom from bogus_table'
    end

    it 'returns `sql_wrap` with `query_wrapper` if present and with style properties nil or not autogenerated' do
      query_wrapper = 'bogus template <%= sql %>'

      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        kind: 'carto',
        options: {
          'interactivity' => '',
          'style_version' => '',
          'table_name' => 'bogus_table',
          'query_wrapper' => query_wrapper,
          'style_properties' => {}
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson[:options]['sql_wrap'].should eq query_wrapper

      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        kind: 'carto',
        options: {
          'interactivity' => '',
          'style_version' => '',
          'table_name' => 'bogus_table',
          'query_wrapper' => query_wrapper,
          'style_properties' => { 'autogenerated' => false }
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson[:options]['sql_wrap'].should eq query_wrapper
    end

    it 'does not return `sql_wrap` with `query_wrapper` if present without style properties or autogenerated' do
      query_wrapper = 'bogus template <%= sql %>'

      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        kind: 'carto',
        options: {
          'interactivity' => '',
          'style_version' => '',
          'table_name' => 'bogus_table',
          'query_wrapper' => query_wrapper
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson[:options]['sql_wrap'].should be_nil

      layer = OpenStruct.new(
        public_values: { 'options' => { 'visible' => nil } },
        kind: 'carto',
        options: {
          'interactivity' => '',
          'style_version' => '',
          'table_name' => 'bogus_table',
          'query_wrapper' => query_wrapper,
          'style_properties' => { 'autogenerated' => true }
        }
      )

      vizjson = CartoDB::LayerModule::Presenter.new(layer).to_vizjson_v2
      vizjson[:options]['sql_wrap'].should be_nil
    end

    it 'keeps the visible attribute when rendering' do
      layer = OpenStruct.new(
        kind:           'torque',
        options: {
          'query'         => 'select the_geom from bogus_table',
          'tile_style'    => '',
          'interactivity' => '',
          'style_version' => '',
          'table_name'    => 'bogus_table',
          'query_wrapper' => 'bogus template <%= sql %>',
          'visible' => true
        },
      )

      options = {
        visualization_id: '',
        sql: '',
      }

      vizjson = CartoDB::LayerModule::Presenter.new(layer, options).to_vizjson_v2
      vizjson[:options]['visible'].should == true
    end

  end
end
