From 757d86225b333ff2dc8ca8fb781560f12952f64d Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 5 Dec 2024 22:20:11 +0700 Subject: [PATCH 01/14] impl mongo db --- .../MongoDbDataSourceDashboard.cs | 74 ++++++++++ e2e/Sandbox/Reveal/AuthenticationProvider.cs | 5 + e2e/Sandbox/Reveal/DataSourceProvider.cs | 11 ++ .../Dashboards/TestMongoDb.rdash | Bin 0 -> 1478 bytes .../MongoDbDataSourceItemFixture.cs | 83 +++++++++++- .../DataSources/MongoDbDataSourceFixture.cs | 128 ++++++++++++++++++ .../MongoDbDataSourceItemFixture.cs | 67 --------- .../Reveal.Sdk.Dom.Tests.csproj | 7 + .../DataSourceItems/MongoDbDataSourceItem.cs | 6 +- .../Data/DataSources/MongoDBDataSource.cs | 9 +- 10 files changed, 311 insertions(+), 79 deletions(-) create mode 100644 e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs create mode 100644 src/Reveal.Sdk.Dom.Tests/Dashboards/TestMongoDb.rdash create mode 100644 src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs delete mode 100644 src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceItemFixture.cs diff --git a/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs b/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs new file mode 100644 index 00000000..9d3f9b79 --- /dev/null +++ b/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs @@ -0,0 +1,74 @@ +using Reveal.Sdk.Dom; +using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Visualizations; +using System.Collections.Generic; + +namespace Sandbox.Factories +{ + internal class MongoDbDataSourceDashboard + { + internal static RdashDocument CreateDashboard() + { + var mongoDbDs = new MongoDBDataSource() + { + Id = "MyMongoDatasource", + Title = "MyMongoDatasource", + Subtitle = "My MongoDB", + ProcessDataOnServerDefaultValue = true, + ProcessDataOnServerReadOnly = false, + }; + + var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) + { + Id = "MyMongoDatasourceItem", + Title = "MyMongoDatasourceItem", + Subtitle = "Test Collection", + Collection = "data", + Fields = new List + { + new TextField("_id"), + new TextField("name"), + new NumberField("price"), + new DateTimeField("available_since"), + new TextField("category"), + new NumberField("year_value"), + new NumberField("month_value"), + new NumberField("day_value"), + new NumberField("hour_value"), + new NumberField("minutes_value"), + new NumberField("seconds_value"), + new NumberField("milliseconds_value"), + new NumberField("numeric_value1"), + new NumberField("numeric_value2"), + new NumberField("numeric_value3"), + new TextField("image_url") + + } + }; + + var document = new RdashDocument("My Dashboard"); + + document.Visualizations.Add(new GridVisualization("Test List", testCollection).SetColumns("name", "category", "price")); + + var jsonData = document.ToJsonString(); + + // var filePath = "test.rdash"; + + // try + // { + + // if (File.Exists(filePath)) + // File.Delete(filePath); + + // document.Save(filePath); + // } + // catch + // { + // throw; + // } + + + return document; + } + } +} diff --git a/e2e/Sandbox/Reveal/AuthenticationProvider.cs b/e2e/Sandbox/Reveal/AuthenticationProvider.cs index cf83f9ab..afea3670 100644 --- a/e2e/Sandbox/Reveal/AuthenticationProvider.cs +++ b/e2e/Sandbox/Reveal/AuthenticationProvider.cs @@ -7,6 +7,7 @@ using Reveal.Sdk.Data.MySql; using Reveal.Sdk.Data.PostgreSQL; using Reveal.Sdk.Data.Oracle; +using Reveal.Sdk.Data.MongoDB; using System.Threading.Tasks; using Reveal.Sdk.Data.Amazon.Redshift; using Reveal.Sdk.Data.Google.Analytics4; @@ -51,6 +52,10 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour userCredential = new RVBearerTokenDataSourceCredential(_token, null); } + else if (dataSource is RVMongoDBDataSource) + { + userCredential = new RVUsernamePasswordDataSourceCredential("user01", "*****", "admin"); + } else if (dataSource is RVSnowflakeDataSource) { userCredential = new RVUsernamePasswordDataSourceCredential("snow-flake-username", "snow-flake-password"); diff --git a/e2e/Sandbox/Reveal/DataSourceProvider.cs b/e2e/Sandbox/Reveal/DataSourceProvider.cs index 765f5518..a0c8f92a 100644 --- a/e2e/Sandbox/Reveal/DataSourceProvider.cs +++ b/e2e/Sandbox/Reveal/DataSourceProvider.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Reveal.Sdk.Data.Excel; using Reveal.Sdk.Data.Microsoft.AnalysisServices; +using Reveal.Sdk.Data.MongoDB; namespace Sandbox.RevealSDK { @@ -60,6 +61,16 @@ public Task ChangeDataSourceItemAsync(RVDataSourceItem dataSou // ds.Database = "Northwind"; //} + if (dataSourceItem is RVMongoDBDataSourceItem mongoDbDataSourceItem) + { + mongoDbDataSourceItem.Collection = "data"; + + var ds = mongoDbDataSourceItem.DataSource as RVMongoDBDataSource; + + ds.ConnectionString = "mongodb+srv://user01:*******@cluster0.ta2xrrt.mongodb.net/"; + ds.Database = "test"; + } + return Task.FromResult(dataSourceItem); } } diff --git a/src/Reveal.Sdk.Dom.Tests/Dashboards/TestMongoDb.rdash b/src/Reveal.Sdk.Dom.Tests/Dashboards/TestMongoDb.rdash new file mode 100644 index 0000000000000000000000000000000000000000..ff3e44926490013aa190c7b150f9c7700b289bb7 GIT binary patch literal 1478 zcmV;%1v&aqO9KQH000080Cul@S?!teDo6zY01Gbw01f~E07PMPXku?+a%3)Qb8l|k zT3c`1HWYpzu>W8Pdu)OXS#EoCe;N#e85RVB-v_}htOg^*pF@gRd=|XZ)+@>qBp!irOjC(E zDlUKGFw6zLMr)c2d0DU=3d;;Fcx$xHV4rck0HMrZ)bGLIwfIk(p2RxSQ52Y&`t@#W zoy5u%hUcdjXUFJZesOw!@Y}`F$-%`5x;QxhHI7e?&rZYh@W^m_psQ(yRM5vq)zwQ% z@+9prOt@y^5px`BLbh|#<}FPLU1H$IeMU&2X$FxrZs%jpXokX^AXdWaQ!D*x&LWry zNa~}QmNmU6Mat)-8(d)^v61t|zviSh>Q+XgCjNvsPfQbVjY1$+giZ?y zm%w2OP&xQr0$KA`;1k6`gY_xTJF7+i;xNzLhQIooHt8Z(+_ zDniI)3o0n0Nda>gKy*3)$oQoUfmblmnhE=$@JV|4j99tXtApSE(*#awf>Ygl_snCb!}m|;f0ZBsFRu5X6X{FQBZ!MUi+j8Q zoJe0NSp#x4&buMLJSsSe!$Y-$?^ww?7fz&Clq{*}_{7q|hy~~sA%BCucNPo=?~C*a%J)Hj;1*S z{@j-z3)nkiB8CNy_N+`joO~r}|H{Q!*#`YXnRhyQoR zR6o8NZb2pXs)SuNz{ea%e_dg+k(AYq!#I`+LsLYw%B>Ah;xfxiF?UtB-214Qj3-_%N&kZNPhzpDAFL4>6{2Ht?xh$_7mK+*XxFFF)jDeD;)yoo zw{~fCh3k|@TU5KM&@JVYs5Az_&E4(zZuH);j)2P-S+F)NB{r; literal 0 HcmV?d00001 diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/MongoDbDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/MongoDbDataSourceItemFixture.cs index 9f34a02c..0cb0dd1b 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/MongoDbDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/MongoDbDataSourceItemFixture.cs @@ -1,6 +1,11 @@ using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Data; +using System.IO; +using System; using Xunit; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using Reveal.Sdk.Dom.Visualizations; namespace Reveal.Sdk.Dom.Tests.Data.DataSourceItems { @@ -33,7 +38,7 @@ public void Collection_SetsAndGetsValue_WithInputs() // Assert Assert.Equal("TestCollection", item.Collection); - Assert.Equal("TestCollection", item.Properties.GetValue("Collection")); + Assert.Equal("TestCollection", item.Properties.GetValue("Table")); } [Fact] @@ -49,5 +54,81 @@ public void ProcessDataOnServer_SetsAndGetsValue_WithInputs() Assert.True(item.ProcessDataOnServer); Assert.True(item.Properties.GetValue("ServerAggregation")); } + + [Fact] + public void RDashDocument_HasCorrectDataSourceItem_WhenLoadFromFile() + { + // Arrange + var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestMongoDb.rdash"); + + // Act + var document = RdashDocument.Load(filePath); + var dataSource = document.DataSources[0]; + var dataSourceItem = document.Visualizations[0].DataDefinition.DataSourceItem; + + // Assert + Assert.Equal(dataSource.Id, dataSourceItem.DataSourceId); + Assert.Equal(DataSourceProvider.MongoDB, dataSource.Provider); + Assert.NotNull(dataSourceItem.Properties.GetValue("Table")); + Assert.True(dataSourceItem.Properties.GetValue("ServerAggregation")); + } + + [Fact] + public void RDashDocument_ProduceAsExpected_WhenExportJson() + { + // Arrange + var mongoDbDs = new MongoDBDataSource() + { + Id = "MyMongoDatasource", + Title = "MyMongoDatasource", + Subtitle = "My MongoDB", + ProcessDataOnServerDefaultValue = true, + ProcessDataOnServerReadOnly = false, + }; + + var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) + { + Id = "MyMongoDatasourceItem", + Title = "MyMongoDatasourceItem", + Subtitle = "Test Collection", + Collection = "data", + Fields = new List + { + new TextField("_id"), + new TextField("name"), + } + }; + + var document = new RdashDocument("My Dashboard"); + + document.Visualizations.Add(new GridVisualization("Test List", testCollection).SetColumns("name")); + + var expectedDsJson = """ + { + "_type": "DataSourceItemType", + "Id": "MyMongoDatasourceItem", + "Title": "MyMongoDatasourceItem", + "Subtitle": "Test Collection", + "DataSourceId": "MyMongoDatasource", + "HasTabularData": true, + "HasAsset": false, + "Properties": { + "Collection": "data" + }, + "Parameters": {} + } + """; + + // Act + var jsonData = document.ToJsonString(); + var jObject = JObject.Parse(jsonData); + var datasourceJson = jObject["Widgets"][0]["DataSpec"]["DataSourceItem"]; + + // Deserialize JSON strings to JObjects to make comparing them easier + var expectedJObject = JObject.Parse(expectedDsJson); + + // Assert + Assert.Equal(expectedJObject, datasourceJson); + } } } \ No newline at end of file diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs new file mode 100644 index 00000000..f19bb3c7 --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs @@ -0,0 +1,128 @@ +using Newtonsoft.Json.Linq; +using Reveal.Sdk.Dom.Core.Extensions; +using Reveal.Sdk.Dom.Data; +using System.Collections.Generic; +using System.IO; +using System; +using Xunit; +using Reveal.Sdk.Dom.Visualizations; + +namespace Reveal.Sdk.Dom.Tests.Data.DataSources +{ + public class MongoDbDataSourceFixture + { + [Fact] + public void Constructor_SetsProviderToMongoDB_WhenConstructed() + { + // Act + var dataSource = new MongoDBDataSource(); + + // Assert + Assert.Equal(DataSourceProvider.MongoDB, dataSource.Provider); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ProcessDataOnServerDefaultValue_SetsAndGetsValue_WithDifferentInputs(bool defaultValue) + { + // Arrange + var dataSource = new MongoDBDataSource(); + + // Act + dataSource.ProcessDataOnServerDefaultValue = defaultValue; + + // Assert + Assert.Equal(defaultValue, dataSource.ProcessDataOnServerDefaultValue); + Assert.Equal(defaultValue, dataSource.Properties.GetValue("ServerAggregationDefault")); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ProcessDataOnServerReadOnly_SetsAndGetsValue_WithDifferentInputs(bool readOnlyValue) + { + // Arrange + var dataSource = new MongoDBDataSource(); + + // Act + dataSource.ProcessDataOnServerReadOnly = readOnlyValue; + + // Assert + Assert.Equal(readOnlyValue, dataSource.ProcessDataOnServerReadOnly); + Assert.Equal(readOnlyValue, dataSource.Properties.GetValue("ServerAggregationReadOnly")); + } + + [Fact] + public void RDashDocument_HasCorrectDataSource_WhenLoadFromFile() + { + // Arrange + var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestMongoDb.rdash"); + + // Act + var document = RdashDocument.Load(filePath); + var dataSource = document.DataSources[0]; + + // Assert + Assert.Equal(DataSourceProvider.MongoDB, dataSource.Provider); + Assert.NotNull(dataSource.Properties.GetValue("ServerAggregationDefault")); + Assert.NotNull(dataSource.Properties.GetValue("ServerAggregationReadOnly")); + } + + [Fact] + public void RDashDocument_ProduceAsExpected_WhenExportJson() + { + // Arrange + var mongoDbDs = new MongoDBDataSource() + { + Id = "MyMongoDatasource", + Title = "MyMongoDatasource", + Subtitle = "My MongoDB", + ProcessDataOnServerDefaultValue = true, + ProcessDataOnServerReadOnly = false, + }; + + var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) + { + Id = "MyMongoDatasourceItem", + Title = "MyMongoDatasourceItem", + Subtitle = "Test Collection", + Collection = "data", + Fields = new List + { + new TextField("_id"), + new TextField("name"), + } + }; + + var document = new RdashDocument("My Dashboard"); + + document.Visualizations.Add(new GridVisualization("Test List", testCollection).SetColumns("name")); + + var expectedDsJson = """ + { + "_type": "DataSourceType", + "Id": "MyMongoDatasource", + "Provider": "MONGODB", + "Description": "MyMongoDatasource", + "Subtitle": "My MongoDB", + "Properties": { + "ServerAggregationDefault": true, + "ServerAggregationReadOnly": false + } + } + """; + + // Act + var jsonData = document.ToJsonString(); + var jObject = JObject.Parse(jsonData); + var datasourceJson = jObject["DataSources"][0]; + + // Deserialize JSON strings to JObjects to make comparing them easier + var expectedJObject = JObject.Parse(expectedDsJson); + + // Assert + Assert.Equal(expectedJObject, datasourceJson); + } + } +} \ No newline at end of file diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceItemFixture.cs deleted file mode 100644 index 21c6c821..00000000 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceItemFixture.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Reveal.Sdk.Dom.Core.Extensions; -using Reveal.Sdk.Dom.Data; -using Xunit; - -namespace Reveal.Sdk.Dom.Tests.Data.DataSources -{ - public class MongoDbDataSourceItemFixture - { - [Fact] - public void Constructor_SetsProviderToMongoDB_WhenConstructed() - { - // Act - var dataSource = new MongoDBDataSource(); - - // Assert - Assert.Equal(DataSourceProvider.MongoDB, dataSource.Provider); - } - - [Theory] - [InlineData("mongodb://localhost:27017")] - [InlineData(null)] - public void ConnectionString_SetsAndGetsValue_WithDifferentInputs(string connectionString) - { - // Arrange - var dataSource = new MongoDBDataSource(); - - // Act - dataSource.ConnectionString = connectionString; - - // Assert - Assert.Equal(connectionString, dataSource.ConnectionString); - Assert.Equal(connectionString, dataSource.Properties.GetValue("ConnectionString")); - } - - [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessDataOnServerDefaultValue_SetsAndGetsValue_WithDifferentInputs(bool defaultValue) - { - // Arrange - var dataSource = new MongoDBDataSource(); - - // Act - dataSource.ProcessDataOnServerDefaultValue = defaultValue; - - // Assert - Assert.Equal(defaultValue, dataSource.ProcessDataOnServerDefaultValue); - Assert.Equal(defaultValue, dataSource.Properties.GetValue("ServerAggregationDefault")); - } - - [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessDataOnServerReadOnly_SetsAndGetsValue_WithDifferentInputs(bool readOnlyValue) - { - // Arrange - var dataSource = new MongoDBDataSource(); - - // Act - dataSource.ProcessDataOnServerReadOnly = readOnlyValue; - - // Assert - Assert.Equal(readOnlyValue, dataSource.ProcessDataOnServerReadOnly); - Assert.Equal(readOnlyValue, dataSource.Properties.GetValue("ServerAggregationReadOnly")); - } - } -} \ No newline at end of file diff --git a/src/Reveal.Sdk.Dom.Tests/Reveal.Sdk.Dom.Tests.csproj b/src/Reveal.Sdk.Dom.Tests/Reveal.Sdk.Dom.Tests.csproj index 551d874d..8ae80127 100644 --- a/src/Reveal.Sdk.Dom.Tests/Reveal.Sdk.Dom.Tests.csproj +++ b/src/Reveal.Sdk.Dom.Tests/Reveal.Sdk.Dom.Tests.csproj @@ -51,6 +51,13 @@ PreserveNewest + + Always + + + + + diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/MongoDbDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/MongoDbDataSourceItem.cs index 0ae51b64..63b29b3b 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/MongoDbDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/MongoDbDataSourceItem.cs @@ -3,7 +3,7 @@ namespace Reveal.Sdk.Dom.Data { - internal class MongoDbDataSourceItem : DataSourceItem, IProcessDataOnServer + public class MongoDbDataSourceItem : DataSourceItem, IProcessDataOnServer { public MongoDbDataSourceItem(string title, DataSource dataSource) : base(title, dataSource) @@ -12,8 +12,8 @@ public MongoDbDataSourceItem(string title, DataSource dataSource) : [JsonIgnore] public string Collection { - get => Properties.GetValue("Collection"); - set => Properties.SetItem("Collection", value); + get => Properties.GetValue("Table"); + set => Properties.SetItem("Table", value); } [JsonIgnore] diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs index 0fb4fc49..5a07419c 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs @@ -3,20 +3,13 @@ namespace Reveal.Sdk.Dom.Data { - internal class MongoDBDataSource : DatabaseDataSource + public class MongoDBDataSource : DataSource { public MongoDBDataSource() { Provider = DataSourceProvider.MongoDB; } - [JsonIgnore] - public string ConnectionString - { - get => Properties.GetValue("ConnectionString"); - set => Properties.SetItem("ConnectionString", value); - } - [JsonIgnore] public bool ProcessDataOnServerDefaultValue { From 86244ce067373f2a6731fde031a4f316f94d4933 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 10 Dec 2024 16:21:24 +0700 Subject: [PATCH 02/14] readd connection string and database field in MongoDB ds --- .../MongoDbDataSourceDashboard.cs | 4 ++- e2e/Sandbox/Reveal/DataSourceProvider.cs | 14 ++++---- .../DataSources/MongoDbDataSourceFixture.cs | 32 +++++++++++++++++++ .../Data/DataSources/MongoDBDataSource.cs | 14 ++++++++ .../Data/Enums/DataSourceType.cs | 1 + 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs b/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs index 9d3f9b79..8e2f0ffa 100644 --- a/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs +++ b/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs @@ -1,4 +1,4 @@ -using Reveal.Sdk.Dom; +using Reveal.Sdk.Dom; using Reveal.Sdk.Dom.Data; using Reveal.Sdk.Dom.Visualizations; using System.Collections.Generic; @@ -16,6 +16,8 @@ internal static RdashDocument CreateDashboard() Subtitle = "My MongoDB", ProcessDataOnServerDefaultValue = true, ProcessDataOnServerReadOnly = false, + ConnectionString = "mongodb+srv://cluster0.ta2xrrt.mongodb.net", + Database = "test" }; var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) diff --git a/e2e/Sandbox/Reveal/DataSourceProvider.cs b/e2e/Sandbox/Reveal/DataSourceProvider.cs index a0c8f92a..3a985542 100644 --- a/e2e/Sandbox/Reveal/DataSourceProvider.cs +++ b/e2e/Sandbox/Reveal/DataSourceProvider.cs @@ -61,15 +61,15 @@ public Task ChangeDataSourceItemAsync(RVDataSourceItem dataSou // ds.Database = "Northwind"; //} - if (dataSourceItem is RVMongoDBDataSourceItem mongoDbDataSourceItem) - { - mongoDbDataSourceItem.Collection = "data"; + //if (dataSourceItem is RVMongoDBDataSourceItem mongoDbDataSourceItem) + //{ + // mongoDbDataSourceItem.Collection = "data"; - var ds = mongoDbDataSourceItem.DataSource as RVMongoDBDataSource; + // var ds = mongoDbDataSourceItem.DataSource as RVMongoDBDataSource; - ds.ConnectionString = "mongodb+srv://user01:*******@cluster0.ta2xrrt.mongodb.net/"; - ds.Database = "test"; - } + // ds.ConnectionString = "mongodb+srv://user01:*******@cluster0.ta2xrrt.mongodb.net/"; + // ds.Database = "test"; + //} return Task.FromResult(dataSourceItem); } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs index f19bb3c7..7baca001 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs @@ -21,6 +21,38 @@ public void Constructor_SetsProviderToMongoDB_WhenConstructed() Assert.Equal(DataSourceProvider.MongoDB, dataSource.Provider); } + [Theory] + [InlineData("mongodb://localhost:27017")] + [InlineData(null)] + public void ConnectionString_SetsAndGetsValue_WithDifferentInputs(string connectionString) + { + // Arrange + var dataSource = new MongoDBDataSource(); + + // Act + dataSource.ConnectionString = connectionString; + + // Assert + Assert.Equal(connectionString, dataSource.ConnectionString); + Assert.Equal(connectionString, dataSource.Properties.GetValue("ConnectionString")); + } + + [Theory] + [InlineData("test")] + [InlineData(null)] + public void Database_SetsAndGetsValue_WithDifferentInputs(string dbName) + { + // Arrange + var dataSource = new MongoDBDataSource(); + + // Act + dataSource.Database = dbName; + + // Assert + Assert.Equal(dbName, dataSource.Database); + Assert.Equal(dbName, dataSource.Properties.GetValue("Database")); + } + [Theory] [InlineData(true)] [InlineData(false)] diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs index 5a07419c..604ad4f1 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs @@ -10,6 +10,20 @@ public MongoDBDataSource() Provider = DataSourceProvider.MongoDB; } + [JsonIgnore] + public string ConnectionString + { + get => Properties.GetValue("ConnectionString"); + set => Properties.SetItem("ConnectionString", value); + } + + [JsonIgnore] + public string Database + { + get => Properties.GetValue("Database"); + set => Properties.SetItem("Database", value); + } + [JsonIgnore] public bool ProcessDataOnServerDefaultValue { diff --git a/src/Reveal.Sdk.Dom/Data/Enums/DataSourceType.cs b/src/Reveal.Sdk.Dom/Data/Enums/DataSourceType.cs index 06822a5d..7b0e2dfe 100644 --- a/src/Reveal.Sdk.Dom/Data/Enums/DataSourceType.cs +++ b/src/Reveal.Sdk.Dom/Data/Enums/DataSourceType.cs @@ -7,5 +7,6 @@ public enum DataSourceType MySql, Oracle, PostgreSQL, + MongoDB, } } From 001414eb4973181298a651e2f28b3a6629744696 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 12 Dec 2024 16:24:34 +0700 Subject: [PATCH 03/14] apply new dashboard creator --- .../DashboardCreators/MongoDashboard.cs | 65 +++++++++++++++- .../MongoDbDataSourceDashboard.cs | 76 ------------------- 2 files changed, 64 insertions(+), 77 deletions(-) delete mode 100644 e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs diff --git a/e2e/Sandbox/DashboardCreators/MongoDashboard.cs b/e2e/Sandbox/DashboardCreators/MongoDashboard.cs index 27256a45..c6b4cfc3 100644 --- a/e2e/Sandbox/DashboardCreators/MongoDashboard.cs +++ b/e2e/Sandbox/DashboardCreators/MongoDashboard.cs @@ -1,4 +1,6 @@ using Reveal.Sdk.Dom; +using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Visualizations; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +15,68 @@ internal class MongoDashboard : IDashboardCreator public RdashDocument CreateDashboard() { - throw new NotImplementedException(); + var mongoDbDs = new MongoDBDataSource() + { + Id = "MyMongoDatasource", + Title = "MyMongoDatasource", + Subtitle = "My MongoDB", + ProcessDataOnServerDefaultValue = true, + ProcessDataOnServerReadOnly = false, + ConnectionString = "mongodb+srv://cluster0.ta2xrrt.mongodb.net", + Database = "test" + }; + + var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) + { + Id = "MyMongoDatasourceItem", + Title = "MyMongoDatasourceItem", + Subtitle = "Test Collection", + Collection = "data", + Fields = new List + { + new TextField("_id"), + new TextField("name"), + new NumberField("price"), + new DateTimeField("available_since"), + new TextField("category"), + new NumberField("year_value"), + new NumberField("month_value"), + new NumberField("day_value"), + new NumberField("hour_value"), + new NumberField("minutes_value"), + new NumberField("seconds_value"), + new NumberField("milliseconds_value"), + new NumberField("numeric_value1"), + new NumberField("numeric_value2"), + new NumberField("numeric_value3"), + new TextField("image_url") + + } + }; + + var document = new RdashDocument("My Dashboard"); + + document.Visualizations.Add(new GridVisualization("Test List", testCollection).SetColumns("name", "category", "price")); + + var jsonData = document.ToJsonString(); + + // var filePath = "test.rdash"; + + // try + // { + + // if (File.Exists(filePath)) + // File.Delete(filePath); + + // document.Save(filePath); + // } + // catch + // { + // throw; + // } + + + return document; } } } diff --git a/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs b/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs deleted file mode 100644 index 8e2f0ffa..00000000 --- a/e2e/Sandbox/DashboardCreators/MongoDbDataSourceDashboard.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Reveal.Sdk.Dom; -using Reveal.Sdk.Dom.Data; -using Reveal.Sdk.Dom.Visualizations; -using System.Collections.Generic; - -namespace Sandbox.Factories -{ - internal class MongoDbDataSourceDashboard - { - internal static RdashDocument CreateDashboard() - { - var mongoDbDs = new MongoDBDataSource() - { - Id = "MyMongoDatasource", - Title = "MyMongoDatasource", - Subtitle = "My MongoDB", - ProcessDataOnServerDefaultValue = true, - ProcessDataOnServerReadOnly = false, - ConnectionString = "mongodb+srv://cluster0.ta2xrrt.mongodb.net", - Database = "test" - }; - - var testCollection = new MongoDbDataSourceItem("DB Test", mongoDbDs) - { - Id = "MyMongoDatasourceItem", - Title = "MyMongoDatasourceItem", - Subtitle = "Test Collection", - Collection = "data", - Fields = new List - { - new TextField("_id"), - new TextField("name"), - new NumberField("price"), - new DateTimeField("available_since"), - new TextField("category"), - new NumberField("year_value"), - new NumberField("month_value"), - new NumberField("day_value"), - new NumberField("hour_value"), - new NumberField("minutes_value"), - new NumberField("seconds_value"), - new NumberField("milliseconds_value"), - new NumberField("numeric_value1"), - new NumberField("numeric_value2"), - new NumberField("numeric_value3"), - new TextField("image_url") - - } - }; - - var document = new RdashDocument("My Dashboard"); - - document.Visualizations.Add(new GridVisualization("Test List", testCollection).SetColumns("name", "category", "price")); - - var jsonData = document.ToJsonString(); - - // var filePath = "test.rdash"; - - // try - // { - - // if (File.Exists(filePath)) - // File.Delete(filePath); - - // document.Save(filePath); - // } - // catch - // { - // throw; - // } - - - return document; - } - } -} From df30fe52457d9a5ff3962ba9e341dea056300286 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 12 Dec 2024 19:07:01 +0700 Subject: [PATCH 04/14] fix failed test --- e2e/Sandbox/Reveal/AuthenticationProvider.cs | 3 +++ .../DataSources/MongoDbDataSourceFixture.cs | 17 +++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/e2e/Sandbox/Reveal/AuthenticationProvider.cs b/e2e/Sandbox/Reveal/AuthenticationProvider.cs index afea3670..492e34c7 100644 --- a/e2e/Sandbox/Reveal/AuthenticationProvider.cs +++ b/e2e/Sandbox/Reveal/AuthenticationProvider.cs @@ -18,11 +18,14 @@ using Reveal.Sdk.Data.MySql; using Reveal.Sdk.Data.OData; using Reveal.Sdk.Data.Oracle; +using Reveal.Sdk.Data.Snowflake; +using Reveal.Sdk.Data.PostgreSQL; using Reveal.Sdk.Data.Microsoft.SynapseAnalytics; using Google.Apis.Auth.OAuth2; using System.IO; using System.Text; + namespace Sandbox.RevealSDK { internal class AuthenticationProvider : IRVAuthenticationProvider diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs index 7baca001..c6b14d06 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs @@ -133,15 +133,16 @@ public void RDashDocument_ProduceAsExpected_WhenExportJson() var expectedDsJson = """ { - "_type": "DataSourceType", - "Id": "MyMongoDatasource", - "Provider": "MONGODB", - "Description": "MyMongoDatasource", - "Subtitle": "My MongoDB", - "Properties": { + "_type": "DataSourceType", + "Id": "MyMongoDatasource", + "Provider": "MONGODB", + "Description": "MyMongoDatasource", + "Subtitle": "My MongoDB", + "Properties": { "ServerAggregationDefault": true, "ServerAggregationReadOnly": false - } + }, + "Settings": {} } """; @@ -151,7 +152,7 @@ public void RDashDocument_ProduceAsExpected_WhenExportJson() var datasourceJson = jObject["DataSources"][0]; // Deserialize JSON strings to JObjects to make comparing them easier - var expectedJObject = JObject.Parse(expectedDsJson); + var expectedJObject = JObject.Parse(expectedDsJson); // Assert Assert.Equal(expectedJObject, datasourceJson); From 90ca6ebde8e7e29818e0af66f8ad50e6163fb1e8 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 13 Dec 2024 10:39:50 +0700 Subject: [PATCH 05/14] revert change base mongodb --- .../Data/DataSources/MongoDbDataSourceFixture.cs | 16 ---------------- .../Data/DataSources/MongoDBDataSource.cs | 9 +-------- 2 files changed, 1 insertion(+), 24 deletions(-) diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs index c6b14d06..ed992e1b 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs @@ -37,22 +37,6 @@ public void ConnectionString_SetsAndGetsValue_WithDifferentInputs(string connect Assert.Equal(connectionString, dataSource.Properties.GetValue("ConnectionString")); } - [Theory] - [InlineData("test")] - [InlineData(null)] - public void Database_SetsAndGetsValue_WithDifferentInputs(string dbName) - { - // Arrange - var dataSource = new MongoDBDataSource(); - - // Act - dataSource.Database = dbName; - - // Assert - Assert.Equal(dbName, dataSource.Database); - Assert.Equal(dbName, dataSource.Properties.GetValue("Database")); - } - [Theory] [InlineData(true)] [InlineData(false)] diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs index 604ad4f1..96caecc4 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSources/MongoDBDataSource.cs @@ -3,7 +3,7 @@ namespace Reveal.Sdk.Dom.Data { - public class MongoDBDataSource : DataSource + public class MongoDBDataSource : DatabaseDataSource { public MongoDBDataSource() { @@ -17,13 +17,6 @@ public string ConnectionString set => Properties.SetItem("ConnectionString", value); } - [JsonIgnore] - public string Database - { - get => Properties.GetValue("Database"); - set => Properties.SetItem("Database", value); - } - [JsonIgnore] public bool ProcessDataOnServerDefaultValue { From a70f2095f0315cd23d1246184dbdfa57d36b397e Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 13 Dec 2024 15:30:48 +0700 Subject: [PATCH 06/14] Update Google Big Query Data Source and Data Source Item --- .../GoogleBigQueryDashboard.cs | 32 +++++++- e2e/Sandbox/Reveal/AuthenticationProvider.cs | 12 ++- .../GoogleBigQueryDataSourceItemFixture.cs | 77 ++++++++++++++++--- .../GoogleBigQueryDataSourceFixture.cs | 6 +- .../GoogleBigQueryDataSourceItem.cs | 26 ++++++- .../DataSources/GoogleBigQueryDataSource.cs | 6 +- 6 files changed, 138 insertions(+), 21 deletions(-) diff --git a/e2e/Sandbox/DashboardCreators/GoogleBigQueryDashboard.cs b/e2e/Sandbox/DashboardCreators/GoogleBigQueryDashboard.cs index 31e4b580..39adc36f 100644 --- a/e2e/Sandbox/DashboardCreators/GoogleBigQueryDashboard.cs +++ b/e2e/Sandbox/DashboardCreators/GoogleBigQueryDashboard.cs @@ -1,4 +1,7 @@ -using Reveal.Sdk.Dom; +using AngleSharp.Dom; +using Reveal.Sdk.Dom; +using Reveal.Sdk.Dom.Data; +using Reveal.Sdk.Dom.Visualizations; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +16,32 @@ internal class GoogleBigQueryDashboard : IDashboardCreator public RdashDocument CreateDashboard() { - throw new NotImplementedException(); + var document = new RdashDocument(); + + var dataSource = new GoogleBigQueryDataSource() + { + Id = "BigQueryDSId", + Title = "Google BigQuery DS", + Subtitle = "Public Gg BigQuery", + ProjectId = "bigquery-public-data" + }; + var dataSourceItem = new GoogleBigQueryDataSourceItem("Google BigQuery DS Item", dataSource) + { + Id = "BigQueryDSItemId", + Title = "Google", + Subtitle = "Ameria Health rankings", + DataSetId = "america_health_rankings", + Table = "ahr", + ProjectId = "bigquery-public-data", + Fields = new List { + new NumberField("edition"), + new TextField("state_name") + } + }; + + document.Visualizations.Add(new GridVisualization("List edition states", dataSourceItem).SetColumns("edition", "state_name")); + + return document; } } } diff --git a/e2e/Sandbox/Reveal/AuthenticationProvider.cs b/e2e/Sandbox/Reveal/AuthenticationProvider.cs index 492e34c7..87618830 100644 --- a/e2e/Sandbox/Reveal/AuthenticationProvider.cs +++ b/e2e/Sandbox/Reveal/AuthenticationProvider.cs @@ -85,7 +85,9 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour } else if (dataSource is RVBigQueryDataSource) { - userCredential = new RVBearerTokenDataSourceCredential("token", null); + var token_task = CreateJwtToken(); + var token = token_task.Result; + userCredential = new RVBearerTokenDataSourceCredential(token, null); } else if (dataSource is RVGoogleDriveDataSource) { @@ -161,5 +163,13 @@ internal string RetrieveGoogleDriveBearerToken() return accessToken; } + + async Task CreateJwtToken() + { + var credentials = GoogleCredential.FromJson("credential-json"); + + var token = await credentials.UnderlyingCredential.GetAccessTokenForRequestAsync("https://bigquery.googleapis.com/").ConfigureAwait(false); + return token; + } } } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs index ea7e0b81..a9dc78b4 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Data; using Xunit; @@ -12,22 +13,65 @@ namespace Reveal.Sdk.Dom.Tests.Data.DataSourceItems public class GoogleBigQueryDataSourceItemFixture { [Fact] - public void Constructor_SetsTitleAndDatasource_AsProvided() + public void Constructor_CreateGGBigQueryDSItem_WithTitle() { // Arrange - var dataSource = new GoogleBigQueryDataSource(); - var title = "Test title"; + var title = "GG BigQuery DS Title"; // Act - var dataSourceItem = new GoogleBigQueryDataSourceItem(title, dataSource); + var dataSourceItem = new GoogleBigQueryDataSourceItem(title); // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); Assert.Equal(title, dataSourceItem.Title); - Assert.Equal(dataSource, dataSourceItem.DataSource); + Assert.NotNull(dataSourceItem.DataSource); + Assert.Equal(title, dataSourceItem.DataSource.Title); + Assert.IsType(dataSourceItem.DataSource); + } + + [Theory] + [InlineData("DS Title", "DS Title", "DS Item Title", "DS Item Title")] + [InlineData(null, "DS Item Title", "DS Item Title", "DS Item Title")] + public void Constructor_CreateGGBigQueryDSItem_WithTitleAndDatSource(string dsTitle, string expectedDSTitle, string dsItemTitle, string expectedDSItemTitle) + { + // Arrange + var dataSource = new DataSource() { Title = dsTitle }; + + // Act + var dataSourceItem = new GoogleBigQueryDataSourceItem(dsItemTitle, dataSource); + + // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); + Assert.Equal(expectedDSTitle, dataSourceItem.DataSource.Title); + Assert.Equal(expectedDSItemTitle, dataSourceItem.Title); + Assert.Equal(dataSource.Id, dataSourceItem.DataSource.Id); + Assert.Equal(dataSource.Id, dataSourceItem.DataSourceId); + Assert.NotSame(dataSource, dataSourceItem.DataSource); + Assert.IsType(dataSourceItem.DataSource); + } + + [Theory] + [InlineData("DS Title", "DS Title", "DS Item Title", "DS Item Title")] + [InlineData(null, "DS Item Title", "DS Item Title", "DS Item Title")] + public void Constructor_CreateGGBigQueryDSItem_WithTitleAndGGBigQueryDatSource(string dsTitle, string expectedDSTitle, string dsItemTitle, string expectedDSItemTitle) + { + // Arrange + var dataSource = new GoogleBigQueryDataSource() { Title = dsTitle }; + + // Act + var dataSourceItem = new GoogleBigQueryDataSourceItem(dsItemTitle, dataSource); + + // Assert + Assert.Equal(SchemaTypeNames.DataSourceItemType, dataSourceItem.SchemaTypeName); + Assert.Equal(expectedDSTitle, dataSourceItem.DataSource.Title); + Assert.Equal(expectedDSItemTitle, dataSourceItem.Title); + Assert.Equal(dataSource.Id, dataSourceItem.DataSource.Id); + Assert.Equal(dataSource.Id, dataSourceItem.DataSourceId); + Assert.Same(dataSource, dataSourceItem.DataSource); } [Fact] - public void DataSetId_SaveValueAndProperties_WhenSet() + public void GetDataSetId_ReturnSameValue_WithSetValue() { // Arrange var dataSource = new GoogleBigQueryDataSource(); @@ -39,16 +83,16 @@ public void DataSetId_SaveValueAndProperties_WhenSet() // Assert Assert.Equal(datasetId, dataSourceItem.DataSetId); - Assert.Equal(datasetId, dataSourceItem.Properties.GetValue("DataSetId")); + Assert.Equal(datasetId, dataSourceItem.Properties.GetValue("datasetId")); } [Fact] - public void PropertyId_SaveValueAndProperties_WhenSet() + public void GetProjectId_ReturnSameValue_WithSetValue() { // Arrange var dataSource = new GoogleBigQueryDataSource(); var dataSourceItem = new GoogleBigQueryDataSourceItem("Test", dataSource); - var projectId = "TestPropertyId"; + var projectId = "ProjectId"; // Act dataSourceItem.ProjectId = projectId; @@ -57,5 +101,20 @@ public void PropertyId_SaveValueAndProperties_WhenSet() Assert.Equal(projectId, dataSourceItem.ProjectId); Assert.Equal(projectId, dataSourceItem.Properties.GetValue("ProjectId")); } + + [Fact] + public void GetTable_ReturnSamevalue_WithSetValue() + { + // Arrange + var dataSourceItem = new GoogleBigQueryDataSourceItem("Big Query DS Item"); + var tableName = "TestTable"; + + // Act + dataSourceItem.Table = tableName; + + // Assert + Assert.Equal(tableName, dataSourceItem.Table); + Assert.Equal(tableName, dataSourceItem.Properties.GetValue("tableId")); + } } } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs index 49352f00..7a738557 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs @@ -12,7 +12,7 @@ namespace Reveal.Sdk.Dom.Tests.Data.DataSources public class GoogleBigQueryDataSourceFixture { [Fact] - public void Constructor_SetProviderToGoogleBigQuery_WhenConstructed() + public void Constructor_SetProviderToGoogleBigQuery_WithoutParameters() { // Act var dataSource = new GoogleBigQueryDataSource(); @@ -22,7 +22,7 @@ public void Constructor_SetProviderToGoogleBigQuery_WhenConstructed() } [Fact] - public void ProjectId_SaveValueAndProperties_WhenSet() + public void SetProjectId_ReturnSame_WithSetValue() { // Arrange var dataSource = new GoogleBigQueryDataSource(); @@ -33,7 +33,7 @@ public void ProjectId_SaveValueAndProperties_WhenSet() // Assert Assert.Equal(projectId, dataSource.ProjectId); - Assert.Equal(projectId, dataSource.Properties.GetValue("ProjectId")); + Assert.Equal(projectId, dataSource.Properties.GetValue("projectId")); } } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSourceItems/GoogleBigQueryDataSourceItem.cs b/src/Reveal.Sdk.Dom/Data/DataSourceItems/GoogleBigQueryDataSourceItem.cs index 17a4c0e4..a47280a5 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSourceItems/GoogleBigQueryDataSourceItem.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSourceItems/GoogleBigQueryDataSourceItem.cs @@ -3,17 +3,25 @@ namespace Reveal.Sdk.Dom.Data { - internal class GoogleBigQueryDataSourceItem : TableDataSourceItem + public class GoogleBigQueryDataSourceItem : DataSourceItem { + public GoogleBigQueryDataSourceItem(string title) : + base(title, new GoogleBigQueryDataSource()) + { } + public GoogleBigQueryDataSourceItem(string title, DataSource dataSource) : base(title, dataSource) { } + public GoogleBigQueryDataSourceItem(string title, GoogleBigQueryDataSource dataSource) : + base(title, dataSource) + { } + [JsonIgnore] public string DataSetId { - get => Properties.GetValue("DataSetId"); - set => Properties.SetItem("DataSetId", value); + get => Properties.GetValue("datasetId"); + set => Properties.SetItem("datasetId", value); } [JsonIgnore] @@ -22,5 +30,17 @@ public string ProjectId get => Properties.GetValue("ProjectId"); set => Properties.SetItem("ProjectId", value); } + + [JsonIgnore] + public string Table + { + get => Properties.GetValue("tableId"); + set => Properties.SetItem("tableId", value); + } + + protected override DataSource CreateDataSourceInstance(DataSource dataSource) + { + return Create(dataSource); + } } } diff --git a/src/Reveal.Sdk.Dom/Data/DataSources/GoogleBigQueryDataSource.cs b/src/Reveal.Sdk.Dom/Data/DataSources/GoogleBigQueryDataSource.cs index 9ea3f5ea..13cbca3e 100644 --- a/src/Reveal.Sdk.Dom/Data/DataSources/GoogleBigQueryDataSource.cs +++ b/src/Reveal.Sdk.Dom/Data/DataSources/GoogleBigQueryDataSource.cs @@ -3,7 +3,7 @@ namespace Reveal.Sdk.Dom.Data { - internal class GoogleBigQueryDataSource : DatabaseDataSource + public class GoogleBigQueryDataSource : DatabaseDataSource { public GoogleBigQueryDataSource() { @@ -13,8 +13,8 @@ public GoogleBigQueryDataSource() [JsonIgnore] public string ProjectId { - get => Properties.GetValue("ProjectId"); - set => Properties.SetItem("ProjectId", value); + get => Properties.GetValue("projectId"); + set => Properties.SetItem("projectId", value); } } } From 5347a639ec4c02a8d7a8990da17252dcf8103851 Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 13 Dec 2024 15:46:12 +0700 Subject: [PATCH 07/14] Create Unit test for ToJsonString --- .../GoogleBigQueryDataSourceItemFixture.cs | 44 +++++++++++++++++++ .../GoogleBigQueryDataSourceFixture.cs | 39 +++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs index a9dc78b4..d08c9b32 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSourceItems/GoogleBigQueryDataSourceItemFixture.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json.Linq; using Reveal.Sdk.Dom.Core.Constants; using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Data; @@ -116,5 +117,48 @@ public void GetTable_ReturnSamevalue_WithSetValue() Assert.Equal(tableName, dataSourceItem.Table); Assert.Equal(tableName, dataSourceItem.Properties.GetValue("tableId")); } + + [Fact] + public void ToJsonString_CreatesFormattedJson_NoConditions() + { + // Arrange + var expectedJson = """ + { + "_type": "DataSourceItemType", + "Id": "bigqueryDSItemId", + "Title": "Big Query", + "Subtitle": "America Health rankings", + "DataSourceId": "bigquery", + "HasTabularData": true, + "HasAsset": false, + "Properties": { + "datasetId": "america_health_rankings", + "tableId": "ahr" + }, + "Settings": {} + } + """; + var dataSource = new GoogleBigQueryDataSource() + { + Id = "bigquery", + }; + var dataSourceItem = new GoogleBigQueryDataSourceItem("Big Query", dataSource) + { + Id = "bigqueryDSItemId", + Subtitle = "America Health rankings", + HasTabularData = true, + HasAsset = false, + DataSetId = "america_health_rankings", + Table = "ahr", + }; + var expectedJObject = JObject.Parse(expectedJson); + + // Act + var json = dataSourceItem.ToJsonString(); + var actualJObject = JObject.Parse(json); + + // Assert + Assert.Equal(expectedJObject, actualJObject); + } } } diff --git a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs index 7a738557..669eda6c 100644 --- a/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs +++ b/src/Reveal.Sdk.Dom.Tests/Data/DataSources/GoogleBigQueryDataSourceFixture.cs @@ -1,4 +1,5 @@ -using Reveal.Sdk.Dom.Core.Extensions; +using Newtonsoft.Json.Linq; +using Reveal.Sdk.Dom.Core.Extensions; using Reveal.Sdk.Dom.Data; using System; using System.Collections.Generic; @@ -35,5 +36,41 @@ public void SetProjectId_ReturnSame_WithSetValue() Assert.Equal(projectId, dataSource.ProjectId); Assert.Equal(projectId, dataSource.Properties.GetValue("projectId")); } + + [Fact] + public void ToJsonString_CreatesFormattedJson_NoConditions() + { + // Arrange + var expectedJson = """ + { + "_type": "DataSourceType", + "Id": "ggBigQueryDSId", + "Provider": "BIG_QUERY", + "Description": "Big Query", + "Subtitle": "Public Data", + "Properties": { + "projectId": "bigquery-public-data" + }, + "Settings": {} + } + """; + + var dataSource = new GoogleBigQueryDataSource() + { + Id = "ggBigQueryDSId", + Title = "Big Query", + Subtitle = "Public Data", + ProjectId = "bigquery-public-data" + }; + + // Act + var json = dataSource.ToJsonString(); + var expectedJObject = JObject.Parse(expectedJson); + var actualJObject = JObject.Parse(json); + + // Assert + Assert.Equal(expectedJObject, actualJObject); + + } } } From 3976aad2f922a7097e5aa08d7681a161b6f51446 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 19 Dec 2024 16:49:23 +0700 Subject: [PATCH 08/14] Update to use RetrieveGoogleDriveBearerToken to get token for GG bigquery --- e2e/Sandbox/Reveal/AuthenticationProvider.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/e2e/Sandbox/Reveal/AuthenticationProvider.cs b/e2e/Sandbox/Reveal/AuthenticationProvider.cs index 87618830..5b0f988f 100644 --- a/e2e/Sandbox/Reveal/AuthenticationProvider.cs +++ b/e2e/Sandbox/Reveal/AuthenticationProvider.cs @@ -85,8 +85,7 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour } else if (dataSource is RVBigQueryDataSource) { - var token_task = CreateJwtToken(); - var token = token_task.Result; + var token = RetrieveGoogleDriveBearerToken(); userCredential = new RVBearerTokenDataSourceCredential(token, null); } else if (dataSource is RVGoogleDriveDataSource) @@ -156,6 +155,7 @@ internal string RetrieveGoogleDriveBearerToken() memoryStream.Write(jsonKeyBytes, 0, jsonKeyBytes.Length); memoryStream.Seek(0, SeekOrigin.Begin); var credential = GoogleCredential.FromStream(memoryStream).CreateScoped("https://www.googleapis.com/auth/drive", + "https://www.googleapis.com/auth/bigquery", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"); @@ -163,13 +163,5 @@ internal string RetrieveGoogleDriveBearerToken() return accessToken; } - - async Task CreateJwtToken() - { - var credentials = GoogleCredential.FromJson("credential-json"); - - var token = await credentials.UnderlyingCredential.GetAccessTokenForRequestAsync("https://bigquery.googleapis.com/").ConfigureAwait(false); - return token; - } } } From 86504713f2385dadc4ed0c8f91af0fef33251955 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 19 Dec 2024 21:50:35 +0700 Subject: [PATCH 09/14] create unit tests for ITargetExtensions --- .../Extensions/ITargetExtensionsFixture.cs | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITargetExtensionsFixture.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITargetExtensionsFixture.cs b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITargetExtensionsFixture.cs new file mode 100644 index 00000000..7df472d7 --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITargetExtensionsFixture.cs @@ -0,0 +1,102 @@ +using Moq; +using Reveal.Sdk.Dom.Visualizations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Visualizations.Extensions +{ + public class ITargetExtensionsFixture + { + [Fact] + public void SetTarget_UpdateTargets_WithFieldName() + { + // Arrange + var mockTarget = new Mock(); + var targets = new List() { new MeasureColumn() { DataField = new NumberDataField("InitialField") } }; + mockTarget.Setup(s => s.Targets).Returns(targets); + var targetObj = mockTarget.Object; + + var fieldName = "TestField"; + var numberField = new NumberDataField(fieldName); + var expectedTargetList = new List() { new MeasureColumn() { DataField = numberField } }; + + // Act + targetObj.SetTarget(fieldName); + + // Assert + Assert.Equivalent(expectedTargetList, targetObj.Targets); + } + + [Fact] + public void SetTarget_UpdateTargets_WithNumberDataField() + { + // Arrange + var mockTarget = new Mock(); + var targets = new List() { new MeasureColumn() { DataField = new NumberDataField("InitialField") } }; + mockTarget.Setup(s => s.Targets).Returns(targets); + var targetObj = mockTarget.Object; + + var fieldName = "TestField"; + var numberField = new NumberDataField(fieldName); + var expectedTargetList = new List() { new MeasureColumn() { DataField = numberField } }; + + // Act + targetObj.SetTarget(numberField); + + // Assert + Assert.Equivalent(expectedTargetList, targetObj.Targets); + } + + [Fact] + public void SetTargets_UpdateTargets_WithListFieldNames() + { + // Arrange + var mockTarget = new Mock(); + var targets = new List() { new MeasureColumn() { DataField = new NumberDataField("InitialField") } }; + mockTarget.Setup(s => s.Targets).Returns(targets); + var targetObj = mockTarget.Object; + + var fieldNames = new List() { "FirstField", "SecondField" }; + var expectedTargetList = fieldNames.Select(fieldName => new MeasureColumn() + { + DataField = new NumberDataField(fieldName) + }); + + // Act + targetObj.SetTargets(fieldNames.ToArray()); + + // Assert + Assert.Equivalent(expectedTargetList, targetObj.Targets); + } + + [Fact] + public void SetTargets_UpdateTargets_WithListNumberDataField() + { + // Arrange + var mockTarget = new Mock(); + var targets = new List() { new MeasureColumn() { DataField = new NumberDataField("InitialField") } }; + mockTarget.Setup(s => s.Targets).Returns(targets); + var targetObj = mockTarget.Object; + + var numberFields = new List() + { + new NumberDataField("FirstField"), + new NumberDataField("SecondField"), + }; + var expectedTargetList = numberFields.Select(field => new MeasureColumn() + { + DataField = field + }); + + // Act + targetObj.SetTargets(numberFields.ToArray()); + + // Assert + Assert.Equivalent(expectedTargetList, targetObj.Targets); + } + } +} From 27f4b2eb1c1d00bb957d5db7316eff757097bc25 Mon Sep 17 00:00:00 2001 From: Viktor Date: Thu, 19 Dec 2024 22:15:49 +0700 Subject: [PATCH 10/14] Update --- e2e/Sandbox/Reveal/AuthenticationProvider.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/e2e/Sandbox/Reveal/AuthenticationProvider.cs b/e2e/Sandbox/Reveal/AuthenticationProvider.cs index 5b0f988f..22854454 100644 --- a/e2e/Sandbox/Reveal/AuthenticationProvider.cs +++ b/e2e/Sandbox/Reveal/AuthenticationProvider.cs @@ -24,6 +24,8 @@ using Google.Apis.Auth.OAuth2; using System.IO; using System.Text; +using System.Linq; +using System.Collections.Generic; namespace Sandbox.RevealSDK @@ -51,7 +53,7 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour } else if (dataSource is RVGoogleDriveDataSource) { - var _token = RetrieveGoogleDriveBearerToken(); + var _token = RetrieveGoogleBearerToken(new List() { "https://www.googleapis.com/auth/drive" }); userCredential = new RVBearerTokenDataSourceCredential(_token, null); } @@ -85,7 +87,7 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour } else if (dataSource is RVBigQueryDataSource) { - var token = RetrieveGoogleDriveBearerToken(); + var token = RetrieveGoogleBearerToken(new List() { "https://www.googleapis.com/auth/bigquery" }); userCredential = new RVBearerTokenDataSourceCredential(token, null); } else if (dataSource is RVGoogleDriveDataSource) @@ -132,7 +134,7 @@ public Task ResolveCredentialsAsync(RVDashboardDataSour return Task.FromResult(userCredential); } - internal string RetrieveGoogleDriveBearerToken() + internal string RetrieveGoogleBearerToken(List scopes) { var jsonKey = @" { @@ -154,10 +156,9 @@ internal string RetrieveGoogleDriveBearerToken() byte[] jsonKeyBytes = Encoding.UTF8.GetBytes(jsonKey); memoryStream.Write(jsonKeyBytes, 0, jsonKeyBytes.Length); memoryStream.Seek(0, SeekOrigin.Begin); - var credential = GoogleCredential.FromStream(memoryStream).CreateScoped("https://www.googleapis.com/auth/drive", - "https://www.googleapis.com/auth/bigquery", - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile"); + scopes.Add("https://www.googleapis.com/auth/userinfo.email"); + scopes.Add("https://www.googleapis.com/auth/userinfo.profile"); + var credential = GoogleCredential.FromStream(memoryStream).CreateScoped(scopes); var accessToken = credential.UnderlyingCredential.GetAccessTokenForRequestAsync().Result; From 87192bd091d54cffe4346d392b489f43a13ee6f2 Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 20 Dec 2024 09:15:06 +0700 Subject: [PATCH 11/14] create unit tests for ITabularColumnsExtensions --- .../ITabularColumnsExtensionsFixture.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITabularColumnsExtensionsFixture.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITabularColumnsExtensionsFixture.cs b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITabularColumnsExtensionsFixture.cs new file mode 100644 index 00000000..144b3a2f --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ITabularColumnsExtensionsFixture.cs @@ -0,0 +1,32 @@ +using Moq; +using Reveal.Sdk.Dom.Visualizations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Visualizations.Extensions +{ + public class ITabularColumnsExtensionsFixture + { + [Fact] + public void SetColumns_UpdateColumns_WithListFieldNames() + { + // Arrange + var mockTabularColumn = new Mock(); + mockTabularColumn.SetupGet(s => s.Columns).Returns(new List { new TabularColumn("InitialColumn") }); + var visualization = mockTabularColumn.Object; + + var fieldNames = new List { "FirstField", "SecondField" }; + var expectedColumns = fieldNames.Select(fieldName => new TabularColumn(fieldName)).ToList(); + + // Act + visualization.SetColumns(fieldNames.ToArray()); + + // Assert + Assert.Equivalent(expectedColumns, visualization.Columns); + } + } +} From f1a7677a1fae4c585acc7662299797a7be338f75 Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 20 Dec 2024 10:20:20 +0700 Subject: [PATCH 12/14] Create unit tests for IRowExtension class --- .../Extensions/IRowsExtensionsFixture.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IRowsExtensionsFixture.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IRowsExtensionsFixture.cs b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IRowsExtensionsFixture.cs new file mode 100644 index 00000000..94ab5a42 --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IRowsExtensionsFixture.cs @@ -0,0 +1,94 @@ +using Moq; +using Reveal.Sdk.Dom.Visualizations; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Visualizations.Extensions +{ + public class IRowsExtensionsFixture + { + [Fact] + public void SetRow_UpdateRows_WithFieldName() + { + // Arrange + var mockRow = new Mock(); + mockRow.Setup(s => s.Rows).Returns(new List() { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockRow.Object; + + var fieldName = "TestField"; + var expectedRows = new List { new DimensionColumn() { DataField = new TextDataField(fieldName) } }; + + // Act + visualization.SetRow(fieldName); + + // Assert + Assert.Equivalent(expectedRows, visualization.Rows); + } + + [Fact] + public void SetRow_UpdateRows_WithDimensionDataField() + { + // Arrange + var mockRow = new Mock(); + mockRow.Setup(s => s.Rows).Returns(new List() { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockRow.Object; + + var mockDimensionDataField = new Mock("TestField"); + var field = mockDimensionDataField.Object; + var expectedRows = new List { new DimensionColumn() { DataField = field } }; + + // Act + visualization.SetRow(field); + + // Assert + Assert.Equivalent(expectedRows, visualization.Rows); + } + + [Fact] + public void SetRows_UpdateRows_WithFieldNames() + { + // Arrange + var mockRow = new Mock(); + mockRow.Setup(s => s.Rows).Returns(new List() { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockRow.Object; + + var fieldNames = new List{ "FirstField", "SecondField" }; + var expectedVSRows = fieldNames.Select(fieldName => new DimensionColumn() { DataField= new TextDataField(fieldName) }); + + // Act + visualization.SetRows(fieldNames.ToArray()); + + // Assert + Assert.Equivalent(expectedVSRows, visualization.Rows); + } + + [Fact] + public void SetRows_UpdateRows_WithListDimensionDataFields() + { + // Arrange + var mockRow = new Mock(); + mockRow.Setup(s => s.Rows).Returns(new List() { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockRow.Object; + + var listFields = new List(); + var expectedRows = new List(); + + var firstMockDimensionDataField = new Mock("FirstFieldName"); + var firstField = firstMockDimensionDataField.Object; + expectedRows.Add(new DimensionColumn() { DataField = firstField }); + listFields.Add(firstField); + + var secondMockDimensionDataField = new Mock("SecondFieldName"); + var secondField = secondMockDimensionDataField.Object; + expectedRows.Add(new DimensionColumn() { DataField = secondField }); + listFields.Add(secondField); + + // Act + visualization.SetRows(listFields.ToArray()); + + // Assert + Assert.Equivalent(expectedRows, visualization.Rows); + } + } +} From b976ff510e317efef69a0f259144f5dc755275f0 Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 20 Dec 2024 11:55:35 +0700 Subject: [PATCH 13/14] Create unit test for IMapExtensions --- .../Extensions/IMapExtensionsFixture.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IMapExtensionsFixture.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IMapExtensionsFixture.cs b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IMapExtensionsFixture.cs new file mode 100644 index 00000000..ad105e48 --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/IMapExtensionsFixture.cs @@ -0,0 +1,33 @@ +using Moq; +using Reveal.Sdk.Dom.Visualizations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Visualizations.Extensions +{ + public class IMapExtensionsFixture + { + [Fact] + public void SetMap_UpdateMap_WithMapName() + { + // Arrange + var map = new MockMap(); + var expectedMapName = "TestMap"; + + // Act + map.SetMap(expectedMapName); + + // Assert + Assert.Equal(expectedMapName, map.Map); + } + + private class MockMap : IMap + { + public string Map { get; set; } + } + } +} From 2bc58930735e712c536db0b77a5fda6ce3d7054a Mon Sep 17 00:00:00 2001 From: Viktor Date: Fri, 20 Dec 2024 15:12:41 +0700 Subject: [PATCH 14/14] Create unit test for ILabelsExtensions --- .../Extensions/ILabelsExtensions.csFixture.cs | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ILabelsExtensions.csFixture.cs diff --git a/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ILabelsExtensions.csFixture.cs b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ILabelsExtensions.csFixture.cs new file mode 100644 index 00000000..aabd94b3 --- /dev/null +++ b/src/Reveal.Sdk.Dom.Tests/Visualizations/Extensions/ILabelsExtensions.csFixture.cs @@ -0,0 +1,104 @@ +using Moq; +using Reveal.Sdk.Dom.Visualizations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Reveal.Sdk.Dom.Tests.Visualizations.Extensions +{ + public class ILabelsExtensions + { + [Fact] + public void SetLabel_UpdateLabels_WithFieldName() + { + // Arrange + var mockLabel = new Mock(); + mockLabel.Setup(x => x.Labels).Returns(new List { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockLabel.Object; + + var fieldName = "TestName"; + var expectedLabels = new List { new DimensionColumn() { DataField = new TextDataField(fieldName) } }; + + // Act + visualization.SetLabel(fieldName); + + // Assert + Assert.Equivalent(expectedLabels, visualization.Labels); + } + + [Fact] + public void SetLabel_UpdateLabels_WithDimensionDataField() + { + // Arrange + var mockLabel = new Mock(); + mockLabel.Setup(x => x.Labels).Returns(new List { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockLabel.Object; + + var mockDimensionDataField = new Mock("TestField"); + var dimensionDataField = mockDimensionDataField.Object; + var expectedLabels = new List { new DimensionColumn() { DataField = dimensionDataField } }; + + // Act + visualization.SetLabel(dimensionDataField); + + // Assert + Assert.Equivalent(expectedLabels, visualization.Labels); + } + + [Fact] + public void SetLabels_UpdateLabels_WithListFieldNames() + { + // Arrange + var mockLabel = new Mock(); + mockLabel.Setup(x => x.Labels).Returns(new List { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockLabel.Object; + + var listFieldNames = new List + { + "FirstField", + "SecondField" + }; + var expectedLabels = listFieldNames.Select(fieldName => new DimensionColumn() + { + DataField = new TextDataField(fieldName) + }).ToList(); + + // Act + visualization.SetLabels(listFieldNames.ToArray()); + + // Assert + Assert.Equivalent(expectedLabels, visualization.Labels); + } + + [Fact] + public void SetLabels_UpdateLabels_WithListDimensionDateFields() + { + // Arrange + var mockLabel = new Mock(); + mockLabel.Setup(x => x.Labels).Returns(new List { new DimensionColumn() { DataField = new TextDataField("InitialField") } }); + var visualization = mockLabel.Object; + + var listFields = new List(); + var expectedLabels = new List(); + + var mockDimensionDataField1 = new Mock("FirstField"); + var firstField = mockDimensionDataField1.Object; + listFields.Add(firstField); + expectedLabels.Add(new DimensionColumn(firstField)); + + var mockDimensionDataField2 = new Mock("SecondField"); + var secondField = mockDimensionDataField2.Object; + listFields.Add(secondField); + expectedLabels.Add(new DimensionColumn(secondField)); + + // Act + visualization.SetLabels(listFields.ToArray()); + + // Assert + Assert.Equivalent(expectedLabels, visualization.Labels); + } + } +}