Skip to content

Commit

Permalink
add google sheet data source
Browse files Browse the repository at this point in the history
  • Loading branch information
hainv-infragistics committed Dec 4, 2024
1 parent 0a78b79 commit 14f117d
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 39 deletions.
39 changes: 39 additions & 0 deletions e2e/Sandbox/Factories/GoogleSheetDataSourceDashboards.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Reveal.Sdk.Dom;
using Reveal.Sdk.Dom.Data;
using Reveal.Sdk.Dom.Visualizations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sandbox.Factories
{
public class GoogleSheetDataSourceDashboards
{
public static RdashDocument CreateDashboard()
{
var googleSheetDS = new GoogleSheetsDataSourceItem("Google Sheet Data Source", "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms")
{
Title = "Google Sheet Data Source",
Subtitle = "Google Sheet Data Source Subtitle",
Sheet = "Class Data",
Fields = new List<IField>
{
new TextField("Student Name"),
new TextField("Gender"),
new TextField("Class Level"),
new TextField("Home State"),
new TextField("Major"),
new TextField("Extracurricular Activity")
}
};

var document = new RdashDocument("My Dashboard");

document.Visualizations.Add(new GridVisualization("Class List", googleSheetDS).SetColumns("Student Name", "Gender", "Major", "Extracurricular Activity"));

return document;
}
}
}
7 changes: 4 additions & 3 deletions e2e/Sandbox/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public MainWindow()

RevealSdkSettings.DataSourceProvider = new Sandbox.RevealSDK.DataSourceProvider();
RevealSdkSettings.AuthenticationProvider = new AuthenticationProvider();
RevealSdkSettings.DataSources.RegisterMicrosoftSqlServer().RegisterMicrosoftAnalysisServices();
RevealSdkSettings.DataSources.RegisterMicrosoftSqlServer().RegisterMicrosoftAnalysisServices().RegisterGoogleDrive();

