Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.UnitTests/NotebookConvert/NotebookConvertServiceTests.cs
Karl Burtram f288bee294 Make nullable warnings a per file opt-in (#1842)
* Make nullable warnings a per file opt-in

* Remove unneeded compiler directives

* Remove compiler directive for User Data
2023-02-03 18:10:07 -08:00

171 lines
3.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using NUnit.Framework;
using Microsoft.SqlTools.ServiceLayer.NotebookConvert;
using Newtonsoft.Json;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.NotebookConvert
{
[TestFixture]
public class NotebookConvertServiceTests
{
private const string sampleSqlQuery = @"
/*
* Initial multiline comment
*/
-- Comment before batch
SELECT * FROM sys.databases
-- Compare Row Counts in Tables From Two Different Databases With the Same Schema
SELECT * -- inline single line comment
/* inline multiline
* comment
*/
FROM sys.databases
-- ending single line comment
/**
* Ending multiline
* comment
*/
";
private const string sampleNotebook = @"{
""metadata"": {
""kernelspec"": {
""name"": ""SQL"",
""display_name"": ""SQL"",
""language"": ""sql""
},
""language_info"": {
""name"": ""sql"",
""version"": """"
}
},
""nbformat_minor"": 2,
""nbformat"": 4,
""cells"": [
{
""cell_type"": ""markdown"",
""source"": [
""* Initial multiline comment""
]
},
{
""cell_type"": ""markdown"",
""source"": [
""Comment before batch""
]
},
{
""cell_type"": ""code"",
""source"": [
""SELECT * FROM sys.databases\n"",
""\n"",
""-- Compare Row Counts in Tables From Two Different Databases With the Same Schema\n"",
""\n"",
""SELECT * -- inline single line comment\n"",
""/* inline multiline\n"",
"" * comment\n"",
"" */\n"",
""FROM sys.databases""
]
},
{
""cell_type"": ""markdown"",
""source"": [
""ending single line comment""
]
},
{
""cell_type"": ""markdown"",
""source"": [
""* Ending multiline \n"",
""* comment""
]
}
]
}";
[Test]
public void ConvertSqlToNotebook()
{
var notebook = NotebookConvertService.ConvertSqlToNotebook(sampleSqlQuery);
var notebookString = JsonConvert.SerializeObject(notebook, Formatting.Indented);
Assert.That(notebookString, Is.EqualTo(sampleNotebook));
}
[Test]
public void ConvertNullSqlToNotebook()
{
var emptyNotebook = @"{
""metadata"": {
""kernelspec"": {
""name"": ""SQL"",
""display_name"": ""SQL"",
""language"": ""sql""
},
""language_info"": {
""name"": ""sql"",
""version"": """"
}
},
""nbformat_minor"": 2,
""nbformat"": 4,
""cells"": []
}";
var notebook = NotebookConvertService.ConvertSqlToNotebook(null);
var notebookString = JsonConvert.SerializeObject(notebook, Formatting.Indented);
Assert.That(notebookString, Is.EqualTo(emptyNotebook));
}
[Test]
public void ConvertNotebookToSql()
{
var expectedSqlQuery = @"/*
* Initial multiline comment
*/
/*
Comment before batch
*/
SELECT * FROM sys.databases
-- Compare Row Counts in Tables From Two Different Databases With the Same Schema
SELECT * -- inline single line comment
/* inline multiline
* comment
*/
FROM sys.databases
/*
ending single line comment
*/
/*
* Ending multiline
* comment
*/";
var notebook = JsonConvert.DeserializeObject<NotebookDocument>(sampleNotebook);
var query = NotebookConvertService.ConvertNotebookDocToSql(notebook);
Assert.That(query, Is.EqualTo(expectedSqlQuery));
}
[Test]
public void ConvertNullNotebookToSql()
{
var query = NotebookConvertService.ConvertNotebookDocToSql(null);
Assert.That(query, Is.EqualTo(string.Empty));
}
}
}