-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from PxTools/vs_and_Grouping
Always uses SqlDbConfigsStatic.DefaultDatabase, all pxwebapi2 export …
- Loading branch information
Showing
14 changed files
with
353 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,3 +482,4 @@ $RECYCLE.BIN/ | |
|
||
# Vim temporary swap files | ||
*.swp | ||
/ManualTests/sqlDb.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.Configuration; | ||
|
||
using PCAxis.Sql.DbConfig; | ||
|
||
namespace ManualTests | ||
{ | ||
[Ignore] | ||
[TestClass] | ||
public class ConfigTest | ||
{ | ||
|
||
[TestMethod] | ||
public void TestMethod1() | ||
{ | ||
//System.Configuration.ConfigurationFileMap configMap = new ConfigurationFileMap("./app.config"); | ||
//System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(configMap); | ||
//var sdfdsz = configuration.AppSettings["dsa"]; | ||
//string value = configuration.AppSettings["TestKey"]; | ||
string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; | ||
var k = ConfigurationManager.AppSettings["TestKey"]; | ||
Assert.AreEqual("testvalue", k); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMethod2() | ||
{ | ||
|
||
var k = ConfigurationManager.AppSettings["dbconfigFile"]; | ||
Assert.AreEqual("SqlDb.config", k); | ||
} | ||
|
||
|
||
|
||
[TestMethod] | ||
public void TestGetsDefaultDatabase() | ||
{ | ||
var dbConf = SqlDbConfigsStatic.DefaultDatabase; | ||
|
||
var langCount = dbConf.GetAllLanguages().Count; | ||
Assert.IsTrue(langCount > 1); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetValueSet() | ||
{ | ||
string vsId = "KOKkommuneregion0000008"; | ||
PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new PCAxis.Sql.ApiUtils.ApiUtil(); | ||
PCAxis.Sql.Models.ValueSet vs = entryPoint.GetValueSet(vsId, "no"); | ||
PCAxis.Sql.Models.ValueSet vs_en = entryPoint.GetValueSet(vsId, "en"); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
[TestMethod] | ||
public void TestGet2ValueSet() | ||
{ | ||
string vsId = "NoSuchVS"; | ||
|
||
PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new PCAxis.Sql.ApiUtils.ApiUtil(); | ||
PCAxis.Sql.Models.ValueSet vs = entryPoint.GetValueSet(vsId, "no"); | ||
|
||
PCAxis.Sql.Models.ValueSet vs_en = entryPoint.GetValueSet(vsId, "en"); | ||
|
||
|
||
} | ||
|
||
[TestMethod] | ||
public void TestGetGrouping() | ||
{ | ||
//string getId = "AldGrupp19Grp5"; | ||
//string getId = "GrkretsBydel2002"; | ||
string getId = "EUlandNY"; | ||
|
||
PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new(); | ||
PCAxis.Sql.Models.Grouping data_no = entryPoint.GetGrouping(getId, "no"); | ||
|
||
PCAxis.Sql.Models.Grouping data_en = entryPoint.GetGrouping(getId, "en"); | ||
|
||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PCAxis.Sql\PCAxis.Sql.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="sqlDb.config"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="testhost.dll.config"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<appSettings> | ||
<add key="TestKey" value="testvalue"/> | ||
<add key="dbconfigFile" value="SqlDb.config"/> | ||
</appSettings> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
using PCAxis.Sql.DbConfig; | ||
using PCAxis.Sql.Models; | ||
using PCAxis.Sql.Repositories; | ||
|
||
namespace PCAxis.Sql.ApiUtils | ||
{ | ||
//For things not found in PXSQLBuilder | ||
//needed by Pxwebapi2 | ||
//returned data should be defined in PCAxis.Sql.Models if complex | ||
public class ApiUtil | ||
{ | ||
readonly List<string> _languagesInDbConfig; | ||
public ApiUtil() | ||
{ | ||
var config = SqlDbConfigsStatic.DefaultDatabase; | ||
//stringCollection --> List<string> | ||
_languagesInDbConfig = config.GetAllLanguages().Cast<string>().ToList(); | ||
} | ||
|
||
//Exceptions ? What if the valueset only exists in another language: Exceptions! | ||
public ValueSet GetValueSet(string name, string language) | ||
{ | ||
//validate input | ||
string valueSetId = ValidateIdString(name); | ||
string languageCode = ValidateLangCodeString(language, _languagesInDbConfig); | ||
|
||
ValueSetRepository mValueSetRepository = new ValueSetRepository(); | ||
return mValueSetRepository.GetValueSet(valueSetId, languageCode); | ||
} | ||
|
||
public Grouping GetGrouping(string name, string language) | ||
{ | ||
//validate input | ||
string groupingId = ValidateIdString(name); | ||
string languageCode = ValidateLangCodeString(language, _languagesInDbConfig); | ||
|
||
GroupingRepository mGroupingRepository = new GroupingRepository(); | ||
return mGroupingRepository.GetGrouping(groupingId, languageCode); | ||
} | ||
|
||
|
||
// liste med tabellid og publ dato | ||
|
||
//GetMenuLookupTables | ||
//GetMenuLookupFolders | ||
/*namespace PxWeb.Code.Api2.DataSource.Cnmm | ||
{ | ||
public static class SqlDbConfigExtensions | ||
{ | ||
public static Dictionary<string, ItemSelection>? GetMenuLookupTables(this SqlDbConfig DB, string language, IOptions<PxApiConfigurationOptions> configOptions) | ||
{ | ||
return GetMenuLookup(DB, language, false, configOptions); | ||
} | ||
public static Dictionary<string, ItemSelection>? GetMenuLookupFolders(this SqlDbConfig DB, string language, IOptions<PxApiConfigurationOptions> configOptions) | ||
{ | ||
return GetMenuLookup(DB, language, true, configOptions); | ||
} | ||
private static Dictionary<string, ItemSelection>? GetMenuLookup(this SqlDbConfig DB, string language, bool folders, IOptions<PxApiConfigurationOptions> configOptions) | ||
{ | ||
// Check language to avoid SQL injection | ||
if (!configOptions.Value.Languages.Any(l => l.Id == language)) | ||
{ | ||
throw new ArgumentException($"Illegal language {language}"); | ||
} | ||
*/ | ||
|
||
//dump to pxfile ? | ||
|
||
private static string ValidateLangCodeString(string input, List<string> languagesInDbConfig) | ||
{ | ||
if (input == null) | ||
{ | ||
throw new ArgumentException("The language cannot be null."); | ||
} | ||
if (!languagesInDbConfig.Contains(input)) | ||
{ | ||
throw new ArgumentException("Cant find language in config."); | ||
} | ||
|
||
return input; | ||
|
||
} | ||
private static string ValidateIdString(string input) | ||
{ | ||
if (input == null) | ||
{ | ||
throw new ArgumentException("The id string cannot be null."); | ||
} | ||
|
||
if (!Regex.IsMatch(input, @"^[\w\t \-:.]+$", RegexOptions.None, TimeSpan.FromSeconds(2))) | ||
{ | ||
throw new ArgumentException("The string contains invalid characters. Only letters, digits, underscores, tabs, spaces, hyphens, colons and periods are allowed."); | ||
} | ||
return input; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.