Skip to content

Commit 864a059

Browse files
committed
WIP fix: tests
1 parent 0c84d62 commit 864a059

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

src/SDK/Language/Python.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public function getFiles(): array
210210
'destination' => '{{ spec.title | caseSnake}}/operator.py',
211211
'template' => 'python/package/operator.py.twig',
212212
],
213+
[
214+
'scope' => 'default',
215+
'destination' => 'test/test_operator.py',
216+
'template' => 'python/test/test_operator.py.twig',
217+
],
213218
[
214219
'scope' => 'default',
215220
'destination' => '{{ spec.title | caseSnake}}/exception.py',
@@ -739,6 +744,42 @@ protected function hasGenericType(?string $model, array $spec): bool
739744
return false;
740745
}
741746

747+
/**
748+
* Creates an example for a response model with the given name
749+
*
750+
* @param string $model
751+
* @param array $spec
752+
* @return string
753+
*/
754+
protected function getResponseModelExample(string $model, array $spec): string
755+
{
756+
$modelDef = $spec['definitions'][$model];
757+
758+
$result = [];
759+
foreach ($modelDef['properties'] ?? [] as $property) {
760+
if (!$property['required']) {
761+
continue;
762+
}
763+
764+
if ($property['type'] === 'object') {
765+
echo $property;
766+
return '';
767+
}
768+
769+
$result[$property['name']] = match ($property['type']) {
770+
'object' => $this->getResponseModelExample($property['sub_schema'], $spec),
771+
'array' => [],
772+
'string' => $property['example'] ?? '',
773+
'boolean' => true,
774+
default => $property['example'],
775+
};
776+
}
777+
778+
$json = json_encode($result, JSON_PRETTY_PRINT);
779+
780+
return str_replace('true', "True", $json);
781+
}
782+
742783
public function getFilters(): array
743784
{
744785
return [
@@ -828,6 +869,9 @@ public function getFilters(): array
828869
new TwigFilter('requestModelExample', function (array $parameter, array $spec, string $serviceName = '') {
829870
return $this->getRequestModelExample($parameter, $spec, $serviceName);
830871
}),
872+
new TwigFilter('responseModelExample', function (string $model, array $spec) {
873+
return $this->getResponseModelExample($model, $spec);
874+
})
831875
];
832876
}
833877
}

templates/python/test/services/test_service.py.twig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import unittest
44

55
from appwrite.client import Client
66
from appwrite.input_file import InputFile
7+
from appwrite.models import *
78
from appwrite.services.{{ service.name | caseSnake }} import {{ service.name | caseUcfirst }}
89

9-
1010
class {{ service.name | caseUcfirst }}ServiceTest(unittest.TestCase):
1111

1212
def setUp(self):
@@ -17,16 +17,12 @@ class {{ service.name | caseUcfirst }}ServiceTest(unittest.TestCase):
1717
@requests_mock.Mocker()
1818
def test_{{ method.name | caseSnake }}(self, m):
1919
{%~ if method.type == 'webAuth' %}
20-
data = ''
20+
data = None
2121
{%~ elseif method.type == 'location' %}
2222
data = bytearray()
2323
{%~ else %}
2424
{%~ if method.responseModel and method.responseModel != 'any' %}
25-
data = {
26-
{%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%}
27-
'{{property.name}}': {% if property.type == 'object' %}{}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example | escapeDollarSign}}'{% elseif property.type == 'boolean' %}True{% else %}{{property.example}}{% endif %},{%~ endfor ~%}{% set break = true %}{%- else -%}{% set continue = true %}{%- endif -%}
28-
{%~ endfor ~%}
29-
}
25+
data = {{ method.responseModel | responseModelExample(spec) | raw }}
3026
{%~ else %}
3127
data = ''
3228
{%~ endif %}
@@ -35,9 +31,13 @@ class {{ service.name | caseUcfirst }}ServiceTest(unittest.TestCase):
3531
m.request(requests_mock.ANY, requests_mock.ANY, {% if method.type == 'location' %}body=data{% else %}text=json.dumps(data){% endif %}, headers=headers)
3632

