From 4805204e238febdd8fc2d4789cc491b7eeb4bdd1 Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 4 Jun 2024 12:32:32 +0200 Subject: [PATCH 01/10] added outputformat to metadata and data endpoint. --- PxWeb/Mappers/DatasetMapper.cs | 2 +- PxWeb/Mappers/FolderResponseMapper.cs | 2 +- PxWeb/Mappers/ILinkCreator.cs | 2 +- PxWeb/Mappers/LinkCreator.cs | 49 +++++++++++++++----- PxWeb/Mappers/TableMetadataResponseMapper.cs | 2 +- PxWeb/Mappers/TableResponseMapper.cs | 2 +- PxWeb/Mappers/TablesResponseMapper.cs | 2 +- 7 files changed, 43 insertions(+), 18 deletions(-) diff --git a/PxWeb/Mappers/DatasetMapper.cs b/PxWeb/Mappers/DatasetMapper.cs index 02c2a966..971f35f1 100644 --- a/PxWeb/Mappers/DatasetMapper.cs +++ b/PxWeb/Mappers/DatasetMapper.cs @@ -134,7 +134,7 @@ public Dataset Map(PXModel model, string id, string language) AddTableNotes(model, dataset); List linksOnRoot = new List(); - linksOnRoot.Add(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.self, id.ToUpper(), language, true)); + linksOnRoot.AddRange(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.self, id.ToUpper(), language, true)); linksOnRoot.Add(_linkCreator.GetTableDataLink(LinkCreator.LinkRelationEnum.data, id.ToUpper(), language, true)); //"type": "application/json" diff --git a/PxWeb/Mappers/FolderResponseMapper.cs b/PxWeb/Mappers/FolderResponseMapper.cs index 2d58cd9d..b1e2e878 100644 --- a/PxWeb/Mappers/FolderResponseMapper.cs +++ b/PxWeb/Mappers/FolderResponseMapper.cs @@ -117,7 +117,7 @@ private Table MapTable(TableLink child) table.Links.Add(_linkCreator.GetTableLink(LinkCreator.LinkRelationEnum.self, tableId, _language, true)); // Links to metadata - table.Links.Add(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, tableId, _language, true)); + table.Links.AddRange(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, tableId, _language, true)); // Links to data table.Links.Add(_linkCreator.GetTableDataLink(LinkCreator.LinkRelationEnum.data, tableId, _language, true)); diff --git a/PxWeb/Mappers/ILinkCreator.cs b/PxWeb/Mappers/ILinkCreator.cs index 7d7341ce..9d2f45b3 100644 --- a/PxWeb/Mappers/ILinkCreator.cs +++ b/PxWeb/Mappers/ILinkCreator.cs @@ -8,7 +8,7 @@ public interface ILinkCreator { Link GetTablesLink(LinkRelationEnum relation, string language, string? query, int pagesize, int pageNumber, bool showLangParam = true); Link GetTableLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); - Link GetTableMetadataJsonLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); + List GetTableMetadataJsonLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); Link GetTableDataLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); Link GetCodelistLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); Link GetFolderLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true); diff --git a/PxWeb/Mappers/LinkCreator.cs b/PxWeb/Mappers/LinkCreator.cs index 251ce2ec..bdd98682 100644 --- a/PxWeb/Mappers/LinkCreator.cs +++ b/PxWeb/Mappers/LinkCreator.cs @@ -20,10 +20,14 @@ public enum LinkRelationEnum } private readonly string _urlBase; + private readonly string _defaultDataFormat; + private readonly List _metaFormats = new List { "json-px", "json-stat2" }; + //Could not get the strings cleanly from MetadataOutputFormatType. Anybody? public LinkCreator(IOptions configOptions) { _urlBase = configOptions.Value.BaseURL; + _defaultDataFormat = configOptions.Value.DefaultOutputFormat; } public Link GetTablesLink(LinkRelationEnum relation, string language, string? query, int pagesize, int pageNumber, bool showLangParam = true) { @@ -40,19 +44,26 @@ public Link GetTableLink(LinkRelationEnum relation, string id, string language, var link = new Link(); link.Rel = relation.ToString(); link.Hreflang = language; - link.Href = CreateURL($"tables/{id}", language, showLangParam); + link.Href = CreateURL($"tables/{id}", language, showLangParam, null); return link; } - public Link GetTableMetadataJsonLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true) + public List GetTableMetadataJsonLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true) { - var link = new Link(); - link.Rel = relation.ToString(); - link.Hreflang = language; - link.Href = CreateURL($"tables/{id}/metadata", language, showLangParam); + List links = new List(); - return link; + foreach (string outFormat in _metaFormats) + { + var link = new Link(); + link.Rel = relation.ToString(); + link.Hreflang = language; + + link.Href = CreateURL($"tables/{id}/metadata", language, showLangParam, outFormat); + links.Add(link); + } + + return links; } public Link GetTableDataLink(LinkRelationEnum relation, string id, string language, bool showLangParam = true) @@ -60,7 +71,7 @@ public Link GetTableDataLink(LinkRelationEnum relation, string id, string langua var link = new Link(); link.Rel = relation.ToString(); link.Hreflang = language; - link.Href = CreateURL($"tables/{id}/data", language, showLangParam); + link.Href = CreateURL($"tables/{id}/data", language, showLangParam, _defaultDataFormat); return link; } @@ -70,7 +81,7 @@ public Link GetCodelistLink(LinkRelationEnum relation, string id, string languag var link = new Link(); link.Rel = relation.ToString(); link.Hreflang = language; - link.Href = CreateURL($"codeLists/{id}", language, showLangParam); + link.Href = CreateURL($"codeLists/{id}", language, showLangParam, null); return link; } @@ -80,7 +91,7 @@ public Link GetDefaultSelectionLink(LinkRelationEnum relation, string id, string var link = new Link(); link.Rel = relation.ToString(); link.Hreflang = language; - link.Href = CreateURL($"tables/{id}/defaultselection", language, showLangParam); + link.Href = CreateURL($"tables/{id}/defaultselection", language, showLangParam, null); return link; } @@ -90,11 +101,11 @@ public Link GetFolderLink(LinkRelationEnum relation, string id, string language, var link = new Link(); link.Rel = relation.ToString(); link.Hreflang = language; - link.Href = CreateURL($"navigation/{id}", language, showLangParam); + link.Href = CreateURL($"navigation/{id}", language, showLangParam, null); return link; } - private string CreateURL(string endpointUrl, string language, bool showLangParam) + private string CreateURL(string endpointUrl, string language, bool showLangParam, string? outputFormat) { StringBuilder sb = new StringBuilder(); @@ -107,6 +118,20 @@ private string CreateURL(string endpointUrl, string language, bool showLangParam sb.Append(language); } + if (!string.IsNullOrEmpty(outputFormat)) + { + if (showLangParam) + { + sb.Append('&'); + } + else + { + sb.Append('?'); + } + sb.Append("outputFormat="); + sb.Append(outputFormat); + } + return sb.ToString(); } private string CreatePageURL(string endpointUrl, string language, bool showLangParam, string? query, int pagesize, int pageNumber) diff --git a/PxWeb/Mappers/TableMetadataResponseMapper.cs b/PxWeb/Mappers/TableMetadataResponseMapper.cs index e64a0a0f..d5f92c00 100644 --- a/PxWeb/Mappers/TableMetadataResponseMapper.cs +++ b/PxWeb/Mappers/TableMetadataResponseMapper.cs @@ -65,7 +65,7 @@ public TableMetadataResponse Map(PXModel model, string id, string language) tm.Links = new System.Collections.Generic.List(); // Links to metadata - tm.Links.Add(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.self, id.ToUpper(), language, true)); + tm.Links.AddRange(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.self, id.ToUpper(), language, true)); // Links to data tm.Links.Add(_linkCreator.GetTableDataLink(LinkCreator.LinkRelationEnum.data, id.ToUpper(), language, true)); diff --git a/PxWeb/Mappers/TableResponseMapper.cs b/PxWeb/Mappers/TableResponseMapper.cs index aef5dd06..c84540f8 100644 --- a/PxWeb/Mappers/TableResponseMapper.cs +++ b/PxWeb/Mappers/TableResponseMapper.cs @@ -28,7 +28,7 @@ public TableResponse Map(SearchResult searchResult, string lang) linkList.Add(_linkCreator.GetTableLink(LinkCreator.LinkRelationEnum.self, searchResult.Id.ToUpper(), lang, true)); // Links to metadata - linkList.Add(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, searchResult.Id.ToUpper(), lang, true)); + linkList.AddRange(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, searchResult.Id.ToUpper(), lang, true)); // Links to data linkList.Add(_linkCreator.GetTableDataLink(LinkCreator.LinkRelationEnum.data, searchResult.Id.ToUpper(), lang, true)); diff --git a/PxWeb/Mappers/TablesResponseMapper.cs b/PxWeb/Mappers/TablesResponseMapper.cs index 6372269d..47611e91 100644 --- a/PxWeb/Mappers/TablesResponseMapper.cs +++ b/PxWeb/Mappers/TablesResponseMapper.cs @@ -68,7 +68,7 @@ public TablesResponse Map(SearchResultContainer searchResultContainer, string la linkList.Add(_linkCreator.GetTableLink(LinkCreator.LinkRelationEnum.self, item.Id.ToUpper(), lang, true)); // Links to metadata - linkList.Add(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, item.Id.ToUpper(), lang, true)); + linkList.AddRange(_linkCreator.GetTableMetadataJsonLink(LinkCreator.LinkRelationEnum.metadata, item.Id.ToUpper(), lang, true)); // Links to data linkList.Add(_linkCreator.GetTableDataLink(LinkCreator.LinkRelationEnum.data, item.Id.ToUpper(), lang, true)); From 0355b73836668087e3e4c751c55e32e12c790d4f Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 4 Jun 2024 14:22:03 +0200 Subject: [PATCH 02/10] Temporary "post-it" file :-) --- temp_metadataformat_diff.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 temp_metadataformat_diff.md diff --git a/temp_metadataformat_diff.md b/temp_metadataformat_diff.md new file mode 100644 index 00000000..9e976f5f --- /dev/null +++ b/temp_metadataformat_diff.md @@ -0,0 +1,20 @@ +#from 1 run of metadata for tab004 + +| Item | px-json |jsonstat2|| +| ------ | ------ |------ |-----| +|"show": "value"/"code"| no | on all variables|| +|"elimination":| not on time and content| on all variables|| +|"infofile": "ENVIRONMENT"| no | yes || +|"contents":"Total air emissions"| no | yes || +|"descriptiondefault"| no | yes || +|stub and heading| no | yes || +|matrix| no | yes || +|license|yes|no|| +|"timeUnit": "Annual"|yes|no|| +|"firstPeriod": "1990"|yes|no|| +|"lastPeriod": "2017"|yes|no|| +|"adjustment": "None"|yes|no|| +|"measuringType": "Stock"|yes|no|| +|"priceType": "Undefined"|yes|no|| + + \ No newline at end of file From 4e74880a5707d4343a711da5e4c22716a45545f3 Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 4 Jun 2024 15:56:12 +0200 Subject: [PATCH 03/10] Big Tests for TableController now using Microsoft.AspNetCore.Mvc.Testing --- PxWeb.sln | 6 + .../TableController/TableApiControllerTest.cs | 191 ----------- .../TableController/test_appsettings.json | 105 ------ .../ExpectedJson/ListAllTables.json | 212 ++++++++++++ .../ExpectedJson/MetadataById_tab004.json | 315 ++++++++++++++++++ .../ExpectedJson/TableById_tab004.json | 40 +++ PxWebApi_Mvc.Tests/GlobalUsings.cs | 1 + PxWebApi_Mvc.Tests/PxWebApi_Mvc.Tests.csproj | 28 ++ PxWebApi_Mvc.Tests/TableApiControllerTest.cs | 105 ++++++ PxWebApi_Mvc.Tests/Util.cs | 24 ++ 10 files changed, 731 insertions(+), 296 deletions(-) delete mode 100644 PxWebApi.BigTests/TableController/TableApiControllerTest.cs delete mode 100644 PxWebApi.BigTests/TableController/test_appsettings.json create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/ListAllTables.json create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004.json create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/TableById_tab004.json create mode 100644 PxWebApi_Mvc.Tests/GlobalUsings.cs create mode 100644 PxWebApi_Mvc.Tests/PxWebApi_Mvc.Tests.csproj create mode 100644 PxWebApi_Mvc.Tests/TableApiControllerTest.cs create mode 100644 PxWebApi_Mvc.Tests/Util.cs diff --git a/PxWeb.sln b/PxWeb.sln index 3efb359d..6d4e7c31 100644 --- a/PxWeb.sln +++ b/PxWeb.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Px.Search.Lucene", "Px.Sear EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PxWebApi.BigTests", "PxWebApi.BigTests\PxWebApi.BigTests.csproj", "{E52A7944-28C6-4004-9282-1AE8BC539F8F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PxWebApi_Mvc.Tests", "PxWebApi_Mvc.Tests\PxWebApi_Mvc.Tests.csproj", "{DE680238-98E9-4FCE-9D55-9B849F893238}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {E52A7944-28C6-4004-9282-1AE8BC539F8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {E52A7944-28C6-4004-9282-1AE8BC539F8F}.Release|Any CPU.ActiveCfg = Release|Any CPU {E52A7944-28C6-4004-9282-1AE8BC539F8F}.Release|Any CPU.Build.0 = Release|Any CPU + {DE680238-98E9-4FCE-9D55-9B849F893238}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE680238-98E9-4FCE-9D55-9B849F893238}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE680238-98E9-4FCE-9D55-9B849F893238}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE680238-98E9-4FCE-9D55-9B849F893238}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PxWebApi.BigTests/TableController/TableApiControllerTest.cs b/PxWebApi.BigTests/TableController/TableApiControllerTest.cs deleted file mode 100644 index 3b74be38..00000000 --- a/PxWebApi.BigTests/TableController/TableApiControllerTest.cs +++ /dev/null @@ -1,191 +0,0 @@ -using System; - -using PxWeb.Helper.Api2; - -namespace PxWebApi.BigTests.TableController -{ - [TestClass] - public class TableApiControllerTest - { - private static readonly Mock hostingEnvironmentMock = new Mock(); - - [ClassInitialize] - public static void ClassSetup(TestContext context) - { - var wwwrootPath = Util.GetFullPathToFile(@"PxWeb/wwwroot/"); - hostingEnvironmentMock - .Setup(m => m.RootPath) - .Returns(wwwrootPath); - - } - - - [TestMethod] - [Description("Same input-file gives same output string.")] - public void ListAllTables() - { - PxWeb.Controllers.Api2.TableApiController tac = GetController(); - - var result = tac.ListAllTables(null, null, null, null, null, null); - - var aa = result.ToJSON(false); - var bb = "{\"Value\":{\"language\":\"en\",\"tables\":[{\"updated\":\"2023-05-25T13:42:00\",\"firstPeriod\":\"1990\",\"lastPeriod\":\"2017\",\"category\":\"public\",\"variableNames\":[\"sector\",\"greenhouse gas\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB004\",\"label\":\"Total air emissions by sector, greenhouse gas, contents and year\",\"description\":\"\"},{\"updated\":\"2023-08-31T13:42:00\",\"firstPeriod\":\"2010\",\"lastPeriod\":\"2015\",\"category\":\"public\",\"variableNames\":[\"region\",\"land use\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB005?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB005/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB005/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB005\",\"label\":\"Land use in Sweden, hectares by region, land use, contents and year\",\"description\":\"\"},{\"updated\":\"2023-05-25T14:13:00\",\"firstPeriod\":\"2010\",\"lastPeriod\":\"2016\",\"category\":\"public\",\"variableNames\":[\"treatment category\",\"waste category\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB003?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB003/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB003/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB003\",\"label\":\"Disposal facilities by treatment category, waste category, contents and year\",\"description\":\"\"},{\"updated\":\"2023-05-25T13:42:00\",\"firstPeriod\":\"1981\",\"lastPeriod\":\"2001\",\"category\":\"public\",\"variableNames\":[\"region\",\"sex\",\"age\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB001?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB001/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB001/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB001\",\"label\":\"Population by region, sex, age, contents and year\",\"description\":\"\"},{\"updated\":\"2023-05-25T13:42:00\",\"firstPeriod\":\"1991\",\"lastPeriod\":\"2001\",\"category\":\"public\",\"variableNames\":[\"region\",\"sex\",\"age\",\"citizenship\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB002?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB002/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB002/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB002\",\"label\":\"Population by region, sex, age, citizenship, contents and year\",\"description\":\"\"}],\"page\":{\"pageNumber\":1,\"pageSize\":20,\"totalElements\":5,\"totalPages\":1,\"links\":[]},\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/?lang=en&pagesize=20&pageNumber=1\"}]},\"Formatters\":[],\"ContentTypes\":[],\"StatusCode\":200}"; - - - Assert.AreEqual(bb, aa); - } - - [TestMethod] - [Description("Same input-file gives same output string.")] - public void GetTableById() - { - PxWeb.Controllers.Api2.TableApiController tac = GetController(); - - var result = tac.GetTableById("TAB004", null); - - var aa = result.ToJSON(false); - var bb = "{\"Value\":{\"language\":\"en\",\"updated\":\"2023-05-25T13:42:00\",\"firstPeriod\":\"1990\",\"lastPeriod\":\"2017\",\"category\":\"public\",\"variableNames\":[\"sector\",\"greenhouse gas\",\"contents\",\"year\"],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004?lang=en\"},{\"rel\":\"metadata\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en\"}],\"type\":\"Table\",\"id\":\"TAB004\",\"label\":\"Total air emissions by sector, greenhouse gas, contents and year\",\"description\":\"\",\"sortCode\":\"TAB004\"},\"Formatters\":[],\"ContentTypes\":[],\"StatusCode\":200}"; - - - Assert.AreEqual(bb, aa); - } - - - - [TestMethod] - [Description("Same input-file gives same output string.")] - public void GetMetadataById() - { - PxWeb.Controllers.Api2.TableApiController tac = GetController(); - - var result = tac.GetMetadataById("TAB004", null, null); - - var actual = result.ToJSON(false); - var expected = "{\"Value\":{\"language\":\"en\",\"id\":\"TAB004\",\"label\":\"Total air emissions by sector, greenhouse gas, contents and year\",\"aggregationAllowed\":false,\"officialStatistics\":false,\"subjectCode\":\"EN\",\"subjectLabel\":\"Environment\",\"source\":\"Statistics Sweden\",\"license\":\"https://creativecommons.org/share-your-work/public-domain/cc0/\",\"updated\":\"2023-05-25T13:42:00Z\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"},{\"mandatory\":false,\"text\":\".. = Data not available\"}],\"contacts\":[{\"raw\":\"Test Testsson, SCB#Tel: 08-111 222 33#Fax: 08-222 333 44#E-mail: test.testsson@scb.se\"},{\"raw\":\"Test2 Testsson2, SCB#Tel: 08-333 444 55#Fax: 08-444 555 66#E-mail: test2.testsson2@scb.se\"}],\"variables\":[{\"elimination\":false,\"values\":[{\"code\":\"0.1\",\"label\":\"NATIONAL TOTAL (excluding LULUCF, excluding international transports)\"},{\"code\":\"0.2\",\"label\":\"NATIONAL TOTAL (excluding LULUCF, including international transports)\"},{\"code\":\"0.3\",\"label\":\"NATIONAL TOTAL (including LULUCF, excluding international transports)\"},{\"code\":\"0.4\",\"label\":\"NATIONAL TOTAL (including LULUCF, including international transports)\"},{\"code\":\"1.0\",\"label\":\"OFF-ROAD VEHICLES AND OTHER MACHINERY, TOTAL\"},{\"code\":\"10.0\",\"label\":\"LAND-USE, LAND-USE CHANGE AND FORESTRY (LULUCF), TOTAL\"},{\"code\":\"2.0\",\"label\":\"WASTE, TOTAL\"},{\"code\":\"3.0\",\"label\":\"ELECTRICITY AND DISTRICT HEATING, TOTAL\"},{\"code\":\"4.0\",\"label\":\"INDUSTRY, TOTAL\"},{\"code\":\"5.0\",\"label\":\"INTERNATIONAL TRANSPORT, TOTAL\"},{\"code\":\"6.0\",\"label\":\"AGRICULTURE, TOTAL\"},{\"code\":\"7.0\",\"label\":\"SOLVENT USE AND OTHER PRODUCT USE, TOTAL\"},{\"code\":\"8.0\",\"label\":\"DOMESTIC TRANSPORT, TOTAL\"},{\"code\":\"9.0\",\"label\":\"HEATING OF HOUSES AND PREMISES, TOTAL\"}],\"id\":\"SECTOR\",\"label\":\"sector\",\"type\":\"RegularVariable\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"}]},{\"elimination\":false,\"values\":[{\"code\":\"CH4\",\"label\":\"Methane (CH4) (t)\"},{\"code\":\"CH4_CO2-ekv.\",\"label\":\"Methane (CH4) (kt CO2-eqv.)\"},{\"code\":\"CO2\",\"label\":\"Carbon Dioxide (CO2) (kt)\"},{\"code\":\"CO2-BIO\",\"label\":\"Biogenic carbon dioxide (CO2) from fuels (kt)\"},{\"code\":\"CO2-ekv.\",\"label\":\"Total Greenhouse Gases (kt CO2-eqv.)\"},{\"code\":\"HFC\",\"label\":\"Hydrofluorocarbons (HFCs) (kt CO2-eqv.)\"},{\"code\":\"N2O\",\"label\":\"Nitrous Oxide (N2O) (t)\"},{\"code\":\"N2O_CO2-ekv.\",\"label\":\"Nitrous Oxide (N2O) (kt CO2-eqv.)\"},{\"code\":\"PFC\",\"label\":\"Perfluorocarbons (PFCs) (kt CO2-eqv.)\"},{\"code\":\"SF6\",\"label\":\"Sulphur Hexafluoride (SF6) (kg)\"},{\"code\":\"SF6_CO2-ekv.\",\"label\":\"Sulphur Hexafluoride (SF6) (kt CO2-eqv.)\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"}]}],\"id\":\"GREENHOUSEGAS\",\"label\":\"greenhouse gas\",\"type\":\"RegularVariable\"},{\"values\":[{\"adjustment\":\"None\",\"measuringType\":\"Stock\",\"preferedNumberOfDecimals\":0,\"priceType\":\"Undefined\",\"unit\":\"Device varies with the subject\",\"code\":\"Emission\",\"label\":\"Substance\"}],\"id\":\"ContentsCode\",\"label\":\"contents\",\"type\":\"ContentsVariable\"},{\"timeUnit\":\"Annual\",\"firstPeriod\":\"1990\",\"lastPeriod\":\"2017\",\"values\":[{\"code\":\"1990\",\"label\":\"1990\"},{\"code\":\"1991\",\"label\":\"1991\"},{\"code\":\"1992\",\"label\":\"1992\"},{\"code\":\"1993\",\"label\":\"1993\"},{\"code\":\"1994\",\"label\":\"1994\"},{\"code\":\"1995\",\"label\":\"1995\"},{\"code\":\"1996\",\"label\":\"1996\"},{\"code\":\"1997\",\"label\":\"1997\"},{\"code\":\"1998\",\"label\":\"1998\"},{\"code\":\"1999\",\"label\":\"1999\"},{\"code\":\"2000\",\"label\":\"2000\"},{\"code\":\"2001\",\"label\":\"2001\"},{\"code\":\"2002\",\"label\":\"2002\"},{\"code\":\"2003\",\"label\":\"2003\"},{\"code\":\"2004\",\"label\":\"2004\"},{\"code\":\"2005\",\"label\":\"2005\"},{\"code\":\"2006\",\"label\":\"2006\"},{\"code\":\"2007\",\"label\":\"2007\"},{\"code\":\"2008\",\"label\":\"2008\"},{\"code\":\"2009\",\"label\":\"2009\"},{\"code\":\"2010\",\"label\":\"2010\"},{\"code\":\"2011\",\"label\":\"2011\"},{\"code\":\"2012\",\"label\":\"2012\"},{\"code\":\"2013\",\"label\":\"2013\"},{\"code\":\"2014\",\"label\":\"2014\"},{\"code\":\"2015\",\"label\":\"2015\"},{\"code\":\"2016\",\"label\":\"2016\"},{\"code\":\"2017\",\"label\":\"2017\"}],\"id\":\"TIME\",\"label\":\"year\",\"type\":\"TimeVariable\"}],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en\"}]},\"Formatters\":[],\"ContentTypes\":[]}"; - - - - Assert.AreEqual(expected.Substring(0, 5), actual.Substring(0, 5), "Diff in first 5."); - Assert.AreEqual(expected.Length, actual.Length, "Not correct length."); - - - //updated causes problems. When expected and actual is made in different places and input is in localtime. - int posOfUpdatedString = expected.IndexOf("2023-05-25T13:42:00"); - actual = actual.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + actual.Substring(posOfUpdatedString + 13); - expected = expected.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + expected.Substring(posOfUpdatedString + 13); - - for (int i = 0; i < actual.Length; i += 25) - { - int lengthToCompare = Math.Min(50, actual.Length - i); - Assert.AreEqual(expected.Substring(i, lengthToCompare), actual.Substring(i, lengthToCompare)); - } - - } - - - /* - [TestMethod] - [Description("Same input-file gives same output string.")] - public void GetTableData() - { - PxWeb.Controllers.Api2.TableApiController tac = GetController(); - - Mock HttpResponseMock = new Mock(); - Mock HttpContextMock = new Mock(); - HttpContextMock - .Setup(m => m.Response) - .Returns(HttpResponseMock.Object); - - Mock ControllerContextMock = new Mock(); - ControllerContextMock - .Setup(m => m.HttpContext) - .Returns(HttpContextMock.Object); - - tac.ControllerContext = ControllerContextMock.Object; - //This stops with: - //Test method PxWebApi.BigTests.TableController.TableApiControllerTest.GetTableData threw exception: - //System.NotSupportedException: Unsupported expression: m => m.HttpContext - //Non - overridable members(here: ActionContext.get_HttpContext) may not be used in setup / verification expressions. - - // We mock what we do not understand :-) - - - var result = tac.GetTableData("TAB004", "en", new Dictionary>(), new Dictionary(), new Dictionary(), null); - - var aa = result.ToJSON(false); - var bb = "{\"Value\":{\"language\":\"en\",\"id\":\"TAB004\",\"label\":\"Total air emissions by sector, greenhouse gas, contents and year\",\"aggregationAllowed\":false,\"officalStatistics\":false,\"subjectCode\":\"EN\",\"subjectLabel\":\"Environment\",\"source\":\"Statistics Sweden\",\"licence\":\"https://creativecommons.org/share-your-work/public-domain/cc0/\",\"updated\":\"2023-05-25T13:42:00Z\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"},{\"mandatory\":false,\"text\":\".. = Data not available\"}],\"contacts\":[{\"raw\":\"Test Testsson, SCB#Tel: 08-111 222 33#Fax: 08-222 333 44#E-mail: test.testsson@scb.se\"},{\"raw\":\"Test2 Testsson2, SCB#Tel: 08-333 444 55#Fax: 08-444 555 66#E-mail: test2.testsson2@scb.se\"}],\"variables\":[{\"elimination\":false,\"values\":[{\"code\":\"0.1\",\"label\":\"NATIONAL TOTAL (excluding LULUCF, excluding international transports)\"},{\"code\":\"0.2\",\"label\":\"NATIONAL TOTAL (excluding LULUCF, including international transports)\"},{\"code\":\"0.3\",\"label\":\"NATIONAL TOTAL (including LULUCF, excluding international transports)\"},{\"code\":\"0.4\",\"label\":\"NATIONAL TOTAL (including LULUCF, including international transports)\"},{\"code\":\"1.0\",\"label\":\"OFF-ROAD VEHICLES AND OTHER MACHINERY, TOTAL\"},{\"code\":\"10.0\",\"label\":\"LAND-USE, LAND-USE CHANGE AND FORESTRY (LULUCF), TOTAL\"},{\"code\":\"2.0\",\"label\":\"WASTE, TOTAL\"},{\"code\":\"3.0\",\"label\":\"ELECTRICITY AND DISTRICT HEATING, TOTAL\"},{\"code\":\"4.0\",\"label\":\"INDUSTRY, TOTAL\"},{\"code\":\"5.0\",\"label\":\"INTERNATIONAL TRANSPORT, TOTAL\"},{\"code\":\"6.0\",\"label\":\"AGRICULTURE, TOTAL\"},{\"code\":\"7.0\",\"label\":\"SOLVENT USE AND OTHER PRODUCT USE, TOTAL\"},{\"code\":\"8.0\",\"label\":\"DOMESTIC TRANSPORT, TOTAL\"},{\"code\":\"9.0\",\"label\":\"HEATING OF HOUSES AND PREMISES, TOTAL\"}],\"id\":\"SECTOR\",\"label\":\"sector\",\"type\":\"RegularVariable\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"}]},{\"elimination\":false,\"values\":[{\"code\":\"CH4\",\"label\":\"Methane (CH4) (t)\"},{\"code\":\"CH4_CO2-ekv.\",\"label\":\"Methane (CH4) (kt CO2-eqv.)\"},{\"code\":\"CO2\",\"label\":\"Carbon Dioxide (CO2) (kt)\"},{\"code\":\"CO2-BIO\",\"label\":\"Biogenic carbon dioxide (CO2) from fuels (kt)\"},{\"code\":\"CO2-ekv.\",\"label\":\"Total Greenhouse Gases (kt CO2-eqv.)\"},{\"code\":\"HFC\",\"label\":\"Hydrofluorocarbons (HFCs) (kt CO2-eqv.)\"},{\"code\":\"N2O\",\"label\":\"Nitrous Oxide (N2O) (t)\"},{\"code\":\"N2O_CO2-ekv.\",\"label\":\"Nitrous Oxide (N2O) (kt CO2-eqv.)\"},{\"code\":\"PFC\",\"label\":\"Perfluorocarbons (PFCs) (kt CO2-eqv.)\"},{\"code\":\"SF6\",\"label\":\"Sulphur Hexafluoride (SF6) (kg)\"},{\"code\":\"SF6_CO2-ekv.\",\"label\":\"Sulphur Hexafluoride (SF6) (kt CO2-eqv.)\",\"notes\":[{\"mandatory\":false,\"text\":\"Footnote text\"}]}],\"id\":\"GREENHOUSEGAS\",\"label\":\"greenhouse gas\",\"type\":\"RegularVariable\"},{\"values\":[{\"adjustment\":\"None\",\"measuringType\":\"Stock\",\"preferedNumberOfDecimals\":0,\"priceType\":\"Undefined\",\"unit\":\"Device varies with the subject\",\"code\":\"Emission\",\"label\":\"Substance\"}],\"id\":\"ContentsCode\",\"label\":\"contents\",\"type\":\"ContentsVariable\"},{\"timeUnit\":\"Annual\",\"firstPeriod\":\"1990\",\"lastPeriod\":\"2017\",\"values\":[{\"code\":\"1990\",\"label\":\"1990\"},{\"code\":\"1991\",\"label\":\"1991\"},{\"code\":\"1992\",\"label\":\"1992\"},{\"code\":\"1993\",\"label\":\"1993\"},{\"code\":\"1994\",\"label\":\"1994\"},{\"code\":\"1995\",\"label\":\"1995\"},{\"code\":\"1996\",\"label\":\"1996\"},{\"code\":\"1997\",\"label\":\"1997\"},{\"code\":\"1998\",\"label\":\"1998\"},{\"code\":\"1999\",\"label\":\"1999\"},{\"code\":\"2000\",\"label\":\"2000\"},{\"code\":\"2001\",\"label\":\"2001\"},{\"code\":\"2002\",\"label\":\"2002\"},{\"code\":\"2003\",\"label\":\"2003\"},{\"code\":\"2004\",\"label\":\"2004\"},{\"code\":\"2005\",\"label\":\"2005\"},{\"code\":\"2006\",\"label\":\"2006\"},{\"code\":\"2007\",\"label\":\"2007\"},{\"code\":\"2008\",\"label\":\"2008\"},{\"code\":\"2009\",\"label\":\"2009\"},{\"code\":\"2010\",\"label\":\"2010\"},{\"code\":\"2011\",\"label\":\"2011\"},{\"code\":\"2012\",\"label\":\"2012\"},{\"code\":\"2013\",\"label\":\"2013\"},{\"code\":\"2014\",\"label\":\"2014\"},{\"code\":\"2015\",\"label\":\"2015\"},{\"code\":\"2016\",\"label\":\"2016\"},{\"code\":\"2017\",\"label\":\"2017\"}],\"id\":\"TIME\",\"label\":\"year\",\"type\":\"TimeVariable\"}],\"links\":[{\"rel\":\"self\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en\"},{\"rel\":\"data\",\"hreflang\":\"en\",\"href\":\"https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en\"}]},\"Formatters\":[],\"ContentTypes\":[]}"; - - - Assert.AreEqual(bb, aa); - } - */ - - - private PxWeb.Controllers.Api2.TableApiController GetController() - { - IConfigurationRoot configuration = GetConfigFile(); - - IOptions pxApiConfigurationOptions = Util.GetIOptions(configuration, "PxApiConfiguration"); - IPxApiConfigurationService pxApiConfigurationService = new PxApiConfigurationService(pxApiConfigurationOptions); - - Mock> loggerMock = new Mock>(); - - //GetIOptions - IOptions pxFileConfigurationOptions = Util.GetIOptions(configuration, "DataSource:PX"); - IPxFileConfigurationService pxFileConfigurationService = new PxFileConfigurationService(pxFileConfigurationOptions); - - - var mockIPxCache = new Mock(); - - - var loggerMockItemSelectorResolverPxFactory = new Mock>(); - IItemSelectionResolverFactory itemSelectorResolverPxFactory = new ItemSelectorResolverPxFactory(pxFileConfigurationService, hostingEnvironmentMock.Object, loggerMockItemSelectorResolverPxFactory.Object); - IItemSelectionResolver itemSelectionResolver = new ItemSelectionResolverPxFile(mockIPxCache.Object, itemSelectorResolverPxFactory, pxApiConfigurationService); - - var loggerMockTablePathResolverPxFile = new Mock>(); - ITablePathResolver tablePathResolver = new TablePathResolverPxFile(mockIPxCache.Object, hostingEnvironmentMock.Object, pxApiConfigurationService, loggerMockTablePathResolverPxFile.Object); - ICodelistMapper codelistMapper = new CodelistMapper(); - - IDataSource iDataSource = new PxFileDataSource(pxFileConfigurationService, itemSelectionResolver, tablePathResolver, - hostingEnvironmentMock.Object, codelistMapper); - - ILanguageHelper languageHelper = new LanguageHelper(pxApiConfigurationService); - - ILinkCreator linkCreator = new LinkCreator(pxApiConfigurationOptions); - ITableMetadataResponseMapper responseMapper = new TableMetadataResponseMapper(linkCreator, pxApiConfigurationOptions); - IDatasetMapper datasetMapper = new DatasetMapper(linkCreator, pxApiConfigurationOptions); - - IOptions luceneConfigurationOptions = Util.GetIOptions(configuration, "LuceneConfiguration"); - ILuceneConfigurationService luceneConfigurationService = new LuceneConfigurationService(luceneConfigurationOptions, hostingEnvironmentMock.Object); - - ISearchBackend backend = new LuceneBackend(luceneConfigurationService); - - ITablesResponseMapper tablesResponseMapper = new TablesResponseMapper(linkCreator, pxApiConfigurationOptions); - ITableResponseMapper tableResponseMapper = new TableResponseMapper(linkCreator, pxApiConfigurationOptions); - ICodelistResponseMapper codelistResponseMapper = new CodelistResponseMapper(linkCreator, pxApiConfigurationOptions); - ISerializeManager serializeManager = new SerializeManager(); - ISelectionHandler selectionHandler = new SelectionHandler(pxApiConfigurationService); - - ISelectionResponseMapper selectionResponseMapper = new SelectionResponseMapper(linkCreator); - - return new PxWeb.Controllers.Api2.TableApiController(iDataSource, languageHelper, responseMapper, datasetMapper, - backend, pxApiConfigurationOptions, tablesResponseMapper, tableResponseMapper, - codelistResponseMapper, selectionResponseMapper, serializeManager, selectionHandler); - - } - - - - private static IConfigurationRoot GetConfigFile() - { - string conf_dir_String = Util.GetFullPathToFile(@"PxWebApi.BigTests/TableController/"); - - var builder = new ConfigurationBuilder() - .SetBasePath(conf_dir_String) - .AddJsonFile("test_appsettings.json", optional: false, reloadOnChange: true); - - IConfigurationRoot configuration = builder.Build(); - return configuration; - } - } -} diff --git a/PxWebApi.BigTests/TableController/test_appsettings.json b/PxWebApi.BigTests/TableController/test_appsettings.json deleted file mode 100644 index 93b0574c..00000000 --- a/PxWebApi.BigTests/TableController/test_appsettings.json +++ /dev/null @@ -1,105 +0,0 @@ -{ - "DataSource": { - "DataSourceType": "PX", - "PX": { - "StrictAggregations": "true" - }, - "CNMM": { - "DatabaseID": "ssd" - } - }, - "PxApiConfiguration": { - "AAAAAAA":"BBBBBBBBB", - "Languages": [ - { - "Id": "en", - "Label": "English" - }, - { - "Id": "sv", - "Label": "Svenska" - } - ], - "DefaultLanguage": "en", - "MaxDataCells": 10000, - "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", - "SourceReferences": [ - { - "Language": "en", - "Text": "Source: AAAAAAAAA Statistics Sweden" - }, - { - "Language": "sv", - "Text": "Källa: AAAAAAAAAA SCB" - } - ], - "Cors": { - "Enabled": true, - "Origins": "*" - }, - "CacheTime": 86400, - "SearchEngine": "Lucene", - "PageSize": 20, - "BaseURL": "https://www.pxapi.com/api/v2/", - "OutputFormats": [ - "xlsx", - "xlsx_doublecolumn", - "csv", - "csv_tab", - "csv_tabhead", - "csv_comma", - "csv_commahead", - "csv_space", - "csv_spacehead", - "csv_semicolon", - "csv_semicolonhead", - "csv2", - "csv3", - "json", - "json-stat", - "json-stat2", - "parquet", - "html5_table", - "relational_table", - "px" - ], - "DefaultOutputFormat": "px" - }, - "LuceneConfiguration": { - "IndexDirectory": "Database" - }, - "AdminProtection": { - "IpWhitelist": ["127.0.0.1", "::1"], - "AdminKey": "test" - }, - "CacheMiddleware": { - "CacheTime": 10, - "BufferThreshold": 40960 - }, - "IpRateLimiting": { - "EnableEndpointRateLimiting": false, - "StackBlockedRequests": false, - "RealIpHeader": "X-Forwarded-For", - "ClientIdHeader": "", - "HttpStatusCode": 429, - "IpWhitelist": ["::1/10", "127.0.0.1"], - "EndpointWhitelist": ["get:/v2/config"], - "ClientWhitelist": [], - "GeneralRules": [ - { - "Endpoint": "*", - "Period": "10s", - "Limit": 30 - } - ] - }, - - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "config1": "" -} diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/ListAllTables.json b/PxWebApi_Mvc.Tests/ExpectedJson/ListAllTables.json new file mode 100644 index 00000000..b934d8db --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/ListAllTables.json @@ -0,0 +1,212 @@ +{ + "language": "en", + "tables": [ + { + "type": "Table", + "id": "TAB004", + "label": "Total air emissions by sector, greenhouse gas, contents and year", + "description": "", + "updated": "2023-05-25T13:42:00Z", + "firstPeriod": "1990", + "lastPeriod": "2017", + "category": "public", + "variableNames": [ + "sector", + "greenhouse gas", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en&outputFormat=px" + } + ] + }, + { + "type": "Table", + "id": "TAB005", + "label": "Land use in Sweden, hectares by region, land use, contents and year", + "description": "", + "updated": "2023-08-31T13:42:00Z", + "firstPeriod": "2010", + "lastPeriod": "2015", + "category": "public", + "variableNames": [ + "region", + "land use", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB005?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB005/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB005/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB005/data?lang=en&outputFormat=px" + } + ] + }, + { + "type": "Table", + "id": "TAB003", + "label": "Disposal facilities by treatment category, waste category, contents and year", + "description": "", + "updated": "2023-05-25T14:13:00Z", + "firstPeriod": "2010", + "lastPeriod": "2016", + "category": "public", + "variableNames": [ + "treatment category", + "waste category", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB003?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB003/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB003/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB003/data?lang=en&outputFormat=px" + } + ] + }, + { + "type": "Table", + "id": "TAB001", + "label": "Population by region, sex, age, contents and year", + "description": "", + "updated": "2023-05-25T13:42:00Z", + "firstPeriod": "1981", + "lastPeriod": "2001", + "category": "public", + "variableNames": [ + "region", + "sex", + "age", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB001?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB001/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB001/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB001/data?lang=en&outputFormat=px" + } + ] + }, + { + "type": "Table", + "id": "TAB002", + "label": "Population by region, sex, age, citizenship, contents and year", + "description": "", + "updated": "2023-05-25T13:42:00Z", + "firstPeriod": "1991", + "lastPeriod": "2001", + "category": "public", + "variableNames": [ + "region", + "sex", + "age", + "citizenship", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB002?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB002/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB002/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB002/data?lang=en&outputFormat=px" + } + ] + } + ], + "page": { + "pageNumber": 1, + "pageSize": 20, + "totalElements": 5, + "totalPages": 1, + "links": [] + }, + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/?lang=en&pagesize=20&pageNumber=1" + } + ] +} \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004.json b/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004.json new file mode 100644 index 00000000..76541eb0 --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004.json @@ -0,0 +1,315 @@ +{ + "language": "en", + "id": "TAB004", + "label": "Total air emissions by sector, greenhouse gas, contents and year", + "aggregationAllowed": false, + "officialStatistics": false, + "subjectCode": "EN", + "subjectLabel": "Environment", + "source": "Statistics Sweden", + "license": "https://creativecommons.org/share-your-work/public-domain/cc0/", + "updated": "2023-05-25T13:42:00Z", + "notes": [ + { + "mandatory": false, + "text": "Footnote text" + }, + { + "mandatory": false, + "text": ".. = Data not available" + } + ], + "contacts": [ + { + "raw": "Test Testsson, SCB#Tel: 08-111 222 33#Fax: 08-222 333 44#E-mail: test.testsson@scb.se" + }, + { + "raw": "Test2 Testsson2, SCB#Tel: 08-333 444 55#Fax: 08-444 555 66#E-mail: test2.testsson2@scb.se" + } + ], + "variables": [ + { + "id": "SECTOR", + "label": "sector", + "type": "RegularVariable", + "notes": [ + { + "mandatory": false, + "text": "Footnote text" + } + ], + "elimination": false, + "values": [ + { + "code": "0.1", + "label": "NATIONAL TOTAL (excluding LULUCF, excluding international transports)" + }, + { + "code": "0.2", + "label": "NATIONAL TOTAL (excluding LULUCF, including international transports)" + }, + { + "code": "0.3", + "label": "NATIONAL TOTAL (including LULUCF, excluding international transports)" + }, + { + "code": "0.4", + "label": "NATIONAL TOTAL (including LULUCF, including international transports)" + }, + { + "code": "1.0", + "label": "OFF-ROAD VEHICLES AND OTHER MACHINERY, TOTAL" + }, + { + "code": "10.0", + "label": "LAND-USE, LAND-USE CHANGE AND FORESTRY (LULUCF), TOTAL" + }, + { + "code": "2.0", + "label": "WASTE, TOTAL" + }, + { + "code": "3.0", + "label": "ELECTRICITY AND DISTRICT HEATING, TOTAL" + }, + { + "code": "4.0", + "label": "INDUSTRY, TOTAL" + }, + { + "code": "5.0", + "label": "INTERNATIONAL TRANSPORT, TOTAL" + }, + { + "code": "6.0", + "label": "AGRICULTURE, TOTAL" + }, + { + "code": "7.0", + "label": "SOLVENT USE AND OTHER PRODUCT USE, TOTAL" + }, + { + "code": "8.0", + "label": "DOMESTIC TRANSPORT, TOTAL" + }, + { + "code": "9.0", + "label": "HEATING OF HOUSES AND PREMISES, TOTAL" + } + ] + }, + { + "id": "GREENHOUSEGAS", + "label": "greenhouse gas", + "type": "RegularVariable", + "elimination": false, + "values": [ + { + "code": "CH4", + "label": "Methane (CH4) (t)" + }, + { + "code": "CH4_CO2-ekv.", + "label": "Methane (CH4) (kt CO2-eqv.)" + }, + { + "code": "CO2", + "label": "Carbon Dioxide (CO2) (kt)" + }, + { + "code": "CO2-BIO", + "label": "Biogenic carbon dioxide (CO2) from fuels (kt)" + }, + { + "code": "CO2-ekv.", + "label": "Total Greenhouse Gases (kt CO2-eqv.)" + }, + { + "code": "HFC", + "label": "Hydrofluorocarbons (HFCs) (kt CO2-eqv.)" + }, + { + "code": "N2O", + "label": "Nitrous Oxide (N2O) (t)" + }, + { + "code": "N2O_CO2-ekv.", + "label": "Nitrous Oxide (N2O) (kt CO2-eqv.)" + }, + { + "code": "PFC", + "label": "Perfluorocarbons (PFCs) (kt CO2-eqv.)" + }, + { + "code": "SF6", + "label": "Sulphur Hexafluoride (SF6) (kg)" + }, + { + "code": "SF6_CO2-ekv.", + "label": "Sulphur Hexafluoride (SF6) (kt CO2-eqv.)", + "notes": [ + { + "mandatory": false, + "text": "Footnote text" + } + ] + } + ] + }, + { + "id": "ContentsCode", + "label": "contents", + "type": "ContentsVariable", + "values": [ + { + "adjustment": "None", + "measuringType": "Stock", + "preferedNumberOfDecimals": 0, + "priceType": "Undefined", + "unit": "Device varies with the subject", + "code": "Emission", + "label": "Substance" + } + ] + }, + { + "id": "TIME", + "label": "year", + "type": "TimeVariable", + "timeUnit": "Annual", + "firstPeriod": "1990", + "lastPeriod": "2017", + "values": [ + { + "code": "1990", + "label": "1990" + }, + { + "code": "1991", + "label": "1991" + }, + { + "code": "1992", + "label": "1992" + }, + { + "code": "1993", + "label": "1993" + }, + { + "code": "1994", + "label": "1994" + }, + { + "code": "1995", + "label": "1995" + }, + { + "code": "1996", + "label": "1996" + }, + { + "code": "1997", + "label": "1997" + }, + { + "code": "1998", + "label": "1998" + }, + { + "code": "1999", + "label": "1999" + }, + { + "code": "2000", + "label": "2000" + }, + { + "code": "2001", + "label": "2001" + }, + { + "code": "2002", + "label": "2002" + }, + { + "code": "2003", + "label": "2003" + }, + { + "code": "2004", + "label": "2004" + }, + { + "code": "2005", + "label": "2005" + }, + { + "code": "2006", + "label": "2006" + }, + { + "code": "2007", + "label": "2007" + }, + { + "code": "2008", + "label": "2008" + }, + { + "code": "2009", + "label": "2009" + }, + { + "code": "2010", + "label": "2010" + }, + { + "code": "2011", + "label": "2011" + }, + { + "code": "2012", + "label": "2012" + }, + { + "code": "2013", + "label": "2013" + }, + { + "code": "2014", + "label": "2014" + }, + { + "code": "2015", + "label": "2015" + }, + { + "code": "2016", + "label": "2016" + }, + { + "code": "2017", + "label": "2017" + } + ] + } + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en&outputFormat=px" + } + ] +} \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/TableById_tab004.json b/PxWebApi_Mvc.Tests/ExpectedJson/TableById_tab004.json new file mode 100644 index 00000000..d40924da --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/TableById_tab004.json @@ -0,0 +1,40 @@ +{ + "type": "Table", + "id": "TAB004", + "label": "Total air emissions by sector, greenhouse gas, contents and year", + "description": "", + "sortCode": "TAB004", + "updated": "2023-05-25T13:42:00Z", + "firstPeriod": "1990", + "lastPeriod": "2017", + "category": "public", + "variableNames": [ + "sector", + "greenhouse gas", + "contents", + "year" + ], + "links": [ + { + "rel": "self", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004?lang=en" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-px" + }, + { + "rel": "metadata", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-stat2" + }, + { + "rel": "data", + "hreflang": "en", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en&outputFormat=px" + } + ], + "language": "en" +} \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/GlobalUsings.cs b/PxWebApi_Mvc.Tests/GlobalUsings.cs new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/PxWebApi_Mvc.Tests/GlobalUsings.cs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/PxWebApi_Mvc.Tests.csproj b/PxWebApi_Mvc.Tests/PxWebApi_Mvc.Tests.csproj new file mode 100644 index 00000000..63507562 --- /dev/null +++ b/PxWebApi_Mvc.Tests/PxWebApi_Mvc.Tests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/PxWebApi_Mvc.Tests/TableApiControllerTest.cs b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs new file mode 100644 index 00000000..d5bc1558 --- /dev/null +++ b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs @@ -0,0 +1,105 @@ +using System.Net; + +using Microsoft.AspNetCore.Mvc.Testing; + +using PxWeb; + +namespace PxWebApi_Mvc.Tests +{ + [TestClass] + public class TableApiControllerTest + { + private static readonly string expectedJsonDir = Util.GetFullPathToFile(@"PxWebApi_Mvc.Tests/ExpectedJson/"); + + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + //expectedJsonDir = Util.GetFullPathToFile(@"PxWebApi_Mvc.Tests/ExpectedJson/"); + } + + + [TestMethod] + public async Task ListAllTables() + { + await using var application = new WebApplicationFactory(); + using var client = application.CreateClient(); + + var response = await client.GetAsync("/api/v2/tables?lang=en"); + + Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); + + string rawActual = await response.Content.ReadAsStringAsync(); + string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "ListAllTables.json")); + + AssertJson(rawExpected, rawActual); + } + + + + [TestMethod] + public async Task GetTableById_tab004() + { + await using var application = new WebApplicationFactory(); + using var client = application.CreateClient(); + + var response = await client.GetAsync("/api/v2/tables/tab004?lang=en"); + + Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); + + string rawActual = await response.Content.ReadAsStringAsync(); + string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "TableById_tab004.json")); + + AssertJson(rawExpected, rawActual); + + } + + [TestMethod] + public async Task GetMetadataById_tab004() + { + await using var application = new WebApplicationFactory(); + using var client = application.CreateClient(); + + var response = await client.GetAsync("/api/v2/tables/tab004/metadata?lang=en"); + + Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); + + string rawActual = await response.Content.ReadAsStringAsync(); + string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "MetadataById_tab004.json")); + + AssertJson(rawExpected, rawActual); + + } + + + + + private void AssertJson(string inExpected, string inActual) + { + //RegexOptions options = RegexOptions.None; + //Regex regex = new Regex("[ ]{2,}", options); + + //string expected = regex.Replace(inExpected.Replace(Environment.NewLine, ""), " "); + //string actual = regex.Replace(inActual.Replace(Environment.NewLine, ""), " "); + + + // prettyprints, but changes & to \u0026, so it has to be applied to expected as well + string actual = System.Text.Json.Nodes.JsonNode.Parse(inActual).ToString(); + string expected = System.Text.Json.Nodes.JsonNode.Parse(inExpected).ToString(); + + //updated causes problems. When expected and actual is made in different places and input is in localtime. + int posOfUpdatedString = expected.IndexOf("2023-05-25T13:42:00"); + actual = actual.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + actual.Substring(posOfUpdatedString + 13); + expected = expected.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + expected.Substring(posOfUpdatedString + 13); + + + Assert.AreEqual(expected.Substring(0, 5), actual.Substring(0, 5), "Diff in first 5."); + + for (int i = 0; i < actual.Length; i += 25) + { + int lengthToCompare = Math.Min(50, actual.Length - i); + Assert.AreEqual(expected.Substring(i, lengthToCompare), actual.Substring(i, lengthToCompare)); + } + + } + } +} diff --git a/PxWebApi_Mvc.Tests/Util.cs b/PxWebApi_Mvc.Tests/Util.cs new file mode 100644 index 00000000..9322b1d9 --- /dev/null +++ b/PxWebApi_Mvc.Tests/Util.cs @@ -0,0 +1,24 @@ +namespace PxWebApi_Mvc.Tests +{ + internal class Util + { + internal static string GetFullPathToFile(string pathRelativeUnitTestingFile) + { + string folderProjectLevel = GetPathToPxWebProject(); + string final = System.IO.Path.Combine(folderProjectLevel, pathRelativeUnitTestingFile); + return final; + } + + private static string GetPathToPxWebProject() + { + string pathAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location; + string directoryName = System.IO.Path.GetDirectoryName(pathAssembly) ?? throw new System.Exception("GetDirectoryName(pathAssembly) is null for:" + pathAssembly); + string folderAssembly = directoryName.Replace("\\", "/"); + if (folderAssembly.EndsWith("/") == false) folderAssembly = folderAssembly + "/"; + string folderProjectLevel = System.IO.Path.GetFullPath(folderAssembly + "../../../../"); + return folderProjectLevel; + } + + + } +} From 2ff18f6d6f4cfc123c500d4babef7b63de020316 Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 4 Jun 2024 16:06:47 +0200 Subject: [PATCH 04/10] whitespace --- PxWebApi_Mvc.Tests/Util.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/PxWebApi_Mvc.Tests/Util.cs b/PxWebApi_Mvc.Tests/Util.cs index 9322b1d9..807480f0 100644 --- a/PxWebApi_Mvc.Tests/Util.cs +++ b/PxWebApi_Mvc.Tests/Util.cs @@ -19,6 +19,5 @@ private static string GetPathToPxWebProject() return folderProjectLevel; } - } } From e6c8f4f9781f94c686dbd47d993fe1ab9f4e852d Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 4 Jun 2024 17:26:36 +0200 Subject: [PATCH 05/10] Refactored tests. Fixed duplicate link to codelist for jsonstat2. --- PxWeb/Mappers/DatasetMapper.cs | 28 +++-- .../ConfigApiControllerTest.cs | 50 --------- .../ConfigController/test_appsettings.json | 105 ------------------ PxWebApi.BigTests/PxWebApi.BigTests.csproj | 4 - PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs | 31 ++++++ .../ExpectedJson/GetApiConfiguration.json | 63 +++++++++++ PxWebApi_Mvc.Tests/TableApiControllerTest.cs | 51 +-------- PxWebApi_Mvc.Tests/Util.cs | 44 +++++++- 8 files changed, 162 insertions(+), 214 deletions(-) delete mode 100644 PxWebApi.BigTests/ConfigController/ConfigApiControllerTest.cs delete mode 100644 PxWebApi.BigTests/ConfigController/test_appsettings.json create mode 100644 PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/GetApiConfiguration.json diff --git a/PxWeb/Mappers/DatasetMapper.cs b/PxWeb/Mappers/DatasetMapper.cs index 971f35f1..281597b5 100644 --- a/PxWeb/Mappers/DatasetMapper.cs +++ b/PxWeb/Mappers/DatasetMapper.cs @@ -75,14 +75,6 @@ public Dataset Map(PXModel model, string id, string language) AddValueNotes(variableValue, dataset, dimensionValue); - //Codelists - var codeLists = new System.Collections.Generic.List(); - MapCodelists(codeLists, variable); - if (codeLists != null) - { - dataset.AddCodelist(dimensionValue, codeLists); - } - if (!variable.IsContentVariable) continue; var unitDecimals = (variableValue.HasPrecision()) ? variableValue.Precision : model.Meta.ShowDecimals; @@ -117,6 +109,7 @@ public Dataset Map(PXModel model, string id, string language) //Variable notes AddVariableNotes(variable, dataset, dimensionValue); + //MetaID CollectMetaIdsForVariable(variable, ref metaIdsHelper); if (metaIdsHelper.Count > 0) @@ -124,6 +117,16 @@ public Dataset Map(PXModel model, string id, string language) dataset.AddDimensionLink(dimensionValue, metaIdsHelper); } + + //Codelists + var codeLists = new System.Collections.Generic.List(); + MapCodelists(codeLists, variable); + if (codeLists != null) + { + dataset.AddCodelist(dimensionValue, codeLists); + } + + dataset.Size.Add(variable.Values.Count); dataset.Id.Add(variable.Code); @@ -182,7 +185,14 @@ private void AddUpdated(PXModel model, DatasetSubclass dataset) .FirstOrDefault(); // ReSharper disable once PossibleNullReferenceException - tempDateTime = lastUpdatedContentsVariable.ContentInfo.LastUpdated.PxDateStringToDateTime(); + if (lastUpdatedContentsVariable != null) + { + tempDateTime = lastUpdatedContentsVariable.ContentInfo.LastUpdated.PxDateStringToDateTime(); + } + else + { + tempDateTime = model.Meta.CreationDate.PxDateStringToDateTime(); + } } else if (model.Meta.ContentInfo.LastUpdated != null) { diff --git a/PxWebApi.BigTests/ConfigController/ConfigApiControllerTest.cs b/PxWebApi.BigTests/ConfigController/ConfigApiControllerTest.cs deleted file mode 100644 index 5eb521d7..00000000 --- a/PxWebApi.BigTests/ConfigController/ConfigApiControllerTest.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace PxWebApi.BigTests.ConfigController -{ - [TestClass] - public class ConfigApiControllerTest - { - - - [TestMethod] - [Description("Same input-file gives same output string.")] - - public void GetApiConfiguration() - { - string conf_dir_String = Util.GetFullPathToFile(@"PxWebApi.BigTests/ConfigController/"); - var builder = new ConfigurationBuilder() - .SetBasePath(conf_dir_String) - .AddJsonFile("test_appsettings.json", optional: false, reloadOnChange: true); - - IConfigurationRoot configuration = builder.Build(); - - IOptions pxApiConfigurationOptions = Util.GetIOptions(configuration, "PxApiConfiguration"); - IPxApiConfigurationService pxApiConfigurationService = new PxApiConfigurationService(pxApiConfigurationOptions); - - IOptions optionsRate = Util.GetIOptions(configuration, "IpRateLimiting"); - - var loggerMock = new Mock>(); - - PxWeb.Controllers.Api2.ConfigurationApiController cac = - new PxWeb.Controllers.Api2.ConfigurationApiController(pxApiConfigurationService, optionsRate, loggerMock.Object); - - var result = cac.GetApiConfiguration(); - var aa = result.ToJSON(false); - var bb = "{\"Value\":{\"apiVersion\":\"2.0\",\"languages\":[{\"id\":\"en\",\"label\":\"English\"},{\"id\":\"sv\",\"label\":\"Svenska\"}],\"defaultLanguage\":\"en\",\"maxDataCells\":10000,\"maxCallsPerTimeWindow\":30,\"timeWindow\":10,\"license\":\"https://creativecommons.org/share-your-work/public-domain/cc0/\",\"sourceReferences\":[{\"language\":\"en\",\"text\":\"Source: AAAAAAAAA Statistics Sweden\"},{\"language\":\"sv\",\"text\":\"Källa: AAAAAAAAAA SCB\"}],\"defaultMetadataFormat\":0,\"defaultDataFormat\":\"px\",\"dataFormats\":[\"xlsx\",\"xlsx_doublecolumn\",\"csv\",\"csv_tab\",\"csv_tabhead\",\"csv_comma\",\"csv_commahead\",\"csv_space\",\"csv_spacehead\",\"csv_semicolon\",\"csv_semicolonhead\",\"csv2\",\"csv3\",\"json\",\"json-stat\",\"json-stat2\",\"parquet\",\"html5_table\",\"relational_table\",\"px\"],\"features\":[{\"id\":\"CORS\",\"params\":[{\"key\":\"enabled\",\"value\":\"True\"}]}]},\"Formatters\":[],\"ContentTypes\":[]}"; - - - - Assert.AreEqual(bb, aa); - /* - loggerMock.Verify( x => x.Log( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny(), - (Func)It.IsAny()), - Times.AtLeastOnce); - */ - - } - - } -} diff --git a/PxWebApi.BigTests/ConfigController/test_appsettings.json b/PxWebApi.BigTests/ConfigController/test_appsettings.json deleted file mode 100644 index 93b0574c..00000000 --- a/PxWebApi.BigTests/ConfigController/test_appsettings.json +++ /dev/null @@ -1,105 +0,0 @@ -{ - "DataSource": { - "DataSourceType": "PX", - "PX": { - "StrictAggregations": "true" - }, - "CNMM": { - "DatabaseID": "ssd" - } - }, - "PxApiConfiguration": { - "AAAAAAA":"BBBBBBBBB", - "Languages": [ - { - "Id": "en", - "Label": "English" - }, - { - "Id": "sv", - "Label": "Svenska" - } - ], - "DefaultLanguage": "en", - "MaxDataCells": 10000, - "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", - "SourceReferences": [ - { - "Language": "en", - "Text": "Source: AAAAAAAAA Statistics Sweden" - }, - { - "Language": "sv", - "Text": "Källa: AAAAAAAAAA SCB" - } - ], - "Cors": { - "Enabled": true, - "Origins": "*" - }, - "CacheTime": 86400, - "SearchEngine": "Lucene", - "PageSize": 20, - "BaseURL": "https://www.pxapi.com/api/v2/", - "OutputFormats": [ - "xlsx", - "xlsx_doublecolumn", - "csv", - "csv_tab", - "csv_tabhead", - "csv_comma", - "csv_commahead", - "csv_space", - "csv_spacehead", - "csv_semicolon", - "csv_semicolonhead", - "csv2", - "csv3", - "json", - "json-stat", - "json-stat2", - "parquet", - "html5_table", - "relational_table", - "px" - ], - "DefaultOutputFormat": "px" - }, - "LuceneConfiguration": { - "IndexDirectory": "Database" - }, - "AdminProtection": { - "IpWhitelist": ["127.0.0.1", "::1"], - "AdminKey": "test" - }, - "CacheMiddleware": { - "CacheTime": 10, - "BufferThreshold": 40960 - }, - "IpRateLimiting": { - "EnableEndpointRateLimiting": false, - "StackBlockedRequests": false, - "RealIpHeader": "X-Forwarded-For", - "ClientIdHeader": "", - "HttpStatusCode": 429, - "IpWhitelist": ["::1/10", "127.0.0.1"], - "EndpointWhitelist": ["get:/v2/config"], - "ClientWhitelist": [], - "GeneralRules": [ - { - "Endpoint": "*", - "Period": "10s", - "Limit": 30 - } - ] - }, - - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "config1": "" -} diff --git a/PxWebApi.BigTests/PxWebApi.BigTests.csproj b/PxWebApi.BigTests/PxWebApi.BigTests.csproj index 8ed1d926..caaaf0c1 100644 --- a/PxWebApi.BigTests/PxWebApi.BigTests.csproj +++ b/PxWebApi.BigTests/PxWebApi.BigTests.csproj @@ -30,8 +30,4 @@ - - - - diff --git a/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs b/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs new file mode 100644 index 00000000..aedf254f --- /dev/null +++ b/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs @@ -0,0 +1,31 @@ +using System.Net; + +using Microsoft.AspNetCore.Mvc.Testing; + +using PxWeb; + +namespace PxWebApi_Mvc.Tests +{ + [TestClass] + public class ConfigApiControllerTest + { + + + [TestMethod] + public async Task GetApiConfiguration() + { + await using var application = new WebApplicationFactory(); + using var client = application.CreateClient(); + + var response = await client.GetAsync("/api/v2/config"); + + Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); + + string rawActual = await response.Content.ReadAsStringAsync(); + string rawExpected = File.ReadAllText(Path.Combine(Util.ExpectedJsonDir(), "GetApiConfiguration.json")); + + Util.AssertJson(rawExpected, rawActual); + } + + } +} diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/GetApiConfiguration.json b/PxWebApi_Mvc.Tests/ExpectedJson/GetApiConfiguration.json new file mode 100644 index 00000000..1d60eb0c --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/GetApiConfiguration.json @@ -0,0 +1,63 @@ +{ + "apiVersion": "2.0", + "languages": [ + { + "id": "en", + "label": "English" + }, + { + "id": "sv", + "label": "Svenska" + } + ], + "defaultLanguage": "en", + "maxDataCells": 10000, + "maxCallsPerTimeWindow": 30, + "timeWindow": 10, + "license": "https://creativecommons.org/share-your-work/public-domain/cc0/", + "sourceReferences": [ + { + "language": "en", + "text": "Source: Statistics Sweden" + }, + { + "language": "sv", + "text": "Källa: SCB" + } + ], + "defaultMetadataFormat": 0, + "defaultDataFormat": "px", + "dataFormats": [ + "xlsx", + "xlsx_doublecolumn", + "csv", + "csv_tab", + "csv_tabhead", + "csv_comma", + "csv_commahead", + "csv_space", + "csv_spacehead", + "csv_semicolon", + "csv_semicolonhead", + "csv2", + "csv3", + "json", + "json-stat", + "json-stat2", + "parquet", + "html5_table", + "relational_table", + "px" + ], + "features": [ + { + "id": "CORS", + "params": [ + { + "key": "enabled", + "value": "True" + } + ] + } + ] +} \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/TableApiControllerTest.cs b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs index d5bc1558..d428070a 100644 --- a/PxWebApi_Mvc.Tests/TableApiControllerTest.cs +++ b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs @@ -9,14 +9,6 @@ namespace PxWebApi_Mvc.Tests [TestClass] public class TableApiControllerTest { - private static readonly string expectedJsonDir = Util.GetFullPathToFile(@"PxWebApi_Mvc.Tests/ExpectedJson/"); - - [ClassInitialize] - public static void ClassSetup(TestContext context) - { - //expectedJsonDir = Util.GetFullPathToFile(@"PxWebApi_Mvc.Tests/ExpectedJson/"); - } - [TestMethod] public async Task ListAllTables() @@ -29,9 +21,9 @@ public async Task ListAllTables() Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); string rawActual = await response.Content.ReadAsStringAsync(); - string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "ListAllTables.json")); + string rawExpected = File.ReadAllText(Path.Combine(Util.ExpectedJsonDir(), "ListAllTables.json")); - AssertJson(rawExpected, rawActual); + Util.AssertJson(rawExpected, rawActual); } @@ -47,9 +39,9 @@ public async Task GetTableById_tab004() Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); string rawActual = await response.Content.ReadAsStringAsync(); - string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "TableById_tab004.json")); + string rawExpected = File.ReadAllText(Path.Combine(Util.ExpectedJsonDir(), "TableById_tab004.json")); - AssertJson(rawExpected, rawActual); + Util.AssertJson(rawExpected, rawActual); } @@ -64,42 +56,11 @@ public async Task GetMetadataById_tab004() Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); string rawActual = await response.Content.ReadAsStringAsync(); - string rawExpected = File.ReadAllText(Path.Combine(expectedJsonDir, "MetadataById_tab004.json")); + string rawExpected = File.ReadAllText(Path.Combine(Util.ExpectedJsonDir(), "MetadataById_tab004.json")); - AssertJson(rawExpected, rawActual); + Util.AssertJson(rawExpected, rawActual); } - - - - private void AssertJson(string inExpected, string inActual) - { - //RegexOptions options = RegexOptions.None; - //Regex regex = new Regex("[ ]{2,}", options); - - //string expected = regex.Replace(inExpected.Replace(Environment.NewLine, ""), " "); - //string actual = regex.Replace(inActual.Replace(Environment.NewLine, ""), " "); - - - // prettyprints, but changes & to \u0026, so it has to be applied to expected as well - string actual = System.Text.Json.Nodes.JsonNode.Parse(inActual).ToString(); - string expected = System.Text.Json.Nodes.JsonNode.Parse(inExpected).ToString(); - - //updated causes problems. When expected and actual is made in different places and input is in localtime. - int posOfUpdatedString = expected.IndexOf("2023-05-25T13:42:00"); - actual = actual.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + actual.Substring(posOfUpdatedString + 13); - expected = expected.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + expected.Substring(posOfUpdatedString + 13); - - - Assert.AreEqual(expected.Substring(0, 5), actual.Substring(0, 5), "Diff in first 5."); - - for (int i = 0; i < actual.Length; i += 25) - { - int lengthToCompare = Math.Min(50, actual.Length - i); - Assert.AreEqual(expected.Substring(i, lengthToCompare), actual.Substring(i, lengthToCompare)); - } - - } } } diff --git a/PxWebApi_Mvc.Tests/Util.cs b/PxWebApi_Mvc.Tests/Util.cs index 807480f0..cfcfc06b 100644 --- a/PxWebApi_Mvc.Tests/Util.cs +++ b/PxWebApi_Mvc.Tests/Util.cs @@ -1,7 +1,14 @@ -namespace PxWebApi_Mvc.Tests +using System.Text.Json.Nodes; + +namespace PxWebApi_Mvc.Tests { internal class Util { + internal static string ExpectedJsonDir() + { + return GetFullPathToFile(@"PxWebApi_Mvc.Tests/ExpectedJson/"); + } + internal static string GetFullPathToFile(string pathRelativeUnitTestingFile) { string folderProjectLevel = GetPathToPxWebProject(); @@ -19,5 +26,40 @@ private static string GetPathToPxWebProject() return folderProjectLevel; } + + internal static void AssertJson(string inExpected, string inActual) + { + //RegexOptions options = RegexOptions.None; + //Regex regex = new Regex("[ ]{2,}", options); + + //string expected = regex.Replace(inExpected.Replace(Environment.NewLine, ""), " "); + //string actual = regex.Replace(inActual.Replace(Environment.NewLine, ""), " "); + + + // prettyprints, but changes & to \u0026, so it has to be applied to expected as well + JsonNode? jsonNodeActual = System.Text.Json.Nodes.JsonNode.Parse(inActual); + string actual = jsonNodeActual != null ? jsonNodeActual.ToString() : "jsonNodeActual is null"; + + JsonNode? jsonNodeExpected = System.Text.Json.Nodes.JsonNode.Parse(inExpected); + string expected = jsonNodeExpected != null ? jsonNodeExpected.ToString() : "jsonNodeExpected is null"; + + //updated causes problems. When expected and actual is made in different places and input is in localtime. + int posOfUpdatedString = expected.IndexOf("2023-05-25T13:42:00"); + if (posOfUpdatedString != -1) + { + actual = actual.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + actual.Substring(posOfUpdatedString + 13); + expected = expected.Substring(0, posOfUpdatedString) + "XXXX-XX-XXTXX" + expected.Substring(posOfUpdatedString + 13); + } + + + Assert.AreEqual(expected.Substring(0, 5), actual.Substring(0, 5), "Diff in first 5."); + + for (int i = 0; i < actual.Length; i += 25) + { + int lengthToCompare = Math.Min(50, actual.Length - i); + Assert.AreEqual(expected.Substring(i, lengthToCompare), actual.Substring(i, lengthToCompare)); + } + + } } } From dc2a7565994535e38ad94b5a580f6a736c960515 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 5 Jun 2024 09:36:26 +0200 Subject: [PATCH 06/10] Px-json now work also if stockfa is missing from datasource --- PxWeb/Mappers/TableMetadataResponseMapper.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/PxWeb/Mappers/TableMetadataResponseMapper.cs b/PxWeb/Mappers/TableMetadataResponseMapper.cs index d5f92c00..6e0403bc 100644 --- a/PxWeb/Mappers/TableMetadataResponseMapper.cs +++ b/PxWeb/Mappers/TableMetadataResponseMapper.cs @@ -505,6 +505,10 @@ private string GetLastTimePeriod(Variable variable) private ContentValue.MeasuringTypeEnum GetMeasuringType(string stockfa) { + if (stockfa == null) + { + return ContentValue.MeasuringTypeEnum.OtherEnum; + } switch (stockfa.ToUpper()) { case "S": From 9c15b44922e6195c2f576f0d4019e2beaf0dd914 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 5 Jun 2024 10:52:18 +0200 Subject: [PATCH 07/10] To simplyfy merge from main of Global route prefix middleware (#127), will be removed --- .../ConfigController/test_appsettings.json | 105 ++++++++++++++++++ .../TableController/test_appsettings.json | 105 ++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 PxWebApi.BigTests/ConfigController/test_appsettings.json create mode 100644 PxWebApi.BigTests/TableController/test_appsettings.json diff --git a/PxWebApi.BigTests/ConfigController/test_appsettings.json b/PxWebApi.BigTests/ConfigController/test_appsettings.json new file mode 100644 index 00000000..93b0574c --- /dev/null +++ b/PxWebApi.BigTests/ConfigController/test_appsettings.json @@ -0,0 +1,105 @@ +{ + "DataSource": { + "DataSourceType": "PX", + "PX": { + "StrictAggregations": "true" + }, + "CNMM": { + "DatabaseID": "ssd" + } + }, + "PxApiConfiguration": { + "AAAAAAA":"BBBBBBBBB", + "Languages": [ + { + "Id": "en", + "Label": "English" + }, + { + "Id": "sv", + "Label": "Svenska" + } + ], + "DefaultLanguage": "en", + "MaxDataCells": 10000, + "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", + "SourceReferences": [ + { + "Language": "en", + "Text": "Source: AAAAAAAAA Statistics Sweden" + }, + { + "Language": "sv", + "Text": "Källa: AAAAAAAAAA SCB" + } + ], + "Cors": { + "Enabled": true, + "Origins": "*" + }, + "CacheTime": 86400, + "SearchEngine": "Lucene", + "PageSize": 20, + "BaseURL": "https://www.pxapi.com/api/v2/", + "OutputFormats": [ + "xlsx", + "xlsx_doublecolumn", + "csv", + "csv_tab", + "csv_tabhead", + "csv_comma", + "csv_commahead", + "csv_space", + "csv_spacehead", + "csv_semicolon", + "csv_semicolonhead", + "csv2", + "csv3", + "json", + "json-stat", + "json-stat2", + "parquet", + "html5_table", + "relational_table", + "px" + ], + "DefaultOutputFormat": "px" + }, + "LuceneConfiguration": { + "IndexDirectory": "Database" + }, + "AdminProtection": { + "IpWhitelist": ["127.0.0.1", "::1"], + "AdminKey": "test" + }, + "CacheMiddleware": { + "CacheTime": 10, + "BufferThreshold": 40960 + }, + "IpRateLimiting": { + "EnableEndpointRateLimiting": false, + "StackBlockedRequests": false, + "RealIpHeader": "X-Forwarded-For", + "ClientIdHeader": "", + "HttpStatusCode": 429, + "IpWhitelist": ["::1/10", "127.0.0.1"], + "EndpointWhitelist": ["get:/v2/config"], + "ClientWhitelist": [], + "GeneralRules": [ + { + "Endpoint": "*", + "Period": "10s", + "Limit": 30 + } + ] + }, + + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "config1": "" +} diff --git a/PxWebApi.BigTests/TableController/test_appsettings.json b/PxWebApi.BigTests/TableController/test_appsettings.json new file mode 100644 index 00000000..93b0574c --- /dev/null +++ b/PxWebApi.BigTests/TableController/test_appsettings.json @@ -0,0 +1,105 @@ +{ + "DataSource": { + "DataSourceType": "PX", + "PX": { + "StrictAggregations": "true" + }, + "CNMM": { + "DatabaseID": "ssd" + } + }, + "PxApiConfiguration": { + "AAAAAAA":"BBBBBBBBB", + "Languages": [ + { + "Id": "en", + "Label": "English" + }, + { + "Id": "sv", + "Label": "Svenska" + } + ], + "DefaultLanguage": "en", + "MaxDataCells": 10000, + "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", + "SourceReferences": [ + { + "Language": "en", + "Text": "Source: AAAAAAAAA Statistics Sweden" + }, + { + "Language": "sv", + "Text": "Källa: AAAAAAAAAA SCB" + } + ], + "Cors": { + "Enabled": true, + "Origins": "*" + }, + "CacheTime": 86400, + "SearchEngine": "Lucene", + "PageSize": 20, + "BaseURL": "https://www.pxapi.com/api/v2/", + "OutputFormats": [ + "xlsx", + "xlsx_doublecolumn", + "csv", + "csv_tab", + "csv_tabhead", + "csv_comma", + "csv_commahead", + "csv_space", + "csv_spacehead", + "csv_semicolon", + "csv_semicolonhead", + "csv2", + "csv3", + "json", + "json-stat", + "json-stat2", + "parquet", + "html5_table", + "relational_table", + "px" + ], + "DefaultOutputFormat": "px" + }, + "LuceneConfiguration": { + "IndexDirectory": "Database" + }, + "AdminProtection": { + "IpWhitelist": ["127.0.0.1", "::1"], + "AdminKey": "test" + }, + "CacheMiddleware": { + "CacheTime": 10, + "BufferThreshold": 40960 + }, + "IpRateLimiting": { + "EnableEndpointRateLimiting": false, + "StackBlockedRequests": false, + "RealIpHeader": "X-Forwarded-For", + "ClientIdHeader": "", + "HttpStatusCode": 429, + "IpWhitelist": ["::1/10", "127.0.0.1"], + "EndpointWhitelist": ["get:/v2/config"], + "ClientWhitelist": [], + "GeneralRules": [ + { + "Endpoint": "*", + "Period": "10s", + "Limit": 30 + } + ] + }, + + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "config1": "" +} From 57b54d78284ca4e99e86af2cafdde50bb755d15e Mon Sep 17 00:00:00 2001 From: JohannesFinsveen <56305961+JohannesFinsveen@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:01:51 +0200 Subject: [PATCH 08/10] Create readme.md --- PxWebApi_Mvc.Tests/ExpectedJson/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/readme.md diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/readme.md b/PxWebApi_Mvc.Tests/ExpectedJson/readme.md new file mode 100644 index 00000000..9b624888 --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/readme.md @@ -0,0 +1 @@ +These json files have been made by copying the result from swagger. From b10b883d9abbd388fea1cba98c42c0b872d7c7a8 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 5 Jun 2024 11:06:42 +0200 Subject: [PATCH 09/10] Removing: To simplyfy merge from main of Global route prefix middleware (#127), will be removed --- .../ConfigController/test_appsettings.json | 106 ------------------ .../TableController/test_appsettings.json | 106 ------------------ 2 files changed, 212 deletions(-) delete mode 100644 PxWebApi.BigTests/ConfigController/test_appsettings.json delete mode 100644 PxWebApi.BigTests/TableController/test_appsettings.json diff --git a/PxWebApi.BigTests/ConfigController/test_appsettings.json b/PxWebApi.BigTests/ConfigController/test_appsettings.json deleted file mode 100644 index ac527803..00000000 --- a/PxWebApi.BigTests/ConfigController/test_appsettings.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "DataSource": { - "DataSourceType": "PX", - "PX": { - "StrictAggregations": "true" - }, - "CNMM": { - "DatabaseID": "ssd" - } - }, - "PxApiConfiguration": { - "AAAAAAA":"BBBBBBBBB", - "Languages": [ - { - "Id": "en", - "Label": "English" - }, - { - "Id": "sv", - "Label": "Svenska" - } - ], - "DefaultLanguage": "en", - "MaxDataCells": 10000, - "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", - "SourceReferences": [ - { - "Language": "en", - "Text": "Source: AAAAAAAAA Statistics Sweden" - }, - { - "Language": "sv", - "Text": "Källa: AAAAAAAAAA SCB" - } - ], - "Cors": { - "Enabled": true, - "Origins": "*" - }, - "CacheTime": 86400, - "SearchEngine": "Lucene", - "PageSize": 20, - "BaseURL": "https://www.pxapi.com/api/v2", - "RoutePrefix": "/api/v2", - "OutputFormats": [ - "xlsx", - "xlsx_doublecolumn", - "csv", - "csv_tab", - "csv_tabhead", - "csv_comma", - "csv_commahead", - "csv_space", - "csv_spacehead", - "csv_semicolon", - "csv_semicolonhead", - "csv2", - "csv3", - "json", - "json-stat", - "json-stat2", - "parquet", - "html5_table", - "relational_table", - "px" - ], - "DefaultOutputFormat": "px" - }, - "LuceneConfiguration": { - "IndexDirectory": "Database" - }, - "AdminProtection": { - "IpWhitelist": ["127.0.0.1", "::1"], - "AdminKey": "test" - }, - "CacheMiddleware": { - "CacheTime": 10, - "BufferThreshold": 40960 - }, - "IpRateLimiting": { - "EnableEndpointRateLimiting": false, - "StackBlockedRequests": false, - "RealIpHeader": "X-Forwarded-For", - "ClientIdHeader": "", - "HttpStatusCode": 429, - "IpWhitelist": ["::1/10", "127.0.0.1"], - "EndpointWhitelist": ["get:/v2/config"], - "ClientWhitelist": [], - "GeneralRules": [ - { - "Endpoint": "*", - "Period": "10s", - "Limit": 30 - } - ] - }, - - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "config1": "" -} diff --git a/PxWebApi.BigTests/TableController/test_appsettings.json b/PxWebApi.BigTests/TableController/test_appsettings.json deleted file mode 100644 index ac527803..00000000 --- a/PxWebApi.BigTests/TableController/test_appsettings.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "DataSource": { - "DataSourceType": "PX", - "PX": { - "StrictAggregations": "true" - }, - "CNMM": { - "DatabaseID": "ssd" - } - }, - "PxApiConfiguration": { - "AAAAAAA":"BBBBBBBBB", - "Languages": [ - { - "Id": "en", - "Label": "English" - }, - { - "Id": "sv", - "Label": "Svenska" - } - ], - "DefaultLanguage": "en", - "MaxDataCells": 10000, - "License": "https://creativecommons.org/share-your-work/public-domain/cc0/", - "SourceReferences": [ - { - "Language": "en", - "Text": "Source: AAAAAAAAA Statistics Sweden" - }, - { - "Language": "sv", - "Text": "Källa: AAAAAAAAAA SCB" - } - ], - "Cors": { - "Enabled": true, - "Origins": "*" - }, - "CacheTime": 86400, - "SearchEngine": "Lucene", - "PageSize": 20, - "BaseURL": "https://www.pxapi.com/api/v2", - "RoutePrefix": "/api/v2", - "OutputFormats": [ - "xlsx", - "xlsx_doublecolumn", - "csv", - "csv_tab", - "csv_tabhead", - "csv_comma", - "csv_commahead", - "csv_space", - "csv_spacehead", - "csv_semicolon", - "csv_semicolonhead", - "csv2", - "csv3", - "json", - "json-stat", - "json-stat2", - "parquet", - "html5_table", - "relational_table", - "px" - ], - "DefaultOutputFormat": "px" - }, - "LuceneConfiguration": { - "IndexDirectory": "Database" - }, - "AdminProtection": { - "IpWhitelist": ["127.0.0.1", "::1"], - "AdminKey": "test" - }, - "CacheMiddleware": { - "CacheTime": 10, - "BufferThreshold": 40960 - }, - "IpRateLimiting": { - "EnableEndpointRateLimiting": false, - "StackBlockedRequests": false, - "RealIpHeader": "X-Forwarded-For", - "ClientIdHeader": "", - "HttpStatusCode": 429, - "IpWhitelist": ["::1/10", "127.0.0.1"], - "EndpointWhitelist": ["get:/v2/config"], - "ClientWhitelist": [], - "GeneralRules": [ - { - "Endpoint": "*", - "Period": "10s", - "Limit": 30 - } - ] - }, - - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "config1": "" -} From 4e26fff82a5fc4fd05e35d3d6e0d14f1bbd6e2a7 Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 5 Jun 2024 13:13:15 +0200 Subject: [PATCH 10/10] removed /api/v2 from paths in tests, added test for js2 --- PxWeb/Models/Api2/DatasetSubclass.cs | 2 +- PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs | 3 +- .../ExpectedJson/MetadataById_tab004_js2.json | 250 ++++++++++++++++++ PxWebApi_Mvc.Tests/TableApiControllerTest.cs | 23 +- 4 files changed, 272 insertions(+), 6 deletions(-) create mode 100644 PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004_js2.json diff --git a/PxWeb/Models/Api2/DatasetSubclass.cs b/PxWeb/Models/Api2/DatasetSubclass.cs index 4f0056f8..1cf50f41 100644 --- a/PxWeb/Models/Api2/DatasetSubclass.cs +++ b/PxWeb/Models/Api2/DatasetSubclass.cs @@ -285,7 +285,7 @@ public void AddLinksOnRoot(List links) { if (link.Rel == "self") { - this.Href = link.Href + "&outputFormat=json-stat2"; + this.Href = link.Href; } else { diff --git a/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs b/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs index aedf254f..c198c023 100644 --- a/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs +++ b/PxWebApi_Mvc.Tests/ConfigApiControllerTest.cs @@ -10,14 +10,13 @@ namespace PxWebApi_Mvc.Tests public class ConfigApiControllerTest { - [TestMethod] public async Task GetApiConfiguration() { await using var application = new WebApplicationFactory(); using var client = application.CreateClient(); - var response = await client.GetAsync("/api/v2/config"); + var response = await client.GetAsync("/config"); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); diff --git a/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004_js2.json b/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004_js2.json new file mode 100644 index 00000000..1e1448c8 --- /dev/null +++ b/PxWebApi_Mvc.Tests/ExpectedJson/MetadataById_tab004_js2.json @@ -0,0 +1,250 @@ +{ + "version": "2.0", + "class": "dataset", + "href": "https://www.pxapi.com/api/v2/tables/TAB004/metadata?lang=en&outputFormat=json-stat2", + "label": "Total air emissions by sector, greenhouse gas, contents and year", + "source": "Statistics Sweden", + "updated": "2023-05-25T13:42:00Z", + "link": { + "data": [ + { + "href": "https://www.pxapi.com/api/v2/tables/TAB004/data?lang=en&outputFormat=px" + } + ] + }, + "note": [ + "Footnote text", + ".. = Data not available" + ], + "role": { + "time": [ + "TIME" + ], + "metric": [ + "ContentsCode" + ] + }, + "id": [ + "SECTOR", + "GREENHOUSEGAS", + "ContentsCode", + "TIME" + ], + "size": [ + 14, + 11, + 1, + 28 + ], + "dimension": { + "SECTOR": { + "label": "sector", + "note": [ + "Footnote text" + ], + "category": { + "index": { + "0.1": 0, + "0.2": 1, + "0.3": 2, + "0.4": 3, + "1.0": 4, + "10.0": 5, + "2.0": 6, + "3.0": 7, + "4.0": 8, + "5.0": 9, + "6.0": 10, + "7.0": 11, + "8.0": 12, + "9.0": 13 + }, + "label": { + "0.1": "NATIONAL TOTAL (excluding LULUCF, excluding international transports)", + "0.2": "NATIONAL TOTAL (excluding LULUCF, including international transports)", + "0.3": "NATIONAL TOTAL (including LULUCF, excluding international transports)", + "0.4": "NATIONAL TOTAL (including LULUCF, including international transports)", + "1.0": "OFF-ROAD VEHICLES AND OTHER MACHINERY, TOTAL", + "10.0": "LAND-USE, LAND-USE CHANGE AND FORESTRY (LULUCF), TOTAL", + "2.0": "WASTE, TOTAL", + "3.0": "ELECTRICITY AND DISTRICT HEATING, TOTAL", + "4.0": "INDUSTRY, TOTAL", + "5.0": "INTERNATIONAL TRANSPORT, TOTAL", + "6.0": "AGRICULTURE, TOTAL", + "7.0": "SOLVENT USE AND OTHER PRODUCT USE, TOTAL", + "8.0": "DOMESTIC TRANSPORT, TOTAL", + "9.0": "HEATING OF HOUSES AND PREMISES, TOTAL" + } + }, + "extension": { + "elimination": false, + "show": "value", + "codeLists": [] + } + }, + "GREENHOUSEGAS": { + "label": "greenhouse gas", + "category": { + "index": { + "CH4": 0, + "CH4_CO2-ekv.": 1, + "CO2": 2, + "CO2-BIO": 3, + "CO2-ekv.": 4, + "HFC": 5, + "N2O": 6, + "N2O_CO2-ekv.": 7, + "PFC": 8, + "SF6": 9, + "SF6_CO2-ekv.": 10 + }, + "label": { + "CH4": "Methane (CH4) (t)", + "CH4_CO2-ekv.": "Methane (CH4) (kt CO2-eqv.)", + "CO2": "Carbon Dioxide (CO2) (kt)", + "CO2-BIO": "Biogenic carbon dioxide (CO2) from fuels (kt)", + "CO2-ekv.": "Total Greenhouse Gases (kt CO2-eqv.)", + "HFC": "Hydrofluorocarbons (HFCs) (kt CO2-eqv.)", + "N2O": "Nitrous Oxide (N2O) (t)", + "N2O_CO2-ekv.": "Nitrous Oxide (N2O) (kt CO2-eqv.)", + "PFC": "Perfluorocarbons (PFCs) (kt CO2-eqv.)", + "SF6": "Sulphur Hexafluoride (SF6) (kg)", + "SF6_CO2-ekv.": "Sulphur Hexafluoride (SF6) (kt CO2-eqv.)" + }, + "note": { + "SF6_CO2-ekv.": [ + "Footnote text" + ] + } + }, + "extension": { + "elimination": false, + "show": "value", + "codeLists": [] + } + }, + "ContentsCode": { + "label": "contents", + "category": { + "index": { + "Emission": 0 + }, + "label": { + "Emission": "Substance" + }, + "unit": { + "Emission": { + "base": "Device varies with the subject", + "decimals": 0 + } + } + }, + "extension": { + "elimination": false, + "show": "value", + "codeLists": [] + } + }, + "TIME": { + "label": "year", + "category": { + "index": { + "1990": 0, + "1991": 1, + "1992": 2, + "1993": 3, + "1994": 4, + "1995": 5, + "1996": 6, + "1997": 7, + "1998": 8, + "1999": 9, + "2000": 10, + "2001": 11, + "2002": 12, + "2003": 13, + "2004": 14, + "2005": 15, + "2006": 16, + "2007": 17, + "2008": 18, + "2009": 19, + "2010": 20, + "2011": 21, + "2012": 22, + "2013": 23, + "2014": 24, + "2015": 25, + "2016": 26, + "2017": 27 + }, + "label": { + "1990": "1990", + "1991": "1991", + "1992": "1992", + "1993": "1993", + "1994": "1994", + "1995": "1995", + "1996": "1996", + "1997": "1997", + "1998": "1998", + "1999": "1999", + "2000": "2000", + "2001": "2001", + "2002": "2002", + "2003": "2003", + "2004": "2004", + "2005": "2005", + "2006": "2006", + "2007": "2007", + "2008": "2008", + "2009": "2009", + "2010": "2010", + "2011": "2011", + "2012": "2012", + "2013": "2013", + "2014": "2014", + "2015": "2015", + "2016": "2016", + "2017": "2017" + } + }, + "extension": { + "elimination": false, + "show": "code", + "codeLists": [] + } + } + }, + "extension": { + "px": { + "infofile": "ENVIRONMENT", + "tableid": "TAB004", + "decimals": 0, + "official-statistics": false, + "aggregallowed": false, + "language": "en", + "contents": "Total air emissions", + "descriptiondefault": false, + "heading": [ + "contents", + "year" + ], + "stub": [ + "sector", + "greenhouse gas" + ], + "matrix": "TAB004", + "subject-code": "EN", + "subject-area": "Environment" + }, + "contact": [ + { + "raw": "Test Testsson, SCB#Tel: 08-111 222 33#Fax: 08-222 333 44#E-mail: test.testsson@scb.se" + }, + { + "raw": "Test2 Testsson2, SCB#Tel: 08-333 444 55#Fax: 08-444 555 66#E-mail: test2.testsson2@scb.se" + } + ] + } +} \ No newline at end of file diff --git a/PxWebApi_Mvc.Tests/TableApiControllerTest.cs b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs index d428070a..8d1b8c63 100644 --- a/PxWebApi_Mvc.Tests/TableApiControllerTest.cs +++ b/PxWebApi_Mvc.Tests/TableApiControllerTest.cs @@ -16,7 +16,7 @@ public async Task ListAllTables() await using var application = new WebApplicationFactory(); using var client = application.CreateClient(); - var response = await client.GetAsync("/api/v2/tables?lang=en"); + var response = await client.GetAsync("/tables?lang=en"); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); @@ -34,7 +34,7 @@ public async Task GetTableById_tab004() await using var application = new WebApplicationFactory(); using var client = application.CreateClient(); - var response = await client.GetAsync("/api/v2/tables/tab004?lang=en"); + var response = await client.GetAsync("/tables/tab004?lang=en"); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); @@ -51,7 +51,7 @@ public async Task GetMetadataById_tab004() await using var application = new WebApplicationFactory(); using var client = application.CreateClient(); - var response = await client.GetAsync("/api/v2/tables/tab004/metadata?lang=en"); + var response = await client.GetAsync("/tables/tab004/metadata?lang=en"); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); @@ -62,5 +62,22 @@ public async Task GetMetadataById_tab004() } + [TestMethod] + public async Task GetMetadataById_tab004_js2() + { + await using var application = new WebApplicationFactory(); + using var client = application.CreateClient(); + + var response = await client.GetAsync("/tables/tab004/metadata?lang=en&outputFormat=json-stat2"); + + Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); + + string rawActual = await response.Content.ReadAsStringAsync(); + string rawExpected = File.ReadAllText(Path.Combine(Util.ExpectedJsonDir(), "MetadataById_tab004_js2.json")); + + Util.AssertJson(rawExpected, rawActual); + + } + } }