-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_example_session.py
More file actions
221 lines (180 loc) · 6.64 KB
/
Copy pathtest_example_session.py
File metadata and controls
221 lines (180 loc) · 6.64 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
###############################################################################
#
# MIT License
#
# Copyright (c) 2024 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
"""Tests for tuna.example.session"""
import os
import sys
import argparse
from unittest.mock import Mock, MagicMock, patch
sys.path.append("../tuna")
sys.path.append("tuna")
from tuna.example.session import SessionExample
from tuna.db.session_mixin import SessionMixin
from sqlalchemy import inspect
from sqlalchemy import UniqueConstraint
def test_session_example_table_name():
"""Test SessionExample __tablename__"""
session = SessionExample()
assert hasattr(SessionExample, '__tablename__')
assert SessionExample.__tablename__ == "session_example"
def test_session_example_unique_constraint():
"""Test SessionExample UniqueConstraint"""
assert hasattr(SessionExample, '__table_args__')
constraints = SessionExample.__table_args__
# Find UniqueConstraint
has_unique_constraint = False
for constraint in constraints:
if isinstance(constraint, UniqueConstraint):
assert 'arch' in constraint.columns
assert 'num_cu' in constraint.columns
assert 'rocm_v' in constraint.columns
assert 'reason' in constraint.columns
assert 'docker' in constraint.columns
assert constraint.name == "uq_idx"
has_unique_constraint = True
assert has_unique_constraint, "UniqueConstraint not found"
def test_session_example_inherits_session_mixin():
"""Test SessionExample inherits from SessionMixin"""
session = SessionExample()
assert isinstance(session, SessionMixin)
def test_get_query_filters():
"""Test get_query method filters correctly"""
session_obj = SessionExample()
mock_session = MagicMock()
# Create a mock query object
mock_query = MagicMock()
mock_session.query.return_value = mock_query
mock_query.filter.return_value = mock_query
# Create test entry
entry = argparse.Namespace()
entry.arch = 'gfx90a'
entry.num_cu = 104
entry.rocm_v = '5.7.1'
entry.reason = 'test_reason'
entry.docker = 'miopentuna'
# Create session object with same values
sess_obj = SessionExample()
sess_obj.arch = 'gfx90a'
sess_obj.num_cu = 104
sess_obj.rocm_v = '5.7.1'
sess_obj.reason = 'test_reason'
sess_obj.docker = 'miopentuna'
# Call get_query
query = session_obj.get_query(mock_session, SessionExample, entry)
# Verify query was created (it returns mock_query in our test)
assert query is not None
def test_add_new_session():
"""Test add_new_session method"""
session = SessionExample()
# Create mock args
args = argparse.Namespace()
args.label = 'test_label'
args.docker_name = 'test_docker'
args.arch = 'gfx90a'
args.num_cu = 104
args.rocm_v = '5.7.1'
args.ticket = 'TEST-123'
# Create mock worker
worker = Mock()
worker.machine = Mock()
worker.machine.arch = 'gfx906'
worker.machine.num_cu = 60
worker.get_rocm_v = Mock(return_value='5.6.0')
# Call add_new_session
session.add_new_session(args, worker)
# Verify values were set
assert session.reason == 'test_label'
assert session.docker == 'test_docker'
assert session.arch == 'gfx90a' # From args
assert session.num_cu == 104 # From args
assert session.rocm_v == '5.7.1' # From args
assert session.ticket == 'TEST-123'
def test_add_new_session_falls_back_to_worker():
"""Test add_new_session falls back to worker values when args missing"""
session = SessionExample()
# Create mock args without arch/num_cu/rocm_v
args = argparse.Namespace()
args.label = 'test_label'
args.docker_name = 'test_docker'
args.arch = None
args.num_cu = None
args.rocm_v = None
args.ticket = None
# Create mock worker with machine
worker = Mock()
worker.machine = Mock()
worker.machine.arch = 'gfx906'
worker.machine.num_cu = 60
worker.get_rocm_v = Mock(return_value='5.6.0')
# Call add_new_session
session.add_new_session(args, worker)
# Verify values fell back to worker
assert session.reason == 'test_label'
assert session.docker == 'test_docker'
assert session.arch == 'gfx906' # From worker
assert session.num_cu == 60 # From worker
assert session.rocm_v == '5.6.0' # From worker
assert session.ticket == 'N/A' # Default
def test_add_new_session_calls_parent_and_insert():
"""Test add_new_session calls parent method and insert_session"""
session = SessionExample()
# Create mock args and worker
args = argparse.Namespace()
args.label = 'test_label'
args.docker_name = 'test_docker'
args.arch = 'gfx90a'
args.num_cu = 104
args.rocm_v = '5.7.1'
args.ticket = 'N/A'
worker = Mock()
worker.machine = Mock()
worker.machine.arch = 'gfx90a'
worker.machine.num_cu = 104
worker.get_rocm_v = Mock(return_value='5.7.1')
# Mock insert_session to return a session ID
session.insert_session = Mock(return_value=42)
# Call add_new_session
result = session.add_new_session(args, worker)
# Verify insert_session was called and result returned
assert session.insert_session.called
assert result == 42
def test_add_new_session_returns_session_id():
"""Test add_new_session returns session ID"""
session = SessionExample()
args = argparse.Namespace()
args.label = 'test_label'
args.docker_name = 'test_docker'
args.arch = 'gfx90a'
args.num_cu = 104
args.rocm_v = '5.7.1'
worker = Mock()
worker.machine = Mock()
worker.machine.arch = 'gfx90a'
worker.machine.num_cu = 104
worker.get_rocm_v = Mock(return_value='5.7.1')
# Mock insert_session
with patch.object(session, 'insert_session', return_value=123):
result = session.add_new_session(args, worker)
assert result == 123