Skip to content

Commit

Permalink
Merge pull request #11 from instructions-d-installation/test_overhaul
Browse files Browse the repository at this point in the history
Test overhaul
  • Loading branch information
TimoEg authored May 20, 2024
2 parents 6b501ed + 89db817 commit 819a641
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 84 deletions.
36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import pytest
import os
import yaml
import json


@pytest.fixture
def test_data_flags_options():
file_path = os.path.join(os.path.dirname(__file__), 'data', 'flags_options_example.schema.yml')
with open(file_path,"r") as file:
return yaml.safe_load(file)

@pytest.fixture
def test_data_user_input(request):
file_name = request.param
file_path = os.path.join(os.path.dirname(__file__), 'data', file_name)
with open(file_path,"r") as file:
data = file.read()
test_input = json.loads(data)
return [file_name,test_input]

44 changes: 44 additions & 0 deletions tests/data/flags_options_example.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/examples/scikit-learn/scikit-learn-instruction.schema.yml
title: Scikit-learn installation schema
description: This is a Schema to construct installation instructions for the python package scikit-learn by Timo Ege.
type: object
properties:
os:
title: Operating System
description: The operating system in which the package is installed.
enum:
- Windows
- macOS
- Linux
default: Windows

packager:
title: Packager
description: The package manager of your choosing.
enum:
- pip
- conda
default: pip

virtualenv:
title: Use pip virtualenv
description: Choose if you want to use a virtual environment to install the package.
type: boolean
default: false

compute_platform:
title: Compute Platform
description: Should your gpu or your cpu handle the task?
anyOf:
- title: CUDA 11.8
const: cu118
- title: CUDA 12.1
const: cu121
default: cu118

required:
- os
- packager

additionalProperties: false
6 changes: 6 additions & 0 deletions tests/data/pytorch_invalid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"build": "preview",
"os": "win",
"package": "piiip",
"compute_platform": "ro60"
}
6 changes: 6 additions & 0 deletions tests/data/pytorch_valid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"build": "preview",
"os": "win",
"package": "pip",
"compute_platform": "ro60"
}
5 changes: 5 additions & 0 deletions tests/data/scikit_invalid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"os": "Kali",
"packager": "pip",
"virtualenv": False
}
5 changes: 5 additions & 0 deletions tests/data/scikit_valid_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"os": "Windows",
"packager": "pip",
"virtualenv": False
}
50 changes: 3 additions & 47 deletions tests/test_get_flags_and_options_from_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,9 @@
import click
import yaml

def test_get_flags_and_options():
example_schema = '''$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/examples/scikit-learn/scikit-learn-instruction.schema.yml
title: Scikit-learn installation schema
description: This is a Schema to construct installation instructions for the python package scikit-learn by Timo Ege.
type: object
properties:
os:
title: Operating System
description: The operating system in which the package is installed.
enum:
- Windows
- macOS
- Linux
default: Windows
packager:
title: Packager
description: The package manager of your choosing.
enum:
- pip
- conda
default: pip
virtualenv:
title: Use pip virtualenv
description: Choose if you want to use a virtual environment to install the package.
type: boolean
default: false
compute_platform:
title: Compute Platform
description: Should your gpu or your cpu handle the task?
anyOf:
- title: CUDA 11.8
const: cu118
- title: CUDA 12.1
const: cu121
default: cu118
required:
- os
- packager
additionalProperties: false'''
example = yaml.safe_load(example_schema)
options = get_flags_and_options(example)
def test_get_flags_and_options(test_data_flags_options):
example_schema = test_data_flags_options
options = get_flags_and_options(example_schema)

assert len(options) == 4

Expand Down
60 changes: 23 additions & 37 deletions tests/test_installation_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,39 @@
# limitations under the License.


from pytest import raises
import pytest

from installation_instruction.installation_instruction import InstallationInstruction

def test_validate_and_render_pytorch():
valid_user_input = {
"build": "preview",
"os": "win",
"package": "pip",
"compute_platform": "ro60"
}
@pytest.mark.parametrize("test_data_user_input",["pytorch_invalid_data.txt","pytorch_valid_data.txt"])
def test_validate_and_render_pytorch(test_data_user_input):

bad_user_input = {
"build": "preview",
"os": "win",
"package": "piiip",
"compute_platform": "ro60"
}

test_data = test_data_user_input
file_name = test_data[0]
user_input = test_data[1]
install = InstallationInstruction.from_file("examples/pytorch/pytorch-instruction.schema.yml.jinja")

good_installation_instruction = install.validate_and_render(valid_user_input)

assert ('Windows does not support ROCm!', True) == good_installation_instruction

with raises(Exception):
install.validate_and_render(bad_user_input)

if "_valid_" in file_name:
good_installation_instruction = install.validate_and_render(user_input)

def test_validate_and_render_scikit():
valid_user_input = {
"os": "Windows",
"packager": "pip",
"virtualenv": False
}
assert ('Windows does not support ROCm!', True) == good_installation_instruction
elif "_invalid_" in file_name:
with pytest.raises(Exception):
install.validate_and_render(user_input)

invalid_user_input = {
"os": "Kali",
"packager": "pip",
"virtualenv": False
}
@pytest.mark.parametrize("test_data_user_input",["scikit_invalid_data.txt","scikit_valid_data.txt"])
def test_validate_and_render_scikit(test_data_user_input):
test_data = test_data_user_input
file_name = test_data[0]
user_input = test_data[1]

install = InstallationInstruction.from_file("examples/scikit-learn/scikit-learn-instruction.schema.yml.jinja")

good_installation_instruction = install.validate_and_render(valid_user_input)
if "_valid_" in file_name:
good_installation_instruction = install.validate_and_render(user_input)

assert ('pip install -U scikit-learn', False) == good_installation_instruction
assert ('pip install -U scikit-learn', False) == good_installation_instruction

with raises(Exception):
install.validate_and_render(invalid_user_input)
elif "_invalid_" in file_name:
with pytest.raises(Exception):
install.validate_and_render(user_input)

0 comments on commit 819a641

Please sign in to comment.