Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 7fdb35d

Browse files
authored
[Prefix] Added job to generate verified auxiliary file (#202)
1 parent 4c2dea9 commit 7fdb35d

7 files changed

Lines changed: 129 additions & 1 deletion

File tree

src/Search.GenerateAuxiliaryData/Job.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ internal class Job
3131
private const string ScriptRankingsTotal = "SqlScripts.Rankings.sql";
3232
private const string OutputNameRankings = "rankings.v1.json";
3333

34+
private const string ScriptVerifiedPackages = "SqlScripts.VerifiedPackages.sql";
35+
private const string OutputNameVerifiedPackages = "verifiedPackages.json";
36+
3437
private List<SqlExporter> _sqlExportScriptsToRun;
3538
private CloudBlobContainer _destContainer;
3639

@@ -52,6 +55,7 @@ public override void Init(IDictionary<string, string> jobArgsDictionary)
5255
_destContainer = destination.CreateCloudBlobClient().GetContainerReference(destinationContainerName);
5356

5457
_sqlExportScriptsToRun = new List<SqlExporter> {
58+
new VerifiedPackagesExporter(LoggerFactory.CreateLogger<VerifiedPackagesExporter>(), packageDatabaseConnString, _destContainer, ScriptVerifiedPackages, OutputNameVerifiedPackages),
5559
new NestedJArrayExporter(LoggerFactory.CreateLogger<NestedJArrayExporter>(), packageDatabaseConnString, _destContainer, ScriptCuratedFeed, OutputNameCuratedFeed, Col0CuratedFeed, Col1CuratedFeed),
5660
new NestedJArrayExporter(LoggerFactory.CreateLogger<NestedJArrayExporter>(), packageDatabaseConnString, _destContainer, ScriptOwners, OutputNameOwners, Col0Owners, Col1Owners),
5761
new RankingsExporter(LoggerFactory.CreateLogger<RankingsExporter>(), statisticsDatabaseConnString, _destContainer, ScriptRankingsTotal, OutputNameRankings)

src/Search.GenerateAuxiliaryData/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.CompilerServices;
23
using System.Runtime.InteropServices;
34

45
[assembly: AssemblyTitle("Search.GenerateAuxiliaryData")]
@@ -12,4 +13,5 @@
1213
[assembly: ComVisible(false)]
1314
[assembly: Guid("cf7645a4-b4c0-4827-b6b0-ae42108071c6")]
1415
[assembly: AssemblyVersion("1.0.0.0")]
15-
[assembly: AssemblyFileVersion("1.0.0.0")]
16+
[assembly: AssemblyFileVersion("1.0.0.0")]
17+
[assembly: InternalsVisibleTo("Tests.Search.GenerateAuxiliaryData")]

src/Search.GenerateAuxiliaryData/Search.GenerateAuxiliaryData.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="RankingsExporter.cs" />
9292
<Compile Include="SqlExporter.cs" />
9393
<Compile Include="SqlExporterException.cs" />
94+
<Compile Include="VerifiedPackagesExporter.cs" />
9495
</ItemGroup>
9596
<ItemGroup>
9697
<None Include="App.config" />
@@ -113,6 +114,9 @@
113114
<ItemGroup>
114115
<EmbeddedResource Include="SqlScripts\Rankings.sql" />
115116
</ItemGroup>
117+
<ItemGroup>
118+
<EmbeddedResource Include="SqlScripts\VerifiedPackages.sql" />
119+
</ItemGroup>
116120
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
117121
<Import Project="..\..\build\sign.targets" Condition="Exists('..\..\build\sign.targets')" />
118122
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT [PackageRegistrations].[Id] 'Id'
2+
FROM [PackageRegistrations] (NOLOCK)
3+
WHERE [PackageRegistrations].[IsVerified] = 1
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Data;
6+
using System.Data.SqlClient;
7+
using Microsoft.Extensions.Logging;
8+
using Microsoft.WindowsAzure.Storage.Blob;
9+
using Newtonsoft.Json.Linq;
10+
11+
namespace Search.GenerateAuxiliaryData
12+
{
13+
internal sealed class VerifiedPackagesExporter : SqlExporter
14+
{
15+
private const string _colPackageId = "Id";
16+
private readonly string _verifiedPackagesScript;
17+
18+
public VerifiedPackagesExporter(
19+
ILogger<SqlExporter> logger,
20+
string defaultConnectionString,
21+
CloudBlobContainer defaultDestinationContainer,
22+
string defaultVerifiedPackagesScript,
23+
string defaultName)
24+
: base(logger, defaultConnectionString, defaultDestinationContainer, defaultName)
25+
{
26+
_verifiedPackagesScript = defaultVerifiedPackagesScript;
27+
}
28+
29+
protected override JContainer GetResultOfQuery(SqlConnection connection)
30+
{
31+
var verifiedPackagesCommand = new SqlCommand(GetEmbeddedSqlScript(_verifiedPackagesScript), connection);
32+
verifiedPackagesCommand.CommandType = CommandType.Text;
33+
verifiedPackagesCommand.CommandTimeout = 60;
34+
35+
SqlDataReader reader = null;
36+
37+
try
38+
{
39+
reader = verifiedPackagesCommand.ExecuteReader();
40+
41+
return GetVerifiedPackages(reader);
42+
}
43+
finally
44+
{
45+
reader?.Close();
46+
}
47+
}
48+
49+
internal JArray GetVerifiedPackages(IDataReader reader)
50+
{
51+
if (reader == null) throw new ArgumentNullException(nameof(reader));
52+
53+
var colNames = GetColMappingFromSqlDataReader(reader);
54+
var result = new JArray();
55+
56+
while (reader.Read())
57+
{
58+
result.Add(reader.GetString(colNames[_colPackageId]));
59+
}
60+
61+
return result;
62+
}
63+
}
64+
}

tests/Tests.Search.GenerateAuxiliaryData/Tests.Search.GenerateAuxiliaryData.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
</ItemGroup>
108108
<ItemGroup>
109109
<Compile Include="RankingsExporterTests.cs" />
110+
<Compile Include="VerifiedPackagesExporterTests.cs" />
110111
</ItemGroup>
111112
<ItemGroup>
112113
<None Include="app.config" />
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Data;
5+
using Microsoft.Extensions.Logging;
6+
using Moq;
7+
using Newtonsoft.Json;
8+
using Search.GenerateAuxiliaryData;
9+
using Xunit;
10+
11+
namespace Tests.Search.GenerateAuxiliaryData
12+
{
13+
public class VerifiedPackagesExporterTests
14+
{
15+
[Fact]
16+
public void GetVerifiedPackagesReturnsJsonString()
17+
{
18+
var dataReader = new Mock<IDataReader>(MockBehavior.Strict);
19+
20+
dataReader.SetupGet(x => x.FieldCount)
21+
.Returns(1);
22+
dataReader.Setup(x => x.GetName(It.Is<int>(i => i == 0)))
23+
.Returns("Id");
24+
dataReader.SetupSequence(x => x.Read())
25+
.Returns(true)
26+
.Returns(true)
27+
.Returns(false);
28+
dataReader.SetupSequence(x => x.GetString(It.Is<int>(i => i == 0)))
29+
.Returns("My.Test.Package")
30+
.Returns("Hello.World");
31+
32+
var exporter = CreateExporter();
33+
var actualResult = exporter.GetVerifiedPackages(dataReader.Object);
34+
35+
Assert.Equal("[\"My.Test.Package\",\"Hello.World\"]", actualResult.ToString(Formatting.None));
36+
37+
dataReader.VerifyAll();
38+
}
39+
40+
private static VerifiedPackagesExporter CreateExporter()
41+
{
42+
return new VerifiedPackagesExporter(
43+
new LoggerFactory().CreateLogger<VerifiedPackagesExporter>(),
44+
defaultConnectionString: "a",
45+
defaultDestinationContainer: null,
46+
defaultVerifiedPackagesScript: "b",
47+
defaultName: "c");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)