<commit_before>#
# Copyright 2011-2015 Jeff Bush
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Common utilities for tests
#

import subprocess
import os
import sys

COMPILER_DIR='/usr/local/llvm-nyuzi/bin/'
BASE_DIR=os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + '/../')
LIB_DIR=BASE_DIR+'/software/libs/'
BIN_DIR=BASE_DIR+'/bin/'

HEX_FILE='obj/test.hex'

class TestException:
	def __init__(self, output):
		self.output = output

def compile_test(source_file, optlevel='3'):
	try:
		subprocess.check_output(['mkdir', '-p', 'obj'], stderr=subprocess.STDOUT)
		subprocess.check_output([COMPILER_DIR + 'clang', '-o', 'obj/test.elf',
			'-w',
			'-O' + optlevel,
			source_file,
			LIB_DIR + 'libc/crt0.o',
			LIB_DIR + 'libc/libc.a',
			LIB_DIR + 'libos/libos.a',
			'-I' + LIB_DIR + 'libc/include',
			'-I' + LIB_DIR + 'libos'],
			stderr=subprocess.STDOUT)
		subprocess.check_output([COMPILER_DIR + 'elf2hex', '-o', HEX_FILE, 'obj/test.elf'],
			stderr=subprocess.STDOUT)
	except subprocess.CalledProcessError as exc:
		raise TestException('Compilation failed:\n' + exc.output)

	return HEX_FILE

def assemble_test(source_file):
	try:
		subprocess.check_output(['mkdir', '-p', 'obj'])
		subprocess.check_output([COMPILER_DIR + 'clang', '-o', 'obj/test.elf', source_file])
		subprocess.check_output([COMPILER_DIR + 'elf2hex', '-o', HEX_FILE, 'obj/test.elf'])
	except subprocess.CalledProcessError as exc:
		raise TestException('Assembly failed:\n' + exc.output)

	return HEX_FILE

def run_emulator(block_device=None, dump_file=None, dump_base=None, dump_length=None):
	args = [BIN_DIR + 'emulator']
	if block_device:
		args += ['-b', block_device]

	if dump_file:
		args += [ '-d', dump_file + ',' + hex(dump_base) + ',' + hex(dump_length) ]

	args += [ HEX_FILE ]

	try:
		output = subprocess.check_output(args)
	except subprocess.CalledProcessError as exc:
		raise TestException('Emulator returned error: ' + exc.output)

	return output

def run_verilator(block_device=None, dump_file=None, dump_base=None, dump_length=None, extra_args=None):
	args = [BIN_DIR + 'verilator_model']
	if block_device:
		args += [ '+block=' + block_device ]

	if dump_file:
		args += ['+memdumpfile=' + dump_file, '+memdumpbase=' + hex(dump_base)[2:],
			'+memdumplen=' + hex(dump_length)[2:]]

	if extra_args:
		args += extra_args

	args += ['+bin=' + HEX_FILE]
	output = subprocess.check_output(args)
	if output.find('***HALTED***') == -1:
		raise TestException(output + '\nProgram did not halt normally')

	return output

def assert_files_equal(file1, file2, error_msg = ''):
	len1 = os.stat(file1).st_size
	len2 = os.stat(file2).st_size
	if len1 != len2:
		raise TestException('file mismatch: different lengths')

	BUFSIZE = 0x1000
	block_offset = 0
	with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
		while True:
			block1 = fp1.read(BUFSIZE)
			block2 = fp2.read(BUFSIZE)
			if block1 != block2:
				for i in range(len(block1)):
					if block1[i] != block2[i]:
						# Show the difference
						exception_text = error_msg + ':\n'
						rounded_offset = i & ~15
						exception_text += '%08x' % (block_offset + rounded_offset),
						for x in range(16):
							exception_text += '%02x' % ord(block1[rounded_offset + x]),

						exception_text += '\n%08x' % (block_offset + rounded_offset),
						for x in range(16):
							exception_text += '%02x' % ord(block2[rounded_offset + x]),

						exception_text += '\n        ',
						for x in range(16):
							if block1[rounded_offset + x] != block2[rounded_offset + x]:
								exception_text += '^^',
							else:
								exception_text += '  ',

						raise TestException(exception_text)

			if not block1:
				return

			block_offset += BUFSIZE

tests = []

def register_tests(func, params):
	global tests
	tests += [(func, param) for param in params]

def execute_tests():
	ALIGN=30
	failing_tests = []
	for func, param in tests:
		print param + (' ' * (ALIGN - len(param))),
		try:
			func(param)
			print '[\x1b[32mPASS\x1b[0m]'
		except KeyboardInterrupt:
			sys.exit(1)
		except TestException as exc:
			print '[\x1b[31mFAIL\x1b[0m]'
			failing_tests += [(param, exc.output)]
		except Exception as exc:
			print '[\x1b[31mFAIL\x1b[0m]'
			failing_tests += [(param, 'Caught exception ' + str(sys.exc_info()[0]))]

	if failing_tests:
		print 'Failing tests:'
		for name, output in failing_tests:
			print name
			print output

	print str(len(failing_tests)) + '/' + str(len(tests)) + ' tests failed'
	if failing_tests != []:
		sys.exit(1)
<commit_msg>test_harness: don't need to spawn command to create object directory<commit_after>#
# Copyright 2011-2015 Jeff Bush
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Common utilities for tests
#

import subprocess
import os
import sys

COMPILER_DIR='/usr/local/llvm-nyuzi/bin/'
BASE_DIR=os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + '/../')
LIB_DIR=BASE_DIR+'/software/libs/'
BIN_DIR=BASE_DIR+'/bin/'

HEX_FILE='obj/test.hex'

class TestException:
	def __init__(self, output):
		self.output = output

def compile_test(source_file, optlevel='3'):
	if not os.path.exists('obj'):
		os.makedirs('obj')

	try:
		subprocess.check_output([COMPILER_DIR + 'clang', '-o', 'obj/test.elf',
			'-w',
			'-O' + optlevel,
			source_file,
			LIB_DIR + 'libc/crt0.o',
			LIB_DIR + 'libc/libc.a',
			LIB_DIR + 'libos/libos.a',
			'-I' + LIB_DIR + 'libc/include',
			'-I' + LIB_DIR + 'libos'],
			stderr=subprocess.STDOUT)
		subprocess.check_output([COMPILER_DIR + 'elf2hex', '-o', HEX_FILE, 'obj/test.elf'],
			stderr=subprocess.STDOUT)
	except subprocess.CalledProcessError as exc:
		raise TestException('Compilation failed:\n' + exc.output)

	return HEX_FILE

def assemble_test(source_file):
	if not os.path.exists('obj'):
		os.makedirs('obj')

	try:
		subprocess.check_output([COMPILER_DIR + 'clang', '-o', 'obj/test.elf', source_file])
		subprocess.check_output([COMPILER_DIR + 'elf2hex', '-o', HEX_FILE, 'obj/test.elf'])
	except subprocess.CalledProcessError as exc:
		raise TestException('Assembly failed:\n' + exc.output)

	return HEX_FILE

def run_emulator(block_device=None, dump_file=None, dump_base=None, dump_length=None):
	args = [BIN_DIR + 'emulator']
	if block_device:
		args += ['-b', block_device]

	if dump_file:
		args += [ '-d', dump_file + ',' + hex(dump_base) + ',' + hex(dump_length) ]

	args += [ HEX_FILE ]

	try:
		output = subprocess.check_output(args)
	except subprocess.CalledProcessError as exc:
		raise TestException('Emulator returned error: ' + exc.output)

	return output

def run_verilator(block_device=None, dump_file=None, dump_base=None, dump_length=None, extra_args=None):
	args = [BIN_DIR + 'verilator_model']
	if block_device:
		args += [ '+block=' + block_device ]

	if dump_file:
		args += ['+memdumpfile=' + dump_file, '+memdumpbase=' + hex(dump_base)[2:],
			'+memdumplen=' + hex(dump_length)[2:]]

	if extra_args:
		args += extra_args

	args += ['+bin=' + HEX_FILE]
	output = subprocess.check_output(args)
	if output.find('***HALTED***') == -1:
		raise TestException(output + '\nProgram did not halt normally')

	return output

def assert_files_equal(file1, file2, error_msg = ''):
	len1 = os.stat(file1).st_size
	len2 = os.stat(file2).st_size
	if len1 != len2:
		raise TestException('file mismatch: different lengths')

	BUFSIZE = 0x1000
	block_offset = 0
	with open(file1, 'rb') as fp1, open(file2, 'rb') as fp2:
		while True:
			block1 = fp1.read(BUFSIZE)
			block2 = fp2.read(BUFSIZE)
			if block1 != block2:
				for i in range(len(block1)):
					if block1[i] != block2[i]:
						# Show the difference
						exception_text = error_msg + ':\n'
						rounded_offset = i & ~15
						exception_text += '%08x' % (block_offset + rounded_offset),
						for x in range(16):
							exception_text += '%02x' % ord(block1[rounded_offset + x]),

						exception_text += '\n%08x' % (block_offset + rounded_offset),
						for x in range(16):
							exception_text += '%02x' % ord(block2[rounded_offset + x]),

						exception_text += '\n        ',
						for x in range(16):
							if block1[rounded_offset + x] != block2[rounded_offset + x]:
								exception_text += '^^',
							else:
								exception_text += '  ',

						raise TestException(exception_text)

			if not block1:
				return

			block_offset += BUFSIZE

tests = []

def register_tests(func, params):
	global tests
	tests += [(func, param) for param in params]

