Skip to content

Commit 0a5cadd

Browse files
authored
add elixir workflow (#21215)
* update tests to use built-in json module instead of jason * update base_url * temporarily disable type-casting for dates * retry failing tests
1 parent b20bf8b commit 0a5cadd

File tree

8 files changed

+43
-24
lines changed

8 files changed

+43
-24
lines changed

modules/openapi-generator/src/test/resources/3_0/elixir/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ paths:
13611361
schema:
13621362
$ref: "#/components/schemas/AllOfWithSingleRef"
13631363
servers:
1364+
- url: "https://petstore.swagger.io/v2"
13641365
- url: "http://{server}.swagger.io:{port}/v2"
13651366
description: petstore server
13661367
variables:

samples/client/petstore/elixir/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ You can override the URL of your server (e.g. if you have a separate development
3131
configuration files).
3232

3333
```elixir
34-
config :openapi_petstore, base_url: "http://petstore.swagger.io:80/v2"
34+
config :openapi_petstore, base_url: "https://petstore.swagger.io/v2"
3535
```
3636

3737
Multiple clients for the same API with different URLs can be created passing different `base_url`s when calling
3838
`OpenapiPetstore.Connection.new/1`:
3939

4040
```elixir
41-
client = OpenapiPetstore.Connection.new(base_url: "http://petstore.swagger.io:80/v2")
41+
client = OpenapiPetstore.Connection.new(base_url: "https://petstore.swagger.io/v2")
4242
```
4343

4444
[exdoc]: https://github.com/elixir-lang/ex_doc

samples/client/petstore/elixir/config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# General application configuration
88
import Config
99

10-
config :openapi_petstore, base_url: "http://petstore.swagger.io:80/v2"
10+
config :openapi_petstore, base_url: "https://petstore.swagger.io/v2"
1111

1212
# Import environment specific config. This must remain at the bottom
1313
# of this file so it overrides the configuration defined above.

samples/client/petstore/elixir/test/deserializer_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule DeserializerTest do
3030
"""
3131

3232
test "jason_decode/2 with valid JSON" do
33-
assert Deserializer.jason_decode(@valid_json, Pet) ==
33+
assert Deserializer.json_decode(@valid_json, Pet) ==
3434
{:ok,
3535
%Pet{
3636
id: 14,
@@ -43,7 +43,7 @@ defmodule DeserializerTest do
4343
end
4444

4545
test "jason_decode/2 with invalid JSON" do
46-
assert Deserializer.jason_decode(~s/{: 1}/, Pet) ==
47-
{:error, %Jason.DecodeError{data: "{: 1}", position: 1, token: nil}}
46+
assert Deserializer.json_decode(~s/{: 1}/, Pet) ==
47+
{:error, {:invalid_byte, 1, 58}}
4848
end
4949
end

samples/client/petstore/elixir/test/format_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ defmodule FormatTest do
3333
string: "Hello world!",
3434
byte: "U3dhZ2dlciByb2Nrcw==",
3535
binary: <<1, 2, 3>>,
36-
date: ~D[2013-10-20],
37-
dateTime: ~U[2013-10-20T18:20:30Z],
36+
date: "2013-10-20",
37+
dateTime: "2013-10-20T19:20:30+01:00",
3838
uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
3939
password: "green?horse",
4040
pattern_with_digits: "1234567890",
@@ -73,7 +73,7 @@ defmodule FormatTest do
7373
string: nil,
7474
byte: "U3dhZ2dlciByb2Nrcw==",
7575
binary: nil,
76-
date: ~D[2013-10-20],
76+
date: "2013-10-20",
7777
dateTime: nil,
7878
uuid: nil,
7979
password: "green?horse",

samples/client/petstore/elixir/test/mixed_properties_and_additional_properties_class_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule MixedPropertiesAndAdditionalPropertiesClass do
1515
|> Model.decode() ==
1616
%Model{
1717
uuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6",
18-
dateTime: ~U[2013-10-20T18:20:30Z],
18+
dateTime: "2013-10-20T19:20:30+01:00",
1919
map: %{
2020
# TODO values should be Dog and Cat structs instead of an Animal
2121
"doggie" => %Animal{

samples/client/petstore/elixir/test/outer_enum_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ defmodule OuterEnumTest do
1414
"""
1515

1616
@tag timeout: :infinity
17-
test "jason_decode/2 with valid JSON" do
18-
assert Deserializer.jason_decode(@valid_json, EnumTest) ==
17+
test "json_decode/2 with valid JSON" do
18+
assert Deserializer.json_decode(@valid_json, EnumTest) ==
1919
{:ok,
2020
%EnumTest{
2121
enum_string: "UPPER",

samples/client/petstore/elixir/test/pet_test.exs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ defmodule PetTest do
2424
{:ok, %Tesla.Env{} = response} = PetApi.add_pet(connection, pet)
2525
assert response.status == 200
2626

27-
{:ok, pet} = PetApi.get_pet_by_id(connection, petId)
28-
assert pet.id == petId
29-
assert pet.name == "elixir client test"
30-
assert pet.photoUrls == ["http://test_elixir_unit_test.com"]
31-
assert pet.category == %Category{id: petId, name: "test elixir category"}
32-
assert pet.tags == [%Tag{:id => petId, :name => "test elixir tag"}]
27+
retry_assert(fn ->
28+
{:ok, pet} = PetApi.get_pet_by_id(connection, petId)
29+
assert pet.id == petId
30+
assert pet.name == "elixir client test"
31+
assert pet.photoUrls == ["http://test_elixir_unit_test.com"]
32+
assert pet.category == %Category{id: petId, name: "test elixir category"}
33+
assert pet.tags == [%Tag{:id => petId, :name => "test elixir tag"}]
34+
end)
3335

3436
{:ok, response} = PetApi.delete_pet(connection, petId)
3537
assert response.status == 200
36-
{:ok, response} = PetApi.get_pet_by_id(connection, petId)
37-
assert response.status == 404
38+
retry_assert(fn ->
39+
{:ok, response} = PetApi.get_pet_by_id(connection, petId)
40+
assert response.status == 404
41+
end)
3842
end
3943

4044
test "update a pet", %{connection: connection} do
@@ -50,10 +54,24 @@ defmodule PetTest do
5054
{:ok, response} = PetApi.update_pet(connection, pet)
5155
assert response.status == 200
5256

53-
{:ok, pet} = PetApi.get_pet_by_id(connection, petId)
54-
assert pet.id == petId
55-
assert pet.name == "elixir client updatePet"
56-
assert pet.status == "pending"
57+
retry_assert(fn ->
58+
{:ok, pet} = PetApi.get_pet_by_id(connection, petId)
59+
assert pet.id == petId
60+
assert pet.name == "elixir client updatePet"
61+
assert pet.status == "pending"
62+
end, 5, 100)
63+
end
64+
65+
def retry_assert(fun, attempts \\ 3, delay \\ 100)
66+
def retry_assert(_fun, 0, _delay), do: flunk("assertion failed after retries")
67+
def retry_assert(fun, attempts, delay) do
68+
try do
69+
fun.()
70+
rescue
71+
_e ->
72+
Process.sleep(delay)
73+
retry_assert(fun, attempts - 1, delay)
74+
end
5775
end
5876

5977
test "find pet by status", %{connection: connection} do

0 commit comments

Comments
 (0)