Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests - Dotnet #743

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/SDK/Language/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ public function getFiles(): array
'destination' => '/src/{{ spec.title | caseUcfirst }}/{{ spec.title | caseUcfirst }}.csproj',
'template' => 'dotnet/src/Appwrite/Appwrite.csproj.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/{{ spec.title | caseUcfirst }}.Tests.csproj',
'template' => 'dotnet/src/Appwrite.Tests/Appwrite.Tests.csproj.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Client.cs',
Expand All @@ -346,21 +351,41 @@ public function getFiles(): array
'destination' => '/src/{{ spec.title | caseUcfirst }}/ID.cs',
'template' => 'dotnet/src/Appwrite/ID.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/IDTest.cs',
'template' => 'dotnet/src/Appwrite.Tests/IDTest.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Permission.cs',
'template' => 'dotnet/src/Appwrite/Permission.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/PermissionTest.cs',
'template' => 'dotnet/src/Appwrite.Tests/PermissionTest.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Query.cs',
'template' => 'dotnet/src/Appwrite/Query.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/QueryTest.cs',
'template' => 'dotnet/src/Appwrite.Tests/QueryTest.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Role.cs',
'template' => 'dotnet/src/Appwrite/Role.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/RoleTest.cs',
'template' => 'dotnet/src/Appwrite.Tests/RoleTest.cs.twig',
],
[
'scope' => 'default',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Extensions/Extensions.cs',
Expand Down Expand Up @@ -391,10 +416,20 @@ public function getFiles(): array
'destination' => '/src/{{ spec.title | caseUcfirst }}/Services/{{service.name | caseUcfirst}}.cs',
'template' => 'dotnet/src/Appwrite/Services/ServiceTemplate.cs.twig',
],
[
'scope' => 'service',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/Services/{{service.name | caseUcfirst}}ServiceTest.cs',
'template' => 'dotnet/src/Appwrite.Tests/Services/ServiceTest.cs.twig',
],
[
'scope' => 'definition',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs',
'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig',
],
[
'scope' => 'definition',
'destination' => '/src/{{ spec.title | caseUcfirst }}.Tests/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}Test.cs',
'template' => 'dotnet/src/Appwrite.Tests/Models/ModelTest.cs.twig',
]
];
}
Expand Down
5 changes: 4 additions & 1 deletion templates/dotnet/base/utils.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
{% endmacro %}
{% macro resultType(namespace, method) %}
{% if method.type == "webAuth" %}bool{% elseif method.type == "location" %}byte[]{% elseif not method.responseModel or method.responseModel == 'any' %}object{% else %}Models.{{method.responseModel | caseUcfirst | overrideIdentifier }}{% endif %}
{% endmacro %}
{% endmacro %}
{% macro sub_schema(property) %}
{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}
{% endmacro %}
22 changes: 22 additions & 0 deletions templates/dotnet/src/Appwrite.Tests/Appwrite.Tests.csproj.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1"/>
<PackageReference Include="Moq" Version="4.18.4"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Appwrite\Appwrite.csproj" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions templates/dotnet/src/Appwrite.Tests/IDTest.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;

namespace Appwrite.Tests;

public class IDTest
{
[Test]
public void TestUnique()
{
Assert.That(ID.Unique(), Is.EqualTo("unique()"));
}

[Test]
public void TestCustom()
{
Assert.That(ID.Custom("custom"), Is.EqualTo("custom"));
}
}
33 changes: 33 additions & 0 deletions templates/dotnet/src/Appwrite.Tests/Models/ModelTest.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% import 'dotnet/base/utils.twig' as utils -%}
using NUnit.Framework;
using System.Collections.Generic;

using Appwrite.Models;

namespace Appwrite.Tests.Models;

public class {{ definition.name | caseUcfirst }}Test
{
[Test]
public void TestSerialization()
{
var model = new {{ definition.name | caseUcfirst | overrideIdentifier }}(
{% for property in definition.properties %}
{{ property.name | escapeKeyword | removeDollarSign }}: {% if property.type == 'array' %}new {{ utils.sub_schema(property) }}(){% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}new Preferences(new Dictionary<string, object>()){% elseif property.type == 'object' %}new Dictionary<string, object>(){% elseif property.type == 'string' %}"{{property['x-example']}}"{% elseif property.type == 'boolean' %}true{% elseif property.type == 'number' %}{{property['x-example'] | number_format(1)}}{% else %}{{property['x-example']}}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
{% endfor %}
{% if definition.additionalProperties %}
data: new Dictionary<string, object>()
{% endif %}
);

var map = model.ToMap();
var result = {{ definition.name | caseUcfirst | overrideIdentifier }}.From(map);

{% for property in definition.properties %}
Assert.That(
result.{{ property.name | caseUcfirst | escapeKeyword | removeDollarSign }}{% if property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}.Data{% endif %},
Is.EqualTo({% if property.type == 'array' %}new {{ utils.sub_schema(property) }}(){% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}new Dictionary<string, object> { {"data", new Dictionary<string, object>()} }{% elseif property.type == 'object' %}new Dictionary<string, object>(){% elseif property.type == 'string' %}"{{property['x-example']}}"{% elseif property.type == 'boolean' %}true{% elseif property.type == 'number' %}{{property['x-example'] | number_format(1)}}{% else %}{{property['x-example']}}{% endif %})
);
{% endfor %}
}
}
36 changes: 36 additions & 0 deletions templates/dotnet/src/Appwrite.Tests/PermissionTest.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NUnit.Framework;