def execute_tests():
	ALIGN=30
	failing_tests = []
	for func, param in tests:
		print param + (' ' * (ALIGN - len(param))),
		try:
			func(param)
			print '[\x1b[32mPASS\x1b[0m]'
		except KeyboardInterrupt:
			sys.exit(1)
		except TestException as exc:
			print '[\x1b[31mFAIL\x1b[0m]'
			failing_tests += [(param, exc.output)]
		except Exception as exc:
			print '[\x1b[31mFAIL\x1b[0m]'
			failing_tests += [(param, 'Caught exception ' + str(sys.exc_info()[0]))]

	if failing_tests:
		print 'Failing tests:'
		for name, output in failing_tests:
			print name
			print output

	print str(len(failing_tests)) + '/' + str(len(tests)) + ' tests failed'
	if failing_tests != []:
		sys.exit(1)
<|endoftext|>


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

from __future__ import division, absolute_import, unicode_literals

import datetime as dt

import numpy as np
import pytest
from numpy.testing import assert_allclose

from apexpy import helpers

##############################################################################
# NOTE: whenever function outputs are tested against hard-coded numbers,     #
# the test results (numbers) were obtained by running the code that is       #
# tested. Therefore these tests below only check that nothing changes when   #
# refactoring etc., and not if the results are actually correct              #
##############################################################################

###============================================================================
### Test checklat
###============================================================================

def test_checklat_scalar():
    assert helpers.checklat(90) == 90
    assert helpers.checklat(0) == 0
    assert helpers.checklat(-90) == -90

    assert helpers.checklat(90+1e-5) == 90
    assert helpers.checklat(-90-1e-5) == -90

    assert type(helpers.checklat(0.)) == float
    assert type(helpers.checklat(0)) == int
    assert type(helpers.checklat(90+1e-5)) == int

    with pytest.raises(ValueError):
        helpers.checklat(90+1e-4)
    with pytest.raises(ValueError):
        helpers.checklat(-90-1e-4)

def test_checklat_message():
    with pytest.raises(ValueError) as excinfo:
        helpers.checklat(100)
    assert str(excinfo.value).startswith('lat must be in')
    with pytest.raises(ValueError) as excinfo:
        helpers.checklat(100, name='glat')
    assert str(excinfo.value).startswith('glat')

def test_checklat_array():
    assert_allclose(helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-5]),
                    np.array([-90, -90, 0, 90, 90]), rtol=0, atol=1e-8)

    assert type(helpers.checklat([0])) == list
    assert type(helpers.checklat(np.array([0]))) == np.ndarray

    with pytest.raises(ValueError):
        helpers.checklat([-90-1e-4, -90, 0, 90, 90+1e-5])

    with pytest.raises(ValueError):
        helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-4])

###============================================================================
### Test getsinIm
###============================================================================

def test_getsinIm_scalar():
    assert_allclose(helpers.getsinIm(60), 0.96076892283052284)
    assert_allclose(helpers.getsinIm(10), 0.33257924500670238)
    assert type(helpers.getsinIm(60)) != np.ndarray

def test_getsinIm_1Darray():
    assert_allclose(helpers.getsinIm([60, 10]),
                    [0.96076892283052284, 0.33257924500670238])

def test_getsinIm_2Darray():
    assert_allclose(helpers.getsinIm([[60, 10], [60, 10]]),
                    [[0.96076892283052284, 0.33257924500670238],
                     [0.96076892283052284, 0.33257924500670238]])

###============================================================================
### Test getcosIm
###============================================================================

def test_getcosIm_scalar():
    assert_allclose(helpers.getcosIm(60), 0.27735009811261463)
    assert_allclose(helpers.getcosIm(10), 0.94307531289434765)
    assert type(helpers.getcosIm(60)) != np.ndarray

def test_getcosIm_1Darray():
    assert_allclose(helpers.getcosIm([60, 10]),
                    [0.27735009811261463, 0.94307531289434765])

def test_getcosIm_2Darray():
    assert_allclose(helpers.getcosIm([[60, 10], [60, 10]]),
                    [[0.27735009811261463, 0.94307531289434765],
                     [0.27735009811261463, 0.94307531289434765]])

###============================================================================
### Test toYearFraction
###============================================================================

def test_toYearFraction():
    assert_allclose(helpers.toYearFraction(dt.datetime(2001, 1, 1, 0, 0, 0)),
                    2001)
    assert_allclose(helpers.toYearFraction(dt.date(2001, 1, 1)), 2001)
    assert_allclose(helpers.toYearFraction(dt.datetime(2002, 1, 1, 0, 0, 0)),
                    2002)
    assert_allclose(helpers.toYearFraction(dt.datetime(2005, 2, 3, 4, 5, 6)),
                    2005.090877283105)
    assert_allclose(helpers.toYearFraction(dt.datetime(2005, 12, 11, 10, 9, 8)),
                    2005.943624682902)

###============================================================================
### Test gc2gdlat
###============================================================================

def test_gc2gdlat():
    assert_allclose(helpers.gc2gdlat(0), 0)
    assert_allclose(helpers.gc2gdlat(90), 90)
    assert_allclose(helpers.gc2gdlat(30), 30.166923849507356)
    assert_allclose(helpers.gc2gdlat(60), 60.166364190170931)

###============================================================================
### Test subsol
###============================================================================

def test_subsol():
    assert_allclose(helpers.subsol(dt.datetime(2005, 2, 3, 4, 5, 6)),
                    (-16.505391672592904, 122.17768157084515))
    assert_allclose(helpers.subsol(dt.datetime(2010, 12, 11, 10, 9, 8)),
                    (-23.001554595838947, 26.008999999955023))

    with pytest.raises(ValueError):
        helpers.subsol(dt.datetime(1600, 12, 31, 23, 59, 59))
    assert_allclose(helpers.subsol(dt.datetime(1601, 1, 1, 0, 0, 0)),
                    (-23.06239721771427, -178.90131731228584))
    with pytest.raises(ValueError):
        helpers.subsol(dt.datetime(2101, 1, 1, 0, 0, 0))
    assert_allclose(helpers.subsol(dt.datetime(2100, 12, 31, 23, 59, 59)),
                    (-23.021061422069053, -179.23129780639425))

def datetime64_to_datetime(dt64):
    """Convert a numpy.datetime64 to datetime.datetime, works outside 32 bit int seconds range of 1970
    """
    year_floor = dt64.astype('datetime64[Y]')
    month_floor = dt64.astype('datetime64[M]')
    day_floor = dt64.astype('datetime64[D]')
    year = year_floor.astype(int) + 1970
    month = (month_floor - year_floor).astype('timedelta64[M]').astype(int) + 1
    day = (day_floor - month_floor).astype('timedelta64[D]').astype(int) + 1
    return dt.datetime(year, month, day)

def test_subsol_array():
    """Verify subsolar point calculation using an array of np.datetime64

    Notes
    -----
    Tested by ensurnig the array of np.datetime64 is equivalent to converting
    using single dt.datetime values

    """
    dates = np.arange(np.datetime64("1601"), np.datetime64("2100"), np.timedelta64(100, 'D')).astype('datetime64[s]')
    sslat, sslon = helpers.subsol(dates)
    for i, date in enumerate(dates):
        datetime = datetime64_to_datetime(date)
        true_sslat, true_sslon = helpers.subsol(datetime)
        assert sslat[i] == true_sslat
        assert sslon[i] == true_sslon

if __name__ == '__main__':
    pytest.main()
<commit_msg>STY: updated comments and line length<commit_after># -*- coding: utf-8 -*-

from __future__ import division, absolute_import, unicode_literals

import datetime as dt

import numpy as np
import pytest
from numpy.testing import assert_allclose

from apexpy import helpers

# ----------------------------------------------------------------------------
# NOTE: whenever function outputs are tested against hard-coded numbers, the
# test results (numbers) were obtained by running the code that is tested.
# Therefore these tests below only check that nothing changes when refactoring,
# etc., and not if the results are actually correct
# ----------------------------------------------------------------------------

# ============================================================================
#  Test checklat
# ============================================================================

def test_checklat_scalar():
    assert helpers.checklat(90) == 90
    assert helpers.checklat(0) == 0
    assert helpers.checklat(-90) == -90

    assert helpers.checklat(90+1e-5) == 90
    assert helpers.checklat(-90-1e-5) == -90

    assert type(helpers.checklat(0.)) == float
    assert type(helpers.checklat(0)) == int
    assert type(helpers.checklat(90+1e-5)) == int

    with pytest.raises(ValueError):
        helpers.checklat(90+1e-4)
    with pytest.raises(ValueError):
        helpers.checklat(-90-1e-4)

def test_checklat_message():
    with pytest.raises(ValueError) as excinfo:
        helpers.checklat(100)
    assert str(excinfo.value).startswith('lat must be in')
    with pytest.raises(ValueError) as excinfo:
        helpers.checklat(100, name='glat')
    assert str(excinfo.value).startswith('glat')

def test_checklat_array():
    assert_allclose(helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-5]),
                    np.array([-90, -90, 0, 90, 90]), rtol=0, atol=1e-8)

    assert type(helpers.checklat([0])) == list
    assert type(helpers.checklat(np.array([0]))) == np.ndarray

    with pytest.raises(ValueError):
        helpers.checklat([-90-1e-4, -90, 0, 90, 90+1e-5])

    with pytest.raises(ValueError):
        helpers.checklat([-90-1e-5, -90, 0, 90, 90+1e-4])

# ============================================================================
#  Test getsinIm
# ============================================================================

def test_getsinIm_scalar():
    assert_allclose(helpers.getsinIm(60), 0.96076892283052284)
    assert_allclose(helpers.getsinIm(10), 0.33257924500670238)
    assert type(helpers.getsinIm(60)) != np.ndarray

