From a452f6db65b83e7be5985104acc034959058feb4 Mon Sep 17 00:00:00 2001 From: stbn Date: Thu, 29 Apr 2021 15:59:07 +0200 Subject: [PATCH] add&fix test --- app/models/gobierto_data/dataset.rb | 1 - .../gobierto_data/dataset_presenter.rb | 2 +- .../api/v1/datasets/catalog.xml.erb | 6 +- .../locales/gobierto_data/presenters/ca.yml | 2 +- config/locales/gobierto_data/views/ca.yml | 2 + config/locales/gobierto_data/views/en.yml | 2 + config/locales/gobierto_data/views/es.yml | 2 + config/routes.rb | 3 +- .../api/v1/datasets_controller_test.rb | 38 +++++++- .../gobierto_common/custom_fields.yml | 2 +- test/fixtures/gobierto_data/catalog.xml | 95 +++++++++++++++++++ test/fixtures/gobierto_data/datasets.yml | 21 ++-- test/fixtures/sites.yml | 4 +- .../gobierto_data/dataset_presenter_test.rb | 2 +- 14 files changed, 154 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/gobierto_data/catalog.xml diff --git a/app/models/gobierto_data/dataset.rb b/app/models/gobierto_data/dataset.rb index 65a137c7fa..0e9ac80a20 100644 --- a/app/models/gobierto_data/dataset.rb +++ b/app/models/gobierto_data/dataset.rb @@ -18,7 +18,6 @@ class Dataset < ApplicationRecord scope :sorted, -> { order(data_updated_at: :desc) } scope :visibles, -> { where(visibility_level: "active") } - scope :by_site, ->(site_id) { where(site_id: site_id) } translates :name diff --git a/app/presenters/gobierto_data/dataset_presenter.rb b/app/presenters/gobierto_data/dataset_presenter.rb index 733d995a5a..1d6df40df2 100644 --- a/app/presenters/gobierto_data/dataset_presenter.rb +++ b/app/presenters/gobierto_data/dataset_presenter.rb @@ -58,7 +58,7 @@ def url_helpers end def site_locale - site.configuration_data["default_locale"] + site.configuration.default_locale end def description_custom_field_record(dataset) diff --git a/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb b/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb index f79c345384..3f15a5550b 100644 --- a/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb +++ b/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb @@ -6,7 +6,7 @@ xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" - xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><% cache @catalog do %> + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <%= @catalog[:identifier_uri] %> <%= @catalog[:title] %> @@ -34,7 +34,7 @@ <%= @identifier_uri %> - <%= dataset[:title] %> in CSV format + <%= dataset[:title] %> <%= t 'gobierto_data.catalog.csv_format' %> <%= dataset[:description] %> <%= distribution[:download_url] %> <%= distribution[:format] %> @@ -43,5 +43,5 @@ <% end %> <% end %> - <% end %> + diff --git a/config/locales/gobierto_data/presenters/ca.yml b/config/locales/gobierto_data/presenters/ca.yml index 4b18ad0796..92169988fc 100644 --- a/config/locales/gobierto_data/presenters/ca.yml +++ b/config/locales/gobierto_data/presenters/ca.yml @@ -4,5 +4,5 @@ ca: gobierto_data: catalog: description: Catàleg public de dades dels conjunts de dades publicades per% - {site}, a través de la URL% {publisher_url} + {site}, a través de la URL %{publisher_url} title: Catàleg DCAT de conjunts de dades de %{site} en format rdf/xml dcat diff --git a/config/locales/gobierto_data/views/ca.yml b/config/locales/gobierto_data/views/ca.yml index 3b3ecdae56..c5be1d618a 100644 --- a/config/locales/gobierto_data/views/ca.yml +++ b/config/locales/gobierto_data/views/ca.yml @@ -1,6 +1,8 @@ --- ca: gobierto_data: + catalog: + csv_format: en format CSV data_type: boolean: boolean date: data diff --git a/config/locales/gobierto_data/views/en.yml b/config/locales/gobierto_data/views/en.yml index 7c8bdba413..c2b9cae929 100644 --- a/config/locales/gobierto_data/views/en.yml +++ b/config/locales/gobierto_data/views/en.yml @@ -1,5 +1,7 @@ --- en: + catalog: + csv_format: at CSV format gobierto_data: data_type: boolean: boolean diff --git a/config/locales/gobierto_data/views/es.yml b/config/locales/gobierto_data/views/es.yml index 6cf38bd5c5..0a9391682a 100644 --- a/config/locales/gobierto_data/views/es.yml +++ b/config/locales/gobierto_data/views/es.yml @@ -1,5 +1,7 @@ --- es: + catalog: + csv_format: en formato CSV gobierto_data: data_type: boolean: boolean diff --git a/config/routes.rb b/config/routes.rb index 6cc04b6007..300ab5d1f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -641,8 +641,7 @@ resources :favorites, only: [:index] collection do get :meta - # get :catalog - get "catalog" => "datasets#catalog" + get :catalog end member do get "meta" => "datasets#dataset_meta" diff --git a/test/controllers/gobierto_data/api/v1/datasets_controller_test.rb b/test/controllers/gobierto_data/api/v1/datasets_controller_test.rb index a72cf5eda3..1a58d99db0 100644 --- a/test/controllers/gobierto_data/api/v1/datasets_controller_test.rb +++ b/test/controllers/gobierto_data/api/v1/datasets_controller_test.rb @@ -67,6 +67,10 @@ def datasets_md_with_translations @datasets_md_with_translations ||= gobierto_common_custom_fields(:madrid_data_datasets_custom_field_md_with_translations) end + def datasets_descriptions + @datasets_descriptions ||= gobierto_common_custom_fields(:madrid_data_datasets_custom_field_description) + end + def other_site_dataset @other_site_dataset ||= gobierto_data_datasets(:santander_dataset) end @@ -93,7 +97,8 @@ def array_data(dataset) dataset.rails_model&.columns_hash&.transform_values(&:type)&.to_s, GobiertoCommon::CustomFieldRecord.find_by(item: dataset, custom_field: datasets_category)&.value_string, GobiertoCommon::CustomFieldRecord.find_by(item: dataset, custom_field: datasets_md_without_translations)&.value_string, - GobiertoCommon::CustomFieldRecord.find_by(item: dataset, custom_field: datasets_md_with_translations)&.value_string + GobiertoCommon::CustomFieldRecord.find_by(item: dataset, custom_field: datasets_md_with_translations)&.value_string || "", + GobiertoCommon::CustomFieldRecord.find_by(item: dataset, custom_field: datasets_descriptions)&.value_string ] end @@ -168,8 +173,9 @@ def test_index_as_csv parsed_csv = CSV.parse(response_data).map { |row| row.map(&:to_s) } assert_equal active_datasets_count + 1, parsed_csv.count - assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations), parsed_csv.first - assert_includes parsed_csv, array_data(dataset) + assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations description-datasets), parsed_csv.first + + assert_includes parsed_csv.drop(1).take(1), array_data(dataset) refute_includes parsed_csv, array_data(other_site_dataset) end end @@ -202,7 +208,7 @@ def test_index_xlsx_format assert_equal 1, parsed_xlsx.worksheets.count sheet = parsed_xlsx.worksheets.first assert_nil sheet[active_datasets_count + 1] - assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations), sheet[0].cells.map(&:value) + assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations description-datasets), sheet[0].cells.map(&:value) values = (1..active_datasets_count).map do |row_number| sheet[row_number].cells.map { |cell| cell.value.to_s } end @@ -315,12 +321,34 @@ def test_index_when_md_custom_field_changes_translations_availability parsed_csv = CSV.parse(response_data).map { |row| row.map(&:to_s) } assert_equal active_datasets_count + 1, parsed_csv.count - assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations), parsed_csv.first + assert_equal %w(id name slug table_name data_updated_at columns category md-without-translations md-with-translations description-datasets), parsed_csv.first assert_includes parsed_csv, array_data(dataset) refute_includes parsed_csv, array_data(other_site_dataset) end end + def test_catalog + with(site: site) do + get catalog_gobierto_data_api_v1_datasets_path(format: :xml), as: :xml + assert_response :success + + response_xml = response.parsed_body + expected = File.read("test/fixtures/gobierto_data/catalog.xml") + assert_equal response_xml, expected + end + end + + def test_catalog_dont_show_draft_dataset + with(site: site) do + get catalog_gobierto_data_api_v1_datasets_path(format: :xml), as: :xml + assert_response :success + response_xml = response.parsed_body + + refute_includes response_xml, 'Interest Groups' + refute_includes response_xml, 'Grupos de Interés' + end + end + end end end diff --git a/test/fixtures/gobierto_common/custom_fields.yml b/test/fixtures/gobierto_common/custom_fields.yml index 580212c638..ea675fd8f1 100644 --- a/test/fixtures/gobierto_common/custom_fields.yml +++ b/test/fixtures/gobierto_common/custom_fields.yml @@ -350,7 +350,7 @@ madrid_data_datasets_custom_field_description: position: 4 name_translations: <%= {"en": "Description", "es": "Descripción"}.to_json %> field_type: <%= GobiertoCommon::CustomField.field_types[:localized_paragraph] %> - uid: description + uid: description-datasets madrid_custom_field_human_resources_table_plugin: site: madrid diff --git a/test/fixtures/gobierto_data/catalog.xml b/test/fixtures/gobierto_data/catalog.xml new file mode 100644 index 0000000000..bf147f7b1c --- /dev/null +++ b/test/fixtures/gobierto_data/catalog.xml @@ -0,0 +1,95 @@ + + + + http://madrid.gobierto.test/datos + Catalog for datasets of Ayuntamiento de Madrid into format rdf/xml dcat + Public catalog with datasets published by Ayuntamiento de Madrid, through URL http://madrid.gobierto.test/datos + 2019-01-01 08:00:00 +0100 + 2019-01-01 08:00:00 +0100 + en + + + + http://madrid.gobierto.test/datos/users-dataset + Users + + 2020-01-02 08:00:00 +0100 + 2020-01-02 08:00:00 +0100 + en + + + + Ayuntamiento de Madrid + contact@madrid.es + + + + + + Users en format CSV + + http://madrid.gobierto.test/api/v1/data/datasets/users-dataset/download.csv + application/csv + + + + + + http://madrid.gobierto.test/datos/events-dataset + Events + + 2020-01-03 08:00:00 +0100 + 2020-01-03 08:00:00 +0100 + en + + + + Ayuntamiento de Madrid + contact@madrid.es + + + + + + Events en format CSV + + http://madrid.gobierto.test/api/v1/data/datasets/events-dataset/download.csv + application/csv + + + + + + http://madrid.gobierto.test/datos/no-size + No size + + 2020-01-06 08:00:00 +0100 + 2020-01-06 08:00:00 +0100 + en + + + + Ayuntamiento de Madrid + contact@madrid.es + + + + + + No size en format CSV + + http://madrid.gobierto.test/api/v1/data/datasets/no-size/download.csv + application/csv + + + + + + diff --git a/test/fixtures/gobierto_data/datasets.yml b/test/fixtures/gobierto_data/datasets.yml index d5d841a76f..678a1f0f51 100644 --- a/test/fixtures/gobierto_data/datasets.yml +++ b/test/fixtures/gobierto_data/datasets.yml @@ -3,8 +3,8 @@ users_dataset: name_translations: <%= { en: "Users", es: "Usuarios" }.to_json %> table_name: users slug: users-dataset - created_at: <%= Time.current %> - updated_at: <%= Time.current %> + created_at: <%= Time.zone.parse('2020-01-02 8:00') %> + updated_at: <%= Time.zone.parse('2020-01-02 8:00') %> visibility_level: <%= GobiertoData::Dataset.visibility_levels["active"] %> size: <%= { csv: 15.megabytes, json: 25.megabytes }.to_json %> @@ -13,8 +13,8 @@ events_dataset: name_translations: <%= { en: "Events", es: "Eventos" }.to_json %> table_name: gc_events slug: events-dataset - created_at: <%= Time.current %> - updated_at: <%= Time.current %> + created_at: <%= Time.zone.parse('2020-01-03 8:00') %> + updated_at: <%= Time.zone.parse('2020-01-03 8:00') %> visibility_level: <%= GobiertoData::Dataset.visibility_levels["active"] %> size: <%= { csv: 3.megabytes, json: 4.megabytes }.to_json %> @@ -23,8 +23,8 @@ draft_dataset: name_translations: <%= { en: "Interest Groups", es: "Grupos de Interés" }.to_json %> table_name: gp_interest_groups slug: interest-groups-dataset - created_at: <%= Time.current %> - updated_at: <%= Time.current %> + created_at: <%= Time.zone.parse('2020-01-04 8:00') %> + updated_at: <%= Time.zone.parse('2020-01-04 8:00') %> visibility_level: <%= GobiertoData::Dataset.visibility_levels["draft"] %> santander_dataset: @@ -32,8 +32,8 @@ santander_dataset: name_translations: <%= { en: "Santander dataset", es: "Dataset de Santander" }.to_json %> table_name: activities slug: santander-dataset - created_at: <%= Time.current %> - updated_at: <%= Time.current %> + created_at: <%= Time.zone.parse('2020-01-05 8:00') %> + updated_at: <%= Time.zone.parse('2020-01-05 8:00') %> visibility_level: <%= GobiertoData::Dataset.visibility_levels["active"] %> no_size_dataset: @@ -41,7 +41,6 @@ no_size_dataset: name_translations: <%= { en: "No size", es: "No se el size" }.to_json %> table_name: sites slug: no-size - created_at: <%= Time.current %> - updated_at: <%= Time.current %> + created_at: <%= Time.zone.parse('2020-01-06 8:00') %> + updated_at: <%= Time.zone.parse('2020-01-06 8:00') %> visibility_level: <%= GobiertoData::Dataset.visibility_levels["active"] %> - diff --git a/test/fixtures/sites.yml b/test/fixtures/sites.yml index c2b6e097b9..f63f7229d1 100644 --- a/test/fixtures/sites.yml +++ b/test/fixtures/sites.yml @@ -34,8 +34,8 @@ madrid: organization_address: Fake St., 123 organization_document_number: 0123456789A visibility_level: <%= Site.visibility_levels["active"] %> - created_at: <%= 1.year.ago %> - updated_at: <%= 1.week.ago %> + created_at: <%= Time.zone.parse('2019-01-01 8:00') %> + updated_at: <%= Time.zone.parse('2019-01-01 8:00') %> santander: title_translations: <%= { 'en' => 'Transparencia Ciudadana', 'es' => 'Transparencia Ciudadana' }.to_json %> diff --git a/test/presenters/gobierto_data/dataset_presenter_test.rb b/test/presenters/gobierto_data/dataset_presenter_test.rb index 76e9cb9389..0cc96743fd 100644 --- a/test/presenters/gobierto_data/dataset_presenter_test.rb +++ b/test/presenters/gobierto_data/dataset_presenter_test.rb @@ -15,7 +15,7 @@ def setup def test_structure_catalog_building_do_not_show_draft_datasets catalog = @subject.build_catalog - datasets_published = GobiertoData::Dataset.by_site(@site.id).visibles.size + datasets_published = @site.datasets.visibles.size assert_equal datasets_published, catalog[:datasets].size end