namespace Appwrite.Tests;

public class PermissionTest
{
[Test]
public void TestRead()
{
Assert.That(Permission.Read(Role.Any()), Is.EqualTo("read(\"any\")"));
}

[Test]
public void TestWrite()
{
Assert.That(Permission.Write(Role.Any()), Is.EqualTo("write(\"any\")"));
}

[Test]
public void TestCreate()
{
Assert.That(Permission.Create(Role.Any()), Is.EqualTo("create(\"any\")"));
}

[Test]
public void TestUpdate()
{
Assert.That(Permission.Update(Role.Any()), Is.EqualTo("update(\"any\")"));
}

[Test]
public void TestDelete()
{
Assert.That(Permission.Delete(Role.Any()), Is.EqualTo("delete(\"any\")"));
}
}
193 changes: 193 additions & 0 deletions templates/dotnet/src/Appwrite.Tests/QueryTest.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;

namespace Appwrite.Tests;

class BasicFilterQueryTest
{
public string Description { get; set; } = "";
public object Value { get; set; } = default!;
public string ExpectedValues { get; set; } = "";
}

public class QueryTest
{
private readonly List<BasicFilterQueryTest> _tests = new()
{
new BasicFilterQueryTest
{
Description = "with a string",
Value = "s",
ExpectedValues = "[\"s\"]"
},
new BasicFilterQueryTest
{
Description = "with a integer",
Value = 1,
ExpectedValues = "[1]"
},
new BasicFilterQueryTest
{
Description = "with a double",
Value = 1.2,
ExpectedValues = "[1.2]"
},
new BasicFilterQueryTest
{
Description = "with a whole number double",
Value = 1.0,
ExpectedValues = "[1]"
},
new BasicFilterQueryTest
{
Description = "with a bool",
Value = false,
ExpectedValues = "[false]"
},
new BasicFilterQueryTest
{
Description = "with a list",
Value = new [] {"a", "b", "c"},
ExpectedValues = "[\"a\",\"b\",\"c\"]"
}
};

[SetUp]
public void Setup()
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
}

[Test]
public void TestEqual()
{
foreach (var test in _tests)
{
Assert.That(Query.Equal("attr", test.Value), Is.EqualTo($"equal(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestNotEqual()
{
foreach (var test in _tests)
{
Assert.That(Query.NotEqual("attr", test.Value), Is.EqualTo($"notEqual(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestLessThan()
{
foreach (var test in _tests)
{
Assert.That(Query.LessThan("attr", test.Value), Is.EqualTo($"lessThan(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestLessThanEqual()
{
foreach (var test in _tests)
{
Assert.That(Query.LessThanEqual("attr", test.Value), Is.EqualTo($"lessThanEqual(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestGreaterThan()
{
foreach (var test in _tests)
{
Assert.That(Query.GreaterThan("attr", test.Value), Is.EqualTo($"greaterThan(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestGreaterThanEqual()
{
foreach (var test in _tests)
{
Assert.That(Query.GreaterThanEqual("attr", test.Value), Is.EqualTo($"greaterThanEqual(\"attr\", {test.ExpectedValues})"), test.Description);
}
}

[Test]
public void TestSearch()
{
Assert.That(Query.Search("attr", "keyword1 keyword2"), Is.EqualTo("search(\"attr\", [\"keyword1 keyword2\"])"));
}

[Test]
public void TestIsNull()
{
Assert.That(Query.IsNull("attr"), Is.EqualTo("isNull(\"attr\")"));
}

[Test]
public void TestIsNotNull()
{
Assert.That(Query.IsNotNull("attr"), Is.EqualTo("isNotNull(\"attr\")"));
}

[Test]
public void TestBetweenWithIntegers()
{
Assert.That(Query.Between("attr", 1, 2), Is.EqualTo("between(\"attr\", 1, 2)"));
}

[Test]
public void TestBetweenWithDoubles()
{
Assert.That(Query.Between("attr", 1.2, 2.2), Is.EqualTo("between(\"attr\", 1.2, 2.2)"));
}

[Test]
public void TestBetweenWithStrings()
{
Assert.That(Query.Between("attr", "a", "z"), Is.EqualTo("between(\"attr\", \"a\", \"z\")"));
}

[Test]
public void TestSelect()
{
Assert.That(Query.Select(new List<string> {"attr1", "attr2"}), Is.EqualTo("select([\"attr1\",\"attr2\"])"));
}

[Test]
public void TestOrderAsc()
{
Assert.That(Query.OrderAsc("attr"), Is.EqualTo("orderAsc(\"attr\")"));
}

[Test]
public void TestOrderDesc()
{
Assert.That(Query.OrderDesc("attr"), Is.EqualTo("orderDesc(\"attr\")"));
}

[Test]
public void TestCursorBefore()
{
Assert.That(Query.CursorBefore("attr"), Is.EqualTo("cursorBefore(\"attr\")"));
}

[Test]
public void TestCursorAfter()
{
Assert.That(Query.CursorAfter("attr"), Is.EqualTo("cursorAfter(\"attr\")"));
}

[Test]
public void TestLimit()
{
Assert.That(Query.Limit(1), Is.EqualTo("limit(1)"));
}

[Test]
public void TestOffset()
{
Assert.That(Query.Offset(1), Is.EqualTo("offset(1)"));
}
}
Loading