Skip to content

Commit c9ba24d

Browse files
committed
[IMP] add tests for request.env
1 parent bc83889 commit c9ba24d

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from . import models
2-
from . import data
2+
from . import data
3+
from . import controllers
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
from . import test_http
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
from odoo import http
3+
from odoo.http import request
4+
5+
6+
class TestHttpController(http.Controller):
7+
8+
@http.route('/test/request', type='http', auth='public')
9+
def test_request_type(self):
10+
"""Test that request has correct type."""
11+
# Hovering over 'request' should show Request class
12+
req = request
13+
14+
# Hovering over 'request.env' should show Environment | None
15+
env = request.env
16+
17+
# Accessing models via request.env
18+
if request.env:
19+
partner = request.env['res.partner']
20+
users = request.env['res.users']
21+
22+
return "OK"
23+
24+
@http.route('/test/request/search', type='http', auth='user')
25+
def test_request_env_usage(self):
26+
"""Test using request.env in typical scenarios."""
27+
# Direct model access
28+
partners = request.env['res.partner'].search([])
29+
30+
# With sudo
31+
admin_partners = request.env['res.partner'].sudo().search([])
32+
33+
# Accessing user
34+
current_user = request.env.user
35+
36+
return "OK"

server/tests/test_hooks.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
use std::cell::RefCell;
4+
use std::rc::Rc;
5+
6+
use odoo_ls_server::core::odoo::SyncOdoo;
7+
use odoo_ls_server::core::symbols::symbol::Symbol;
8+
use odoo_ls_server::core::file_mgr::FileInfo;
9+
use odoo_ls_server::utils::PathSanitizer;
10+
use odoo_ls_server::threads::SessionInfo;
11+
12+
mod setup;
13+
mod test_utils;
14+
15+
/// Test that odoo.http.request and request.env hooks work correctly.
16+
/// Tests hover and definition.
17+
#[test]
18+
fn test_request_hooks() {
19+
let (mut odoo, config) = setup::setup::setup_server(true);
20+
let mut session = setup::setup::create_init_session(&mut odoo, config);
21+
22+
// Check Odoo version (request hooks require 15.3+)
23+
if session.sync_odoo.version_major < 15 || (session.sync_odoo.version_major == 15 && session.sync_odoo.version_minor < 3) {
24+
panic!("Skipping test_request_hooks: Odoo version < 15.3");
25+
}
26+
27+
let test_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
28+
.join("tests/data/addons/module_1/controllers/test_http.py")
29+
.sanitize();
30+
31+
let Some(file_symbol) = SyncOdoo::get_symbol_of_opened_file(&mut session, &PathBuf::from(&test_file)) else {
32+
panic!("Failed to get file symbol for {}", test_file);
33+
};
34+
35+
let file_mgr = session.sync_odoo.get_file_mgr();
36+
let file_info = file_mgr.borrow().get_file_info(&test_file).unwrap();
37+
38+
test_request_type_hover(&mut session, &file_symbol, &file_info);
39+
test_request_env_definition(&mut session, &file_symbol, &file_info);
40+
}
41+
42+
/// Test that hovering over 'request' shows Request class
43+
/// and that hovering over 'request.env' shows Environment type
44+
fn test_request_type_hover(
45+
session: &mut SessionInfo,
46+
file_symbol: &Rc<RefCell<Symbol>>,
47+
file_info: &Rc<RefCell<FileInfo>>
48+
) {
49+
// Test 1: Hover over 'request' variable (line 11: req = request)
50+
// Should show Request class
51+
let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 11, 14)
52+
.expect("Should get hover text for request");
53+
54+
assert!(
55+
hover_text.contains("Request"),
56+
"Hover over 'request' should show Request class. Got: {}",
57+
hover_text
58+
);
59+
60+
// Test 2: Hover over 'request.env' (line 14: env = request.env)
61+
// Should show Environment | None
62+
let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 14, 21)
63+
.expect("Should get hover text for request.env");
64+
65+
assert!(
66+
hover_text.contains("Environment"),
67+
"Hover over 'request.env' should show Environment type. Got: {}",
68+
hover_text
69+
);
70+
assert!(
71+
hover_text.contains("None"),
72+
"Hover over 'request.env' should show None. Got: {}",
73+
hover_text
74+
);
75+
}
76+
77+
/// Test that request.env provides correct definitions
78+
fn test_request_env_definition(
79+
session: &mut SessionInfo,
80+
file_symbol: &Rc<RefCell<Symbol>>,
81+
file_info: &Rc<RefCell<FileInfo>>
82+
) {
83+
// Test 1: Go-to-definition on 'request' import (line 3: from odoo.http import request)
84+
// Should navigate to request variable in odoo.http
85+
let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 2, 22);
86+
87+
assert!(
88+
!definitions.is_empty(),
89+
"Should find definition for 'request' import"
90+
);
91+
92+
// Verify it points to odoo/http.py or similar
93+
let target_uri = &definitions[0].target_uri.to_string();
94+
assert!(
95+
target_uri.contains("odoo/http.py"),
96+
"Definition should point to http module. Got: {:?}",
97+
target_uri
98+
);
99+
100+
// Test 2: Go-to-definition on 'request.env' (line 15: env = request.env)
101+
// Should navigate to Environment class or env attribute
102+
let definitions = test_utils::get_definition_locs(session, file_symbol, file_info, 14, 21);
103+
104+
assert!(
105+
!definitions.is_empty(),
106+
"Should find definition for 'request.env'"
107+
);
108+
109+
// Test 3: Hover on request.env.user (line 34: current_user = request.env.user)
110+
// Should show proper type for user
111+
let hover_text = test_utils::get_hover_markdown(session, file_symbol, file_info, 33, 35);
112+
113+
if let Some(text) = hover_text {
114+
// env.user should have a type (typically res.users or User)
115+
assert!(
116+
!text.is_empty(),
117+
"request.env.user should have hover information"
118+
);
119+
}
120+
}
121+
122+

0 commit comments

Comments
 (0)