Skip to content

Commit 1229e40

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

File tree

4 files changed

+163
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)