def test_get_task(mock_session): fake_task = models.Task() mock_session.return_value.__enter__.return_value. \ query.return_value.filter.return_value.first.return_value = \ fake_task result = api.get_task(None, 'a9e54256-2b8b-47d9-8ca1-355db52d60f1') assert result == fake_task
def test_create_task(mock_session, mock_uuid): mock_uuid.return_value = '85a56708-a072-4525-9cc2-bb2f4e4a93e1' expected = models.Task() expected.id = '85a56708-a072-4525-9cc2-bb2f4e4a93e1' expected.name = 'volume provsioning' actual = api.create_task(None, dict(name='volume provsioning')) mock_session.return_value.__enter__.return_value.add.assert_called_once() for key, value in actual.__dict__.items(): if hasattr(models.Task, key): assert getattr(expected, key) == value
def create_task(context, values): task = models.Task() for key, value in values.items(): if hasattr(task, key): setattr(task, key, value) if not task.id: task.id = uuid.uuid4() with session_scope() as session: session.add(task) return task