<commit_before>require_relative '../support/factories/tables'

module Carto
  module Factories
    module Visualizations
      include CartoDB::Factories

      def full_visualization_table(carto_user, map)
        Carto::UserTable.find(create_table(name: unique_name('fvt_table'), user_id: carto_user.id).id)
      end

      def create_full_builder_vis(carto_user, privacy: Carto::Visualization::PRIVACY_PUBLIC)
        create_full_visualization(carto_user, visualization_attributes: { version: 3, privacy: privacy })
      end

      # "Full visualization": with map, table... Including actual user table.
      # Table is bound to visualization, and to data_layer if it's not passed.
      def create_full_visualization(
        carto_user,
        canonical_map: FactoryGirl.create(:carto_map_with_layers, user_id: carto_user.id),
        map: FactoryGirl.create(:carto_map_with_layers, user_id: carto_user.id),
        table: full_visualization_table(carto_user, canonical_map),
        data_layer: nil,
        visualization_attributes: {}
      )

        table_visualization = table.visualization || create_table_visualization(carto_user, table)
        visualization = FactoryGirl.create(:carto_visualization, { user: carto_user, map: map }
                                   .merge(visualization_attributes))

        unless data_layer.present?
          data_layer = visualization.map.data_layers.first
          data_layer.options[:table_name] = table.name
          data_layer.options[:query] = "select * from #{table.name}"
          data_layer.options[:sql_wrap] = "select * from (<%= sql %>) __wrap"
          data_layer.save
        end

        visualization.update_column(:active_layer_id, visualization.layers.first.id)

        FactoryGirl.create(:carto_zoom_overlay, visualization: visualization)
        FactoryGirl.create(:carto_search_overlay, visualization: visualization)

        # Need to mock the nonexistant table because factories use Carto::* models
        CartoDB::Visualization::Member.any_instance.stubs(:propagate_name_to).returns(true)
        CartoDB::Visualization::Member.any_instance.stubs(:propagate_privacy_to).returns(true)

        visualization.reload

        return map, table, table_visualization, visualization
      end

      def create_table_visualization(carto_user, table)
        FactoryGirl.create(
          :carto_visualization, user: carto_user, type: 'table', name: table.name, map_id: table.map_id)
      end

      # Helper method for `create_full_visualization` results cleanup
      def destroy_full_visualization(map, table, table_visualization, visualization)
        table_visualization.map.destroy if table_visualization && table_visualization.map
        table_visualization.destroy if table_visualization
        table.service.destroy if table && table.service
        table.destroy if table
        visualization.destroy if visualization
        map.destroy if map
      end

      def destroy_visualization(visualization_id)
        member = CartoDB::Visualization::Member.new(id: visualization_id).fetch
        member.delete
      end
    end
  end
end<commit_msg>Do not destroy already deleted objects (it fails with the new hooks)<commit_after>require_relative '../support/factories/tables'

module Carto
  module Factories
    module Visualizations
      include CartoDB::Factories

      def full_visualization_table(carto_user, map)
        Carto::UserTable.find(create_table(name: unique_name('fvt_table'), user_id: carto_user.id).id)
      end

      def create_full_builder_vis(carto_user, privacy: Carto::Visualization::PRIVACY_PUBLIC)
        create_full_visualization(carto_user, visualization_attributes: { version: 3, privacy: privacy })
      end

      # "Full visualization": with map, table... Including actual user table.
      # Table is bound to visualization, and to data_layer if it's not passed.
      def create_full_visualization(
        carto_user,
        canonical_map: FactoryGirl.create(:carto_map_with_layers, user_id: carto_user.id),
        map: FactoryGirl.create(:carto_map_with_layers, user_id: carto_user.id),
        table: full_visualization_table(carto_user, canonical_map),
        data_layer: nil,
        visualization_attributes: {}
      )

        table_visualization = table.visualization || create_table_visualization(carto_user, table)
        visualization = FactoryGirl.create(:carto_visualization, { user: carto_user, map: map }
                                   .merge(visualization_attributes))

        unless data_layer.present?
          data_layer = visualization.map.data_layers.first
          data_layer.options[:table_name] = table.name
          data_layer.options[:query] = "select * from #{table.name}"
          data_layer.options[:sql_wrap] = "select * from (<%= sql %>) __wrap"
          data_layer.save
        end

        visualization.update_column(:active_layer_id, visualization.layers.first.id)

        FactoryGirl.create(:carto_zoom_overlay, visualization: visualization)
        FactoryGirl.create(:carto_search_overlay, visualization: visualization)

        # Need to mock the nonexistant table because factories use Carto::* models
        CartoDB::Visualization::Member.any_instance.stubs(:propagate_name_to).returns(true)
        CartoDB::Visualization::Member.any_instance.stubs(:propagate_privacy_to).returns(true)

        visualization.reload

        return map, table, table_visualization, visualization
      end

      def create_table_visualization(carto_user, table)
        FactoryGirl.create(
          :carto_visualization, user: carto_user, type: 'table', name: table.name, map_id: table.map_id)
      end

      # Helper method for `create_full_visualization` results cleanup
      def destroy_full_visualization(map, table, table_visualization, visualization)
        table_visualization.destroy if table_visualization && Carto::Visualization.exists?(table_visualization.id)
        table.service.destroy if table && table.service && Carto::UserTable.exists?(table.id)
        table.destroy if table && Carto::UserTable.exists?(table.id)
        visualization.destroy if visualization && Carto::Visualization.exists?(visualization.id)
        map.destroy if map && Carto::Map.exists?(map.id)
      end

      def destroy_visualization(visualization_id)
        member = CartoDB::Visualization::Member.new(id: visualization_id).fetch
        member.delete
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0866738894907909, 'fraction_numerical': 0.00030954960532425323, 'mean_word_length': 3.462707182320442, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7044070', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1785, 'n_words': 369}