3733
response = self.{{ service.name | caseSnake }}.{{ method.name | caseSnake }}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%}
38-
{% if parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.from_bytes(bytearray()){% elseif parameter.type == 'boolean' %}True{% elseif parameter.type == 'string' %}'{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}'{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%}
34+
{% if parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.from_bytes(bytearray(), "example.file"){% elseif parameter.type == 'boolean' %}True{% elseif parameter.type == 'string' %}'{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}'{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%}
3935
)
4036

37+
{%~ if method.type != 'webAuth' and method.responseModel and method.responseModel != 'any' %}
38+
self.assertEqual(response.to_dict(), data)
39+
{%~ else %}
4140
self.assertEqual(response, data)
41+
{%~ endif %}
4242

4343
{% endfor %}

templates/python/test/test_id.py.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from appwrite.id import ID
66
class TestIDMethods(unittest.TestCase):
77

88
def test_unique(self):
9-
self.assertEqual(ID.unique(), 'unique()')
9+
self.assertEqual(len(ID.unique()), 20)
1010

1111
def test_custom(self):
1212
self.assertEqual(ID.custom('custom'), 'custom')

templates/python/test/test_query.py.twig

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,85 +26,85 @@ class TestQueryMethods(unittest.TestCase):
2626
for t in tests:
2727
self.assertEqual(
2828
Query.equal('attr', t.value),
29-
f'equal("attr", {t.expected_values})',
29+
{% verbatim %}f'{{"method":"equal","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
3030
t.description
3131
)
3232

3333
def test_not_equal(self):
3434
for t in tests:
3535
self.assertEqual(
3636
Query.not_equal('attr', t.value),
37-
f'notEqual("attr", {t.expected_values})',
37+
{% verbatim %}f'{{"method":"notEqual","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
3838
t.description
3939
)
4040

4141
def test_less_than(self):
4242
for t in tests:
4343
self.assertEqual(
4444
Query.less_than('attr', t.value),
45-
f'lessThan("attr", {t.expected_values})',
45+
{% verbatim %}f'{{"method":"lessThan","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
4646
t.description
4747
)
4848

4949
def test_less_than_equal(self):
5050
for t in tests:
5151
self.assertEqual(
5252
Query.less_than_equal('attr', t.value),
53-
f'lessThanEqual("attr", {t.expected_values})',
53+
{% verbatim %}f'{{"method":"lessThanEqual","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
5454
t.description
5555
)
5656

5757
def test_greater_than(self):
5858
for t in tests:
5959
self.assertEqual(
6060
Query.greater_than('attr', t.value),
61-
f'greaterThan("attr", {t.expected_values})',
61+
{% verbatim %}f'{{"method":"greaterThan","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
6262
t.description
6363
)
6464

6565
def test_greater_than_equal(self):
6666
for t in tests:
6767
self.assertEqual(
6868
Query.greater_than_equal('attr', t.value),
69-
f'greaterThanEqual("attr", {t.expected_values})',
69+
{% verbatim %}f'{{"method":"greaterThanEqual","attribute":"attr","values":{t.expected_values}}}',{% endverbatim %}
7070
t.description
7171
)
7272

7373
def test_search(self):
74-
self.assertEqual(Query.search('attr', 'keyword1 keyword2'), 'search("attr", ["keyword1 keyword2"])')
74+
self.assertEqual(Query.search('attr', 'keyword1 keyword2'), '{"method":"search","attribute":"attr","values":["keyword1 keyword2"]}')
7575

7676
def test_is_null(self):
77-
self.assertEqual(Query.is_null('attr'), 'isNull("attr")')
77+
self.assertEqual(Query.is_null('attr'), '{"method":"isNull","attribute":"attr"}')
7878

7979
def test_is_not_null(self):
80-
self.assertEqual(Query.is_not_null('attr'), 'isNotNull("attr")')
80+
self.assertEqual(Query.is_not_null('attr'), '{"method":"isNotNull","attribute":"attr"}')
8181

8282
def test_between_with_integers(self):
83-
self.assertEqual(Query.between('attr', 1, 2), 'between("attr", 1, 2)')
83+
self.assertEqual(Query.between('attr', 1, 2), '{"method":"between","attribute":"attr","values":[1,2]}')
8484

8585
def test_between_with_doubles(self):
86-
self.assertEqual(Query.between('attr', 1.0, 2.0), 'between("attr", 1.0, 2.0)')
86+
self.assertEqual(Query.between('attr', 1.0, 2.0), '{"method":"between","attribute":"attr","values":[1.0,2.0]}')
8787

8888
def test_between_with_strings(self):
89-
self.assertEqual(Query.between('attr', 'a', 'z'), 'between("attr", "a" "z")')
89+
self.assertEqual(Query.between('attr', 'a', 'z'), '{"method":"between","attribute":"attr","values":["a","z"]}')
9090

9191
def test_select(self):
92-
self.assertEqual(Query.select(['attr1', 'attr2']), 'select(["attr1","attr2"])')
92+
self.assertEqual(Query.select(['attr1', 'attr2']), '{"method":"select","values":["attr1","attr2"]}')
9393

9494
def test_order_asc(self):
95-
self.assertEqual(Query.order_asc('attr'), 'orderAsc("attr")')
95+
self.assertEqual(Query.order_asc('attr'), '{"method":"orderAsc","attribute":"attr"}')
9696

9797
def test_order_desc(self):
98-
self.assertEqual(Query.order_desc('attr'), 'orderDesc("attr")')
98+
self.assertEqual(Query.order_desc('attr'), '{"method":"orderDesc","attribute":"attr"}')
9999

100100
def test_cursor_before(self):
101-
self.assertEqual(Query.cursor_before('custom'), 'cursorBefore("custom")')
101+
self.assertEqual(Query.cursor_before('custom'), '{"method":"cursorBefore","values":["custom"]}')
102102

103103
def test_cursor_after(self):
104-
self.assertEqual(Query.cursor_after('custom'), 'cursorAfter("custom")')
104+
self.assertEqual(Query.cursor_after('custom'), '{"method":"cursorAfter","values":["custom"]}')
105105

106106
def test_limit(self):
107-
self.assertEqual(Query.limit(1), 'limit(1)')
107+
self.assertEqual(Query.limit(1), '{"method":"limit","values":[1]}')
108108

109109
def test_offset(self):
110-
self.assertEqual(Query.offset(1), 'offset(1)')
110+
self.assertEqual(Query.offset(1), '{"method":"offset","values":[1]}')

0 commit comments

Comments
 (0)