diff --git a/app/controllers/gobierto_data/api/v1/datasets_controller.rb b/app/controllers/gobierto_data/api/v1/datasets_controller.rb index e17832b8cf..4e52fe9297 100644 --- a/app/controllers/gobierto_data/api/v1/datasets_controller.rb +++ b/app/controllers/gobierto_data/api/v1/datasets_controller.rb @@ -11,6 +11,14 @@ class DatasetsController < BaseController skip_before_action :authenticate_in_site, only: [:new, :create, :update, :destroy] skip_before_action :set_admin_with_token, except: [:new, :create, :update, :destroy] + # GET /api/v1/data/catalog.xml + def catalog + @catalog = DatasetPresenter.new(current_site).build_catalog + respond_to do |format| + format.xml + end + end + # GET /api/v1/data/datasets # GET /api/v1/data/datasets.json # GET /api/v1/data/datasets.csv diff --git a/app/models/gobierto_data/dataset.rb b/app/models/gobierto_data/dataset.rb index ba2cdf7050..a0bdeecbad 100644 --- a/app/models/gobierto_data/dataset.rb +++ b/app/models/gobierto_data/dataset.rb @@ -11,6 +11,7 @@ class Dataset < ApplicationRecord include GobiertoAttachments::Attachable include GobiertoCommon::Collectionable include GobiertoCommon::Searchable + include GobiertoCommon::HasCustomFieldRecords multisearchable( against: [:name_translations ,:name_en, :name_es], @@ -30,6 +31,8 @@ class Dataset < ApplicationRecord has_many :visualizations, dependent: :destroy, class_name: "GobiertoData::Visualization" scope :sorted, -> { order(data_updated_at: :desc) } + scope :sorted_by_creation, -> { order(created_at: :desc) } + scope :visibles, -> { where(visibility_level: "active") } translates :name diff --git a/app/presenters/gobierto_data/dataset_presenter.rb b/app/presenters/gobierto_data/dataset_presenter.rb new file mode 100644 index 0000000000..9229918593 --- /dev/null +++ b/app/presenters/gobierto_data/dataset_presenter.rb @@ -0,0 +1,115 @@ +module GobiertoData + class DatasetPresenter + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TranslationHelper + + attr_reader :site + + def initialize(site) + @site = site + end + + def build_catalog + Hash.new.tap do |catalog| + catalog[:languages] = site.configuration.available_locales + catalog[:identifier_uri] = url_helpers.gobierto_data_root_url(host: site.domain) + site.configuration.available_locales.each do |locale| + catalog["title_#{locale}".to_sym] = I18n.t('presenters.gobierto_data.catalog.title',site: site, locale: locale) + catalog["description_#{locale}".to_sym] = I18n.t('presenters.gobierto_data.catalog.description',site: site, publisher_url: url_helpers.gobierto_data_root_url(host: site.domain), locale: locale) + end + catalog[:issued] = site.created_at.iso8601 + catalog[:modified] = site.updated_at.iso8601 + catalog[:homepage] = url_helpers.gobierto_data_root_url(host: site.domain) + catalog[:publisher] = site.configuration.configuration_variables["gobierto_data_catalog_organism"] || "" + catalog[:spatials] = site.configuration.configuration_variables["gobierto_data_spatials"] || [] + catalog[:theme] = site.configuration.configuration_variables["gobierto_data_theme_taxonomy"] || "" + catalog[:datasets] = build_datasets_for_catalog + end + end + + private + + def build_datasets_for_catalog + site.datasets.visibles.sorted_by_creation.map do |dataset| + build_dataset_for_catalog(dataset) + end + end + + def build_dataset_for_catalog(dataset) + Hash.new.tap do |dataset_presenter| + dataset_presenter[:url] = url_helpers.gobierto_data_datasets_url(host: site.domain, id: dataset.slug) + site.configuration.available_locales.each do |locale| + dataset_presenter["title_#{locale}".to_sym] = dataset.name(locale: locale) + dataset_presenter["description_#{locale}".to_sym] = description_from_custom_field_record(dataset)[locale] + end + dataset_presenter[:keywords] = category_from_custom_field_record(dataset) + dataset_presenter[:issued] = dataset.created_at.iso8601 + dataset_presenter[:modified] = dataset.updated_at.iso8601 + dataset_presenter[:update_frequency] = frecuency_from_custom_field_record(dataset).first + dataset_presenter[:update_frequency_in_days] = frecuency_from_custom_field_record(dataset).last + dataset_presenter[:license_url] = license_url_from_custom_field_record(dataset) + dataset_presenter[:distributions] = build_distribution_for_catalog(dataset) + end + end + + def build_distribution_for_catalog(dataset) + [ + Hash.new.tap do |distribution| + site.configuration.available_locales.each do |locale| + distribution["title_#{locale}_extended".to_sym] = "#{dataset.name(locale: locale)} #{I18n.t('presenters.gobierto_data.catalog.at_format')} application/csv" + end + distribution[:format] = 'application/csv' + distribution[:download_url] = url_helpers.download_gobierto_data_api_v1_dataset_url(slug: dataset.slug, host: site.domain) + distribution[:csv_size] = dataset.size&.[](:csv) + end + ] + end + + def url_helpers + Rails.application.routes.url_helpers + end + + def site_locale + site.configuration.default_locale + end + + def description_from_custom_field_record(dataset) + if dataset.custom_field_record_with_uid("description") + dataset.custom_field_record_with_uid("description").payload["description"] + else + "" + end + end + + def license_url_from_custom_field_record(dataset) + custom_field_record = dataset.custom_field_record_with_uid('dataset-license') + if !custom_field_record.nil? && !custom_field_record.payload["dataset-license"].blank? + site.terms.find_by(id: custom_field_record.payload["dataset-license"])[:description_translations][site_locale] + else + "" + end + end + + def category_from_custom_field_record(dataset) + custom_field_record = dataset.custom_field_record_with_uid("category") + if !custom_field_record.nil? && !custom_field_record.payload["category"].blank? + site.terms.find_by(id: custom_field_record.payload["category"])[:name_translations] + else + "" + end + end + + def frecuency_from_custom_field_record(dataset) + custom_field_record = dataset.custom_field_record_with_uid("frequency") + if !custom_field_record.nil? && !custom_field_record.payload["frequency"].blank? + term = site.terms.find_by(id: custom_field_record.payload["frequency"]) + [ + term[:name_translations]["en"], + term[:description_translations]["en"] + ] + else + ["", ""] + end + end + end +end diff --git a/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb b/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb new file mode 100644 index 0000000000..1374c96ead --- /dev/null +++ b/app/views/gobierto_data/api/v1/datasets/catalog.xml.erb @@ -0,0 +1,84 @@ + + + + <%= @catalog[:identifier_uri] %> + <% @catalog[:languages].each do |locale| -%> + <%= @catalog["title_#{locale}".to_sym] %> + <%= @catalog["description_#{locale}".to_sym] %> + <% end -%> + + <%= @catalog[:issued] %> + <%= @catalog[:modified] %> + <% @catalog[:languages].each do |locale| -%> + <%= locale %> + <% end -%> + <% @catalog[:spatials].each do |spatial| -%> + + <% end -%> + + + <% @catalog[:datasets].each do |dataset| -%> + + + <%= dataset[:url]%> + <% @catalog[:languages].each do |locale| -%> + <%= dataset["title_#{locale}".to_sym] %> + <%= dataset["description_#{locale}".to_sym] %> + <%= dataset[:keywords][locale] %> + <% end -%> + <%= dataset[:issued] %> + <%= dataset[:modified] %> + <% if !dataset[:update_frequency].blank? && !dataset[:update_in_days].blank? -%> + + + + + <%= dataset[:update_frequency] %> + <%= dataset[:update_frequency_in_days] %> + + + + + <% end -%> + <% @catalog[:languages].each do |locale| -%> + <%= locale %> + <% end -%> + + + <% @catalog[:spatials].each do |spatial| -%> + + <% end -%> + <% dataset[:distributions].each do |distribution| -%> + + + <%= dataset[:url] %> + <% @catalog[:languages].each do |locale| -%> + <%= dataset["title_#{locale}_extended"] %> + <% end -%> + <%= distribution[:download_url] %> + <% unless distribution[:csv_size].blank? -%> + <%= distribution[:csv_size] %> + <% end -%> + + + <%= distribution[:format] %> + Comma-separated values (CSV) + + + + + <% end -%> + + + <% end -%> + + diff --git a/config/locales/defaults/ca.yml b/config/locales/defaults/ca.yml index 97ce9a86e7..c9f62ca9c3 100644 --- a/config/locales/defaults/ca.yml +++ b/config/locales/defaults/ca.yml @@ -15,7 +15,7 @@ ca: - dv - ds abbr_month_names: - - + - - gen - feb - mar @@ -43,7 +43,7 @@ ca: short: "%d %b" shortest: "%d %b %y" month_names: - - + - - gener - febrer - març diff --git a/config/locales/defaults/en.yml b/config/locales/defaults/en.yml index 30e4547cb6..1ed62f5e6b 100644 --- a/config/locales/defaults/en.yml +++ b/config/locales/defaults/en.yml @@ -15,7 +15,7 @@ en: - Fri - Sat abbr_month_names: - - + - - Jan - Feb - Mar @@ -43,7 +43,7 @@ en: short: "%b %d %Y" shortest: "%d %b %y" month_names: - - + - - January - February - March diff --git a/config/locales/defaults/es.yml b/config/locales/defaults/es.yml index 5970217fcb..f372424c77 100644 --- a/config/locales/defaults/es.yml +++ b/config/locales/defaults/es.yml @@ -15,7 +15,7 @@ es: - vie - sáb abbr_month_names: - - + - - ene - feb - mar @@ -43,7 +43,7 @@ es: short: "%d %b %Y" shortest: "%d %b %y" month_names: - - + - - enero - febrero - marzo diff --git a/config/locales/gobierto_data/presenters/ca.yml b/config/locales/gobierto_data/presenters/ca.yml new file mode 100644 index 0000000000..f4d5bdf13f --- /dev/null +++ b/config/locales/gobierto_data/presenters/ca.yml @@ -0,0 +1,9 @@ +--- +ca: + presenters: + gobierto_data: + catalog: + at_format: en format + description: Catàleg public de dades dels conjunts de dades publicades per% + {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/presenters/en.yml b/config/locales/gobierto_data/presenters/en.yml new file mode 100644 index 0000000000..820f5af79f --- /dev/null +++ b/config/locales/gobierto_data/presenters/en.yml @@ -0,0 +1,9 @@ +--- +en: + presenters: + gobierto_data: + catalog: + at_format: at format + description: Public catalog with datasets published by %{site}, through URL + %{publisher_url} + title: Catalog for datasets of %{site} into format rdf/xml dcat diff --git a/config/locales/gobierto_data/presenters/es.yml b/config/locales/gobierto_data/presenters/es.yml new file mode 100644 index 0000000000..9ab463e299 --- /dev/null +++ b/config/locales/gobierto_data/presenters/es.yml @@ -0,0 +1,10 @@ +--- +es: + presenters: + gobierto_data: + catalog: + at_format: en formato + description: Catalogo publico de datos los conjuntos de datos publicados por + %{site}, a través de la URL %{publisher_url} + title: Catalogo DCAT para los conjuntos de datos de %{site} en formato rdf/xml + dcat diff --git a/config/routes.rb b/config/routes.rb index 306002717d..0cffcecc15 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -642,6 +642,7 @@ resources :favorites, only: [:index] collection do get :meta + get :catalog end member do get "meta" => "datasets#dataset_meta" diff --git a/db/seeds/gobierto_seeds/gobierto_data/recipe.rb b/db/seeds/gobierto_seeds/gobierto_data/recipe.rb index 6900f313ff..9ec13cfeb4 100644 --- a/db/seeds/gobierto_seeds/gobierto_data/recipe.rb +++ b/db/seeds/gobierto_seeds/gobierto_data/recipe.rb @@ -46,10 +46,10 @@ def self.run(site) if vocabulary.new_record? vocabulary.name_translations = { ca: "Freqüència de conjunt de dades", en: "Dataset frequency", es: "Frecuencia de conjunto de datos" } vocabulary.save - vocabulary.terms.create(name_translations: { ca: "Anual", en: "Annual", es: "Anual" }, position: 1) - vocabulary.terms.create(name_translations: { ca: "Trimestral", en: "Quarterly", es: "Trimestral" }, position: 2) - vocabulary.terms.create(name_translations: { ca: "Mensual", en: "Monthly", es: "Mensual" }, position: 3) - vocabulary.terms.create(name_translations: { ca: "Diària", en: "Daily", es: "Diaria" }, position: 4) + vocabulary.terms.create(name_translations: { ca: "Anual", en: "Annual", es: "Anual" }, description_translations: { ca: 365, en: 365, es: 365 }, position: 1) + vocabulary.terms.create(name_translations: { ca: "Trimestral", en: "Quarterly", es: "Trimestral" }, description_translations: { ca: 120, en: 120, es: 120 }, position: 2) + vocabulary.terms.create(name_translations: { ca: "Mensual", en: "Monthly", es: "Mensual" }, description_translations: { ca: 30, en: 30, es: 30 }, position: 3) + vocabulary.terms.create(name_translations: { ca: "Diària", en: "Daily", es: "Diaria" }, description_translations: { ca: 1, en: 1, es: 1 }, position: 4) end frequency.name_translations = { ca: "Freqüència", en: "Frequency", es: "Frecuencia" } frequency.position = 2 @@ -212,7 +212,28 @@ def self.run(site) dataset_default_geometry.options = { configuration: {} } dataset_default_geometry.save end + + unless site.configuration.configuration_variables["gobierto_data_catalog_organism"].present? + extra_conf = {"gobierto_data_catalog_organism" => "https://datos.gob.es/es/recurso/sector-publico/org/Organismo/"} + existing_conf = YAML.load(site.configuration.raw_configuration_variables) || {} + site.configuration.raw_configuration_variables = existing_conf.merge(extra_conf).to_yaml + site.save + end + unless site.configuration.configuration_variables["gobierto_data_spatials"].present? + extra_conf = { "gobierto_data_spatials" => [ "http://datos.gob.es/recurso/sector-publico/territorio/Pais/España", "http://datos.gob.es/recurso/sector-publico/territorio/Autonomia/Madrid", "http://datos.gob.es/recurso/sector-publico/territorio/Provincia/Madrid", "https://datos.gob.es/recurso/sector-publico/territorio/Ciudad/Getafe" ] } + existing_conf = YAML.load(site.configuration.raw_configuration_variables) || {} + site.configuration.raw_configuration_variables = existing_conf.merge(extra_conf).to_yaml + site.save + end + unless site.configuration.configuration_variables["gobierto_data_theme_taxonomy"].present? + extra_conf = { "gobierto_data_theme_taxonomy" => "http://datos.gob.es/kos/sector-publico/sector/sector-publico"} + existing_conf = YAML.load(site.configuration.raw_configuration_variables) || {} + site.configuration.raw_configuration_variables = existing_conf.merge(extra_conf).to_yaml + site.save + end + end + end end end 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_field_records.yml b/test/fixtures/gobierto_common/custom_field_records.yml index e652223dfb..a1ee44a82a 100644 --- a/test/fixtures/gobierto_common/custom_field_records.yml +++ b/test/fixtures/gobierto_common/custom_field_records.yml @@ -391,6 +391,11 @@ users_dataset_md_with_translations_custom_field_record: "md-with-translations" => { en: "Paragraph with translations!", es: "¡Párrafo con traducciones!" } }.to_json %> +users_dataset_md_with_translations_custom_field_record: + item: users_dataset (GobiertoData::Dataset) + custom_field: madrid_data_datasets_custom_field_description + payload: <%= {"description": {"en": "This dataset contains the council' persons related.", "es": "Este dataset contiene las personas relaccionadas con el ayuntamiento."}}.to_json %> + ## Users peter_custom_field_issue: diff --git a/test/fixtures/gobierto_common/custom_fields.yml b/test/fixtures/gobierto_common/custom_fields.yml index e007fd5fcd..ea675fd8f1 100644 --- a/test/fixtures/gobierto_common/custom_fields.yml +++ b/test/fixtures/gobierto_common/custom_fields.yml @@ -344,6 +344,14 @@ madrid_data_datasets_custom_field_md_with_translations: field_type: <%= GobiertoCommon::CustomField.field_types[:localized_paragraph] %> uid: md-with-translations +madrid_data_datasets_custom_field_description: + site: madrid + class_name: GobiertoData::Dataset + position: 4 + name_translations: <%= {"en": "Description", "es": "Descripción"}.to_json %> + field_type: <%= GobiertoCommon::CustomField.field_types[:localized_paragraph] %> + uid: description-datasets + madrid_custom_field_human_resources_table_plugin: site: madrid class_name: GobiertoPlans::Node diff --git a/test/fixtures/gobierto_data/catalog.xml b/test/fixtures/gobierto_data/catalog.xml new file mode 100644 index 0000000000..f237f6c598 --- /dev/null +++ b/test/fixtures/gobierto_data/catalog.xml @@ -0,0 +1,119 @@ + + + + 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 + Catalogo DCAT para los conjuntos de datos de Ayuntamiento de Madrid en formato rdf/xml dcat + Catalogo publico de datos los conjuntos de datos publicados por Ayuntamiento de Madrid, a través de la URL http://madrid.gobierto.test/datos + + 2019-01-01T08:00:00+01:00 + 2019-01-01T08:00:00+01:00 + en + es + + + + + http://madrid.gobierto.test/datos/no-size + No size + + + No se el size + + + 2020-01-06T08:00:00+01:00 + 2020-01-06T08:00:00+01:00 + en + es + + + + + http://madrid.gobierto.test/datos/no-size + + + http://madrid.gobierto.test/api/v1/data/datasets/no-size/download.csv + + + application/csv + Comma-separated values (CSV) + + + + + + + + + http://madrid.gobierto.test/datos/events-dataset + Events + + + Eventos + + + 2020-01-03T08:00:00+01:00 + 2020-01-03T08:00:00+01:00 + en + es + + + + + http://madrid.gobierto.test/datos/events-dataset + + + http://madrid.gobierto.test/api/v1/data/datasets/events-dataset/download.csv + + + application/csv + Comma-separated values (CSV) + + + + + + + + + http://madrid.gobierto.test/datos/users-dataset + Users + + Culture + Usuarios + + Cultura + 2020-01-02T08:00:00+01:00 + 2020-01-02T08:00:00+01:00 + en + es + + + + + http://madrid.gobierto.test/datos/users-dataset + + + http://madrid.gobierto.test/api/v1/data/datasets/users-dataset/download.csv + + + application/csv + Comma-separated values (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 09e2c52358..f63f7229d1 100644 --- a/test/fixtures/sites.yml +++ b/test/fixtures/sites.yml @@ -34,6 +34,8 @@ madrid: organization_address: Fake St., 123 organization_document_number: 0123456789A visibility_level: <%= Site.visibility_levels["active"] %> + 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 new file mode 100644 index 0000000000..807ab0e648 --- /dev/null +++ b/test/presenters/gobierto_data/dataset_presenter_test.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require "test_helper" + +module GobiertoData + class DatasetPresenterTest < ActiveSupport::TestCase + + def setup + super + @site = sites(:madrid) + @subject = DatasetPresenter.new(@site) + @other_site = sites(:huesca) + @other_subject = DatasetPresenter.new(@other_site) + end + + def test_structure_catalog_building_do_not_show_draft_datasets + catalog = @subject.build_catalog + datasets_published = @site.datasets.visibles.size + assert_equal datasets_published, catalog[:datasets].size + end + + def test_structure_catalog_building_using_site_with_datasets + catalog = @subject.build_catalog + assert catalog.has_key? :identifier_uri + @site.configuration.available_locales.each do |locale| + assert catalog.has_key? "title_#{locale}".to_sym + assert catalog.has_key? "description_#{locale}".to_sym + end + assert catalog.has_key? :issued + assert catalog.has_key? :modified + assert catalog.has_key? :homepage + assert catalog.has_key? :publisher + assert catalog.has_key? :spatials + assert catalog.has_key? :theme + assert catalog.has_key? :datasets + catalog[:datasets].each do |dataset| + assert dataset.has_key? :url + @site.configuration.available_locales.each do |locale| + assert dataset.has_key? "title_#{locale}".to_sym + assert dataset.has_key? "description_#{locale}".to_sym + end + assert dataset.has_key? :keywords + assert dataset.has_key? :issued + assert dataset.has_key? :modified + assert dataset.has_key? :update_frequency + assert dataset.has_key? :update_frequency_in_days + assert dataset.has_key? :license_url + assert dataset.has_key? :distributions + dataset[:distributions].each do |distribution| + @site.configuration.available_locales.each do |locale| + assert distribution.has_key? "title_#{locale}_extended".to_sym + end + assert distribution.has_key? :format + assert distribution.has_key? :download_url + assert distribution.has_key? :csv_size + end + end + end + + def test_structure_catalog_building_using_site_without_datasets + catalog = @other_subject.build_catalog + assert_equal 0, catalog[:datasets].size + end + end +end