_revealView.LinkedDashboardProvider = (string dashboardId, string linkTitle) =>
{
Expand Down Expand Up @@ -219,14 +219,15 @@ private async void Read_Dashboard(object sender, RoutedEventArgs e)
private async void Create_Dashboard(object sender, RoutedEventArgs e)
{
//var document = MarketingDashboard.CreateDashboard();
var document = SalesDashboard.CreateDashboard();
//var document = SalesDashboard.CreateDashboard();
//var document = CampaignsDashboard.CreateDashboard();
//var document = HealthcareDashboard.CreateDashboard();
//var document = ManufacturingDashboard.CreateDashboard();
//var document = CustomDashboard.CreateDashboard();
//var document = RestDataSourceDashboards.CreateDashboard();
//var document = SqlServerDataSourceDashboards.CreateDashboard();
//var document = DashboardLinkingDashboard.CreateDashboard();
//var document = DashboardLinkingDashboard.CreateDashboard();
var document = GoogleSheetDataSourceDashboards.CreateDashboard();

var json = document.ToJsonString();

Expand Down
25 changes: 23 additions & 2 deletions e2e/Sandbox/Reveal/AuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
using Reveal.Sdk.Data;
using Reveal.Sdk.Data.Google.Drive;
using Reveal.Sdk.Data.Microsoft.AnalysisServices;
using Reveal.Sdk.Data.Microsoft.SqlServer;
using System;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using System.IO;

namespace Sandbox.RevealSDK
{
internal class AuthenticationProvider : IRVAuthenticationProvider
{
public Task<IRVDataSourceCredential> ResolveCredentialsAsync(RVDashboardDataSource dataSource)
public async Task<IRVDataSourceCredential> ResolveCredentialsAsync(RVDashboardDataSource dataSource)
{
IRVDataSourceCredential userCredential = null;
string _token = string.Empty;
if (dataSource is RVSqlServerDataSource)
{
userCredential = new RVUsernamePasswordDataSourceCredential();
}
else if (dataSource is RVNativeAnalysisServicesDataSource)
{
userCredential = new RVUsernamePasswordDataSourceCredential("username", "password", "domain");
} else if (dataSource is RVGoogleDriveDataSource)
{
if (string.IsNullOrEmpty(_token))
_token = await CreateJwtToken();

userCredential = new RVBearerTokenDataSourceCredential(_token, null);
}
return Task.FromResult(userCredential);
return userCredential;
}

async Task<string> CreateJwtToken()
{
var pathToJsonFile = Path.Combine(Environment.CurrentDirectory, "Data/GoogleServiceAccountAuth.json");
var credentials = GoogleCredential.FromFile(pathToJsonFile).CreateScoped("https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile");
var token = await credentials.UnderlyingCredential.GetAccessTokenForRequestAsync().ConfigureAwait(false);
return token;
}
}
}
8 changes: 4 additions & 4 deletions e2e/Sandbox/Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth.AspNetCore" Version="1.65.0" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.Athena" Version="1.7.1" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.Redshift" Version="1.7.1" />
<PackageReference Include="Reveal.Sdk.Data.Amazon.S3" Version="1.7.1" />
Expand Down Expand Up @@ -48,6 +49,9 @@
<None Update="Dashboards\Sales.rdash">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\GoogleServiceAccountAuth.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Samples.xlsx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -60,8 +64,4 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public void Identitifer_SaveValueAndProperties_WhenSet()
var identifier = "IdentitiferTest";

// Act
dataSourceItem.Identitifer = identifier;
dataSourceItem.Identifier = identifier;

// Assert
Assert.Equal(identifier, dataSourceItem.Identitifer);
Assert.Equal(identifier, dataSourceItem.Properties.GetValue<string>("Identitifer"));
Assert.Equal(identifier, dataSourceItem.Identifier);
Assert.Equal(identifier, dataSourceItem.Properties.GetValue<string>("Identifier"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,106 @@
using System;
using System.Collections.Generic;
using System.IO;
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 Reveal.Sdk.Dom.Visualizations;
using Xunit;

namespace Reveal.Sdk.Dom.Tests.Data.DataSourceItems
{
public class GoogleSheetsDataSourceItemFixture
{
[Fact]
public void Constructor_SetsTitleAndDatasource_AsProvided()
public void Constructor_SetsTitleAndIdentifier_AsProvided()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var title = "Test title";
var identifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem(title, dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem(title, identifier);

// Assert
Assert.Equal(title, dataSourceItem.Title);
Assert.Equal(dataSource, dataSourceItem.DataSource);
Assert.Equal(identifier, dataSourceItem.Identifier);
}

[Fact]
public void Constructor_SetsResourceItemToGoogleDrive_WhenCreate()
{
// Arrange
var title = "Test title";
var identifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem(title, identifier);

// Assert
Assert.Equal(DataSourceProvider.GoogleDrive, dataSourceItem.ResourceItemDataSource.Provider);
Assert.Equal(identifier, dataSourceItem.Identifier);
}

[Fact]
public void FirstRowContainsLabels_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");

// Act 1
dataSourceItem.FirstRowContainsLabels = true;

// Assert
Assert.True(dataSourceItem.FirstRowContainsLabels);
Assert.True(dataSourceItem.Properties.GetValue<bool>("FirstRowContainsLabels"));
Assert.True(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));

// Act 1
dataSourceItem.FirstRowContainsLabels = false;

// Assert
Assert.False(dataSourceItem.FirstRowContainsLabels);
Assert.False(dataSourceItem.Properties.GetValue<bool>("FirstRowContainsLabels"));
Assert.False(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));
}

[Fact]
public void Identifier_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var testIdentifier = "anotherTestIdentifier";

// Act 1
dataSourceItem.Identifier = testIdentifier;

// Assert
Assert.Equal(testIdentifier, dataSourceItem.Identifier);
Assert.Equal(testIdentifier, dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void ResourceItem_IsGoogleDriveDataSourceItem_WhenCreated()
{
// Arrange
var testIdentifier = "testIdentifier";

// Act
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", testIdentifier);

// Assert
Assert.Equal(DataSourceProvider.GoogleDrive, dataSourceItem.ResourceItem.DataSource.Provider);
Assert.IsType<GoogleDriveDataSourceItem>(dataSourceItem.ResourceItem);
Assert.Equal(testIdentifier, dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void NamedRange_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var namedRange = "NamedRangeTest";

// Act
Expand All @@ -69,7 +116,7 @@ public void PivotTable_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var pivotTable = "PivotTableTest";

// Act
Expand All @@ -85,7 +132,7 @@ public void Sheet_SaveValueAndProperties_WhenSet()
{
// Arrange
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", dataSource);
var dataSourceItem = new GoogleSheetsDataSourceItem("Test", "testIdentifier");
var sheet = "SheetTest";

// Act
Expand All @@ -95,5 +142,74 @@ public void Sheet_SaveValueAndProperties_WhenSet()
Assert.Equal(sheet, dataSourceItem.Sheet);
Assert.Equal(sheet, dataSourceItem.Properties.GetValue<string>("Sheet"));
}

[Fact]
public void RDashDocument_CreateTwoDataSources_WhenUseGoogleSheetDataSourceItem()
{
// Arrange
var document = new RdashDocument("Test");
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("My Google Sheet", "testIdentifier").SetFields(new List<IField>() { new TextField("Test") });

dataSourceItem.Sheet = "Sheet1";
dataSourceItem.FirstRowContainsLabels = true;

document.Visualizations.Add(new GridVisualization(dataSourceItem));

// Act
document.Validate();

// Assert
Assert.Equal(2, document.DataSources.Count);
Assert.Equal(DataSourceIds.GSHEET, document.DataSources[0].Id);
Assert.Equal(DataSourceProvider.GoogleSheets, document.DataSources[0].Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, document.DataSources[1].Provider);
}

[Fact]
public void RDashDocument_HasCorrectDataSourceItem_WhenLoadFromFile()
{
// Arrange
var filePath = Path.Combine(Environment.CurrentDirectory, "Dashboards", "TestGoogleSheet.rdash");

// Act
var document = RdashDocument.Load(filePath);
var gsheetDataSource = document.DataSources[0];
var driveDataSource = document.DataSources[1];
var dataSourceItem = document.Visualizations[0].DataDefinition.DataSourceItem;

var value = dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW");

// Assert
Assert.Equal(gsheetDataSource.Id, dataSourceItem.DataSourceId);
Assert.Equal(DataSourceProvider.GoogleSheets, gsheetDataSource.Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, driveDataSource.Provider);
Assert.Equal("Class Data", dataSourceItem.Properties.GetValue<string>("Sheet"));
Assert.True(dataSourceItem.Parameters.GetValue<bool>("TITLES_IN_FIRST_ROW"));
Assert.Equal("1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms", dataSourceItem.ResourceItem.Properties.GetValue<string>("Identifier"));
}

[Fact]
public void RDashDocument_CreateExpectedJson_WhenExport()
{
// Arrange
var document = new RdashDocument("Test");
var dataSource = new GoogleSheetsDataSource();
var dataSourceItem = new GoogleSheetsDataSourceItem("My Google Sheet", "testIdentifier").SetFields(new List<IField>() { new TextField("Test") });

dataSourceItem.Sheet = "Sheet1";
dataSourceItem.FirstRowContainsLabels = true;

document.Visualizations.Add(new GridVisualization(dataSourceItem));

// Act
document.Validate();

// Assert
Assert.Equal(2, document.DataSources.Count);
Assert.Equal(DataSourceIds.GSHEET, document.DataSources[0].Id);
Assert.Equal(DataSourceProvider.GoogleSheets, document.DataSources[0].Provider);
Assert.Equal(DataSourceProvider.GoogleDrive, document.DataSources[1].Provider);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Core.Constants;
using Reveal.Sdk.Dom.Core.Extensions;
using Reveal.Sdk.Dom.Data;
using System;
using System.Collections.Generic;
Expand All @@ -21,5 +22,15 @@ public void Constructor_SetProviderToGoogleSheets_WhenConstructed()
// Assert
Assert.Equal(DataSourceProvider.GoogleSheets, dataSource.Provider);
}

[Fact]
public void Constructor_SetIdToGSheet_WhenConstructed()
{
// Act
var dataSource = new GoogleSheetsDataSource();

// Assert
Assert.Equal(DataSourceIds.GSHEET, dataSource.Id);
}
}
}
Loading

0 comments on commit 14f117d

Please sign in to comment.