<commit_before># frozen_string_literal: true

require "test_helper"
require "support/event_helpers"

module GobiertoPeople
  module Api
    module V1
      class PeopleIndexTest < ActionDispatch::IntegrationTest

        include ::EventHelpers

        FAR_PAST = 10.years.ago.iso8601
        FAR_FUTURE = 10.years.from_now.iso8601

        def setup
          enable_submodule(madrid, :agendas)
          super
        end

        def madrid
          @madrid ||= sites(:madrid)
        end

        def justice_department
          @justice_department ||= gobierto_people_departments(:justice_department)
        end

        def coca_cola_group
          @coca_cola_group ||= gobierto_people_interest_groups(:coca_cola)
        end

        def tamara
          @tamara ||= gobierto_people_people(:tamara)
        end

        def person_attributes
          %w(
            id name email position bio bio_url avatar_url category political_group
            party url created_at updated_at content_block_records
          )
        end

        def people_attending_count
          ::GobiertoCalendars::EventAttendee.where(person_id: madrid.people.pluck(:id))
                                            .pluck(:person_id)
                                            .uniq.size
        end

        def people_with_events_on_justice_department
          [tamara]
        end

        def people_with_events_on_coca_cola_group
          [tamara]
        end

        def short_date(date)
          date[/^\d+-\d+-\d+/]
        end

        def test_people_index_test
          with(site: madrid) do

            get gobierto_people_api_v1_people_path

            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_attending_count, people.size

            assert array_match(person_attributes, people.first.keys)
          end
        end

        def test_people_index_with_filters_test
          with(site: madrid) do

            get(
              gobierto_people_api_v1_people_path,
              params: {
                department_id: justice_department.id,
                from_date: FAR_PAST,
                to_date: FAR_FUTURE
              }
            )

            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_with_events_on_justice_department.size, people.size
            assert_equal tamara.name, people.first["name"]
            assert_match "?end_date=#{ short_date(FAR_FUTURE) }&start_date=#{ short_date(FAR_PAST) }", people.first["url"]
          end
        end

        def test_people_index_with_interest_group_filter
          with(site: madrid) do
            get(
              gobierto_people_api_v1_people_path,
              params: {
                interest_group_id: coca_cola_group.id
              }
            )
            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_with_events_on_coca_cola_group.size, people.size
            assert_equal tamara.name, people.first["name"]
          end
        end

        def test_people_index_test_with_events_history
          tamara.attending_events.destroy_all
          create_event(person: tamara, starts_at: "15-01-2017")
          create_event(person: tamara, starts_at: "16-01-2017")

          with(site: madrid) do

            get(<commit_msg>Fix API test<commit_after># frozen_string_literal: true

require "test_helper"
require "support/event_helpers"

module GobiertoPeople
  module Api
    module V1
      class PeopleIndexTest < ActionDispatch::IntegrationTest

        include ::EventHelpers

        FAR_PAST = 10.years.ago.iso8601
        FAR_FUTURE = 10.years.from_now.iso8601

        attr_accessor(
          :madrid,
          :justice_department,
          :coca_cola_group,
          :tamara,
          :richard
        )

        def setup
          super
          @madrid = sites(:madrid)
          @justice_department = gobierto_people_departments(:justice_department)
          @coca_cola_group = gobierto_people_interest_groups(:coca_cola)
          @tamara = gobierto_people_people(:tamara)
          @richard = gobierto_people_people(:richard)

          enable_submodule(madrid, :agendas)
        end

        def person_attributes
          %w(
            id name email position bio bio_url avatar_url category political_group
            party url created_at updated_at content_block_records
          )
        end

        def people_attending_count
          ::GobiertoCalendars::EventAttendee.where(person_id: madrid.people.pluck(:id))
                                            .pluck(:person_id)
                                            .uniq.size
        end

        def people_with_activity_on_justice_department
          [tamara, richard]
        end

        def people_with_events_on_coca_cola_group
          [tamara]
        end

        def short_date(date)
          date[/^\d+-\d+-\d+/]
        end

        def test_people_index_test
          with(site: madrid) do

            get gobierto_people_api_v1_people_path

            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_attending_count, people.size

            assert array_match(person_attributes, people.first.keys)
          end
        end

        def test_people_index_with_filters_test
          with(site: madrid) do

            get(
              gobierto_people_api_v1_people_path,
              params: {
                department_id: justice_department.id,
                from_date: FAR_PAST,
                to_date: FAR_FUTURE
              }
            )

            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_with_activity_on_justice_department.size, people.size
            assert_equal richard.name, people.first["name"]
            assert_match "?end_date=#{ short_date(FAR_FUTURE) }&start_date=#{ short_date(FAR_PAST) }", people.first["url"]
          end
        end

        def test_people_index_with_interest_group_filter
          with(site: madrid) do
            get(
              gobierto_people_api_v1_people_path,
              params: {
                interest_group_id: coca_cola_group.id
              }
            )
            assert_response :success

            people = JSON.parse(response.body)

            assert_equal people_with_events_on_coca_cola_group.size, people.size
            assert_equal tamara.name, people.first["name"]
          end
        end

        def test_people_index_test_with_events_history
          tamara.attending_events.destroy_all
          create_event(person: tamara, starts_at: "15-01-2017")
          create_event(person: tamara, starts_at: "16-01-2017")

          with(site: madrid) do

            get(
{'fraction_non_alphanumeric': 0.06397794864355143, 'fraction_numerical': 0.009284781662556217, 'mean_word_length': 1.7956204379562044, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3631448', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1974, 'n_words': 344}