def test_getsinIm_1Darray():
    assert_allclose(helpers.getsinIm([60, 10]),
                    [0.96076892283052284, 0.33257924500670238])

def test_getsinIm_2Darray():
    assert_allclose(helpers.getsinIm([[60, 10], [60, 10]]),
                    [[0.96076892283052284, 0.33257924500670238],
                     [0.96076892283052284, 0.33257924500670238]])

# ============================================================================
#  Test getcosIm
# ============================================================================

def test_getcosIm_scalar():
    assert_allclose(helpers.getcosIm(60), 0.27735009811261463)
    assert_allclose(helpers.getcosIm(10), 0.94307531289434765)
    assert type(helpers.getcosIm(60)) != np.ndarray

def test_getcosIm_1Darray():
    assert_allclose(helpers.getcosIm([60, 10]),
                    [0.27735009811261463, 0.94307531289434765])

def test_getcosIm_2Darray():
    assert_allclose(helpers.getcosIm([[60, 10], [60, 10]]),
                    [[0.27735009811261463, 0.94307531289434765],
                     [0.27735009811261463, 0.94307531289434765]])

# ============================================================================
#  Test toYearFraction
# ============================================================================

def test_toYearFraction():
    assert_allclose(helpers.toYearFraction(dt.datetime(2001, 1, 1, 0, 0, 0)),
                    2001)
    assert_allclose(helpers.toYearFraction(dt.date(2001, 1, 1)), 2001)
    assert_allclose(helpers.toYearFraction(dt.datetime(2002, 1, 1, 0, 0, 0)),
                    2002)
    assert_allclose(helpers.toYearFraction(dt.datetime(2005, 2, 3, 4, 5, 6)),
                    2005.090877283105)
    assert_allclose(helpers.toYearFraction(dt.datetime(2005, 12, 11, 10, 9, 8)),
                    2005.943624682902)

# ============================================================================
#  Test gc2gdlat
# ============================================================================

def test_gc2gdlat():
    assert_allclose(helpers.gc2gdlat(0), 0)
    assert_allclose(helpers.gc2gdlat(90), 90)
    assert_allclose(helpers.gc2gdlat(30), 30.166923849507356)
    assert_allclose(helpers.gc2gdlat(60), 60.166364190170931)

# ============================================================================
#  Test subsol
# ============================================================================

def test_subsol():
    assert_allclose(helpers.subsol(dt.datetime(2005, 2, 3, 4, 5, 6)),
                    (-16.505391672592904, 122.17768157084515))
    assert_allclose(helpers.subsol(dt.datetime(2010, 12, 11, 10, 9, 8)),
                    (-23.001554595838947, 26.008999999955023))

    with pytest.raises(ValueError):
        helpers.subsol(dt.datetime(1600, 12, 31, 23, 59, 59))
    assert_allclose(helpers.subsol(dt.datetime(1601, 1, 1, 0, 0, 0)),
                    (-23.06239721771427, -178.90131731228584))
    with pytest.raises(ValueError):
        helpers.subsol(dt.datetime(2101, 1, 1, 0, 0, 0))
    assert_allclose(helpers.subsol(dt.datetime(2100, 12, 31, 23, 59, 59)),
                    (-23.021061422069053, -179.23129780639425))

def datetime64_to_datetime(dt64):
    """Convert numpy datetime64 object to a datetime datetime object

    Notes
    -----
    Works outside 32 bit int second range of 1970

    """
    year_floor = dt64.astype('datetime64[Y]')
    month_floor = dt64.astype('datetime64[M]')
    day_floor = dt64.astype('datetime64[D]')
    year = year_floor.astype(int) + 1970
    month = (month_floor - year_floor).astype('timedelta64[M]').astype(int) + 1
    day = (day_floor - month_floor).astype('timedelta64[D]').astype(int) + 1
    return dt.datetime(year, month, day)

def test_subsol_array():
    """Verify subsolar point calculation using an array of np.datetime64

    Notes
    -----
    Tested by ensurnig the array of np.datetime64 is equivalent to converting
    using single dt.datetime values

    """
    dates = np.arange(np.datetime64("1601"), np.datetime64("2100"),
                      np.timedelta64(100, 'D')).astype('datetime64[s]')
    sslat, sslon = helpers.subsol(dates)
    for i, date in enumerate(dates):
        datetime = datetime64_to_datetime(date)
        true_sslat, true_sslon = helpers.subsol(datetime)
        assert sslat[i] == true_sslat
        assert sslon[i] == true_sslon

if __name__ == '__main__':
    pytest.main()
<|endoftext|>


<commit_before>"""
Test GitHub issue #4. Diagram could not be loaded due to JuggleError
(presumed cyclic resolving of diagram items).
"""

from gaphor.tests import TestCase
from gaphor import UML
from gaphor.storage.storage import load

