Skip to content

Commit

Permalink
Why does the unit test pass O.o
Browse files Browse the repository at this point in the history
  • Loading branch information
pjmagee committed Jun 12, 2024
1 parent 455d74b commit 22b0b47
Showing 1 changed file with 31 additions and 83 deletions.
114 changes: 31 additions & 83 deletions tests/Kiota.Builder.Tests/Writers/Python/CodeUsingWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CodeUsingWriterTests
private readonly CodeNamespace root;
public CodeUsingWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.Python, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
Expand Down Expand Up @@ -95,96 +95,44 @@ public void DoesntAliasRegularSymbols()
[Fact]
public void WritesFutureImportsFirst()
{
var usingWriter = new CodeUsingWriter("foo");

var cd = new ClassDeclaration
{
Name = "bar",
};
// Generated with Kiota mcr.microsoft.com/openapi/kiota:1.15.0

/* Add external imports */
// import datetime
// from __future__ import annotations
// from dataclasses import dataclass, field
// from kiota_abstractions.serialization import Parsable, ParseNode, SerializationWriter
// from kiota_abstractions.serialization import AdditionalDataHolder, Parsable, ParseNode, SerializationWriter
// from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union

cd.AddUsings(new CodeUsing
{
Name = "datetime",
Declaration = new CodeType
{
Name = "-",
IsExternal = true
},

});

cd.AddUsings(new CodeUsing
{
Name = "annotations",
Declaration = new CodeType
{
Name = "__future__",
IsExternal = true
},

});

cd.AddUsings(new CodeUsing
{
Name = "dataclass",
Declaration = new CodeType
{
Name = "dataclasses",
IsExternal = true
}
});

cd.AddUsings(new CodeUsing
{
Name = "field",
Declaration = new CodeType
{
Name = "dataclasses",
IsExternal = true
}
});
var usingWriter = new CodeUsingWriter("foo");

cd.AddUsings(new CodeUsing[]
var codeClass = new ClassDeclaration
{
new CodeUsing
{
Name = "Parsable",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
}, new CodeUsing
{
Name = "ParseNode",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
},
new CodeUsing
{
Name = "SerializationWriter",
Declaration = new CodeType
{
Name = "kiota_abstractions.serialization",
IsExternal = true
}
}}
);

usingWriter.WriteExternalImports(cd, writer);
var result = tw.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
Name = "Test",
};

Assert.Equal("from __future__ import annotations", result[0]);
Assert.Equal("import datetime", result[1]);
codeClass.AddUsings(new CodeUsing { Name = "annotations", Declaration = new CodeType { Name = "__future__", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "dataclass", Declaration = new CodeType { Name = "dataclasses", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "field", Declaration = new CodeType { Name = "dataclasses", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "AdditionalDataHolder", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Parsable", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "ParseNode", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "SerializationWriter", Declaration = new CodeType { Name = "kiota_abstractions.serialization", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Any", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Callable", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Dict", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "List", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Optional", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "TYPE_CHECKING", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "Union", Declaration = new CodeType { Name = "typing", IsExternal = true } });

usingWriter.WriteExternalImports(codeClass, writer);

string[] usings = tw.ToString().Split(tw.NewLine, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal("import datetime", usings[0]);
Assert.Equal("from __future__ import annotations", usings[1]);
Assert.Equal("from dataclasses import dataclass, field", usings[2]);
Assert.Equal("from kiota_abstractions.serialization import AdditionalDataHolder, Parsable, ParseNode, SerializationWriter", usings[3]);
Assert.Equal("from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union", usings[4]);
}
}

0 comments on commit 22b0b47

Please sign in to comment.