Skip to content

Commit

Permalink
add&fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
stbnrivas committed Apr 30, 2021
1 parent 57d273c commit a452f6d
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 28 deletions.
1 change: 0 additions & 1 deletion app/models/gobierto_data/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/presenters/gobierto_data/dataset_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions app/views/gobierto_data/api/v1/datasets/catalog.xml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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#">
<dcat:Catalog rdf:about="https://data.some.org/catalog">
<dct:identifier><%= @catalog[:identifier_uri] %></dct:identifier>
<dct:title><%= @catalog[:title] %></dct:title>
Expand Down Expand Up @@ -34,7 +34,7 @@
<dcat:distribution>
<dcat:Distribution>
<dct:identifier><%= @identifier_uri %></dct:identifier>
<dct:title><%= dataset[:title] %> in CSV format</dct:title>
<dct:title><%= dataset[:title] %> <%= t 'gobierto_data.catalog.csv_format' %></dct:title>
<dct:description><%= dataset[:description] %></dct:description>
<dcat:downloadURL rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI"><%= distribution[:download_url] %></dcat:downloadURL>
<dcat:mediaType><%= distribution[:format] %></dcat:mediaType>
Expand All @@ -43,5 +43,5 @@
<% end %>
</dcat:Dataset>
</dcat:dataset><% end %>
</dcat:Catalog><% end %>
</dcat:Catalog>
</rdf:RDF>
2 changes: 1 addition & 1 deletion config/locales/gobierto_data/presenters/ca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/locales/gobierto_data/views/ca.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
ca:
gobierto_data:
catalog:
csv_format: en format CSV
data_type:
boolean: boolean
date: data
Expand Down
2 changes: 2 additions & 0 deletions config/locales/gobierto_data/views/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
en:
catalog:
csv_format: at CSV format
gobierto_data:
data_type:
boolean: boolean
Expand Down
2 changes: 2 additions & 0 deletions config/locales/gobierto_data/views/es.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
es:
catalog:
csv_format: en formato CSV
gobierto_data:
data_type:
boolean: boolean
Expand Down
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 33 additions & 5 deletions test/controllers/gobierto_data/api/v1/datasets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/gobierto_common/custom_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions test/fixtures/gobierto_data/catalog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
xmlns:time="http://www.w3.org/2006/time#"
xmlns:dct="http://purl.org/dc/terms/"
xmlns:dcat="http://www.w3.org/ns/dcat#"
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#">
<dcat:Catalog rdf:about="https://data.some.org/catalog">
<dct:identifier>http://madrid.gobierto.test/datos</dct:identifier>
<dct:title>Catalog for datasets of Ayuntamiento de Madrid into format rdf/xml dcat</dct:title>
<dct:description>Public catalog with datasets published by Ayuntamiento de Madrid, through URL http://madrid.gobierto.test/datos</dct:description>
<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-01-01 08:00:00 +0100</dct:issued>
<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2019-01-01 08:00:00 +0100</dct:modified>
<dct:language>en</dct:language>
<foaf:homepage rdf:resource="http://madrid.gobierto.test/datos"/>
<dcat:dataset>
<dcat:Dataset rdf:about="http://madrid.gobierto.test/datos/users-dataset">
<dct:identifier>http://madrid.gobierto.test/datos/users-dataset</dct:identifier>
<dct:title>Users</dct:title>
<dct:description></dct:description>
<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-02 08:00:00 +0100</dct:issued>
<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-02 08:00:00 +0100</dct:modified>
<dct:language>en</dct:language>
<dct:license rdf:resource="Users"/>
<dct:publisher>
<foaf:Organization>
<foaf:name>Ayuntamiento de Madrid</foaf:name>
<foaf:mbox>[email protected]</foaf:mbox>
</foaf:Organization>
</dct:publisher>
<dcat:distribution>
<dcat:Distribution>
<dct:identifier></dct:identifier>
<dct:title>Users en format CSV</dct:title>
<dct:description></dct:description>
<dcat:downloadURL rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://madrid.gobierto.test/api/v1/data/datasets/users-dataset/download.csv</dcat:downloadURL>
<dcat:mediaType>application/csv</dcat:mediaType>
</dcat:Distribution>
</dcat:distribution>
</dcat:Dataset>
</dcat:dataset><dcat:dataset>
<dcat:Dataset rdf:about="http://madrid.gobierto.test/datos/events-dataset">
<dct:identifier>http://madrid.gobierto.test/datos/events-dataset</dct:identifier>
<dct:title>Events</dct:title>
<dct:description></dct:description>
<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-03 08:00:00 +0100</dct:issued>
<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-03 08:00:00 +0100</dct:modified>
<dct:language>en</dct:language>
<dct:license rdf:resource="Events"/>
<dct:publisher>
<foaf:Organization>
<foaf:name>Ayuntamiento de Madrid</foaf:name>
<foaf:mbox>[email protected]</foaf:mbox>
</foaf:Organization>
</dct:publisher>
<dcat:distribution>
<dcat:Distribution>
<dct:identifier></dct:identifier>
<dct:title>Events en format CSV</dct:title>
<dct:description></dct:description>
<dcat:downloadURL rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://madrid.gobierto.test/api/v1/data/datasets/events-dataset/download.csv</dcat:downloadURL>
<dcat:mediaType>application/csv</dcat:mediaType>
</dcat:Distribution>
</dcat:distribution>
</dcat:Dataset>
</dcat:dataset><dcat:dataset>
<dcat:Dataset rdf:about="http://madrid.gobierto.test/datos/no-size">
<dct:identifier>http://madrid.gobierto.test/datos/no-size</dct:identifier>
<dct:title>No size</dct:title>
<dct:description></dct:description>
<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-06 08:00:00 +0100</dct:issued>
<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2020-01-06 08:00:00 +0100</dct:modified>
<dct:language>en</dct:language>
<dct:license rdf:resource="No size"/>
<dct:publisher>
<foaf:Organization>
<foaf:name>Ayuntamiento de Madrid</foaf:name>
<foaf:mbox>[email protected]</foaf:mbox>
</foaf:Organization>
</dct:publisher>
<dcat:distribution>
<dcat:Distribution>
<dct:identifier></dct:identifier>
<dct:title>No size en format CSV</dct:title>
<dct:description></dct:description>
<dcat:downloadURL rdf:datatype="http://www.w3.org/2001/XMLSchema#anyURI">http://madrid.gobierto.test/api/v1/data/datasets/no-size/download.csv</dcat:downloadURL>
<dcat:mediaType>application/csv</dcat:mediaType>
</dcat:Distribution>
</dcat:distribution>
</dcat:Dataset>
</dcat:dataset>
</dcat:Catalog>
</rdf:RDF>
21 changes: 10 additions & 11 deletions test/fixtures/gobierto_data/datasets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

Expand All @@ -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 %>

Expand All @@ -23,25 +23,24 @@ 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:
site: santander
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:
site: madrid
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"] %>

4 changes: 2 additions & 2 deletions test/fixtures/sites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
2 changes: 1 addition & 1 deletion test/presenters/gobierto_data/dataset_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a452f6d

Please sign in to comment.