class CyclicDiagramTestCase(TestCase):

    #services = TestCase.services + ['undo_manager']

    def setUp(self):
        super(CyclicDiagramTestCase, self).setUp()

    def test_bug(self):
        """
        This test loads the file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        load('test-diagrams/diagram-#4.gaphor', self.element_factory)

# vi:sw=4:et:ai
<commit_msg>Updated test: now the error scenario is visible.<commit_after>"""
Test GitHub issue #4. Diagram could not be loaded due to JuggleError
(presumed cyclic resolving of diagram items).
"""

from gaphor.tests import TestCase
from gaphor import UML
from gaphor.storage.storage import load

class CyclicDiagramTestCase(TestCase):

    #services = TestCase.services + ['undo_manager']

    def setUp(self):
        super(CyclicDiagramTestCase, self).setUp()

    def test_bug(self):
        """
        This test loads the file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        load('test-diagrams/diagram-#4.gaphor', self.element_factory)

    def test_bug_idle(self):
        """
        This test loads the file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        import gobject, gtk
        def handler():
            try:
                load('test-diagrams/diagram-#4.gaphor', self.element_factory)
            finally:
                gtk.main_quit()

        assert gobject.timeout_add(1, handler) > 0
        gtk.main()

# vi:sw=4:et:ai
<|endoftext|>


<commit_before>"""
Tests for kernel objects.
"""

# pylint: disable=missing-docstring

from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

import numpy as np
import numpy.testing as nt
import scipy.optimize as spop

import reggie.kernels as kernels

### BASE TEST CLASSES #########################################################

class KernelTest(object):
    def __init__(self, kernel, X1, X2):
        self.kernel = kernel
        self.X1 = X1
        self.X2 = X2

    def test_kernel(self):
        K = self.kernel.get_kernel(self.X1, self.X1)
        k = self.kernel.get_dkernel(self.X1)
        nt.assert_allclose(k, K.diagonal())

    def test_dgrad(self):
        g = np.array(list(self.kernel.get_dgrad(self.X1)))
        G = np.array([_.diagonal() for _ in self.kernel.get_grad(self.X1,
                                                                 self.X1)])
        nt.assert_allclose(g, G)

    def test_call(self):
        m = self.X1.shape[0]
        n = self.X2.shape[0]
        K = self.kernel.get_kernel(self.X1, self.X2)
        K_ = np.array([self.kernel(x1, x2)
                       for x1 in self.X1
                       for x2 in self.X2]).reshape(m, n)
        nt.assert_equal(K, K_)

    def test_grad(self):
        x = self.kernel.get_params()
        k = lambda x, x1, x2: self.kernel.copy(x)(x1, x2)

        G = np.array(list(self.kernel.get_grad(self.X1, self.X2)))
        m = self.X1.shape[0]
        n = self.X2.shape[0]

        G_ = np.array([spop.approx_fprime(x, k, 1e-8, x1, x2)
                       for x1 in self.X1
                       for x2 in self.X2]).swapaxes(0, 1).reshape(-1, m, n)

        nt.assert_allclose(G, G_, rtol=1e-6, atol=1e-6)

class RealKernelTest(KernelTest):
    def __init__(self, kernel):
        rng = np.random.RandomState(0)
        X1 = rng.rand(5, kernel.ndim)
        X2 = rng.rand(3, kernel.ndim)
        super(RealKernelTest, self).__init__(kernel, X1, X2)

### PER-INSTANCE TEST CLASSES #################################################

class TestSEARD(RealKernelTest):
    def __init__(self):
        RealKernelTest.__init__(self, kernels.SE(0.8, [0.3, 0.4]))

class TestSEIso(RealKernelTest):
    def __init__(self):
        RealKernelTest.__init__(self, kernels.SE(0.8, 0.3, ndim=2))
<commit_msg>Update kernel tests.<commit_after>"""
Tests for kernel objects.
"""

# pylint: disable=missing-docstring

from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

import numpy as np
import numpy.testing as nt
import scipy.optimize as spop

import reggie.kernels as kernels

### BASE TEST CLASSES #########################################################

class KernelTest(object):
    def __init__(self, kernel, X1, X2):
        self.kernel = kernel
        self.X1 = X1
        self.X2 = X2

    def test_repr(self):
        _ = repr(self.kernel)

    def test_call(self):
        m = self.X1.shape[0]
        n = self.X2.shape[0]
        K = self.kernel.get_kernel(self.X1, self.X2)
        K_ = np.array([self.kernel(x1, x2)
                       for x1 in self.X1
                       for x2 in self.X2]).reshape(m, n)
        nt.assert_equal(K, K_)

    def test_get_kernel(self):
        K = self.kernel.get_kernel(self.X1, self.X1)
        k = self.kernel.get_dkernel(self.X1)
        nt.assert_allclose(k, K.diagonal())

    def test_get_dgrad(self):
        g = np.array(list(self.kernel.get_dgrad(self.X1)))
        G = np.array([_.diagonal() for _ in self.kernel.get_grad(self.X1,
                                                                 self.X1)])
        nt.assert_allclose(g, G)

    def test_get_grad(self):
        x = self.kernel.get_params()
        k = lambda x, x1, x2: self.kernel.copy(x)(x1, x2)

        G = np.array(list(self.kernel.get_grad(self.X1, self.X2)))
        m = self.X1.shape[0]
        n = self.X2.shape[0]

        G_ = np.array([spop.approx_fprime(x, k, 1e-8, x1, x2)
                       for x1 in self.X1
                       for x2 in self.X2]).swapaxes(0, 1).reshape(-1, m, n)

        nt.assert_allclose(G, G_, rtol=1e-6, atol=1e-6)

class RealKernelTest(KernelTest):
    def __init__(self, kernel):
        rng = np.random.RandomState(0)
        X1 = rng.rand(5, kernel.ndim)
        X2 = rng.rand(3, kernel.ndim)
        super(RealKernelTest, self).__init__(kernel, X1, X2)

    def test_get_gradx(self):
        G1 = self.kernel.get_gradx(self.X1, self.X2)
        m = self.X1.shape[0]
        n = self.X2.shape[0]
        d = self.X1.shape[1]
        k = self.kernel

        G2 = np.array([spop.approx_fprime(x1, k, 1e-8, x2)
                       for x1 in self.X1
                       for x2 in self.X2]).reshape(m, n, d)

        nt.assert_allclose(G1, G2, rtol=1e-6, atol=1e-6)

### PER-INSTANCE TEST CLASSES #################################################

class TestSEARD(RealKernelTest):
    def __init__(self):
        RealKernelTest.__init__(self, kernels.SE(0.8, [0.3, 0.4]))

class TestSEIso(RealKernelTest):
    def __init__(self):
        RealKernelTest.__init__(self, kernels.SE(0.8, 0.3, ndim=2))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import random
import string
from unittest import TestCase
from mock import patch
from datetime import datetime
from elasticsearch import Elasticsearch

from elasticdata.manager import (
    without,
    group,
    PersistedEntity,
    EntityManager,
    UPDATE,
    REMOVE,
    ADD,
    RepositoryError,
    EntityNotFound
)
from elasticdata import Type, TimestampedType

class ManagerTestType(Type):
    class Meta:
        scopes = {
            'all': ('foo', 'bar'),
            'small': ('foo', )
        }

class ManagerCallbacksTestType(Type):
    def pre_create(self, em):
        self['pre_create'] = self.get('foo', None)

    def pre_update(self, em):
        self['pre_update'] = self.get('foo', None)

    def pre_delete(self, em):
        pass

    def post_create(self, em):
        pass

    def post_update(self, em):
        pass

    def post_delete(self, em):
        pass

class HelpersTestCase(TestCase):
    def test_without(self):
        self.assertDictEqual({'key1': 1, 'key2': 2}, without(['key3'], {'key1': 1, 'key2': 2, 'key3': 3}))
        self.assertDictEqual({'key1': 1, 'key2': 2, 'key4': 4},
                             without(['key3'],
                                     {'key1': 1, 'key2': 2, 'key3': {'key4': 4}},
                                     move_up={'key3': ['key4']}))

    def test_group(self):
        type_getter = lambda item: item['type']
        data = [{'type': 'a'}, {'type': 'a'}, {'type': 'b'}]
        grouped_data = group(data, type_getter)
        self.assertEqual(len(grouped_data['a']), 2)
        self.assertEqual(len(grouped_data['b']), 1)
        self.assertListEqual(grouped_data.keys(), ['a', 'b'])

class PersistedEntityTestCase(TestCase):
    def test_new_entity(self):
        e = ManagerTestType({'foo': 'bar'})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {'_index': 'default', '_source': {'foo': 'bar'},
                                       '_type': 'manager_test_type', '_op_type': 'create'})
        e = ManagerTestType({'foo': 'bar', 'id': 1})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt,
                             {'_index': 'default', '_source': {'foo': 'bar'}, '_type': 'manager_test_type',
                              '_id': 1, '_op_type': 'create'})
        e = ManagerTestType({'foo': 'bar', 'id': 1, '_parent': '2'})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt,
                             {'_index': 'default', '_source': {'foo': 'bar'},
                              '_type': 'manager_test_type', '_id': 1, '_parent': '2', '_op_type': 'create'})

    def test_update_entity(self):
        e = ManagerTestType({'foo': 'bar', 'id': 1})
        pe = PersistedEntity(e, state=UPDATE)
        self.assertFalse(pe.is_action_needed())
        e['bar'] = 'baz'
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'bar': 'baz'}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        e['foo'] = 'baz'
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'foo': 'baz'}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        del e['bar']
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'bar': None}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        e['foo'] = 'baz'
        self.assertFalse(pe.is_action_needed())

    def test_delete_entity(self):
        e = ManagerTestType({'foo': 'bar'})
        pe = PersistedEntity(e, state=REMOVE)
        self.assertFalse(pe.is_action_needed())
        e = ManagerTestType({'foo': 'bar', 'id': '1'})
        pe = PersistedEntity(e, state=REMOVE)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': '1',
            '_index': 'default',
            '_op_type': 'delete',
            '_type': 'manager_test_type'
        })

class EntityManagerTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super(EntityManagerTestCase, cls).setUpClass()
        cls._index = ''.join(random.choice(string.ascii_lowercase) for _ in range(10))

    def tearDown(self):
        es = Elasticsearch()
        es.indices.delete(index=self._index, ignore=[404])

    @property
    def em(self):
        return EntityManager(index=self._index)

    def test_persist(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(len(em._registry), 1)
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e2)
        self.assertEqual(len(em._registry), 2)
        em.persist(e)
        self.assertEqual(len(em._registry), 2)
        self.assertRaises(TypeError, em.persist, dict())

    def test_remove(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(em._registry.values()[0].state, ADD)
        em.remove(e)
        self.assertEqual(em._registry.values()[0].state, REMOVE)

    def test_flush(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e2)
        em.flush()
        self.assertTrue('id' in e)
        self.assertTrue('id' in e2)
        self.assertTrue(all(map(lambda pe: pe.state == UPDATE, em._registry.values())))
        e2['bar'] = 'foo'
        self.assertTrue(all(map(lambda pe: pe.state == UPDATE, em._registry.values())))
        em.flush()

    def test_find(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        fe = em2.find(e['id'], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe.to_representation())
        self.assertRaises(EntityNotFound, em2.find, 'non-exists', ManagerTestType)

    def test_find_updated(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        e['bar'] = 'baz'
        em.flush()
        fe = em2.find(e['id'], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe.to_representation())

    def test_find_many(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e)
        em.persist(e2)
        em.flush()
        fe = em2.find_many([e['id'], e2['id']], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe[0].to_representation())
        self.assertDictEqual(e2.to_representation(), fe[1].to_representation())

    def test_query(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e2 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e3 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.persist(e2)
        em.persist(e3)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType)
        self.assertEqual(len(fe), 3)

    def test_query_one(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe = em2.query_one({'query': {'term': {'foo': {'value': 'bar'}}}}, ManagerTestType)
        self.assertEqual(fe['id'], e['id'])
        e2 = ManagerTestType({'foo': 'bar'})
        em.persist(e2)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        self.assertRaises(RepositoryError, em2.query_one, {'query': {'term': {'foo': {'value': 'bar'}}}}, ManagerTestType)

    def test_find_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        em.persist(e)
        em.flush()
        fe = em2.find(e['id'], ManagerTestType, scope='all')
        self.assertDictEqual({'foo': 'bar', 'bar': 'baz'}, fe.to_representation())
        fe2 = em2.find(e['id'], ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'bar'}, fe2.to_representation())

    def test_find_many_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        e2 = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        em.persist(e)
        em.persist(e2)
        em.flush()
        fe = em2.find_many([e['id'], e2['id']], ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'bar'}, fe[0].to_representation())
        self.assertDictEqual({'foo': 'bar'}, fe[1].to_representation())

    def test_query_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e2 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e3 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.persist(e2)
        em.persist(e3)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'value'}, fe[0].to_representation())
        self.assertDictEqual({'foo': 'value'}, fe[1].to_representation())
        self.assertDictEqual({'foo': 'value'}, fe[2].to_representation())

    def test_query_one_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe = em2.query_one({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'value'}, fe.to_representation())

    def test_timestamps(self):
        em = self.em
        e = TimestampedType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        self.assertIsInstance(e['created_at'], datetime)
        self.assertIsInstance(e['updated_at'], datetime)
        self.assertEqual(e['created_at'], e['updated_at'])
        e['baz'] = 'bar'
        em.flush()
        self.assertTrue(e['created_at'] < e['updated_at'])
        values = e.to_representation()
        self.assertIsInstance(values['created_at'], datetime)
        self.assertIsInstance(values['updated_at'], datetime)

    def test_pre_create_callback(self):
        em = self.em
        e = ManagerCallbacksTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        em2 = self.em
        e2 = em2.find(e['id'], ManagerCallbacksTestType)
        self.assertEqual(e2['pre_create'], 'bar')

    def test_post_create_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_create') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            mock.assert_called_with(em)

    def test_pre_update_callback(self):
        em = self.em
        e = ManagerCallbacksTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        e['bar'] = 'baz'
        em.flush()
        em2 = self.em
        e2 = em2.find(e['id'], ManagerCallbacksTestType)
        self.assertEqual(e2['pre_update'], 'bar')

    def test_post_update_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_update') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            e['bar'] = 'baz'
            em.flush()
            mock.assert_called_with(em)

    def test_pre_delete_callback(self):
        with patch.object(ManagerCallbacksTestType, 'pre_delete') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            em.remove(e)
            em.flush()
            mock.assert_called_with(em)

    def test_post_delete_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_delete') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            em.remove(e)
            em.flush()
            mock.assert_called_with(em)

    def test_clear(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(len(em._registry), 1)
        em.clear()
        self.assertEqual(len(em._registry), 0)

    def test_highlight_query(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar foo'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'match': {'foo': 'bar'}}, 'highlight': {'foo': {}}}, ManagerTestType)
        self.assertDictEqual(fe.highlight, {'foo': ['foo bar']})
<commit_msg>Fix test_highlight.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import random
import string
from unittest import TestCase
from mock import patch
from datetime import datetime
from elasticsearch import Elasticsearch

from elasticdata.manager import (
    without,
    group,
    PersistedEntity,
    EntityManager,
    UPDATE,
    REMOVE,
    ADD,
    RepositoryError,
    EntityNotFound
)
from elasticdata import Type, TimestampedType

class ManagerTestType(Type):
    class Meta:
        scopes = {
            'all': ('foo', 'bar'),
            'small': ('foo', )
        }

class ManagerCallbacksTestType(Type):
    def pre_create(self, em):
        self['pre_create'] = self.get('foo', None)

    def pre_update(self, em):
        self['pre_update'] = self.get('foo', None)

    def pre_delete(self, em):
        pass

    def post_create(self, em):
        pass

    def post_update(self, em):
        pass

    def post_delete(self, em):
        pass

class HelpersTestCase(TestCase):
    def test_without(self):
        self.assertDictEqual({'key1': 1, 'key2': 2}, without(['key3'], {'key1': 1, 'key2': 2, 'key3': 3}))
        self.assertDictEqual({'key1': 1, 'key2': 2, 'key4': 4},
                             without(['key3'],
                                     {'key1': 1, 'key2': 2, 'key3': {'key4': 4}},
                                     move_up={'key3': ['key4']}))

    def test_group(self):
        type_getter = lambda item: item['type']
        data = [{'type': 'a'}, {'type': 'a'}, {'type': 'b'}]
        grouped_data = group(data, type_getter)
        self.assertEqual(len(grouped_data['a']), 2)
        self.assertEqual(len(grouped_data['b']), 1)
        self.assertListEqual(grouped_data.keys(), ['a', 'b'])

class PersistedEntityTestCase(TestCase):
    def test_new_entity(self):
        e = ManagerTestType({'foo': 'bar'})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {'_index': 'default', '_source': {'foo': 'bar'},
                                       '_type': 'manager_test_type', '_op_type': 'create'})
        e = ManagerTestType({'foo': 'bar', 'id': 1})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt,
                             {'_index': 'default', '_source': {'foo': 'bar'}, '_type': 'manager_test_type',
                              '_id': 1, '_op_type': 'create'})
        e = ManagerTestType({'foo': 'bar', 'id': 1, '_parent': '2'})
        pe = PersistedEntity(e)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt,
                             {'_index': 'default', '_source': {'foo': 'bar'},
                              '_type': 'manager_test_type', '_id': 1, '_parent': '2', '_op_type': 'create'})

    def test_update_entity(self):
        e = ManagerTestType({'foo': 'bar', 'id': 1})
        pe = PersistedEntity(e, state=UPDATE)
        self.assertFalse(pe.is_action_needed())
        e['bar'] = 'baz'
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'bar': 'baz'}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        e['foo'] = 'baz'
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'foo': 'baz'}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        del e['bar']
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': 1,
            '_index': 'default',
            '_op_type': 'update',
            '_type': 'manager_test_type',
            'doc': {'bar': None}
        })
        pe.reset_state()
        self.assertFalse(pe.is_action_needed())
        e['foo'] = 'baz'
        self.assertFalse(pe.is_action_needed())

    def test_delete_entity(self):
        e = ManagerTestType({'foo': 'bar'})
        pe = PersistedEntity(e, state=REMOVE)
        self.assertFalse(pe.is_action_needed())
        e = ManagerTestType({'foo': 'bar', 'id': '1'})
        pe = PersistedEntity(e, state=REMOVE)
        self.assertTrue(pe.is_action_needed())
        self.assertDictEqual(pe.stmt, {
            '_id': '1',
            '_index': 'default',
            '_op_type': 'delete',
            '_type': 'manager_test_type'
        })

class EntityManagerTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super(EntityManagerTestCase, cls).setUpClass()
        cls._index = ''.join(random.choice(string.ascii_lowercase) for _ in range(10))

    def tearDown(self):
        es = Elasticsearch()
        es.indices.delete(index=self._index, ignore=[404])

    @property
    def em(self):
        return EntityManager(index=self._index)

    def test_persist(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(len(em._registry), 1)
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e2)
        self.assertEqual(len(em._registry), 2)
        em.persist(e)
        self.assertEqual(len(em._registry), 2)
        self.assertRaises(TypeError, em.persist, dict())

    def test_remove(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(em._registry.values()[0].state, ADD)
        em.remove(e)
        self.assertEqual(em._registry.values()[0].state, REMOVE)

    def test_flush(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e2)
        em.flush()
        self.assertTrue('id' in e)
        self.assertTrue('id' in e2)
        self.assertTrue(all(map(lambda pe: pe.state == UPDATE, em._registry.values())))
        e2['bar'] = 'foo'
        self.assertTrue(all(map(lambda pe: pe.state == UPDATE, em._registry.values())))
        em.flush()

    def test_find(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        fe = em2.find(e['id'], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe.to_representation())
        self.assertRaises(EntityNotFound, em2.find, 'non-exists', ManagerTestType)

    def test_find_updated(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        e['bar'] = 'baz'
        em.flush()
        fe = em2.find(e['id'], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe.to_representation())

    def test_find_many(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        e2 = ManagerTestType({'bar': 'baz'})
        em.persist(e)
        em.persist(e2)
        em.flush()
        fe = em2.find_many([e['id'], e2['id']], ManagerTestType)
        self.assertDictEqual(e.to_representation(), fe[0].to_representation())
        self.assertDictEqual(e2.to_representation(), fe[1].to_representation())

    def test_query(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e2 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e3 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.persist(e2)
        em.persist(e3)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType)
        self.assertEqual(len(fe), 3)

    def test_query_one(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe = em2.query_one({'query': {'term': {'foo': {'value': 'bar'}}}}, ManagerTestType)
        self.assertEqual(fe['id'], e['id'])
        e2 = ManagerTestType({'foo': 'bar'})
        em.persist(e2)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        self.assertRaises(RepositoryError, em2.query_one, {'query': {'term': {'foo': {'value': 'bar'}}}}, ManagerTestType)

    def test_find_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        em.persist(e)
        em.flush()
        fe = em2.find(e['id'], ManagerTestType, scope='all')
        self.assertDictEqual({'foo': 'bar', 'bar': 'baz'}, fe.to_representation())
        fe2 = em2.find(e['id'], ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'bar'}, fe2.to_representation())

    def test_find_many_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        e2 = ManagerTestType({'foo': 'bar', 'bar': 'baz'})
        em.persist(e)
        em.persist(e2)
        em.flush()
        fe = em2.find_many([e['id'], e2['id']], ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'bar'}, fe[0].to_representation())
        self.assertDictEqual({'foo': 'bar'}, fe[1].to_representation())

    def test_query_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e2 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        e3 = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.persist(e2)
        em.persist(e3)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'value'}, fe[0].to_representation())
        self.assertDictEqual({'foo': 'value'}, fe[1].to_representation())
        self.assertDictEqual({'foo': 'value'}, fe[2].to_representation())

    def test_query_one_scope(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'value', 'bar': 'baz', 'baz': 'foo'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe = em2.query_one({'query': {'term': {'foo': {'value': 'value'}}}}, ManagerTestType, scope='small')
        self.assertDictEqual({'foo': 'value'}, fe.to_representation())

    def test_timestamps(self):
        em = self.em
        e = TimestampedType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        self.assertIsInstance(e['created_at'], datetime)
        self.assertIsInstance(e['updated_at'], datetime)
        self.assertEqual(e['created_at'], e['updated_at'])
        e['baz'] = 'bar'
        em.flush()
        self.assertTrue(e['created_at'] < e['updated_at'])
        values = e.to_representation()
        self.assertIsInstance(values['created_at'], datetime)
        self.assertIsInstance(values['updated_at'], datetime)

    def test_pre_create_callback(self):
        em = self.em
        e = ManagerCallbacksTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        em2 = self.em
        e2 = em2.find(e['id'], ManagerCallbacksTestType)
        self.assertEqual(e2['pre_create'], 'bar')

    def test_post_create_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_create') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            mock.assert_called_with(em)

    def test_pre_update_callback(self):
        em = self.em
        e = ManagerCallbacksTestType({'foo': 'bar'})
        em.persist(e)
        em.flush()
        e['bar'] = 'baz'
        em.flush()
        em2 = self.em
        e2 = em2.find(e['id'], ManagerCallbacksTestType)
        self.assertEqual(e2['pre_update'], 'bar')

    def test_post_update_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_update') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            e['bar'] = 'baz'
            em.flush()
            mock.assert_called_with(em)

    def test_pre_delete_callback(self):
        with patch.object(ManagerCallbacksTestType, 'pre_delete') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            em.remove(e)
            em.flush()
            mock.assert_called_with(em)

    def test_post_delete_callback(self):
        with patch.object(ManagerCallbacksTestType, 'post_delete') as mock:
            em = self.em
            e = ManagerCallbacksTestType({'foo': 'bar'})
            em.persist(e)
            em.flush()
            em.remove(e)
            em.flush()
            mock.assert_called_with(em)

    def test_clear(self):
        em = self.em
        e = ManagerTestType({'foo': 'bar'})
        em.persist(e)
        self.assertEqual(len(em._registry), 1)
        em.clear()
        self.assertEqual(len(em._registry), 0)

    def test_highlight_query(self):
        em = self.em
        em2 = self.em
        e = ManagerTestType({'foo': 'bar foo'})
        em.persist(e)
        em.flush()
        em.get_client().indices.refresh(index=self._index)
        fe, meta = em2.query({'query': {'match': {'foo': 'bar'}}, 'highlight': {'fields': {'foo': {}}}}, ManagerTestType)
        self.assertDictEqual(fe[0].highlight, {'foo': ['bar foo']})
<|endoftext|>


<commit_before>"""
    lamipy project - laminated composites calculations in Python.

    Run_FailureTest.py - Module containing functions for testing
    composite layups.

    version_0.1: Joao Paulo Bernhardt - September 2017
