Skip to content

Commit 8c9b813

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

File tree

4 files changed

+169
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)