forked from open-api-spex/open_api_spex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema_construction_test.exs
77 lines (58 loc) · 1.85 KB
/
schema_construction_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
defmodule OpenApiSpex.SchemaConstructionTest do
use ExUnit.Case
defmodule SchemaCreate do
require OpenApiSpex
OpenApiSpex.schema(%OpenApiSpex.Schema{type: :string})
end
defmodule SchemaWithModuledoc do
@moduledoc "sample moduledoc"
require OpenApiSpex
OpenApiSpex.schema(%OpenApiSpex.Schema{
title: "Lines of code",
description: "How many lines of code were written today",
type: :integer
})
def doc, do: @moduledoc
end
defmodule SchemaWithoutModuledoc do
require OpenApiSpex
OpenApiSpex.schema(%OpenApiSpex.Schema{
title: "Lines of code",
description: "How many lines of code were written today",
type: :integer
})
def doc, do: @moduledoc
end
test "calling schema() macro works with a struct" do
assert %OpenApiSpex.Schema{"x-struct": module, type: schema_type} = SchemaCreate.schema()
assert schema_type == :string
assert module == SchemaCreate
assert SchemaCreate.__struct__()
end
defmodule SchemaWithoutStructDef do
require OpenApiSpex
OpenApiSpex.schema(%{type: :string}, struct?: false)
end
test "able to define schema without defining a struct" do
assert_raise UndefinedFunctionError, fn ->
SchemaWithoutStructDef.__struct__()
end
end
defmodule SchemaWithoutDerive do
require OpenApiSpex
OpenApiSpex.schema(%{type: :string}, derive?: false)
end
test "able to define schema module without a @derive" do
assert_raise Protocol.UndefinedError, fn ->
struct = %SchemaWithoutDerive{}
Jason.encode!(struct)
end
end
test "preserves the moduledoc" do
assert "sample moduledoc" = SchemaWithModuledoc.doc()
end
test "generates a moduledoc from the schema" do
assert "Lines of code\n\nHow many lines of code were written today" =
SchemaWithoutModuledoc.doc()
end
end