mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Clean up notebook export code (#1565)
* Add null checks when converting notebooks to SQL, and SQL to notebooks. * Remove unnecessary newlines and spaces from multi-line comments. * Use the current environment's newline characters when converting cell text to a SQL query. * Add additional unit tests for notebook conversion.
This commit is contained in:
@@ -13,10 +13,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.NotebookConvert
|
||||
[TestFixture]
|
||||
public class NotebookConvertServiceTests
|
||||
{
|
||||
[Test]
|
||||
public void ConvertSqlToNotebook()
|
||||
{
|
||||
var sql = @"
|
||||
private const string sampleSqlQuery = @"
|
||||
/*
|
||||
* Initial multiline comment
|
||||
*/
|
||||
@@ -39,7 +36,7 @@ FROM sys.databases
|
||||
*/
|
||||
";
|
||||
|
||||
var expectedNotebook = @"{
|
||||
private const string sampleNotebook = @"{
|
||||
""metadata"": {
|
||||
""kernelspec"": {
|
||||
""name"": ""SQL"",
|
||||
@@ -90,16 +87,82 @@ FROM sys.databases
|
||||
""cell_type"": ""markdown"",
|
||||
""source"": [
|
||||
""* Ending multiline \n"",
|
||||
"" * comment""
|
||||
""* comment""
|
||||
]
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
var notebook = NotebookConvertService.ConvertSqlToNotebook(sql);
|
||||
[Test]
|
||||
public void ConvertSqlToNotebook()
|
||||
{
|
||||
var notebook = NotebookConvertService.ConvertSqlToNotebook(sampleSqlQuery);
|
||||
var notebookString = JsonConvert.SerializeObject(notebook, Formatting.Indented);
|
||||
Assert.AreEqual(expectedNotebook, notebookString);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user