<commit_before># Copyright 2016 The Switch Authors. All rights reserved.
# Licensed under the Apache License, Version 2, which is in the LICENSE file.

import os
import shutil
import sys
import tempfile
import unittest

import switch_mod.solve
import switch_mod.utilities

# This runs all the Switch examples (in the 'examples' directory) as
# test cases.


TOP_DIR = os.path.dirname(os.path.dirname(__file__))

UPDATE_EXPECTATIONS = False


def read_file(filename):
    with open(filename, "r") as fh:
        return fh.read()


def write_file(filename, data):
    with open(filename, "w") as fh:
        fh.write(data)


def find_example_dirs():
    examples_dir = os.path.join(TOP_DIR, 'examples')
    for dirpath, dirnames, filenames in os.walk(examples_dir):
        for dirname in dirnames:
            path = os.path.join(dirpath, dirname)
            if os.path.exists(os.path.join(path, 'inputs', 'modules.txt')):
                yield path


def make_test(example_dir):
    def test_example():
        temp_dir = tempfile.mkdtemp(prefix='switch_test_')
        try:
            switch_mod.solve.main([
                '--inputs-dir', os.path.join(example_dir, 'inputs'),
                '--outputs-dir', temp_dir])
            total_cost = read_file(os.path.join(temp_dir, 'total_cost.txt'))
        finally:
            shutil.rmtree(temp_dir)
        expectation_file = os.path.join(example_dir, 'outputs',
                                        'total_cost.txt')
        if UPDATE_EXPECTATIONS:
            write_file(expectation_file, total_cost)
        else:
            expected = float(read_file(expectation_file))
            actual = float(total_cost)
            if not switch_mod.utilities.approx_equal(expected, actual,
                                                     tolerance=0.0001):
                raise AssertionError(
                    'Mismatch for total_cost (the objective function value):\n'
                    'Expected value:  {}\n'
                    'Actual value:    {}\n'
                    'Run "tests/examples_test.py --update" to update the '
                    'expectations if this change is expected.'
                    .format(expected, actual))

    name = os.path.relpath(example_dir, TOP_DIR)
    return unittest.FunctionTestCase(
        test_example, description='Example: %s' % name)


def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()
    for example_dir in find_example_dirs():
        suite.addTest(make_test(example_dir))
    return suite


if __name__ == '__main__':
    if sys.argv[1:2] == ['--update']:<commit_msg>Skip example directories that do not have the expectation file specified. This deals with the tricky issue of providing example usage of PySP, which has poor integration with python scripting (i.e. PySP lacks a python API, and instead directs you to command line tools). As we develop better integration with PySP, we will need to integrate it with automated testing, but that is too tricky to deal with for now.<commit_after># Copyright 2016 The Switch Authors. All rights reserved.
# Licensed under the Apache License, Version 2, which is in the LICENSE file.

import os
import shutil
import sys
import tempfile
import unittest

import switch_mod.solve
import switch_mod.utilities

# This runs all the Switch examples (in the 'examples' directory) as
# test cases.


TOP_DIR = os.path.dirname(os.path.dirname(__file__))

UPDATE_EXPECTATIONS = False


def read_file(filename):
    with open(filename, "r") as fh:
        return fh.read()


def write_file(filename, data):
    with open(filename, "w") as fh:
        fh.write(data)


def find_example_dirs():
    examples_dir = os.path.join(TOP_DIR, 'examples')
    for dirpath, dirnames, filenames in os.walk(examples_dir):
        for dirname in dirnames:
            path = os.path.join(dirpath, dirname)
            if os.path.exists(os.path.join(path, 'inputs', 'modules.txt')):
                yield path


def get_expectation_path(example_dir):
    expectation_file = os.path.join(example_dir, 'outputs',
                                        'total_cost.txt')
    if not os.path.isfile( expectation_file ):
        return False
    else:
        return expectation_file


def make_test(example_dir):
    def test_example():
        temp_dir = tempfile.mkdtemp(prefix='switch_test_')
        try:
            switch_mod.solve.main([
                '--inputs-dir', os.path.join(example_dir, 'inputs'),
                '--outputs-dir', temp_dir])
            total_cost = read_file(os.path.join(temp_dir, 'total_cost.txt'))
        finally:
            shutil.rmtree(temp_dir)
        expectation_file = get_expectation_path(example_dir)
        if UPDATE_EXPECTATIONS:
            write_file(expectation_file, total_cost)
        else:
            expected = float(read_file(expectation_file))
            actual = float(total_cost)
            if not switch_mod.utilities.approx_equal(expected, actual,
                                                     tolerance=0.0001):
                raise AssertionError(
                    'Mismatch for total_cost (the objective function value):\n'
                    'Expected value:  {}\n'
                    'Actual value:    {}\n'
                    'Run "tests/examples_test.py --update" to update the '
                    'expectations if this change is expected.'
                    .format(expected, actual))

    name = os.path.relpath(example_dir, TOP_DIR)
    return unittest.FunctionTestCase(
        test_example, description='Example: %s' % name)


def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()
    for example_dir in find_example_dirs():
        if get_expectation_path(example_dir):
            suite.addTest(make_test(example_dir))
    return suite


if __name__ == '__main__':
    if sys.argv[1:2] == ['--update']:
{'fraction_non_alphanumeric': 0.09593468276917844, 'fraction_numerical': 0.004082326926348018, 'mean_word_length': 2.3676975945017182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3478147', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1652, 'n_words': 459}