Skip to content

Commit a265130

Browse files
committed
refactor(ai-proxy): simplify complex ternary
operators with if-else statements - Replace complicated nested ternary operators in TEST 17 with clear if-else blocks - Improve code readability and maintainability in t/plugin/ai-proxy.t - Reindex test file for proper formatting - All linting checks pass Signed-off-by: Orician <[email protected]>
1 parent c498cca commit a265130

File tree

2 files changed

+89
-71
lines changed

2 files changed

+89
-71
lines changed

t/plugin/ai-proxy-multi.t

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -128,47 +128,6 @@ run_tests();
128128

129129
__DATA__
130130
131-
=== TEST 0: schema accepts 'logging' and ignores unknown 'logging_schema'
132-
--- config
133-
location /t {
134-
content_by_lua_block {
135-
local plugin = require("apisix.plugins.ai-proxy-multi")
136-
137-
local ok1, err1 = plugin.check_schema({
138-
instances = {
139-
{
140-
name = "openai-1",
141-
provider = "openai",
142-
weight = 1,
143-
auth = { header = { apikey = "token" } },
144-
options = { model = "gpt-4" },
145-
},
146-
},
147-
logging = { summaries = true },
148-
})
149-
150-
local ok2, err2 = plugin.check_schema({
151-
instances = {
152-
{
153-
name = "openai-1",
154-
provider = "openai",
155-
weight = 1,
156-
auth = { header = { apikey = "token" } },
157-
options = { model = "gpt-4" },
158-
},
159-
},
160-
logging_schema = { summaries = true },
161-
})
162-
163-
-- Unknown field 'logging_schema' should be ignored if additionalProperties are allowed
164-
ngx.say((ok1 and "ok" or ("bad:" .. (err1 or ""))), ":", (ok2 and "ok" or "invalid"))
165-
}
166-
}
167-
--- request
168-
GET /t
169-
--- response_body
170-
ok:ok
171-
172131
=== TEST 1: minimal viable configuration
173132
--- config
174133
location /t {
@@ -694,3 +653,46 @@ qr/6data: \[DONE\]\n\n/
694653
--- error_code: 400
695654
--- response_body eval
696655
qr/.invalid endpoint.*/
656+
657+
658+
659+
=== TEST 16: schema accepts 'logging' and ignores unknown 'logging_schema'
660+
--- config
661+
location /t {
662+
content_by_lua_block {
663+
local plugin = require("apisix.plugins.ai-proxy-multi")
664+
665+
local ok1, err1 = plugin.check_schema({
666+
instances = {
667+
{
668+
name = "openai-1",
669+
provider = "openai",
670+
weight = 1,
671+
auth = { header = { apikey = "token" } },
672+
options = { model = "gpt-4" },
673+
},
674+
},
675+
logging = { summaries = true },
676+
})
677+
678+
local ok2, err2 = plugin.check_schema({
679+
instances = {
680+
{
681+
name = "openai-1",
682+
provider = "openai",
683+
weight = 1,
684+
auth = { header = { apikey = "token" } },
685+
options = { model = "gpt-4" },
686+
},
687+
},
688+
logging_schema = { summaries = true },
689+
})
690+
691+
-- Unknown field 'logging_schema' should be ignored if additionalProperties are allowed
692+
ngx.say((ok1 and "ok" or ("bad:" .. (err1 or ""))), ":", (ok2 and "ok" or "invalid"))
693+
}
694+
}
695+
--- request
696+
GET /t
697+
--- response_body
698+
ok:ok

t/plugin/ai-proxy.t

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -181,36 +181,6 @@ run_tests();
181181

182182
__DATA__
183183
184-
=== TEST 0: schema accepts 'logging' and ignores unknown 'logging_schema'
185-
--- config
186-
location /t {
187-
content_by_lua_block {
188-
local plugin = require("apisix.plugins.ai-proxy")
189-
190-
local ok1, err1 = plugin.check_schema({
191-
provider = "openai",
192-
auth = { header = { apikey = "token" } },
193-
options = { model = "gpt-4" },
194-
logging = { summaries = true, payloads = false },
195-
})
196-
197-
local ok2, err2 = plugin.check_schema({
198-
provider = "openai",
199-
auth = { header = { apikey = "token" } },
200-
options = { model = "gpt-4" },
201-
logging_schema = { summaries = true },
202-
})
203-
204-
-- APISIX schema allows additional properties unless explicitly disallowed.
205-
-- So unknown field 'logging_schema' should be ignored and still pass.
206-
ngx.say((ok1 and "ok" or ("bad:" .. (err1 or ""))), ":", (ok2 and "ok" or "invalid"))
207-
}
208-
}
209-
--- request
210-
GET /t
211-
--- response_body
212-
ok:ok
213-
214184
=== TEST 1: minimal viable configuration
215185
--- config
216186
location /t {
@@ -701,3 +671,49 @@ POST /embeddings
701671
--- error_code: 200
702672
--- response_body_like eval
703673
qr/.*text-embedding-ada-002*/
674+
675+
676+
677+
=== TEST 17: schema accepts 'logging' and ignores unknown 'logging_schema'
678+
--- config
679+
location /t {
680+
content_by_lua_block {
681+
local plugin = require("apisix.plugins.ai-proxy")
682+
683+
local ok1, err1 = plugin.check_schema({
684+
provider = "openai",
685+
auth = { header = { apikey = "token" } },
686+
options = { model = "gpt-4" },
687+
logging = { summaries = true, payloads = false },
688+
})
689+
690+
local ok2, err2 = plugin.check_schema({
691+
provider = "openai",
692+
auth = { header = { apikey = "token" } },
693+
options = { model = "gpt-4" },
694+
logging_schema = { summaries = true },
695+
})
696+
697+
-- APISIX schema allows additional properties unless explicitly disallowed.
698+
-- So unknown field 'logging_schema' should be ignored and still pass.
699+
local msg1
700+
if ok1 then
701+
msg1 = "ok"
702+
else
703+
msg1 = "bad:" .. (err1 or "")
704+
end
705+
706+
local msg2
707+
if ok2 then
708+
msg2 = "ok"
709+
else
710+
msg2 = "invalid"
711+
end
712+
713+
ngx.say(msg1, ":", msg2)
714+
}
715+
}
716+
--- request
717+
GET /t
718+
--- response_body
719+
ok:ok

0 commit comments

Comments
 (0)