"""

import numpy as np
import CLT
import Failure_Criteria as FC

def Test_A():
# This temporary function tests the implementation.

    # Material 1 - Dictionary of properties
    mat1 = {
     "E1"  : 156.4e9,
     "E2"  : 7.786e9,
     "n12" : 0.354,
     "G12" : 3.762e9,
     "Xt" : 1826e6,
     "Xc" : 1134e6,
     "Yt" : 19e6,
     "Yc" : 131e6,
     "S12" : 75e6,
     "S32" : 41e6 }

    # Assembles material list
    mat = [mat1, []]

    # Initializes dictionary of the laminate layup configurations
    # thk = thickness; ang = angle; mat_id = material id
    lam = {"thk": [], "ang": [], "mat_id" : []}

    # Adds 12 layers of 0.127mm thickness and material id 0
    for i in range(12):
        lam["thk"].append(0.127e-3)
        lam["mat_id"].append(0)
    lam["ang"].extend((45, -45, 45, -45, 45, -45, -45, 45, -45, 45, -45, 45))

    # Vector F with the applied generalized stress (unit N/m2 / N.m/m2)
    F = [   2277,   # Nx
            284.6,   # Ny
            0e6,   # Nxy
            0e6,   # Mx
            0e6,   # My
            0e6]   # Mxy

    # Calculates stresses and strains based on CLT.
    # res = calculated results holder;
    # LCS = Laminate System; MCS = Material Coordinate System;
    # inf = inferior; sup = superior.
    res = CLT.calc_stressCLT(mat, lam, F)

    sfTsaiWu    = FC.tsaiwu_2D(mat,
                               lam,
                               res["MCS"]["stress"]["inf"],
                               res["MCS"]["stress"]["sup"])

    sfMaxStress = FC.maxstress_2D(mat,
                                  lam,
                                  res["MCS"]["stress"]["inf"],
                                  res["MCS"]["stress"]["sup"])

    sfMaxStrain = FC.maxstrain_2D(mat,
                                  lam,
                                  res["MCS"]["strain"]["inf"],
                                  res["MCS"]["strain"]["sup"])

Test_A()<commit_msg>Minor correction (unit)<commit_after>"""
    lamipy project - laminated composites calculations in Python.

    Run_FailureTest.py - Module containing functions for testing
    composite layups.

    version_0.1: Joao Paulo Bernhardt - September 2017
