-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser_model_spec.rb
124 lines (114 loc) · 2.94 KB
/
user_model_spec.rb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'json for user model' do
let(:user) do
Class.new do
include EasyTalk::Model
def self.name
'User'
end
end
end
let(:phone) do
Class.new do
include EasyTalk::Model
def self.name
'Phone'
end
define_schema do
title 'Phone'
description 'A phone number'
property :number, String, title: 'Phone Number', format: 'phone' # "phone" is a custom format. It is not a standard format.
property :type, String, title: 'Phone Type'
end
end
end
describe '.json_schema' do
let(:expected_json_schema) do
{
"title": 'User',
"description": 'A user of the system',
"type": 'object',
"properties": {
"name": {
"type": 'string',
"title": "Person's Name"
},
"email": {
"type": 'string',
"title": "Person's email",
"format": 'email'
},
"dob": {
"type": 'string',
"format": 'date',
"title": 'Date of Birth'
},
"group": {
"type": 'integer',
"enum": [
1,
2,
3
],
"default": 1
},
"phones": {
"type": 'array',
"items": {
"title": 'Phone',
"description": 'A phone number',
"type": 'object',
"properties": {
"number": {
"type": 'string',
"title": 'Phone Number',
"format": 'phone'
},
"type": {
"type": 'string',
"title": 'Phone Type'
}
},
"required": %w[
number
type
]
},
"title": 'Phones',
"minItems": 1
},
"tags": {
"type": 'array',
"items": {
"type": 'string'
},
"title": 'Tags'
}
},
"required": %w[
name
email
dob
group
phones
tags
]
}
end
it 'enhances the schema using the provided block' do
stub_const('Phone', phone)
user.define_schema do
title 'User'
description 'A user of the system'
property :name, String, title: "Person's Name"
property :email, String, format: 'email', title: "Person's email"
property :dob, Date, title: 'Date of Birth'
property :group, Integer, enum: [1, 2, 3], default: 1
property :phones, T::Array[Phone], title: 'Phones', min_items: 1
property :tags, T::Array[String], title: 'Tags'
end
expect(user.json_schema).to include_json(expected_json_schema)
end
end
end