Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/mongo db ds #104

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion e2e/Sandbox/DashboardCreators/MongoDashboard.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IField>
{
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;
}
}
}
8 changes: 8 additions & 0 deletions e2e/Sandbox/Reveal/AuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,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
Expand Down Expand Up @@ -51,6 +55,10 @@ public Task<IRVDataSourceCredential> 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");
Expand Down
11 changes: 11 additions & 0 deletions e2e/Sandbox/Reveal/DataSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -60,6 +61,16 @@ public Task<RVDataSourceItem> 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);
}
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -33,7 +38,7 @@ public void Collection_SetsAndGetsValue_WithInputs()

// Assert
Assert.Equal("TestCollection", item.Collection);
Assert.Equal("TestCollection", item.Properties.GetValue<string>("Collection"));
Assert.Equal("TestCollection", item.Properties.GetValue<string>("Table"));
}

[Fact]
Expand All @@ -49,5 +54,81 @@ public void ProcessDataOnServer_SetsAndGetsValue_WithInputs()
Assert.True(item.ProcessDataOnServer);
Assert.True(item.Properties.GetValue<bool>("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<string>("Table"));
Assert.True(dataSourceItem.Properties.GetValue<bool>("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<IField>
{
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);
}
}
}
145 changes: 145 additions & 0 deletions src/Reveal.Sdk.Dom.Tests/Data/DataSources/MongoDbDataSourceFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
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("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<string>("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<bool>("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<bool>("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<string>("ServerAggregationDefault"));
Assert.NotNull(dataSource.Properties.GetValue<string>("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<IField>
{
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
},
"Settings": {}
}
""";

// 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);
}
}
}
Loading
Loading