"""

import numpy as np
import CLT
import Failure_Criteria as FC

def Test_A():
# This temporary function tests the implementation.

    # Material 1 - Dictionary of properties
    mat1 = {
     "E1"  : 156.4e9,
     "E2"  : 7.786e9,
     "n12" : 0.354,
     "G12" : 3.762e9,
     "Xt" : 1826e6,
     "Xc" : 1134e6,
     "Yt" : 19e6,
     "Yc" : 131e6,
     "S12" : 75e6,
     "S32" : 41e6 }

    # Assembles material list
    mat = [mat1, []]

    # Initializes dictionary of the laminate layup configurations
    # thk = thickness; ang = angle; mat_id = material id
    lam = {"thk": [], "ang": [], "mat_id" : []}

    # Adds 12 layers of 0.127mm thickness and material id 0
    for i in range(12):
        lam["thk"].append(0.127e-3)
        lam["mat_id"].append(0)
    lam["ang"].extend((45, -45, 45, -45, 45, -45, -45, 45, -45, 45, -45, 45))

    # Vector F with the applied generalized stress (unit N/m / N.m/m)
    F = [   2277,   # Nx
            284.6,   # Ny
            0e6,   # Nxy
            0e6,   # Mx
            0e6,   # My
            0e6]   # Mxy

    # Calculates stresses and strains based on CLT.
    # res = calculated results holder;
    # LCS = Laminate System; MCS = Material Coordinate System;
    # inf = inferior; sup = superior.
    res = CLT.calc_stressCLT(mat, lam, F)

    sfTsaiWu    = FC.tsaiwu_2D(mat,
                               lam,
                               res["MCS"]["stress"]["inf"],
                               res["MCS"]["stress"]["sup"])

    sfMaxStress = FC.maxstress_2D(mat,
                                  lam,
                                  res["MCS"]["stress"]["inf"],
                                  res["MCS"]["stress"]["sup"])

    sfMaxStrain = FC.maxstrain_2D(mat,
                                  lam,
                                  res["MCS"]["strain"]["inf"],
                                  res["MCS"]["strain"]["sup"])

Test_A()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Notes about unicode handling in psutil
======================================

In psutil these are the APIs returning or dealing with a string
('not tested' means they are not tested to deal with non-ASCII strings):

* Process.cmdline()
* Process.connections('unix')
* Process.cwd()
* Process.environ()
* Process.exe()
* Process.memory_maps()
* Process.name()
* Process.open_files()
* Process.username()             (not tested)

* disk_io_counters()             (not tested)
* disk_partitions()              (not tested)
* disk_usage(str)
* net_connections('unix')
* net_if_addrs()                 (not tested)
* net_if_stats()                 (not tested)
* net_io_counters()              (not tested)
* sensors_fans()                 (not tested)
* sensors_temperatures()         (not tested)
* users()                        (not tested)

* WindowsService.binpath()       (not tested)
* WindowsService.description()   (not tested)
* WindowsService.display_name()  (not tested)
* WindowsService.name()          (not tested)
* WindowsService.status()        (not tested)
* WindowsService.username()      (not tested)

In here we create a unicode path with a funky non-ASCII name and (where
possible) make psutil return it back (e.g. on name(), exe(), open_files(),
etc.) and make sure that:

* psutil never crashes with UnicodeDecodeError
* the returned path matches

For a detailed explanation of how psutil handles unicode see:
- https://github.com/giampaolo/psutil/issues/1040
- https://pythonhosted.org/psutil/#unicode
"""

import errno
import os
import traceback
import warnings
from contextlib import closing

from psutil import BSD
from psutil import OPENBSD
from psutil import OSX
from psutil import POSIX
from psutil import WINDOWS
from psutil._compat import PY3
from psutil._compat import u
from psutil.tests import APPVEYOR
from psutil.tests import ASCII_FS
from psutil.tests import bind_unix_socket
from psutil.tests import chdir
from psutil.tests import copyload_shared_lib
from psutil.tests import create_exe
from psutil.tests import get_test_subprocess
from psutil.tests import HAS_CONNECTIONS_UNIX
from psutil.tests import HAS_ENVIRON
from psutil.tests import HAS_MEMORY_MAPS
from psutil.tests import mock
from psutil.tests import reap_children
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_mkdir
from psutil.tests import safe_rmpath as _safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import TESTFILE_PREFIX
from psutil.tests import TESTFN
from psutil.tests import TESTFN_UNICODE
from psutil.tests import TRAVIS
from psutil.tests import unittest
from psutil.tests import unix_socket_path
import psutil
import psutil.tests

def safe_rmpath(path):
    # XXX
    return _safe_rmpath(path)
    if APPVEYOR:
        # TODO - this is quite random and I'm not sure why it happens,
        # nor I can reproduce it locally:
        # https://ci.appveyor.com/project/giampaolo/psutil/build/job/
        #     jiq2cgd6stsbtn60
        # safe_rmpath() happens after reap_children() so this is weird
        # Perhaps wait_procs() on Windows is broken? Maybe because
        # of STILL_ACTIVE?
        # https://github.com/giampaolo/psutil/blob/
        #     68c7a70728a31d8b8b58f4be6c4c0baa2f449eda/psutil/arch/
        #     windows/process_info.c#L146
        try:
            return _safe_rmpath(path)
        except WindowsError:
            traceback.print_exc()
    else:
        return _safe_rmpath(path)

def subprocess_supports_unicode(name):
    """Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    """
    if PY3:
        return True
    try:
        safe_rmpath(name)
        create_exe(name)
        get_test_subprocess(cmd=[name])
    except UnicodeEncodeError:
        return False
    else:
        reap_children()
        return True

# An invalid unicode string.
if PY3:
    INVALID_NAME = (TESTFN.encode('utf8') + b"f\xc0\x80").decode(
        'utf8', 'surrogateescape')
else:
    INVALID_NAME = TESTFN + "f\xc0\x80"

# ===================================================================
# FS APIs
# ===================================================================

