<commit_before># -*- coding: utf-8 -*-

"""Unit tests for cartoframes.dataset about is_sync prop"""
import unittest
import os
import json
import warnings
import pandas as pd

from cartoframes.context import CartoContext
from cartoframes.dataset import Dataset
from cartoframes.geojson import load_geojson

from utils import _UserUrlLoader

warnings.filterwarnings("ignore")

class TestDatasetSync(unittest.TestCase, _UserUrlLoader):
    def setUp(self):
        if (os.environ.get('APIKEY') is None or
                os.environ.get('USERNAME') is None):
            try:
                creds = json.loads(open('test/secret.json').read())
                self.apikey = creds['APIKEY']
                self.username = creds['USERNAME']
            except:  # noqa: E722
                warnings.warn("Skipping CartoContext tests. To test it, "
                              "create a `secret.json` file in test/ by "
                              "renaming `secret.json.sample` to `secret.json` "
                              "and updating the credentials to match your "
                              "environment.")
                self.apikey = None
                self.username = None
        else:
            self.apikey = os.environ['APIKEY']
            self.username = os.environ['USERNAME']

        self.baseurl = self.user_url().format(username=self.username)
        self.cc = CartoContext(base_url=self.baseurl, api_key=self.apikey)

        self.test_geojson = {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "Point",
                        "coordinates": [
                            -3.1640625,
                            42.032974332441405
                        ]
                    }
                }
            ]
        }

        self.create_dataset_mock()

    def create_dataset_mock(self):
        def mock_download(self):
            self.df = pd.DataFrame({'column_name': [1]})
            return self.df
        Dataset.download = mock_download

        def mock_copyfrom(self, _):
            return True
        Dataset._copyfrom = mock_copyfrom

        def mock_create_table(self, _):
            return True
        Dataset._create_table = mock_create_table

        def mock_create_table_from_query(self):
            return True
        Dataset._create_table_from_query = mock_create_table_from_query

        def mock_exists(self):
            return False
        Dataset.exists = mock_exists

    def test_dataset_sync_from_table(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_table(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.table_name = 'another_table'
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_table_name(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.table_name = table_name
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_schema(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.schema = 'another_schema'
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_schema(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.schema = dataset.schema
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_context(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.cc = None
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_context(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.cc = dataset.cc
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_table_after_download(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_table_after_upload(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_not_sync_from_table_modify_df(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_modify_df_and_upload(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')
        dataset.df = pd.DataFrame({'column_name': [3]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_query(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_query_and_upload(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_sync_from_query_download_modify_upload(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        dataset.download()
        self.assertEqual(dataset.is_sync, False)
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_not_sync_from_dataframe(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_dataframe_upload(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_dataframe_still_sync_if_df_is_the_same(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        dataset.df = dataset.df
        self.assertEqual(dataset.is_sync, True)
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_dataframe_overwriting_df(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        dataset.df = pd.DataFrame({'column_name': [3]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_geodataframe(self):
        gdf = load_geojson(self.test_geojson)
        dataset = Dataset.from_geodataframe(gdf=gdf)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_geojson(self):
        geojson = self.test_geojson
        dataset = Dataset.from_geojson(geojson=geojson)
        self.assertEqual(dataset.is_sync, False)
<commit_msg>restore dataset class<commit_after># -*- coding: utf-8 -*-

"""Unit tests for cartoframes.dataset about is_sync prop"""
import unittest
import os
import json
import warnings
import pandas as pd

from cartoframes.context import CartoContext
from cartoframes.dataset import Dataset
from cartoframes.geojson import load_geojson

from utils import _UserUrlLoader

warnings.filterwarnings("ignore")

class TestDatasetSync(unittest.TestCase, _UserUrlLoader):
    def setUp(self):
        if (os.environ.get('APIKEY') is None or
                os.environ.get('USERNAME') is None):
            try:
                creds = json.loads(open('test/secret.json').read())
                self.apikey = creds['APIKEY']
                self.username = creds['USERNAME']
            except:  # noqa: E722
                warnings.warn("Skipping CartoContext tests. To test it, "
                              "create a `secret.json` file in test/ by "
                              "renaming `secret.json.sample` to `secret.json` "
                              "and updating the credentials to match your "
                              "environment.")
                self.apikey = None
                self.username = None
        else:
            self.apikey = os.environ['APIKEY']
            self.username = os.environ['USERNAME']

        self.baseurl = self.user_url().format(username=self.username)
        self.cc = CartoContext(base_url=self.baseurl, api_key=self.apikey)

        self.test_geojson = {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "Point",
                        "coordinates": [
                            -3.1640625,
                            42.032974332441405
                        ]
                    }
                }
            ]
        }

        self.create_dataset_class_mock()

    def tearDown(self):
        self.restore_dataset_class()

    def create_dataset_class_mock(self):
        self.previous_dataset = {}

        def mock_download(self):
            self.df = pd.DataFrame({'column_name': [1]})
            return self.df
        self.previous_dataset['download'] = Dataset.download
        Dataset.download = mock_download

        def mock_copyfrom(self, _):
            return True
        self.previous_dataset['_copyfrom'] = Dataset._copyfrom
        Dataset._copyfrom = mock_copyfrom

        def mock_create_table(self, _):
            return True
        self.previous_dataset['_create_table'] = Dataset._create_table
        Dataset._create_table = mock_create_table

        def mock_create_table_from_query(self):
            return True
        self.previous_dataset['_create_table_from_query'] = Dataset._create_table_from_query
        Dataset._create_table_from_query = mock_create_table_from_query

        def mock_exists(self):
            return False
        self.previous_dataset['exists'] = Dataset.exists
        Dataset.exists = mock_exists

    def restore_dataset_class(self):
        Dataset.download = self.previous_dataset['download']
        Dataset._copyfrom = self.previous_dataset['_copyfrom']
        Dataset._create_table = self.previous_dataset['_create_table']
        Dataset._create_table_from_query = self.previous_dataset['_create_table_from_query']
        Dataset.exists = self.previous_dataset['exists']

    def test_dataset_sync_from_table(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_table(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.table_name = 'another_table'
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_table_name(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.table_name = table_name
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_schema(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.schema = 'another_schema'
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_schema(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.schema = dataset.schema
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_modified_context(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.cc = None
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_is_sync_if_modified_with_the_same_context(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.cc = dataset.cc
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_table_after_download(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_table_after_upload(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_not_sync_from_table_modify_df(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_table_modify_df_and_upload(self):
        table_name = 'fake_table'
        dataset = Dataset.from_table(table_name=table_name, context=self.cc)
        dataset.download()
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')
        dataset.df = pd.DataFrame({'column_name': [3]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_query(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_query_and_upload(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_sync_from_query_download_modify_upload(self):
        query = "SELECT 1"
        dataset = Dataset.from_query(query=query, context=self.cc)
        dataset.download()
        self.assertEqual(dataset.is_sync, False)
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, False)
        dataset.upload(table_name='another_table')
        self.assertEqual(dataset.is_sync, True)
        self.assertEqual(dataset.table_name, 'another_table')

    def test_dataset_not_sync_from_dataframe(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_sync_from_dataframe_upload(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_sync_from_dataframe_still_sync_if_df_is_the_same(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        dataset.df = dataset.df
        self.assertEqual(dataset.is_sync, True)
        dataset.df = pd.DataFrame({'column_name': [2]})
        self.assertEqual(dataset.is_sync, True)

    def test_dataset_not_sync_from_dataframe_overwriting_df(self):
        df = pd.DataFrame({'column_name': [2]})
        dataset = Dataset.from_dataframe(df=df)
        dataset.upload(table_name='another_table', context=self.cc)
        dataset.df = pd.DataFrame({'column_name': [3]})
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_geodataframe(self):
        gdf = load_geojson(self.test_geojson)
        dataset = Dataset.from_geodataframe(gdf=gdf)
        self.assertEqual(dataset.is_sync, False)

    def test_dataset_not_sync_from_geojson(self):
        geojson = self.test_geojson
        dataset = Dataset.from_geojson(geojson=geojson)
        self.assertEqual(dataset.is_sync, False)
<|endoftext|>


<commit_before># Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""A test base class for tests based on gold file comparison."""

import difflib
import filecmp
import fnmatch
import os
import os.path
import re
import sys
import xml.etree.ElementTree

from tests.coveragetest import TESTS_DIR

def gold_path(path):
    """Get a path to a gold file for comparison."""
    return os.path.join(TESTS_DIR, "gold", path)

def versioned_directory(d):
    """Find a subdirectory of d specific to the Python version.
    For example, on Python 3.6.4 rc 1, it returns the first of these
    directories that exists::
        d/3.6.4.candidate.1
        d/3.6.4.candidate
        d/3.6.4
        d/3.6
        d/3
        d
    Returns: a string, the path to an existing directory.
    """
    ver_parts = list(map(str, sys.version_info))
    for nparts in range(len(ver_parts), -1, -1):
        version = ".".join(ver_parts[:nparts])
        subdir = os.path.join(d, version)
        if os.path.exists(subdir):
            return subdir
    raise Exception(f"Directory missing: {d}")                  # pragma: only failure

def compare(
        expected_dir, actual_dir, file_pattern=None,
        actual_extra=False, scrubs=None,
        ):
    """Compare files matching `file_pattern` in `expected_dir` and `actual_dir`.

    A version-specific subdirectory of `expected_dir` will be used if
    it exists.

    `actual_extra` true means `actual_dir` can have extra files in it
    without triggering an assertion.

    `scrubs` is a list of pairs: regexes to find and replace to scrub the
    files of unimportant differences.

    An assertion will be raised if the directories fail one of their
    matches.

    """
    expected_dir = versioned_directory(expected_dir)

    dc = filecmp.dircmp(expected_dir, actual_dir)
    diff_files = fnmatch_list(dc.diff_files, file_pattern)
    expected_only = fnmatch_list(dc.left_only, file_pattern)
    actual_only = fnmatch_list(dc.right_only, file_pattern)

    def save_mismatch(f):
        """Save a mismatched result to tests/actual."""
        save_path = expected_dir.replace("/gold/", "/actual/")
        os.makedirs(save_path, exist_ok=True)
        with open(os.path.join(save_path, f), "w") as savef:
            with open(os.path.join(actual_dir, f)) as readf:
                savef.write(readf.read())

    # filecmp only compares in binary mode, but we want text mode.  So
    # look through the list of different files, and compare them
    # ourselves.
    text_diff = []
    for f in diff_files:

        expected_file = os.path.join(expected_dir, f)
        with open(expected_file) as fobj:
            expected = fobj.read()
        if expected_file.endswith(".xml"):
            expected = canonicalize_xml(expected)

        actual_file = os.path.join(actual_dir, f)
        with open(actual_file) as fobj:
            actual = fobj.read()
        if actual_file.endswith(".xml"):
            actual = canonicalize_xml(actual)

        if scrubs:
            expected = scrub(expected, scrubs)
            actual = scrub(actual, scrubs)
        if expected != actual:                              # pragma: only failure
            text_diff.append(f'{expected_file} != {actual_file}')
            expected = expected.splitlines()
            actual = actual.splitlines()
            print(f":::: diff {expected_file!r} and {actual_file!r}")
            print("\n".join(difflib.Differ().compare(expected, actual)))
            print(f":::: end diff {expected_file!r} and {actual_file!r}")
            save_mismatch(f)

    if not actual_extra:
        for f in actual_only:
            save_mismatch(f)

    assert not text_diff, "Files differ: %s" % '\n'.join(text_diff)

    assert not expected_only, f"Files in {expected_dir} only: {expected_only}"
    if not actual_extra:
        assert not actual_only, f"Files in {actual_dir} only: {actual_only}"

def canonicalize_xml(xtext):
    """Canonicalize some XML text."""
    root = xml.etree.ElementTree.fromstring(xtext)
    for node in root.iter():
        node.attrib = dict(sorted(node.items()))
    xtext = xml.etree.ElementTree.tostring(root)
    return xtext.decode('utf8')

def contains(filename, *strlist):
    """Check that the file contains all of a list of strings.

    An assert will be raised if one of the arguments in `strlist` is
    missing in `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        assert s in text, f"Missing content in {filename}: {s!r}"

def contains_any(filename, *strlist):
    """Check that the file contains at least one of a list of strings.

    An assert will be raised if none of the arguments in `strlist` is in
    `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        if s in text:
            return

    assert False, (                         # pragma: only failure
        "Missing content in %s: %r [1 of %d]" % (filename, strlist[0], len(strlist),)
    )

def doesnt_contain(filename, *strlist):
    """Check that the file contains none of a list of strings.

    An assert will be raised if any of the strings in `strlist` appears in
    `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        assert s not in text, f"Forbidden content in {filename}: {s!r}"

# Helpers

def fnmatch_list(files, file_pattern):
    """Filter the list of `files` to only those that match `file_pattern`.
    If `file_pattern` is None, then return the entire list of files.
    Returns a list of the filtered files.
    """
    if file_pattern:
        files = [f for f in files if fnmatch.fnmatch(f, file_pattern)]
    return files

def scrub(strdata, scrubs):
    """Scrub uninteresting data from the payload in `strdata`.
    `scrubs` is a list of (find, replace) pairs of regexes that are used on
    `strdata`.  A string is returned.
    """
    for rgx_find, rgx_replace in scrubs:
        strdata = re.sub(rgx_find, rgx_replace, strdata)
    return strdata
<commit_msg>test: mark some only-failure code in the recent goldtest changes<commit_after># Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""A test base class for tests based on gold file comparison."""

import difflib
import filecmp
import fnmatch
import os
import os.path
import re
import sys
import xml.etree.ElementTree

from tests.coveragetest import TESTS_DIR

def gold_path(path):
    """Get a path to a gold file for comparison."""
    return os.path.join(TESTS_DIR, "gold", path)

def versioned_directory(d):
    """Find a subdirectory of d specific to the Python version.
    For example, on Python 3.6.4 rc 1, it returns the first of these
    directories that exists::
        d/3.6.4.candidate.1
        d/3.6.4.candidate
        d/3.6.4
        d/3.6
        d/3
        d
    Returns: a string, the path to an existing directory.
    """
    ver_parts = list(map(str, sys.version_info))
    for nparts in range(len(ver_parts), -1, -1):
        version = ".".join(ver_parts[:nparts])
        subdir = os.path.join(d, version)
        if os.path.exists(subdir):
            return subdir
    raise Exception(f"Directory missing: {d}")                  # pragma: only failure

def compare(
        expected_dir, actual_dir, file_pattern=None,
        actual_extra=False, scrubs=None,
        ):
    """Compare files matching `file_pattern` in `expected_dir` and `actual_dir`.

    A version-specific subdirectory of `expected_dir` will be used if
    it exists.

    `actual_extra` true means `actual_dir` can have extra files in it
    without triggering an assertion.

    `scrubs` is a list of pairs: regexes to find and replace to scrub the
    files of unimportant differences.

    An assertion will be raised if the directories fail one of their
    matches.

    """
    expected_dir = versioned_directory(expected_dir)

    dc = filecmp.dircmp(expected_dir, actual_dir)
    diff_files = fnmatch_list(dc.diff_files, file_pattern)
    expected_only = fnmatch_list(dc.left_only, file_pattern)
    actual_only = fnmatch_list(dc.right_only, file_pattern)

    def save_mismatch(f):                                   # pragma: only failure
        """Save a mismatched result to tests/actual."""
        save_path = expected_dir.replace("/gold/", "/actual/")
        os.makedirs(save_path, exist_ok=True)
        with open(os.path.join(save_path, f), "w") as savef:
            with open(os.path.join(actual_dir, f)) as readf:
                savef.write(readf.read())

    # filecmp only compares in binary mode, but we want text mode.  So
    # look through the list of different files, and compare them
    # ourselves.
    text_diff = []
    for f in diff_files:

        expected_file = os.path.join(expected_dir, f)
        with open(expected_file) as fobj:
            expected = fobj.read()
        if expected_file.endswith(".xml"):
            expected = canonicalize_xml(expected)

        actual_file = os.path.join(actual_dir, f)
        with open(actual_file) as fobj:
            actual = fobj.read()
        if actual_file.endswith(".xml"):
            actual = canonicalize_xml(actual)

        if scrubs:
            expected = scrub(expected, scrubs)
            actual = scrub(actual, scrubs)
        if expected != actual:                              # pragma: only failure
            text_diff.append(f'{expected_file} != {actual_file}')
            expected = expected.splitlines()
            actual = actual.splitlines()
            print(f":::: diff {expected_file!r} and {actual_file!r}")
            print("\n".join(difflib.Differ().compare(expected, actual)))
            print(f":::: end diff {expected_file!r} and {actual_file!r}")
            save_mismatch(f)

    if not actual_extra:                                    # pragma: only failure
        for f in actual_only:
            save_mismatch(f)

    assert not text_diff, "Files differ: %s" % '\n'.join(text_diff)

    assert not expected_only, f"Files in {expected_dir} only: {expected_only}"
    if not actual_extra:
        assert not actual_only, f"Files in {actual_dir} only: {actual_only}"

def canonicalize_xml(xtext):
    """Canonicalize some XML text."""
    root = xml.etree.ElementTree.fromstring(xtext)
    for node in root.iter():
        node.attrib = dict(sorted(node.items()))
    xtext = xml.etree.ElementTree.tostring(root)
    return xtext.decode('utf8')

def contains(filename, *strlist):
    """Check that the file contains all of a list of strings.

    An assert will be raised if one of the arguments in `strlist` is
    missing in `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        assert s in text, f"Missing content in {filename}: {s!r}"

def contains_any(filename, *strlist):
    """Check that the file contains at least one of a list of strings.

    An assert will be raised if none of the arguments in `strlist` is in
    `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        if s in text:
            return

    assert False, (                         # pragma: only failure
        "Missing content in %s: %r [1 of %d]" % (filename, strlist[0], len(strlist),)
    )

def doesnt_contain(filename, *strlist):
    """Check that the file contains none of a list of strings.

    An assert will be raised if any of the strings in `strlist` appears in
    `filename`.

    """
    with open(filename) as fobj:
        text = fobj.read()
    for s in strlist:
        assert s not in text, f"Forbidden content in {filename}: {s!r}"

# Helpers

def fnmatch_list(files, file_pattern):
    """Filter the list of `files` to only those that match `file_pattern`.
    If `file_pattern` is None, then return the entire list of files.
    Returns a list of the filtered files.
    """
    if file_pattern:
        files = [f for f in files if fnmatch.fnmatch(f, file_pattern)]
    return files

def scrub(strdata, scrubs):
    """Scrub uninteresting data from the payload in `strdata`.
    `scrubs` is a list of (find, replace) pairs of regexes that are used on
    `strdata`.  A string is returned.
    """
    for rgx_find, rgx_replace in scrubs:
        strdata = re.sub(rgx_find, rgx_replace, strdata)
    return strdata
<|endoftext|>


<commit_before>import os
import datetime
from unittest import TestCase
from ipernity_api import ipernity, errors
from . import utils

def getfile(fname):
    return os.path.join(os.path.dirname(__file__), 'files', fname)

class IpernityTest(TestCase):
    def __init__(self, *arg, **kwargs):
        TestCase.__init__(self, *arg, **kwargs)
        utils.auto_auth()

    def test_Test(self):
        self.assertEquals('echo', ipernity.Test.echo(echo='echo'))
        self.assertIn('hello', ipernity.Test.hello())

    def test_User(self):
        user_id = 787135
        # get user test
        user = ipernity.User.get(id=user_id)
        self.assertIsInstance(user, ipernity.User)
        # filed test
        self.assertIsInstance(user.is_online, bool)
        self.assertIsInstance(user.is_closed, bool)
        self.assertIsInstance(user.is_pro, bool)
        # count test
        self.assertIsInstance(user.count['network'], int)
        self.assertIsInstance(user.dates['member_since'], datetime.datetime)

        # quota test
        quota = user.getQuota()
        self.assertIsInstance(quota.is_pro, bool)
        left = quota.upload['used']['mb']
        self.assertIsInstance(left, int)

    def test_Album(self):
        album = ipernity.Album.create(title='This is a album')
        # fields validation
        self.assertIsInstance(album.count['docs'], int)
        self.assertIsInstance(album.dates['created_at'], datetime.datetime)

        # edit test
        new_title = 'New title'
        new_desc = 'This is a new description'
        album.edit(title=new_title, description=new_desc)
        self.assertEquals(album.title, new_title)
        self.assertEquals(album.description, new_desc)

        album_id = album.id
        # new album can be get
        album = ipernity.Album.get(id=album_id)
        album.delete()
        # after delete, album shoult not found
        with self.assertRaisesRegexp(errors.IpernityAPIError, 'Album not found'):
            ipernity.Album.get(id=album_id)

    def test_Folder(self):
        folder = ipernity.Folder.create(title='folder title')
        # fields type validation
        self.assertIsInstance(folder.count['albums'], int)
        self.assertIsInstance(folder.dates['created_at'], datetime.datetime)

        folder_id = folder.id
        # after created, folder can retrieve by get
        # TODO: this fail
        #folder = ipernity.Folder.get(id=folder_id)

        folder.delete()
        with self.assertRaisesRegexp(errors.IpernityAPIError, 'not found'):
            ipernity.Folder.get(id=folder_id)

    def test_Upload(self):
        ticket = ipernity.Upload.file(file=getfile('1.jpg'))
        ipernity.Upload.checkTickets(tickets=[ticket])
        #ticket.wait_done()
        #doc = ticket.doc
        doc = ticket.getDoc()

        # doc field test
        self.assertIsInstance(doc.dates['created'], datetime.datetime)
        self.assertIsInstance(doc.count['visits'], int)
        self.assertIsInstance(doc.visibility['share'], int)
        self.assertIsInstance(doc.can['fave'], bool)
        self.assertIsInstance(doc.you['visits'], int)
        self.assertIsInstance(doc.owner, ipernity.User)
        doc_str = str(doc)
        self.assertIn('Doc', doc_str)
        self.assertIn('id', doc_str)
        self.assertIn('title', doc_str)

        # thumb test
        thumb = doc.thumbs[0]
        self.assertIsInstance(thumb, ipernity.Thumb)
        self.assertIsInstance(thumb.w, int)
        self.assertIsInstance(thumb.h, int)

        doc.delete()
<commit_msg>+ tests.ipernity: upload_files<commit_after>import os
import datetime
from unittest import TestCase
from ipernity_api import ipernity, errors
from . import utils

def getfile(fname):
    return os.path.join(os.path.dirname(__file__), 'files', fname)

class IpernityTest(TestCase):
    def __init__(self, *arg, **kwargs):
        TestCase.__init__(self, *arg, **kwargs)
        utils.auto_auth()
        self.user = utils.AUTH_HANDLER.getUser()

    def test_Test(self):
        self.assertEquals('echo', ipernity.Test.echo(echo='echo'))
        self.assertIn('hello', ipernity.Test.hello())

    def test_User(self):
        user_id = self.user.id
        # get user test
        user = ipernity.User.get(id=user_id)
        self.assertIsInstance(user, ipernity.User)
        # filed test
        self.assertIsInstance(user.is_online, bool)
        self.assertIsInstance(user.is_closed, bool)
        self.assertIsInstance(user.is_pro, bool)
        # count test
        self.assertIsInstance(user.count['network'], int)
        self.assertIsInstance(user.dates['member_since'], datetime.datetime)

        # quota test
        quota = user.getQuota()
        self.assertIsInstance(quota.is_pro, bool)
        left = quota.upload['used']['mb']
        self.assertIsInstance(left, int)

    def test_Album(self):
        album = ipernity.Album.create(title='This is a album')
        # fields validation
        self.assertIsInstance(album.count['docs'], int)
        self.assertIsInstance(album.dates['created_at'], datetime.datetime)

        # edit test
        new_title = 'New title'
        new_desc = 'This is a new description'
        album.edit(title=new_title, description=new_desc)
        self.assertEquals(album.title, new_title)
        self.assertEquals(album.description, new_desc)

        album_id = album.id
        # new album can be get
        album = ipernity.Album.get(id=album_id)
        album.delete()
        # after delete, album shoult not found
        with self.assertRaisesRegexp(errors.IpernityAPIError, 'Album not found'):
            ipernity.Album.get(id=album_id)

    def test_Folder(self):
        folder = ipernity.Folder.create(title='folder title')
        # fields type validation
        self.assertIsInstance(folder.count['albums'], int)
        self.assertIsInstance(folder.dates['created_at'], datetime.datetime)

        folder_id = folder.id
        # after created, folder can retrieve by get
        # TODO: this fail
        #folder = ipernity.Folder.get(id=folder_id)

        folder.delete()
        with self.assertRaisesRegexp(errors.IpernityAPIError, 'not found'):
            ipernity.Folder.get(id=folder_id)

    def test_Upload(self):
        ticket = ipernity.Upload.file(file=getfile('1.jpg'))
        ipernity.Upload.checkTickets(tickets=[ticket])
        #ticket.wait_done()
        #doc = ticket.doc
        doc = ticket.getDoc()

        # doc field test
        self.assertIsInstance(doc.dates['created'], datetime.datetime)
        self.assertIsInstance(doc.count['visits'], int)
        self.assertIsInstance(doc.visibility['share'], int)
        self.assertIsInstance(doc.can['fave'], bool)
        self.assertIsInstance(doc.you['visits'], int)
        self.assertIsInstance(doc.owner, ipernity.User)
        doc_str = str(doc)
        self.assertIn('Doc', doc_str)
        self.assertIn('id', doc_str)
        self.assertIn('title', doc_str)

        # thumb test
        thumb = doc.thumbs[0]
        self.assertIsInstance(thumb, ipernity.Thumb)
        self.assertIsInstance(thumb.w, int)
        self.assertIsInstance(thumb.h, int)

        doc.delete()

    def upload_files(self):
        ''' upload some test image and return '''
        def upload_img(fname):
            ticket = ipernity.Upload.file(file=getfile(fname))
            return ticket.getDoc()

        files = ['1.jpg', '2.jpg']
        return [upload_img(fname) for fname in files]
<|endoftext|>


<commit_before>import match

# For some reason it tells me match cannot be imported, please confirm it's working properly, it should work in theory
# match.py line 228 returns results

import random

class Tournament:

    # Initialisation of all needed lists

    # The Roster lists is for the participating players, whereas the Games list is for the corresponding matches

    # Quarter-Finals
    quarterfinalsRoster = []
    quarterfinalsGames = []

    # Semi-Finals
    semifinalsRoster = []
    semifinalsGames = []

    # Final
    finalRoster = []
    finalGame = []

    # The Winner
    winner = []

    def __init__(self, roster): # Constructor; Gets fed a list full of player objects
        random.seed()
        temp = []
        for x in range(len(roster)):
            temp.append(roster[x])
        # self.printList(temp)
        while (len(temp) >= 1):
            rnd = random.randint(0, len(temp)-1)
            self.quarterfinalsRoster.append(temp[rnd])
            temp.remove(temp[rnd])
        # self.matchCreator(self.quarterfinalsRoster, self.quarterfinalsGames)

    def matchCreator(self, roster, games):
        y = int(len(roster)/2)
        for x in (range(0, y)):
            games.append(self.createMatch(roster[x*2], roster[(x*2)+1]))
        self.printList(self.quarterfinalsGames)

    def playMatches(self, games, roster):
        for x in range(len(games)):
            roster.append(games[x].start.winner)

    def tournament_start(self):
        # testTournament = self.Tournament(players)
        # self.printList(self.quarterfinalsRoster)
        self.matchCreator(self.quarterfinalsRoster, self.quarterfinalsGames)
        self.playMatches(self.quarterfinalsGames, self.semifinalsRoster)
        self.matchCreator(self.semifinalsRoster, self.semifinalsGames)
        self.playMatches(self.semifinalsGames, self.finalRoster)
        self.matchCreator(self.finalRoster, self.finalGame)
        self.playMatches(self.finalGame, self.winner)

    def createMatch(self, player1, player2):
        # return player1 + ' ' + player2 + ' ||';
        # Comment the previous line and uncomment the next line when the game_class takes two parameters
        return match.match_class(player1, player2, 1);

    # Debug functions

    # def printList(self, list):
    #     for x in range(len(list)):
    #         print(list[x].name, end= ' ')
    #         #print(list[x], end=' ')
    #     print()
    #
    # def printListx2(self, list):
    #     y = int(len(list)/2)
    #     for x in (range(y)):
    #         print(list[x*2], end= ' ')
    #     print()

# Debug code

# The next three lines are how I imagine the whole thing is launched
# First, you generate a list with all the players, in this case strings are used but I expect player objects to be used
# Then you call the constructor to initialise the tournament and finally you start it with tournament_start

# players = ['Mike', 'Dragan', 'Filippo', 'Gilles', 'Jack', 'Sam', 'Yves', 'Patrick']
# testTournament = Tournament(players)
# testTournament.tournament_start()

# --------------------------------------------------------------------------------------------------------------------

# Tournament.tournament_start(players)
# testTournament.printList(tournament1.bo8Roster)
# testTournament.printListx2(tournament1.bo8Roster)
# testTournament.matchCreator(tournament1.bo8Roster, tournament1.bo8Games)<commit_msg>Update tournament.py<commit_after>import match

# For some reason it tells me match cannot be imported, please confirm it's working properly, it should work in theory
# match.py line 228 returns results

import random

class Tournament:

    # Initialisation of all needed lists

    # The 'Roster' lists are for the participating players, whereas the 'Games' lists are for the corresponding matches

    # Quarter-Finals
    quarterfinalsRoster = []
    quarterfinalsGames = []

    # Semi-Finals
    semifinalsRoster = []
    semifinalsGames = []

    # Final
    finalRoster = []
    finalGame = []

    # The Winner
    winner = []

    def __init__(self, roster): # Constructor; Gets fed a list full of player objects
        random.seed()
        temp = []
        for x in range(len(roster)):
            temp.append(roster[x])
        # self.printList(temp)
        while (len(temp) >= 1):
            rnd = random.randint(0, len(temp)-1)
            self.quarterfinalsRoster.append(temp[rnd])
            temp.remove(temp[rnd])
        # self.matchCreator(self.quarterfinalsRoster, self.quarterfinalsGames)

    def matchCreator(self, roster, games):
        y = int(len(roster)/2)
        for x in (range(0, y)):
            games.append(self.createMatch(roster[x*2], roster[(x*2)+1]))
        self.printList(self.quarterfinalsGames)

    def playMatches(self, games, roster):
        for x in range(len(games)):
            roster.append(games[x].start.winner)

    def tournament_start(self):
        # testTournament = self.Tournament(players)
        # self.printList(self.quarterfinalsRoster)
        self.matchCreator(self.quarterfinalsRoster, self.quarterfinalsGames)
        self.playMatches(self.quarterfinalsGames, self.semifinalsRoster)
        self.matchCreator(self.semifinalsRoster, self.semifinalsGames)
        self.playMatches(self.semifinalsGames, self.finalRoster)
        self.matchCreator(self.finalRoster, self.finalGame)
        self.playMatches(self.finalGame, self.winner)

    def createMatch(self, player1, player2):
        # return player1 + ' ' + player2 + ' ||';
        # Comment the previous line and uncomment the next line when the game_class takes two parameters
        return match.match_class(player1, player2, 1);

    # Debug functions

    # def printList(self, list):
    #     for x in range(len(list)):
    #         print(list[x].name, end= ' ')
    #         #print(list[x], end=' ')
    #     print()
    #
    # def printListx2(self, list):
    #     y = int(len(list)/2)
    #     for x in (range(y)):
    #         print(list[x*2], end= ' ')
    #     print()

# Debug code

# The next three lines are how I imagine the whole thing is launched
# First, you generate a list with all the players, in this case strings are used but I expect player objects to be used
# Then you call the constructor to initialise the tournament and finally you start it with tournament_start

# players = ['Mike', 'Dragan', 'Filippo', 'Gilles', 'Jack', 'Sam', 'Yves', 'Patrick']
# testTournament = Tournament(players)
# testTournament.tournament_start()

# --------------------------------------------------------------------------------------------------------------------

# Tournament.tournament_start(players)
# testTournament.printList(tournament1.bo8Roster)
# testTournament.printListx2(tournament1.bo8Roster)
# testTournament.matchCreator(tournament1.bo8Roster, tournament1.bo8Games)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import textwrap
import unittest

from functools import partial
from tempfile import gettempdir

from django.contrib.gis.geos.polygon import Polygon
from django.contrib.gis.geos.collections import MultiPoint

from openquake.engine import engine
from openquake.engine.db import models
from openquake.engine.engine import _parse_config_file
from openquake.engine.engine import _prepare_config_parameters
from openquake.engine.engine import _prepare_job
from openquake.engine.job import config
from openquake.engine.job.params import config_text_to_list

from tests.utils import helpers

CONFIG_FILE = "config.gem"
CONFIG_WITH_INCLUDES = "config_with_includes.gem"
HAZARD_ONLY = "hazard-config.gem"

REGION_EXPOSURE_TEST_FILE = "ExposureModelFile-helpers.region"
BLOCK_SPLIT_TEST_FILE = "block_split.gem"
REGION_TEST_FILE = "small.region"

def _to_coord_list(geometry):
    pts = []

    if isinstance(geometry, Polygon):
        # Ignore the last coord:
        for c in geometry.coords[0][:-1]:
            pts.append(str(c[1]))
            pts.append(str(c[0]))

        return ', '.join(pts)
    elif isinstance(geometry, MultiPoint):
        for c in geometry.coords:
            pts.append(str(c[1]))
            pts.append(str(c[0]))

        return ', '.join(pts)
    else:
        raise RuntimeError('Unexpected geometry type: %s' % type(geometry))

class ConfigParseTestCase(unittest.TestCase):

    def test_parse_file(self):
        content = '''
            [GENERAL]
            CALCULATION_MODE = Event Based

            [HAZARD]
            MINIMUM_MAGNITUDE = 5.0
            '''
        config_path = helpers.touch(
            dir=gettempdir(), content=textwrap.dedent(content))

        params, sections = _parse_config_file(config_path)

        self.assertEqual(
            {'BASE_PATH': gettempdir(),
             'CALCULATION_MODE': 'Event Based',
             'MINIMUM_MAGNITUDE': '5.0'},
            params)
        self.assertEqual(['GENERAL', 'HAZARD'], sorted(sections))

    def test_parse_missing_files(self):
        config_path = '/does/not/exist'

        self.assertRaises(config.ValidationException, _parse_config_file,
                          config_path)

    def test_prepare_parameters(self):
        content = '''
            [GENERAL]
            CALCULATION_MODE = Event Based
            # unknown parameter
            FOO = 5

            [HAZARD]
            MINIMUM_MAGNITUDE = 5.0
            # not used for this calc mode
            COMPUTE_MEAN_HAZARD_CURVE = true
            '''
        config_path = helpers.touch(
            dir=gettempdir(), content=textwrap.dedent(content))

        params, sections = _parse_config_file(config_path)
        params, sections = _prepare_config_parameters(params, sections)

        self.assertEqual(
            {'BASE_PATH': gettempdir(),
             'MINIMUM_MAGNITUDE': '5.0',
             'CALCULATION_MODE': 'Event Based'},
            params)
        self.assertEqual(['GENERAL', 'HAZARD'], sorted(sections))

    def test_prepare_parameters_for_uhs_set_imt_to_sa(self):
        # The imt is always set to "sa" for uhs jobs.
        content = '''
            [general]

            CALCULATION_MODE = UHS

            SITES = 0.0, 0.0

            DESCRIPTION = Uniform Hazard Spectra Demo

            [HAZARD]

            # parameters for UHS calculations
            UHS_PERIODS = 0.025, 0.45, 2.5
            POES = 0.1, 0.02
            INTENSITY_MEASURE_TYPE = PGA
            '''
        config_path = helpers.touch(
            dir=gettempdir(), content=textwrap.dedent(content))

        params, sections = _parse_config_file(config_path)
        params, sections = _prepare_config_parameters(params, sections)
        self.assertEqual("SA", params["INTENSITY_MEASURE_TYPE"])

    def test_prepare_path_parameters(self):
        content = '''
            [GENERAL]
            CALCULATION_MODE = Event Based
            OUTPUT_DIR = output

            [HAZARD]
            SOURCE_MODEL_LOGIC_TREE_FILE = source-model.xml
            GMPE_LOGIC_TREE_FILE = gmpe.xml

            [RISK]
            EXPOSURE = /absolute/exposure.xml
            VULNERABILITY = vulnerability.xml
            '''
        config_path = helpers.touch(content=textwrap.dedent(content))

        params, sections = _parse_config_file(config_path)
        params, sections = _prepare_config_parameters(params, sections)

        self.assertEqual(
            {'BASE_PATH': gettempdir(),
             'OUTPUT_DIR': 'output',
             'SOURCE_MODEL_LOGIC_TREE_FILE': 'source-model.xml',
             'GMPE_LOGIC_TREE_FILE': 'gmpe.xml',
             'EXPOSURE': '/absolute/exposure.xml',
             'VULNERABILITY': 'vulnerability.xml',
             'CALCULATION_MODE': 'Event Based'},
            params)
        self.assertEqual(['GENERAL', 'HAZARD', 'RISK'], sorted(sections))

def datapath(test, path):
    return helpers.testdata_path("%s/%s" % (test, path))

class PrepareJobTestCase(unittest.TestCase, helpers.DbTestCase):
    """
    Unit tests for the _prepare_job helper function, which creates a new
    job entry with the associated parameters.

    Test data is a trimmed-down version of smoketest config files

    As a side-effect, also tests that the inserted record satisfied
    the DB constraints.
    """

    BASE_CLASSICAL_PARAMS = {
        'CALCULATION_MODE': 'Classical',
        'POES': '0.01 0.1',
        'SOURCE_MODEL_LT_RANDOM_SEED': '23',
        'GMPE_LT_RANDOM_SEED': '5',
        'INTENSITY_MEASURE_TYPE': 'PGA',
        'MINIMUM_MAGNITUDE': '5.0',
        'INVESTIGATION_TIME': '50.0',
        'INCLUDE_GRID_SOURCES': 'true',
        'TREAT_GRID_SOURCE_AS': 'Point Sources',
        'INCLUDE_AREA_SOURCES': 'true',
        'TREAT_AREA_SOURCE_AS': 'Point Sources',
        'QUANTILE_LEVELS': '0.25 0.50',
        'INTENSITY_MEASURE_LEVELS': '0.005, 0.007, 0.0098, 0.0137, 0.0192',
        'GMPE_TRUNCATION_TYPE': '2 Sided',
        'STANDARD_DEVIATION_TYPE': 'Total',
        'MAXIMUM_DISTANCE': '200.0',
        'NUMBER_OF_LOGIC_TREE_SAMPLES': '2',
        'PERIOD': '0.0',
        'DAMPING': '5.0',
        'INCLUDE_FAULT_SOURCE': 'true',
        'FAULT_RUPTURE_OFFSET': '5.0',
        'FAULT_SURFACE_DISCRETIZATION': '1.0',
        'FAULT_MAGNITUDE_SCALING_SIGMA': '0.0',
        'FAULT_MAGNITUDE_SCALING_RELATIONSHIP': 'W&C 1994 Mag-Length Rel.',
        'REFERENCE_VS30_VALUE': '760.0',
        'REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM': '5.0',
        'COMPONENT': 'Average Horizontal (GMRotI50)',
        'CONDITIONAL_LOSS_POE': '0.01',
        'TRUNCATION_LEVEL': '3',
        'COMPUTE_MEAN_HAZARD_CURVE': 'true',
        'AREA_SOURCE_DISCRETIZATION': '0.1',
        'AREA_SOURCE_MAGNITUDE_SCALING_RELATIONSHIP':
            'W&C 1994 Mag-Length Rel.',
        'WIDTH_OF_MFD_BIN': '0.1',
        'SADIGH_SITE_TYPE': 'Rock',
        'INCLUDE_SUBDUCTION_FAULT_SOURCE': 'true',
        'SUBDUCTION_FAULT_RUPTURE_OFFSET': '10.0',
        'SUBDUCTION_FAULT_SURFACE_DISCRETIZATION': '10.0',
        'SUBDUCTION_FAULT_MAGNITUDE_SCALING_SIGMA': '0.0',
        'SUBDUCTION_RUPTURE_ASPECT_RATIO': '1.5',
        'SUBDUCTION_RUPTURE_FLOATING_TYPE': 'Along strike and down dip',
        'SUBDUCTION_FAULT_MAGNITUDE_SCALING_RELATIONSHIP':
            'W&C 1994 Mag-Length Rel.',
        'RUPTURE_ASPECT_RATIO': '1.5',
        'RUPTURE_FLOATING_TYPE': 'Along strike and down dip',
        'BASE_PATH': '/base/path'
    }

    BASE_SCENARIO_PARAMS = {
        'CALCULATION_MODE': 'Scenario',
        'GMPE_MODEL_NAME': 'BA_2008_AttenRel',
        'GMF_RANDOM_SEED': '3',
        'RUPTURE_SURFACE_DISCRETIZATION': '0.1',
        'INTENSITY_MEASURE_TYPE': 'PGA',
        'REFERENCE_VS30_VALUE': '759.0',
        'COMPONENT': 'Average Horizontal (GMRotI50)',
        'PERIOD': '0.0',
        'DAMPING': '5.0',
        'NUMBER_OF_GROUND_MOTION_FIELDS_CALCULATIONS': '5',
        'TRUNCATION_LEVEL': '3',
        'GMPE_TRUNCATION_TYPE': '1 Sided',
        'GROUND_MOTION_CORRELATION': 'true',
        'EPSILON_RANDOM_SEED': '37',
        'BASE_PATH': '/base/path'
    }

    BASE_EVENT_BASED_PARAMS = {
        'CALCULATION_MODE': 'Event Based',
        'SOURCE_MODEL_LT_RANDOM_SEED': '23',
        'GMPE_LT_RANDOM_SEED': '5',
        'INTENSITY_MEASURE_TYPE': 'SA',
        'INCLUDE_GRID_SOURCES': 'false',
        'INCLUDE_SUBDUCTION_FAULT_SOURCE': 'false',
        'RUPTURE_ASPECT_RATIO': '1.5',
        'MINIMUM_MAGNITUDE': '5.0',
        'SUBDUCTION_FAULT_MAGNITUDE_SCALING_SIGMA': '0.0',
        'INVESTIGATION_TIME': '50.0',
        'TREAT_GRID_SOURCE_AS': 'Point Sources',
        'INCLUDE_AREA_SOURCES': 'true',
        'TREAT_AREA_SOURCE_AS': 'Point Sources',
        'INTENSITY_MEASURE_LEVELS': '0.005, 0.007, 0.0098, 0.0137, 0.0192',
        'GROUND_MOTION_CORRELATION': 'false',
        'GMPE_TRUNCATION_TYPE': 'None',
        'STANDARD_DEVIATION_TYPE': 'Total',
        'SUBDUCTION_FAULT_RUPTURE_OFFSET': '10.0',
        'NUMBER_OF_LOGIC_TREE_SAMPLES': '5',
        'PERIOD': '1.0',
        'DAMPING': '5.0',
        'NUMBER_OF_SEISMICITY_HISTORIES': '1',
        'INCLUDE_FAULT_SOURCE': 'true',
        'FAULT_RUPTURE_OFFSET': '5.0',
        'FAULT_SURFACE_DISCRETIZATION': '1.0',
        'FAULT_MAGNITUDE_SCALING_SIGMA': '0.0',
        'FAULT_MAGNITUDE_SCALING_RELATIONSHIP': 'W&C 1994 Mag-Length Rel.',
        'SUBDUCTION_RUPTURE_ASPECT_RATIO': '1.5',
        'REFERENCE_VS30_VALUE': '760.0',
        'REFERENCE_DEPTH_TO_2PT5KM_PER_SEC_PARAM': '5.0',
        'COMPONENT': 'Average Horizontal',
        'CONDITIONAL_LOSS_POE': '0.01',
        'TRUNCATION_LEVEL': '3',
        'AREA_SOURCE_DISCRETIZATION': '0.1',
        'AREA_SOURCE_MAGNITUDE_SCALING_RELATIONSHIP':
            'W&C 1994 Mag-Length Rel.',
        'WIDTH_OF_MFD_BIN': '0.1',
        'SADIGH_SITE_TYPE': 'Rock',
        'SUBDUCTION_FAULT_RUPTURE_OFFSET': '10.0',
        'SUBDUCTION_FAULT_SURFACE_DISCRETIZATION': '10.0',
        'SUBDUCTION_FAULT_MAGNITUDE_SCALING_SIGMA': '0.0',
        'SUBDUCTION_RUPTURE_ASPECT_RATIO': '1.5',
        'SUBDUCTION_RUPTURE_FLOATING_TYPE': 'Along strike and down dip',
        'SUBDUCTION_FAULT_MAGNITUDE_SCALING_RELATIONSHIP':
            'W&C 1994 Mag-Length Rel.',
        'RUPTURE_ASPECT_RATIO': '1.5',
        'RUPTURE_FLOATING_TYPE': 'Along strike and down dip',
        'GMF_RANDOM_SEED': '1',
        'BASE_PATH': '/base/path'
    }

    def setUp(self):
        self.job = engine.prepare_job()

    def tearDown(self):
        if (hasattr(self, "job") and self.job):
            self.teardown_job(self.job)

    def _reload_params(self):
        return models.profile4job(self.job.id)

    def assertFieldsEqual(self, expected, params):
        got_params = dict((k, getattr(params, k)) for k in expected.keys())

        self.assertEqual(expected, got_params)

    def _get_inputs(self, job):
        inputs = [dict(path=i.path, type=i.input_type)
                  for i in models.inputs4job(self.job.id)]

        return sorted(inputs, key=lambda i: (i['type'], i['path']))

    def test_prepare_classical_job_over_sites(self):
        '''
        Same as test_prepare_classical_job, but with geometry specified as
        a list of sites.
        '''
        params = self.BASE_CLASSICAL_PARAMS.copy()
        params['SITES'] = '37.9, -121.9, 37.9, -121.6, 37.5, -121.6'
        params['LREM_STEPS_PER_INTERVAL'] = '5'

        jp = _prepare_job(params, ['HAZARD', 'RISK'], 'openquake', self.job,
                          False)
        self.assertEqual(params['SITES'], _to_coord_list(jp.sites))
        self.assertFieldsEqual(
            {'calc_mode': 'classical',
             'min_magnitude': 5.0,
             'investigation_time': 50.0,
             'component': 'gmroti50',
             'imt': 'pga',
             'period': None,
             'truncation_type': 'twosided',
             'truncation_level': 3.0,
             'reference_vs30_value': 760.0,
             'imls': [0.005, 0.007, 0.0098, 0.0137, 0.0192],
             'poes': [0.01, 0.1],
             'realizations': 2,
             'histories': None,
             'gm_correlated': None,
             }, jp)

    def test_prepare_scenario_job(self):
        abs_path = partial(datapath, "scenario")
        params = self.BASE_SCENARIO_PARAMS.copy()
        params['REGION_VERTEX'] = \
            '34.07, -118.25, 34.07, -118.22, 34.04, -118.22'
        params['REGION_GRID_SPACING'] = '0.02'
        params['SINGLE_RUPTURE_MODEL'] = abs_path("simple-fault-rupture.xml")
        params['EXPOSURE'] = abs_path("LA_small_portfolio.xml")
        params['VULNERABILITY'] = abs_path("vulnerability.xml")

        jp = _prepare_job(params, ['HAZARD', 'RISK'], 'openquake', self.job,
                          False)
        self.assertEqual(params['REGION_VERTEX'], _to_coord_list(jp.region))
        self.assertFieldsEqual(
            {'calc_mode': 'scenario',
             'region_grid_spacing': 0.02,
             'min_magnitude': None,
             'investigation_time': None,
             'component': 'gmroti50',
             'imt': 'pga',
             'period': None,
             'truncation_type': 'onesided',
             'truncation_level': 3.0,
             'reference_vs30_value': 759.0,
             'imls': None,
             'poes': None,
             'realizations': None,
             'histories': None,
             'gm_correlated': True,
             'damping': None,
             'gmf_calculation_number': 5,
             'rupture_surface_discretization': 0.1,
             }, jp)

    def test_prepare_scenario_job_over_sites(self):
        '''
        Same as test_prepare_scenario_job, but with geometry specified as
        a list of sites.
        '''
        params = self.BASE_SCENARIO_PARAMS.copy()
        params['SITES'] = '34.07, -118.25, 34.07, -118.22, 34.04, -118.22'

        jp = _prepare_job(params, ['HAZARD', 'RISK'], 'openquake', self.job,
                          False)
        self.assertEqual(params['SITES'], _to_coord_list(jp.sites))
        self.assertFieldsEqual(
            {'calc_mode': 'scenario',
             'min_magnitude': None,
             'investigation_time': None,
             'component': 'gmroti50',
             'imt': 'pga',
             'period': None,
             'truncation_type': 'onesided',
             'truncation_level': 3.0,
             'reference_vs30_value': 759.0,
             'imls': None,
             'poes': None,
             'realizations': None,
             'histories': None,
             'gm_correlated': True,
             }, jp)

    def test_prepare_event_based_job_over_sites(self):
        '''
        Same as test_prepare_event_based_job, but with geometry specified as
        a list of sites.
        '''

        params = self.BASE_EVENT_BASED_PARAMS.copy()
        params['SITES'] = '33.88, -118.3, 33.88, -118.06, 33.76, -118.06'
        params['LOSS_HISTOGRAM_BINS'] = '25'

        jp = _prepare_job(params, ['HAZARD', 'RISK'], 'openquake', self.job,
                          False)
        self.assertEqual(params['SITES'], _to_coord_list(jp.sites))
        self.assertFieldsEqual(
            {'calc_mode': 'event_based',
             'min_magnitude': 5.0,
             'investigation_time': 50.0,
             'component': 'average',
             'imt': 'sa',
             'period': 1.0,
             'truncation_type': 'none',
             'truncation_level': 3.0,
             'reference_vs30_value': 760.0,
             'imls': [0.005, 0.007, 0.0098, 0.0137, 0.0192],
             'poes': None,
             'realizations': 5,
             'histories': 1,
             'gm_correlated': False,
             }, jp)

class JobUtilsTestCase(unittest.TestCase):
    """Tests for utility functions in the job module."""

    def test_config_text_to_list(self):
        """Exercise :function:`openquake.engine.job.params.\
config_text_to_list`."""
        expected = ['MagDistPMF', 'MagDistEpsPMF', 'FullDisaggMatrix']

        # the input mixes spaces and commas for robustness testing:
        test_input = 'MagDistPMF,MagDistEpsPMF FullDisaggMatrix'

        self.assertEqual(expected, config_text_to_list(test_input))

    def test_config_text_to_list_with_transform(self):
        """Exercise :function:`openquake.engine.job.params.\
config_text_to_list` with a transform specified.
        """
        expected = [0.01, 0.02, 0.03, 0.04]

        # again, mix spaces and commas
        test_input = '0.01,0.02, 0.03 0.04'

        self.assertEqual(expected, config_text_to_list(test_input, float))

    def test_config_text_to_list_all_whitespace_input(self):
        """Exercise :function:`openquake.engine.job.params.\
config_text_to_list` with an input of only spaces. """

        expected = []

        test_input = '     '

        self.assertEqual(expected, config_text_to_list(test_input))
<commit_msg>tests/job_test:<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python

import unittest
import os
import logging
import sys

# Setup test files and logs
dir = os.path.dirname(__file__)
log_file = os.path.join(dir, 'unittest/unit-test.log')

# Logging setup
logging.basicConfig(filename=log_file, level=logging.DEBUG)

# TODO: use subprocess.Popen to suppress output instead of using janky os.system
class TestPatentConfig(unittest.TestCase):
    # Make sure that if os.environ['PATENTROOT'] is set, then we parse it correctly
    # if it is nonexistant/incorrect, then recover
    # if it is not set, then we default to /data/patentdata/patents

    def setUp(self):
        # make sure we can call parse.py using the os module
        # for the purpose of testing command line arguments
        current_directory = os.getcwd()
        if not current_directory.endswith('test'):
            logging.error('Please run from the patentprocessor/test directory')
        # need a default value for later tests
        self.assertTrue(os.environ.has_key('PATENTROOT'))
        os.chdir('..')

    def test_argparse_patentroot(self):
        # test that argparse is setting the variables correctly for patentroot
        exit_status = os.system('python parse.py --patentroot %s' % (os.getcwd() + '/test/unittest/fixtures'))
        # valid directory, but no xml files
        self.assertTrue(exit_status == 0)

        exit_status = os.system('python parse.py --patentroot /dev/null')
        # specify invalid directory, should fail
        self.assertTrue(exit_status != 0)

        # test a working, valid directory
        exit_status = os.system('python parse.py --patentroot %s' % (os.environ['PATENTROOT']))
        # this should pass
        self.assertTrue(exit_status == 0)

    def test_argparse_regex(self):
        # test that argparse is setting the regular expression correctly
        # test valid regex on unittest/fixtures folder
        exit_status = os.system("python parse.py --patentroot %s --xmlregex '2012_\d.xml'" % (os.getcwd() + '/test/unittest/fixtures'))
        self.assertTrue(exit_status == 0)

    def test_argparse_directory(self):
        # test that argparse is setting the variables correctly for directories
        # parse.py should not find any .xml files, but this should still pass
        exit_status = os.system('python parse.py --patentroot %s' % (os.getcwd() + '/test/unittest'))
        self.assertTrue(exit_status == 0)

        # parse.py should concatentate the correct directory and find xml files
        exit_status = os.system("python parse.py --patentroot %s --directory fixtures --xmlregex '2012_\d.xml'" % (os.getcwd() + '/test/unittest'))
        self.assertTrue(exit_status == 0)

        # TODO: make test for iterating through multiple directories

    def test_gets_environment_var(self):
        # sanity check for existing valid path
        logging.info("Testing getting valid env var PATENTROOT")
        self.assertTrue(os.environ.has_key('PATENTROOT'))

    def tearDown(self):
        os.chdir('test')

if __name__ == '__main__':

    open(log_file, 'w')
    unittest.main()
<commit_msg>fix up exit status on unittest<commit_after>#!/usr/bin/env python

import unittest
import os
import logging
import sys

# Setup test files and logs
dir = os.path.dirname(__file__)
log_file = os.path.join(dir, 'unittest/unit-test.log')

# Logging setup
logging.basicConfig(filename=log_file, level=logging.DEBUG)

# TODO: use subprocess.Popen to suppress output instead of using janky os.system
class TestPatentConfig(unittest.TestCase):
    # Make sure that if os.environ['PATENTROOT'] is set, then we parse it correctly
    # if it is nonexistant/incorrect, then recover
    # if it is not set, then we default to /data/patentdata/patents

    def setUp(self):
        # make sure we can call parse.py using the os module
        # for the purpose of testing command line arguments
        current_directory = os.getcwd()
        if not current_directory.endswith('test'):
            logging.error('Please run from the patentprocessor/test directory')
        # need a default value for later tests
        self.assertTrue(os.environ.has_key('PATENTROOT'))
        os.chdir('..')

    def test_argparse_patentroot(self):
        # test that argparse is setting the variables correctly for patentroot
        exit_status = os.system('python parse.py --patentroot %s' % (os.getcwd() + '/test/unittest/fixtures'))
        # valid directory, but no xml files
        self.assertTrue(exit_status == 0)

        exit_status = os.system('python parse.py --patentroot /asdf')
        # specify invalid directory, should not have any files, but still pass
        self.assertTrue(exit_status == 0)

        # test a working, valid directory
        exit_status = os.system('python parse.py --patentroot %s' % (os.environ['PATENTROOT']))
        # this should pass
        self.assertTrue(exit_status == 0)

    def test_argparse_regex(self):
        # test that argparse is setting the regular expression correctly
        # test valid regex on unittest/fixtures folder
        exit_status = os.system("python parse.py --patentroot %s --xmlregex '2012_\d.xml'" % (os.getcwd() + '/test/unittest/fixtures'))
        self.assertTrue(exit_status == 0)

    def test_argparse_directory(self):
        # test that argparse is setting the variables correctly for directories
        # parse.py should not find any .xml files, but this should still pass
        exit_status = os.system('python parse.py --patentroot %s' % (os.getcwd() + '/test/unittest'))
        self.assertTrue(exit_status == 0)

        # parse.py should concatentate the correct directory and find xml files
        exit_status = os.system("python parse.py --patentroot %s --directory fixtures --xmlregex '2012_\d.xml'" % (os.getcwd() + '/test/unittest'))
        self.assertTrue(exit_status == 0)

        # TODO: make test for iterating through multiple directories

    def test_gets_environment_var(self):
        # sanity check for existing valid path
        logging.info("Testing getting valid env var PATENTROOT")
        self.assertTrue(os.environ.has_key('PATENTROOT'))

    def tearDown(self):
        os.chdir('test')

if __name__ == '__main__':

    open(log_file, 'w')
    unittest.main()
<|endoftext|>


<commit_before>"""Profiling support for unit and performance tests."""

import testbase
from testlib.config import parser, post_configure
import testlib.config
import os

__all__ = 'profiled',

all_targets = set()
profile_config = { 'targets': set(),
                   'report': True,
                   'sort': ('time', 'calls'),
                   'limit': None }

def profiled(target=None, **target_opts):
    """Optional function profiling.

    @profiled('label')
    or
    @profiled('label', report=True, sort=('calls',), limit=20)

    Enables profiling for a function when 'label' is targetted for
    profiling.  Report options can be supplied, and override the global
    configuration and command-line options.
    """

    import time, hotshot, hotshot.stats

    # manual or automatic namespacing by module would remove conflict issues
    if target is None:
        target = 'anonymous_target'
    elif target in all_targets:
        print "Warning: redefining profile target '%s'" % target
    all_targets.add(target)

    filename = "%s.prof" % target

    def decorator(fn):
        def profiled(*args, **kw):
            if (target not in profile_config['targets'] and
                not target_opts.get('always', None)):
                return fn(*args, **kw)

            prof = hotshot.Profile(filename)
            began = time.time()
            prof.start()
            try:
                result = fn(*args, **kw)
            finally:
                prof.stop()
                ended = time.time()
                prof.close()

            if not testlib.config.options.quiet:
                print "Profiled target '%s', wall time: %.2f seconds" % (
                    target, ended - began)

            report = target_opts.get('report', profile_config['report'])
            if report and testlib.config.options.verbose:
                sort_ = target_opts.get('sort', profile_config['sort'])
                limit = target_opts.get('limit', profile_config['limit'])
                print "Profile report for target '%s' (%s)" % (
                    target, filename)

                stats = hotshot.stats.load(filename)
                stats.sort_stats(*sort_)
                if limit:
                    stats.print_stats(limit)
                else:
                    stats.print_stats()

            assert_range = target_opts.get('call_range')
            if assert_range:
                if isinstance(assert_range, dict):
                    assert_range = assert_range.get(testlib.config.db, 'default')
                stats = hotshot.stats.load(filename)
                assert stats.total_calls >= assert_range[0] and stats.total_calls <= assert_range[1], stats.total_calls

            os.unlink(filename)
            return result
        try:
            profiled.__name__ = fn.__name__
        except:
            pass
        return profiled
    return decorator
<commit_msg>Added 'function_call_count' assertion decorator.  The full-suite vs. isolated run call count discrepancy needs to be ironed out before this can be applied to zoomark.<commit_after>"""Profiling support for unit and performance tests."""

import testbase
import os, sys
from testlib.config import parser, post_configure
import testlib.config

__all__ = 'profiled', 'function_call_count'

all_targets = set()
profile_config = { 'targets': set(),
                   'report': True,
                   'sort': ('time', 'calls'),
                   'limit': None }

def profiled(target=None, **target_opts):
    """Optional function profiling.

    @profiled('label')
    or
    @profiled('label', report=True, sort=('calls',), limit=20)

    Enables profiling for a function when 'label' is targetted for
    profiling.  Report options can be supplied, and override the global
    configuration and command-line options.
    """

    import time, hotshot, hotshot.stats

    # manual or automatic namespacing by module would remove conflict issues
    if target is None:
        target = 'anonymous_target'
    elif target in all_targets:
        print "Warning: redefining profile target '%s'" % target
    all_targets.add(target)

    filename = "%s.prof" % target

    def decorator(fn):
        def profiled(*args, **kw):
            if (target not in profile_config['targets'] and
                not target_opts.get('always', None)):
                return fn(*args, **kw)

            prof = hotshot.Profile(filename)
            began = time.time()
            prof.start()
            try:
                result = fn(*args, **kw)
            finally:
                prof.stop()
                ended = time.time()
                prof.close()

            if not testlib.config.options.quiet:
                print "Profiled target '%s', wall time: %.2f seconds" % (
                    target, ended - began)

            report = target_opts.get('report', profile_config['report'])
            if report and testlib.config.options.verbose:
                sort_ = target_opts.get('sort', profile_config['sort'])
                limit = target_opts.get('limit', profile_config['limit'])
                print "Profile report for target '%s' (%s)" % (
                    target, filename)

                stats = hotshot.stats.load(filename)
                stats.sort_stats(*sort_)
                if limit:
                    stats.print_stats(limit)
                else:
                    stats.print_stats()

            assert_range = target_opts.get('call_range')
            if assert_range:
                if isinstance(assert_range, dict):
                    assert_range = assert_range.get(testlib.config.db, 'default')
                stats = hotshot.stats.load(filename)
                assert stats.total_calls >= assert_range[0] and stats.total_calls <= assert_range[1], stats.total_calls

            os.unlink(filename)
            return result
        try:
            profiled.__name__ = fn.__name__
        except:
            pass
        return profiled
    return decorator

def function_call_count(count=None, versions={}, variance=0.05):
    """Assert a target for a test case's function call count.

    count
      Optional, general target function call count.

    versions
      Optional, a dictionary of Python version strings to counts,
      for example::

        { '2.5.1': 110,
          '2.5': 100,
          '2.4': 150 }

      The best match for the current running python will be used.
      If none match, 'count' will be used as the fallback.

    variance
      An +/- deviation percentage, defaults to 5%.
    """

    version_info = list(sys.version_info)
    py_version = '.'.join([str(v) for v in sys.version_info])

    while version_info:
        version = '.'.join([str(v) for v in version_info])
        if version in versions:
            count = versions[version]
            break
        version_info.pop()

    if count is None:
        return lambda fn: fn

    import hotshot, hotshot.stats

    def decorator(fn):
        def counted(*args, **kw):
            try:
                filename = "%s.prof" % fn.__name__

                prof = hotshot.Profile(filename)
                prof.start()
                try:
                    result = fn(*args, **kw)
                finally:
                    prof.stop()
                    prof.close()

                stats = hotshot.stats.load(filename)
                calls = stats.total_calls
                deviance = int(count * variance)
                if (calls < (count - deviance) or
                    calls > (count + deviance)):
                    raise AssertionError(
                        "Function call count %s not within %s%% "
                        "of expected %s. (Python version %s)" % (
                        calls, variance, count, py_version))
                return result
            finally:
                if os.path.exists(filename):
                    os.unlink(filename)
        try:
            counted.__name__ = fn.__name__
        except:
            pass
        return counted
    return decorator
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Atsushi Sakai
import os
import sys
import logging as log
import subprocess

#You can set top dir of the test file search
SearchPath="."

def Print(string, color, highlight=False):
    """
    Colored print

    colorlist:
        red,green

    """
    end="\033[1;m"
    pstr=""
    if color == "red":
        if highlight:
            pstr+='\033[1;41m'
        else:
            pstr+='\033[1;31m'
    elif color == "green":
        if highlight:
            pstr+='\033[1;42m'
        else:
            pstr+='\033[1;32m'
    elif color == "yellow":
        if highlight:
            pstr+='\033[1;43m'
        else:
            pstr+='\033[1;33m'

    else:
        print(("Error Unsupported color:"+color))

    print((pstr+string+end))

class Gtest:
    def __init__(self):
           # Get gtest dir path
        self.gtestdir=os.environ.get("GTEST_DIR")
        if self.gtestdir==None:
            log.critical('Cannot find GTEST_DIR environment variable')
            exit(0)

        testPaths=self.SearchTestFiles()

        for path in testPaths:
            self.ExeGTest(path)

    def ExeGTest(self,path):
        """
            Execute gtest

            sample:g++ -lpthread test.cpp -I./gtest/include  -L./gtest/mybuild -lgtest_main -lgtest && ./a.out
        """
        print(self.gtestdir)
        cmd="g++ -lpthread "
        cmd+=path
        cmd+=" -I"
        cmd+=self.gtestdir+"/googletest/include -L"
        cmd+=self.gtestdir+"/googlemock/gtest "
        cmd+=" -lgtest_main -lgtest && ./a.out"
        print(cmd)
        try:
            output = subprocess.check_output(cmd,shell=True)
            returncode = 0
            Print(output,"green")
        except subprocess.CalledProcessError as e:
            output = e.output
            returncode = e.returncode
            Print(output,"red")

    def SearchTestFiles(self):
        """
        Search test file
        """
        testPaths=[]
        for file in self.fild_all_files(SearchPath):
            if "Test.cpp" in file:
                testPaths.append(file)

        if len(testPaths)==0:
            Print('Cannot find any test file.',"yellow")
            exit(0)

        print((str(len(testPaths))+" test files are found"))
        print(testPaths)
        return testPaths

    def fild_all_files(self,directory):
        for root, dirs, files in os.walk(directory):
            yield root
            for file in files:
                yield os.path.join(root, file)

if __name__ == '__main__':
    print(__file__+" start!!")
    argvs = sys.argv
    if len(argvs)>=2:
        SearchPath=argvs[1]
    Gtest()

<commit_msg>bug fix for ubuntu<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Atsushi Sakai
import os
import sys
import logging as log
import subprocess

#You can set top dir of the test file search
SearchPath="../../../"

def Print(string, color, highlight=False):
    """
    Colored print

    colorlist:
        red,green

    """
    end="\033[1;m"
    pstr=""
    if color == "red":
        if highlight:
            pstr+='\033[1;41m'
        else:
            pstr+='\033[1;31m'
    elif color == "green":
        if highlight:
            pstr+='\033[1;42m'
        else:
            pstr+='\033[1;32m'
    elif color == "yellow":
        if highlight:
            pstr+='\033[1;43m'
        else:
            pstr+='\033[1;33m'

    else:
        print(("Error Unsupported color:"+color))

    print((pstr+string+end))

class Gtest:
    def __init__(self):
           # Get gtest dir path
        self.gtestdir=os.environ.get("GTEST_DIR")
        if self.gtestdir==None:
            log.critical('Cannot find GTEST_DIR environment variable')
            exit(0)

        testPaths=self.SearchTestFiles()

        for path in testPaths:
            self.ExeGTest(path)

    def ExeGTest(self,path):
        """
            Execute gtest

            sample:g++ -lpthread test.cpp -I./gtest/include  -L./gtest/mybuild -lgtest_main -lgtest && ./a.out
        """
        print(self.gtestdir)
        cmd="g++ -pthread "
        cmd+=path
        cmd+=" -I"
        cmd+=self.gtestdir+"/googletest/include -L"
        cmd+=self.gtestdir+"/googlemock/gtest "
        cmd+=" -lpthread -lgtest_main -lgtest && ./a.out"
        print(cmd)
        try:
            output = subprocess.check_output(cmd,shell=True)
            returncode = 0
            Print(output,"green")
        except subprocess.CalledProcessError as e:
            output = e.output
            returncode = e.returncode
            Print(output,"red")

    def SearchTestFiles(self):
        """
        Search test file
        """
        testPaths=[]
        for file in self.fild_all_files(SearchPath):
            if "Test.cpp" in file:
                testPaths.append(file)

        if len(testPaths)==0:
            Print('Cannot find any test file.',"yellow")
            exit(0)

        print((str(len(testPaths))+" test files are found"))
        print(testPaths)
        return testPaths

    def fild_all_files(self,directory):
        for root, dirs, files in os.walk(directory):
            yield root
            for file in files:
                yield os.path.join(root, file)

if __name__ == '__main__':
    print(__file__+" start!!")
    argvs = sys.argv
    if len(argvs)>=2:
        SearchPath=argvs[1]
    Gtest()

<|endoftext|>


<commit_before>#!/usr/bin/env python

# This is a quick and dirty example of how to use the MoteIF interface in Python
import os
import sys
import time
import ChatMsg  # Automatically generated by the MIG.

from tinyos.message import MoteIF

MSG_LEN = 59  # Because there is null termination

class Chat:
    def __init__(self, motestring):
        # Create a MoteIF
        self.mif = MoteIF.MoteIF()
        # Attach a source to it
        self.tos_source = self.mif.addSource(motestring)
        # SomeMessageClass.py would be generated by MIG
        self.mif.addListener(self, ChatMsg.ChatMsg)

    # Called by the MoteIF's receive thread when a new message
    # is received
    def receive(self, src, msg):
        print("Received message: {}".format(msg))
        print("Received message: {}".format("".join([chr(c) for c in msg])))

    def send(self, msg):
        print "Sending message: {}...".format(msg)
        smsg = ChatMsg.ChatMsg()
        smsg.setString_msg(msg)
        self.mif.sendMsg(
            self.tos_source, 0xFFFF, smsg.get_amType(), 0, smsg
        )

    def main_loop(self):
        while True:
            msg = raw_input("-->")
            if len(msg) <= MSG_LEN:
                msg = msg + "\x00" * (MSG_LEN - len(msg))
                assert len(msg) == MSG_LEN, len(msg)
                self.send(msg)
            else:
                print("Message too long, try again.")

def main():
   if '-h' in sys.argv or len(sys.argv) < 2:
       print "Usage:", sys.argv[0], "sf@localhost:9002", "adc_rev_volt <Volt>"
       sys.exit()

   m = Chat(sys.argv[1])
   m.main_loop()  # don't expect this to return...

if __name__ == "__main__":
    print "I'm running..."
    main()
<commit_msg>Fix message printing<commit_after>#!/usr/bin/env python

# This is a quick and dirty example of how to use the MoteIF interface in Python
import os
import sys
import time
import ChatMsg  # Automatically generated by the MIG.

from tinyos.message import MoteIF

MSG_LEN = 59  # Because there is null termination

class Chat:
    def __init__(self, motestring):
        # Create a MoteIF
        self.mif = MoteIF.MoteIF()
        # Attach a source to it
        self.tos_source = self.mif.addSource(motestring)
        # SomeMessageClass.py would be generated by MIG
        self.mif.addListener(self, ChatMsg.ChatMsg)

    # Called by the MoteIF's receive thread when a new message
    # is received
    def receive(self, src, msg):
        print("Received message: {}".format(msg.data))

    def send(self, msg):
        print "Sending message: {}...".format(msg)
        smsg = ChatMsg.ChatMsg()
        smsg.setString_msg(msg)
        self.mif.sendMsg(
            self.tos_source, 0xFFFF, smsg.get_amType(), 0, smsg
        )

    def main_loop(self):
        while True:
            msg = raw_input("-->")
            if len(msg) <= MSG_LEN:
                msg = msg + "\x00" * (MSG_LEN - len(msg))
                assert len(msg) == MSG_LEN, len(msg)
                self.send(msg)
            else:
                print("Message too long, try again.")

def main():
   if '-h' in sys.argv or len(sys.argv) < 2:
       print "Usage:", sys.argv[0], "sf@localhost:9002", "adc_rev_volt <Volt>"
       sys.exit()

   m = Chat(sys.argv[1])
   m.main_loop()  # don't expect this to return...

if __name__ == "__main__":
    print "I'm running..."
    main()
<|endoftext|>