class _BaseFSAPIsTests(object):
    funky_name = None

    def setUp(self):
        safe_rmpath(self.funky_name)

    def tearDown(self):
        reap_children()
        safe_rmpath(self.funky_name)

    def safe_rmpath(self, name):
        if POSIX:
            safe_rmpath(name)
        else:
            # https://ci.appveyor.com/project/giampaolo/psutil/build/
            #     1225/job/1yec67sr6e9rl217
            try:
                safe_rmpath(name)
            except OSError as err:
                if err.errno in (errno.EACCES, errno.EPERM):
                    traceback.print_exc()
                else:
                    raise

    def expect_exact_path_match(self):
        raise NotImplementedError("must be implemented in subclass")

    def test_proc_exe(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        p = psutil.Process(subp.pid)
        exe = p.exe()
        self.assertIsInstance(exe, str)
        if self.expect_exact_path_match():
            self.assertEqual(exe, self.funky_name)

    def test_proc_name(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        if WINDOWS:
            # On Windows name() is determined from exe() first, because
            # it's faster; we want to overcome the internal optimization
            # and test name() instead of exe().
            with mock.patch("psutil._psplatform.cext.proc_exe",
                            side_effect=psutil.AccessDenied(os.getpid())) as m:
                name = psutil.Process(subp.pid).name()
                assert m.called
        else:
            name = psutil.Process(subp.pid).name()
        self.assertIsInstance(name, str)
        if self.expect_exact_path_match():
            self.assertEqual(name, os.path.basename(self.funky_name))

    def test_proc_cmdline(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        p = psutil.Process(subp.pid)
        cmdline = p.cmdline()
        for part in cmdline:
            self.assertIsInstance(part, str)
        if self.expect_exact_path_match():
            self.assertEqual(cmdline, [self.funky_name])

    def test_proc_cwd(self):
        safe_mkdir(self.funky_name)
        with chdir(self.funky_name):
            p = psutil.Process()
            cwd = p.cwd()
        self.assertIsInstance(p.cwd(), str)
        if self.expect_exact_path_match():
            self.assertEqual(cwd, self.funky_name)

    def test_proc_open_files(self):
        p = psutil.Process()
        start = set(p.open_files())
        with open(self.funky_name, 'wb'):
            new = set(p.open_files())
        path = (new - start).pop().path
        self.assertIsInstance(path, str)
        if BSD and not path:
            # XXX - see https://github.com/giampaolo/psutil/issues/595
            return self.skipTest("open_files on BSD is broken")
        if self.expect_exact_path_match():
            self.assertEqual(os.path.normcase(path),
                             os.path.normcase(self.funky_name))

    @unittest.skipIf(not POSIX, "POSIX only")
    def test_proc_connections(self):
        suffix = os.path.basename(self.funky_name)
        with unix_socket_path(suffix=suffix) as name:
            try:
                sock = bind_unix_socket(name)
            except UnicodeEncodeError:
                if PY3:
                    raise
                else:
                    raise unittest.SkipTest("not supported")
            with closing(sock):
                conn = psutil.Process().connections('unix')[0]
                self.assertIsInstance(conn.laddr, str)
                # AF_UNIX addr not set on OpenBSD
                if not OPENBSD:
                    self.assertEqual(conn.laddr, name)

    @unittest.skipIf(not POSIX, "POSIX only")
    @unittest.skipIf(not HAS_CONNECTIONS_UNIX, "can't list UNIX sockets")
    @skip_on_access_denied()
    def test_net_connections(self):
        def find_sock(cons):
            for conn in cons:
                if os.path.basename(conn.laddr).startswith(TESTFILE_PREFIX):
                    return conn
            raise ValueError("connection not found")

        suffix = os.path.basename(self.funky_name)
        with unix_socket_path(suffix=suffix) as name:
            try:
                sock = bind_unix_socket(name)
            except UnicodeEncodeError:
                if PY3:
                    raise
                else:
                    raise unittest.SkipTest("not supported")
            with closing(sock):
                cons = psutil.net_connections(kind='unix')
                # AF_UNIX addr not set on OpenBSD
                if not OPENBSD:
                    conn = find_sock(cons)
                    self.assertIsInstance(conn.laddr, str)
                    self.assertEqual(conn.laddr, name)

    def test_disk_usage(self):
        safe_mkdir(self.funky_name)
        psutil.disk_usage(self.funky_name)

    @unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
    @unittest.skipIf(not PY3, "ctypes does not support unicode on PY2")
    def test_memory_maps(self):
        # XXX: on Python 2, using ctypes.CDLL with a unicode path
        # opens a message box which blocks the test run.
        with copyload_shared_lib(dst_prefix=self.funky_name) as funky_path:
            def normpath(p):
                return os.path.realpath(os.path.normcase(p))
            libpaths = [normpath(x.path)
                        for x in psutil.Process().memory_maps()]
            # ...just to have a clearer msg in case of failure
            libpaths = [x for x in libpaths if TESTFILE_PREFIX in x]
            self.assertIn(normpath(funky_path), libpaths)
            for path in libpaths:
                self.assertIsInstance(path, str)

@unittest.skipIf(OSX and TRAVIS, "unreliable on TRAVIS")  # TODO
@unittest.skipIf(ASCII_FS, "ASCII fs")
@unittest.skipIf(not subprocess_supports_unicode(TESTFN_UNICODE),
                 "subprocess can't deal with unicode")
class TestFSAPIs(_BaseFSAPIsTests, unittest.TestCase):
    """Test FS APIs with a funky, valid, UTF8 path name."""
    funky_name = TESTFN_UNICODE

    @classmethod
    def expect_exact_path_match(cls):
        # Do not expect psutil to correctly handle unicode paths on
        # Python 2 if os.listdir() is not able either.
        if PY3:
            return True
        else:
            here = '.' if isinstance(cls.funky_name, str) else u('.')
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                return cls.funky_name in os.listdir(here)

@unittest.skipIf(OSX and TRAVIS, "unreliable on TRAVIS")  # TODO
@unittest.skipIf(not subprocess_supports_unicode(INVALID_NAME),
                 "subprocess can't deal with invalid unicode")
class TestFSAPIsWithInvalidPath(_BaseFSAPIsTests, unittest.TestCase):
    """Test FS APIs with a funky, invalid path name."""
    funky_name = INVALID_NAME

    @classmethod
    def expect_exact_path_match(cls):
        # Invalid unicode names are supposed to work on Python 2.
        return True

@unittest.skipIf(not WINDOWS, "WINDOWS only")
class TestWinProcessName(unittest.TestCase):

    def test_name_type(self):
        # On Windows name() is determined from exe() first, because
        # it's faster; we want to overcome the internal optimization
        # and test name() instead of exe().
        with mock.patch("psutil._psplatform.cext.proc_exe",
                        side_effect=psutil.AccessDenied(os.getpid())) as m:
            self.assertIsInstance(psutil.Process().name(), str)
            assert m.called

# ===================================================================
# Non fs APIs
# ===================================================================

class TestNonFSAPIS(unittest.TestCase):
    """Unicode tests for non fs-related APIs."""

    @unittest.skipIf(not HAS_ENVIRON, "not supported")
    def test_proc_environ(self):
        # Note: differently from others, this test does not deal
        # with fs paths. On Python 2 subprocess module is broken as
        # it's not able to handle with non-ASCII env vars, so
        # we use "è", which is part of the extended ASCII table
        # (unicode point <= 255).
        env = os.environ.copy()
        funky_str = TESTFN_UNICODE if PY3 else 'è'
        env['FUNNY_ARG'] = funky_str
        sproc = get_test_subprocess(env=env)
        p = psutil.Process(sproc.pid)
        env = p.environ()
        for k, v in env.items():
            self.assertIsInstance(k, str)
            self.assertIsInstance(v, str)
        self.assertEqual(env['FUNNY_ARG'], funky_str)

if __name__ == '__main__':
    run_test_module_by_name(__file__)
<commit_msg>remove dead code<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Notes about unicode handling in psutil
======================================

In psutil these are the APIs returning or dealing with a string
('not tested' means they are not tested to deal with non-ASCII strings):

* Process.cmdline()
* Process.connections('unix')
* Process.cwd()
* Process.environ()
* Process.exe()
* Process.memory_maps()
* Process.name()
* Process.open_files()
* Process.username()             (not tested)

* disk_io_counters()             (not tested)
* disk_partitions()              (not tested)
* disk_usage(str)
* net_connections('unix')
* net_if_addrs()                 (not tested)
* net_if_stats()                 (not tested)
* net_io_counters()              (not tested)
* sensors_fans()                 (not tested)
* sensors_temperatures()         (not tested)
* users()                        (not tested)

* WindowsService.binpath()       (not tested)
* WindowsService.description()   (not tested)
* WindowsService.display_name()  (not tested)
* WindowsService.name()          (not tested)
* WindowsService.status()        (not tested)
* WindowsService.username()      (not tested)

In here we create a unicode path with a funky non-ASCII name and (where
possible) make psutil return it back (e.g. on name(), exe(), open_files(),
etc.) and make sure that:

* psutil never crashes with UnicodeDecodeError
* the returned path matches

For a detailed explanation of how psutil handles unicode see:
- https://github.com/giampaolo/psutil/issues/1040
- https://pythonhosted.org/psutil/#unicode
"""

import os
import traceback
import warnings
from contextlib import closing

from psutil import BSD
from psutil import OPENBSD
from psutil import OSX
from psutil import POSIX
from psutil import WINDOWS
from psutil._compat import PY3
from psutil._compat import u
from psutil.tests import APPVEYOR
from psutil.tests import ASCII_FS
from psutil.tests import bind_unix_socket
from psutil.tests import chdir
from psutil.tests import copyload_shared_lib
from psutil.tests import create_exe
from psutil.tests import get_test_subprocess
from psutil.tests import HAS_CONNECTIONS_UNIX
from psutil.tests import HAS_ENVIRON
from psutil.tests import HAS_MEMORY_MAPS
from psutil.tests import mock
from psutil.tests import reap_children
from psutil.tests import run_test_module_by_name
from psutil.tests import safe_mkdir
from psutil.tests import safe_rmpath as _safe_rmpath
from psutil.tests import skip_on_access_denied
from psutil.tests import TESTFILE_PREFIX
from psutil.tests import TESTFN
from psutil.tests import TESTFN_UNICODE
from psutil.tests import TRAVIS
from psutil.tests import unittest
from psutil.tests import unix_socket_path
import psutil
import psutil.tests

def safe_rmpath(path):
    # XXX
    return _safe_rmpath(path)
    if APPVEYOR:
        # TODO - this is quite random and I'm not sure why it happens,
        # nor I can reproduce it locally:
        # https://ci.appveyor.com/project/giampaolo/psutil/build/job/
        #     jiq2cgd6stsbtn60
        # safe_rmpath() happens after reap_children() so this is weird
        # Perhaps wait_procs() on Windows is broken? Maybe because
        # of STILL_ACTIVE?
        # https://github.com/giampaolo/psutil/blob/
        #     68c7a70728a31d8b8b58f4be6c4c0baa2f449eda/psutil/arch/
        #     windows/process_info.c#L146
        try:
            return _safe_rmpath(path)
        except WindowsError:
            traceback.print_exc()
    else:
        return _safe_rmpath(path)

def subprocess_supports_unicode(name):
    """Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    """
    if PY3:
        return True
    try:
        safe_rmpath(name)
        create_exe(name)
        get_test_subprocess(cmd=[name])
    except UnicodeEncodeError:
        return False
    else:
        reap_children()
        return True

# An invalid unicode string.
if PY3:
    INVALID_NAME = (TESTFN.encode('utf8') + b"f\xc0\x80").decode(
        'utf8', 'surrogateescape')
else:
    INVALID_NAME = TESTFN + "f\xc0\x80"

# ===================================================================
# FS APIs
# ===================================================================

class _BaseFSAPIsTests(object):
    funky_name = None

    def setUp(self):
        safe_rmpath(self.funky_name)

    def tearDown(self):
        reap_children()
        safe_rmpath(self.funky_name)

    def expect_exact_path_match(self):
        raise NotImplementedError("must be implemented in subclass")

    def test_proc_exe(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        p = psutil.Process(subp.pid)
        exe = p.exe()
        self.assertIsInstance(exe, str)
        if self.expect_exact_path_match():
            self.assertEqual(exe, self.funky_name)

    def test_proc_name(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        if WINDOWS:
            # On Windows name() is determined from exe() first, because
            # it's faster; we want to overcome the internal optimization
            # and test name() instead of exe().
            with mock.patch("psutil._psplatform.cext.proc_exe",
                            side_effect=psutil.AccessDenied(os.getpid())) as m:
                name = psutil.Process(subp.pid).name()
                assert m.called
        else:
            name = psutil.Process(subp.pid).name()
        self.assertIsInstance(name, str)
        if self.expect_exact_path_match():
            self.assertEqual(name, os.path.basename(self.funky_name))

    def test_proc_cmdline(self):
        create_exe(self.funky_name)
        subp = get_test_subprocess(cmd=[self.funky_name])
        p = psutil.Process(subp.pid)
        cmdline = p.cmdline()
        for part in cmdline:
            self.assertIsInstance(part, str)
        if self.expect_exact_path_match():
            self.assertEqual(cmdline, [self.funky_name])

    def test_proc_cwd(self):
        safe_mkdir(self.funky_name)
        with chdir(self.funky_name):
            p = psutil.Process()
            cwd = p.cwd()
        self.assertIsInstance(p.cwd(), str)
        if self.expect_exact_path_match():
            self.assertEqual(cwd, self.funky_name)

    def test_proc_open_files(self):
        p = psutil.Process()
        start = set(p.open_files())
        with open(self.funky_name, 'wb'):
            new = set(p.open_files())
        path = (new - start).pop().path
        self.assertIsInstance(path, str)
        if BSD and not path:
            # XXX - see https://github.com/giampaolo/psutil/issues/595
            return self.skipTest("open_files on BSD is broken")
        if self.expect_exact_path_match():
            self.assertEqual(os.path.normcase(path),
                             os.path.normcase(self.funky_name))

    @unittest.skipIf(not POSIX, "POSIX only")
    def test_proc_connections(self):
        suffix = os.path.basename(self.funky_name)
        with unix_socket_path(suffix=suffix) as name:
            try:
                sock = bind_unix_socket(name)
            except UnicodeEncodeError:
                if PY3:
                    raise
                else:
                    raise unittest.SkipTest("not supported")
            with closing(sock):
                conn = psutil.Process().connections('unix')[0]
                self.assertIsInstance(conn.laddr, str)
                # AF_UNIX addr not set on OpenBSD
                if not OPENBSD:
                    self.assertEqual(conn.laddr, name)

    @unittest.skipIf(not POSIX, "POSIX only")
    @unittest.skipIf(not HAS_CONNECTIONS_UNIX, "can't list UNIX sockets")
    @skip_on_access_denied()
    def test_net_connections(self):
        def find_sock(cons):
            for conn in cons:
                if os.path.basename(conn.laddr).startswith(TESTFILE_PREFIX):
                    return conn
            raise ValueError("connection not found")

        suffix = os.path.basename(self.funky_name)
        with unix_socket_path(suffix=suffix) as name:
            try:
                sock = bind_unix_socket(name)
            except UnicodeEncodeError:
                if PY3:
                    raise
                else:
                    raise unittest.SkipTest("not supported")
            with closing(sock):
                cons = psutil.net_connections(kind='unix')
                # AF_UNIX addr not set on OpenBSD
                if not OPENBSD:
                    conn = find_sock(cons)
                    self.assertIsInstance(conn.laddr, str)
                    self.assertEqual(conn.laddr, name)

    def test_disk_usage(self):
        safe_mkdir(self.funky_name)
        psutil.disk_usage(self.funky_name)

    @unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
    @unittest.skipIf(not PY3, "ctypes does not support unicode on PY2")
    def test_memory_maps(self):
        # XXX: on Python 2, using ctypes.CDLL with a unicode path
        # opens a message box which blocks the test run.
        with copyload_shared_lib(dst_prefix=self.funky_name) as funky_path:
            def normpath(p):
                return os.path.realpath(os.path.normcase(p))
            libpaths = [normpath(x.path)
                        for x in psutil.Process().memory_maps()]
            # ...just to have a clearer msg in case of failure
            libpaths = [x for x in libpaths if TESTFILE_PREFIX in x]
            self.assertIn(normpath(funky_path), libpaths)
            for path in libpaths:
                self.assertIsInstance(path, str)

@unittest.skipIf(OSX and TRAVIS, "unreliable on TRAVIS")  # TODO
@unittest.skipIf(ASCII_FS, "ASCII fs")
@unittest.skipIf(not subprocess_supports_unicode(TESTFN_UNICODE),
                 "subprocess can't deal with unicode")
class TestFSAPIs(_BaseFSAPIsTests, unittest.TestCase):
    """Test FS APIs with a funky, valid, UTF8 path name."""
    funky_name = TESTFN_UNICODE

    @classmethod
    def expect_exact_path_match(cls):
        # Do not expect psutil to correctly handle unicode paths on
        # Python 2 if os.listdir() is not able either.
        if PY3:
            return True
        else:
            here = '.' if isinstance(cls.funky_name, str) else u('.')
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                return cls.funky_name in os.listdir(here)

@unittest.skipIf(OSX and TRAVIS, "unreliable on TRAVIS")  # TODO
@unittest.skipIf(not subprocess_supports_unicode(INVALID_NAME),
                 "subprocess can't deal with invalid unicode")
class TestFSAPIsWithInvalidPath(_BaseFSAPIsTests, unittest.TestCase):
    """Test FS APIs with a funky, invalid path name."""
    funky_name = INVALID_NAME

    @classmethod
    def expect_exact_path_match(cls):
        # Invalid unicode names are supposed to work on Python 2.
        return True

@unittest.skipIf(not WINDOWS, "WINDOWS only")
class TestWinProcessName(unittest.TestCase):

    def test_name_type(self):
        # On Windows name() is determined from exe() first, because
        # it's faster; we want to overcome the internal optimization
        # and test name() instead of exe().
        with mock.patch("psutil._psplatform.cext.proc_exe",
                        side_effect=psutil.AccessDenied(os.getpid())) as m:
            self.assertIsInstance(psutil.Process().name(), str)
            assert m.called

# ===================================================================
# Non fs APIs
# ===================================================================

class TestNonFSAPIS(unittest.TestCase):
    """Unicode tests for non fs-related APIs."""

    @unittest.skipIf(not HAS_ENVIRON, "not supported")
    def test_proc_environ(self):
        # Note: differently from others, this test does not deal
        # with fs paths. On Python 2 subprocess module is broken as
        # it's not able to handle with non-ASCII env vars, so
        # we use "è", which is part of the extended ASCII table
        # (unicode point <= 255).
        env = os.environ.copy()
        funky_str = TESTFN_UNICODE if PY3 else 'è'
        env['FUNNY_ARG'] = funky_str
        sproc = get_test_subprocess(env=env)
        p = psutil.Process(sproc.pid)
        env = p.environ()
        for k, v in env.items():
            self.assertIsInstance(k, str)
            self.assertIsInstance(v, str)
        self.assertEqual(env['FUNNY_ARG'], funky_str)

if __name__ == '__main__':
    run_test_module_by_name(__file__)
<|endoftext|>