<commit_before>import main
import robocup
import behavior
import constants
import enum
import math

import composite_behavior
import evaluation.ball
import evaluation.defensive_positioning
import skills.mark
import skills.capture

# Finds the positions to place each defender
class DefensiveForward(composite_behavior.CompositeBehavior):
    class State(enum.Enum):
        # Everyone is defending
        blocking = 1
        # Closest defender is collecting, all others are blocking
        collecting = 2

    def __init__(self):
        super().__init__(continuous=True)

        # Robot queue
        self.defenders = [None, None, None]
        # Single robot collecting
        self.collector = None

        self.zone_def_pos = robocup.Point(0, 0)
        self.mark_bots = [None, None]

        self.block_dist = 0.01
        self.block_angle_coeff = 0.5
        self.names = ['mark_main', 'mark_sub', 'mark_zone']
        # Distance offset for path estimation
        self.dodge_dist = .2

        self.time_since_call = 0
        # Estimates defensive positions every X ticks
        self.cache_amnt = 5

        for s in DefensiveForward.State:
            self.add_state(s, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
                            DefensiveForward.State.blocking, lambda: True,
                            'immediately')

        self.add_transition(DefensiveForward.State.blocking,
                            DefensiveForward.State.collecting,
                            lambda: self.within_range(), 'Collecting')

        self.add_transition(
            DefensiveForward.State.collecting, DefensiveForward.State.blocking,
            lambda: not self.within_range(), 'Back to blocking')

        self.add_transition(
            DefensiveForward.State.collecting,
            behavior.Behavior.State.completed,
            lambda: self.collector is not None and evaluation.ball.robot_has_ball(self.collector.robot),
            'Ball collected')

        # Create list of defenders and start the marking
    def on_enter_blocking(self):
        self.zone_def_pos, self.mark_bots[0], self.mark_bots[
            1] = evaluation.defensive_positioning.find_defense_positions()

        for i in range(0, 2):
            self.defenders[i] = skills.mark.Mark()
            self.add_subbehavior(self.defenders[i],
                                 self.names[i],
                                 required=True)
            self.defenders[i].mark_robot = self.mark_bots[i]
            # TODO: Shift to mark point when "get_block_pos" is fixed

        self.defenders[2] = skills.mark.Mark()
        self.add_subbehavior(self.defenders[2], self.names[2], required=True)
        self.defenders[2].mark_point = self.zone_def_pos

        # Reset time since call
        self.time_since_call = 0

    # Continue updating the mark positions
    def execute_blocking(self):
        self.time_since_call += 1

        # You don't want to test if its 0 because it would execute instantly
        if (self.time_since_call % self.cache_amnt == self.cache_amnt - 1):
            self.zone_def_pos, self.mark_bots[0], self.mark_bots[1] = \
                evaluation.defensive_positioning.find_defense_positions(
                    [self.defenders[0].robot, self.defenders[1].robot])

            for i in range(0, 2):
                if (self.defenders[i].mark_robot not in self.mark_bots):
                    self.defenders[i].mark_robot = self.mark_bots[i]
                # TODO: Shift to mark point when "get_block_pos" is fixed

            self.defenders[2].mark_point = self.zone_def_pos

    def on_exit_blocking(self):
        self.remove_all_subbehaviors()

    # Create collector method and then
    def on_enter_collecting(self):
        self.zone_def_pos, self.mark_bots[0], self.mark_bots[1] = \
            evaluation.defensive_positioning.find_defense_positions()

        # Take closest robot to collect
        # Leave other two to move into primary blocking positions
        self.collector = skills.capture.Capture()
        self.add_subbehavior(self.collector, 'collector', required=True)

        for i in range(0, 2):
            self.defenders[i] = skills.mark.Mark()
            self.add_subbehavior(self.defenders[i],
                                 self.names[i],
                                 required=True)
            self.defenders[i].mark_robot = self.mark_bots[i]

    def on_exit_collecting(self):
        self.remove_all_subbehaviors()
        self.collector = None

    # Uses their predicted kick direction to block
    def get_block_pos(self, bot):
        # Get predicted angle of shot
        # Get goal to bot
        # Place point x dist away
        predicted = evaluation.defensive_positioning.predict_kick_direction(
            bot)
        actual = bot.angle

        angle = self.block_angle_coeff * predicted + (
            1 - self.block_angle_coeff) * actual

        x = math.cos(angle)
        y = math.sin(angle)
        pos = robocup.Point(x, y).normalized() + bot.pos

        return None  # pos * self.block_dist

    def we_have_ball(self):
        has_ball = False
        for bot in main.our_robots():
            has_ball = has_ball or robot_has_ball(bot)
        return has_ball

    # Wether any robot can collect the ball before the opponent
    def within_range(self):
        shortest_opp_dist = 10
        shortest_our_dist = 10
        # TODO: Do some sort of prediction as the ball moves
        target_pos = main.ball().pos

        # Find closest opponent robot
        for bot in main.their_robots():
            dist = self.estimate_path_length(bot.pos, target_pos,
                                             main.our_robots())
            if (dist < shortest_opp_dist):
                shortest_opp_dist = dist

        # Find closest robot on our team
        for bot in main.our_robots():
            dist = self.estimate_path_length(bot.pos, target_pos,
                                             main.their_robots())
            if (dist < shortest_our_dist):
                shortest_our_dist = dist

        # Greater than 1 when we are further away
        return shortest_our_dist / shortest_opp_dist < 1.05

    # Estimates the length of a path given a robot
    def estimate_path_length(self, start, end, blocking_robots):
        total = 0
        next_pt = start
        line = robocup.Segment(start, end)
        iterations = 0
        max_iterations = 10

        # While there is a robot in the way
        blocking_robot = self.find_intersecting_robot(line, blocking_robots)
        while (blocking_robot is not None) and (iterations < max_iterations):
            # Find next point
            # Next point is +-dodge_dist * perp_vect
            robot_vec = (blocking_robot.pos - next_pt)
            perp_vec = robot_vec.perp_cw().normalized()

            pt1 = perp_vec * self.dodge_dist + blocking_robot.pos - next_pt
            pt2 = perp_vec * -self.dodge_dist + blocking_robot.pos - next_pt

            # Find shortest path
            if (pt1.mag() < pt2.mag()):
                next_pt = pt1
            else:
                next_pt = pt2

            # Add dist to total
            total += (next_pt - start).mag()

            line = robocup.Segment(next_pt, end)
            blocking_robot = self.find_intersecting_robot(line,
                                                          blocking_robots)
            iterations += 1

        total += (end - next_pt).mag()

        return total

    def find_intersecting_robot(self, line, blocking_robots):
        for bot in blocking_robots:
            if (line.dist_to(bot.pos) < self.dodge_dist):
                return bot

        return None
<commit_msg>Fixed overflow bug<commit_after>import main
import robocup
import behavior
import constants
import enum
import math

import composite_behavior
import evaluation.ball
import evaluation.defensive_positioning
import skills.mark
import skills.capture

# Finds the positions to place each defender
class DefensiveForward(composite_behavior.CompositeBehavior):
    class State(enum.Enum):
        # Everyone is defending
        blocking = 1
        # Closest defender is collecting, all others are blocking
        collecting = 2

    def __init__(self):
        super().__init__(continuous=True)

        # Robot queue
        self.defenders = [None, None, None]
        # Single robot collecting
        self.collector = None

        self.zone_def_pos = robocup.Point(0, 0)
        self.mark_bots = [None, None]

        self.block_dist = 0.01
        self.block_angle_coeff = 0.5
        self.names = ['mark_main', 'mark_sub', 'mark_zone']
        # Distance offset for path estimation
        self.dodge_dist = .2

        self.time_since_call = 0
        # Estimates defensive positions every X ticks
        self.cache_amnt = 5

        for s in DefensiveForward.State:
            self.add_state(s, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
                            DefensiveForward.State.blocking, lambda: True,
                            'immediately')

        self.add_transition(DefensiveForward.State.blocking,
                            DefensiveForward.State.collecting,
                            lambda: self.within_range(), 'Collecting')

        self.add_transition(
            DefensiveForward.State.collecting, DefensiveForward.State.blocking,
            lambda: not self.within_range(), 'Back to blocking')

        self.add_transition(
            DefensiveForward.State.collecting,
            behavior.Behavior.State.completed,
            lambda: self.collector is not None and evaluation.ball.robot_has_ball(self.collector.robot),
            'Ball collected')

        # Create list of defenders and start the marking
    def on_enter_blocking(self):
        self.zone_def_pos, self.mark_bots[0], self.mark_bots[
            1] = evaluation.defensive_positioning.find_defense_positions()

        for i in range(0, 2):
            self.defenders[i] = skills.mark.Mark()
            self.add_subbehavior(self.defenders[i],
                                 self.names[i],
                                 required=True)
            self.defenders[i].mark_robot = self.mark_bots[i]
            # TODO: Shift to mark point when "get_block_pos" is fixed

        self.defenders[2] = skills.mark.Mark()
        self.add_subbehavior(self.defenders[2], self.names[2], required=True)
        self.defenders[2].mark_point = self.zone_def_pos

        # Reset time since call
        self.time_since_call = 0

    # Continue updating the mark positions
    def execute_blocking(self):
        self.time_since_call = (self.time_since_call + 1) % self.cache_amnt

        # You don't want to test if its 0 because it would execute instantly
        if (self.time_since_call % self.cache_amnt == self.cache_amnt - 1):
            self.zone_def_pos, self.mark_bots[0], self.mark_bots[1] = \
                evaluation.defensive_positioning.find_defense_positions(
                    [self.defenders[0].robot, self.defenders[1].robot])

            for i in range(0, 2):
                if (self.defenders[i].mark_robot not in self.mark_bots):
                    self.defenders[i].mark_robot = self.mark_bots[i]
                # TODO: Shift to mark point when "get_block_pos" is fixed

            self.defenders[2].mark_point = self.zone_def_pos

    def on_exit_blocking(self):
        self.remove_all_subbehaviors()

    # Create collector method and then
    def on_enter_collecting(self):
        self.zone_def_pos, self.mark_bots[0], self.mark_bots[1] = \
            evaluation.defensive_positioning.find_defense_positions()

        # Take closest robot to collect
        # Leave other two to move into primary blocking positions
        self.collector = skills.capture.Capture()
        self.add_subbehavior(self.collector, 'collector', required=True)

        for i in range(0, 2):
            self.defenders[i] = skills.mark.Mark()
            self.add_subbehavior(self.defenders[i],
                                 self.names[i],
                                 required=True)
            self.defenders[i].mark_robot = self.mark_bots[i]

    def on_exit_collecting(self):
        self.remove_all_subbehaviors()
        self.collector = None

    # Uses their predicted kick direction to block
    def get_block_pos(self, bot):
        # Get predicted angle of shot
        # Get goal to bot
        # Place point x dist away
        predicted = evaluation.defensive_positioning.predict_kick_direction(
            bot)
        actual = bot.angle

        angle = self.block_angle_coeff * predicted + (
            1 - self.block_angle_coeff) * actual

        x = math.cos(angle)
        y = math.sin(angle)
        pos = robocup.Point(x, y).normalized() + bot.pos

        return None  # pos * self.block_dist

    def we_have_ball(self):
        has_ball = False
        for bot in main.our_robots():
            has_ball = has_ball or robot_has_ball(bot)
        return has_ball

    # Wether any robot can collect the ball before the opponent
    def within_range(self):
        shortest_opp_dist = 10
        shortest_our_dist = 10
        # TODO: Do some sort of prediction as the ball moves
        target_pos = main.ball().pos

        # Find closest opponent robot
        for bot in main.their_robots():
            dist = self.estimate_path_length(bot.pos, target_pos,
                                             main.our_robots())
            if (dist < shortest_opp_dist):
                shortest_opp_dist = dist

        # Find closest robot on our team
        for bot in main.our_robots():
            dist = self.estimate_path_length(bot.pos, target_pos,
                                             main.their_robots())
            if (dist < shortest_our_dist):
                shortest_our_dist = dist

        # Greater than 1 when we are further away
        return shortest_our_dist / shortest_opp_dist < 1.05

    # Estimates the length of a path given a robot
    def estimate_path_length(self, start, end, blocking_robots):
        total = 0
        next_pt = start
        line = robocup.Segment(start, end)
        iterations = 0
        max_iterations = 10

        # While there is a robot in the way
        blocking_robot = self.find_intersecting_robot(line, blocking_robots)
        while (blocking_robot is not None) and (iterations < max_iterations):
            # Find next point
            # Next point is +-dodge_dist * perp_vect
            robot_vec = (blocking_robot.pos - next_pt)
            perp_vec = robot_vec.perp_cw().normalized()

            pt1 = perp_vec * self.dodge_dist + blocking_robot.pos - next_pt
            pt2 = perp_vec * -self.dodge_dist + blocking_robot.pos - next_pt

            # Find shortest path
            if (pt1.mag() < pt2.mag()):
                next_pt = pt1
            else:
                next_pt = pt2

            # Add dist to total
            total += (next_pt - start).mag()

            line = robocup.Segment(next_pt, end)
            blocking_robot = self.find_intersecting_robot(line,
                                                          blocking_robots)
            iterations += 1

        total += (end - next_pt).mag()

        return total

    def find_intersecting_robot(self, line, blocking_robots):
        for bot in blocking_robots:
            if (line.dist_to(bot.pos) < self.dodge_dist):
                return bot

        return None
<|endoftext|>


<commit_before>from decimal import Decimal
import unittest
import datetime

import rdflib  # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from rdflib.namespace import XSD

class TestLiteral(unittest.TestCase):
    def setUp(self):
        pass

    def test_repr_apostrophe(self):
        a = rdflib.Literal("'")
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_repr_quote(self):
        a = rdflib.Literal('"')
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_backslash(self):
        d = r"""
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:foo="http://example.org/foo#">
    <rdf:Description>
      <foo:bar>a\b</foo:bar>
    </rdf:Description>
</rdf:RDF>
"""
        g = rdflib.Graph()
        g.parse(data=d, format="xml")
        a = rdflib.Literal("a\\b")
        b = list(g.objects())[0]
        self.assertEqual(a, b)

    def test_literal_from_bool(self):
        l = rdflib.Literal(True)
        self.assertEqual(l.datatype, rdflib.XSD["boolean"])

class TestNew(unittest.TestCase):
    def testCantPassLangAndDatatype(self):
        self.assertRaises(
            TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
        )

    def testCantPassInvalidLang(self):
        self.assertRaises(
            ValueError, Literal, "foo", lang="999"
        )

    def testFromOtherLiteral(self):
        l = Literal(1)
        l2 = Literal(l)
        self.assertTrue(isinstance(l.value, int))
        self.assertTrue(isinstance(l2.value, int))

        # change datatype
        l = Literal("1")
        l2 = Literal(l, datatype=rdflib.XSD.integer)
        self.assertTrue(isinstance(l2.value, int))

    def testDatatypeGetsAutoURIRefConversion(self):
        # drewp disapproves of this behavior, but it should be
        # represented in the tests
        x = Literal("foo", datatype="http://example.com/")
        self.assertTrue(isinstance(x.datatype, URIRef))

        x = Literal("foo", datatype=Literal("pennies"))
        self.assertEqual(x.datatype, URIRef("pennies"))

class TestRepr(unittest.TestCase):
    def testOmitsMissingDatatypeAndLang(self):
        self.assertEqual(repr(Literal("foo")), "rdflib.term.Literal('foo')")

    def testOmitsMissingDatatype(self):
        self.assertEqual(
            repr(Literal("foo", lang="en")),
            "rdflib.term.Literal('foo', lang='en')",
        )

    def testOmitsMissingLang(self):
        self.assertEqual(
            repr(Literal("foo", datatype=URIRef("http://example.com/"))),
            "rdflib.term.Literal('foo', datatype=rdflib.term.URIRef('http://example.com/'))",
        )

    def testSubclassNameAppearsInRepr(self):
        class MyLiteral(Literal):
            pass

        x = MyLiteral("foo")
        self.assertEqual(repr(x), "MyLiteral('foo')")

class TestDoubleOutput(unittest.TestCase):
    def testNoDanglingPoint(self):
        """confirms the fix for https://github.com/RDFLib/rdflib/issues/237"""
        vv = Literal("0.88", datatype=_XSD_DOUBLE)
        out = vv._literal_n3(use_plain=True)
        self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
    """confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""

    def testTrueBoolean(self):
        test_value = Literal("tRue", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)
        test_value = Literal("1", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)

    def testFalseBoolean(self):
        test_value = Literal("falsE", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)
        test_value = Literal("0", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)

    def testNonFalseBoolean(self):
        test_value = Literal("abcd", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)
        test_value = Literal("10", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)

class TestBindings(unittest.TestCase):
    def testBinding(self):
        class a:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "<<<%s>>>" % self.v

        dtA = rdflib.URIRef("urn:dt:a")
        bind(dtA, a)

        va = a("<<<2>>>")
        la = Literal(va, normalize=True)
        self.assertEqual(la.value, va)
        self.assertEqual(la.datatype, dtA)

        la2 = Literal("<<<2>>>", datatype=dtA)
        self.assertTrue(isinstance(la2.value, a))
        self.assertEqual(la2.value.v, va.v)

        class b:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "B%s" % self.v

        dtB = rdflib.URIRef("urn:dt:b")
        bind(dtB, b, None, lambda x: "<<<%s>>>" % x)

        vb = b("<<<3>>>")
        lb = Literal(vb, normalize=True)
        self.assertEqual(lb.value, vb)
        self.assertEqual(lb.datatype, dtB)

    def testSpecificBinding(self):
        def lexify(s):
            return "--%s--" % s

        def unlexify(s):
            return s[2:-2]

        datatype = rdflib.URIRef("urn:dt:mystring")

        # Datatype-specific rule
        bind(datatype, str, unlexify, lexify, datatype_specific=True)

        s = "Hello"
        normal_l = Literal(s)
        self.assertEqual(str(normal_l), s)
        self.assertEqual(normal_l.toPython(), s)
        self.assertEqual(normal_l.datatype, None)

        specific_l = Literal("--%s--" % s, datatype=datatype)
        self.assertEqual(str(specific_l), lexify(s))
        self.assertEqual(specific_l.toPython(), s)
        self.assertEqual(specific_l.datatype, datatype)

class TestXsdLiterals(unittest.TestCase):
    def test_make_literals(self):
        """
        Tests literal construction.
        """
        inputs = [
            # these literals do not get converted to Python types
            ("ABCD", XSD.integer, None),
            ("ABCD", XSD.gYear, None),
            ("-10000", XSD.gYear, None),
            ("-1921-00", XSD.gYearMonth, None),
            ("1921-00", XSD.gMonthDay, None),
            ("1921-13", XSD.gMonthDay, None),
            ("-1921-00", XSD.gMonthDay, None),
            ("10", XSD.gDay, None),
            ("-1", XSD.gDay, None),
            ("0000", XSD.gYear, None),
            ("0000-00-00", XSD.date, None),
            ("NOT A VALID HEX STRING", XSD.hexBinary, None),
            ("NOT A VALID BASE64 STRING", XSD.base64Binary, None),
            # these literals get converted to python types
            ("1921-05-01", XSD.date, datetime.date),
            ("1921-05-01T00:00:00", XSD.dateTime, datetime.datetime),
            ("1921-05", XSD.gYearMonth, datetime.date),
            ("0001-01", XSD.gYearMonth, datetime.date),
            ("0001-12", XSD.gYearMonth, datetime.date),
            ("2002-01", XSD.gYearMonth, datetime.date),
            ("9999-01", XSD.gYearMonth, datetime.date),
            ("9999-12", XSD.gYearMonth, datetime.date),
            ("1921", XSD.gYear, datetime.date),
            ("2000", XSD.gYear, datetime.date),
            ("0001", XSD.gYear, datetime.date),
            ("9999", XSD.gYear, datetime.date),
            ("1982", XSD.gYear, datetime.date),
            ("2002", XSD.gYear, datetime.date),
            ("1921-05-01T00:00:00+00:30", XSD.dateTime, datetime.datetime),
            ("1921-05-01T00:00:00-00:30", XSD.dateTime, datetime.datetime),
            ("abcdef0123", XSD.hexBinary, bytes),
            ("", XSD.hexBinary, bytes),
            ("UkRGTGli", XSD.base64Binary, bytes),
            ("", XSD.base64Binary, bytes),
            ("0.0000000000000000000000000000001", XSD.decimal, Decimal),
            ("0.1", XSD.decimal, Decimal),
            ("1", XSD.integer, int),
        ]
        self.check_make_literals(inputs)

    @unittest.expectedFailure
    def test_make_literals_ki(self):
        """
        Known issues with literal construction.
        """
        inputs = [
            ("1921-01Z", XSD.gYearMonth, datetime.date),
            ("1921Z", XSD.gYear, datetime.date),
            ("1921-00", XSD.gYearMonth, datetime.date),
            ("1921-05-01Z", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01T00:00:00Z", XSD.dateTime, datetime.datetime),
            ("1e-31", XSD.decimal, None),  # This is not a valid decimal value
        ]
        self.check_make_literals(inputs)

    def check_make_literals(self, inputs):
        for literal_pair in inputs:
            (lexical, type, value_cls) = literal_pair
            with self.subTest(f"testing {literal_pair}"):
                literal = Literal(lexical, datatype=type)
                if value_cls is not None:
                    self.assertIsInstance(literal.value, value_cls)
                else:
                    self.assertIsNone(literal.value)
                self.assertEqual(lexical, f"{literal}")

if __name__ == "__main__":
    unittest.main()
<commit_msg>Added additional tests for Literal language tag.<commit_after>from decimal import Decimal
from typing import Any, Optional, Type
import unittest
import datetime

import rdflib  # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from rdflib.namespace import XSD

import pytest

class TestLiteral(unittest.TestCase):
    def setUp(self):
        pass

    def test_repr_apostrophe(self):
        a = rdflib.Literal("'")
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_repr_quote(self):
        a = rdflib.Literal('"')
        b = eval(repr(a))
        self.assertEqual(a, b)

    def test_backslash(self):
        d = r"""
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:foo="http://example.org/foo#">
    <rdf:Description>
      <foo:bar>a\b</foo:bar>
    </rdf:Description>
</rdf:RDF>
"""
        g = rdflib.Graph()
        g.parse(data=d, format="xml")
        a = rdflib.Literal("a\\b")
        b = list(g.objects())[0]
        self.assertEqual(a, b)

    def test_literal_from_bool(self):
        l = rdflib.Literal(True)
        self.assertEqual(l.datatype, rdflib.XSD["boolean"])

class TestNewPT:
    # NOTE: TestNewPT is written for pytest so that pytest features like
    # parametrize can be used.
    # New tests should be added here instead of in TestNew.
    @pytest.mark.parametrize(
        "lang, exception_type",
        [
            ({}, TypeError),
            ([], TypeError),
            (1, TypeError),
            (b"en", TypeError),
            ("999", ValueError),
            ("-", ValueError),
        ],
    )
    def test_cant_pass_invalid_lang(
        self,
        lang: Any,
        exception_type: Type[Exception],
    ):
        """
        Construction of Literal fails if the language tag is invalid.
        """
        with pytest.raises(exception_type):
            Literal("foo", lang=lang)

class TestNew(unittest.TestCase):
    # NOTE: Please use TestNewPT for new tests instead of this which is written
    # for unittest.
    def testCantPassLangAndDatatype(self):
        self.assertRaises(
            TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
        )

    def testFromOtherLiteral(self):
        l = Literal(1)
        l2 = Literal(l)
        self.assertTrue(isinstance(l.value, int))
        self.assertTrue(isinstance(l2.value, int))

        # change datatype
        l = Literal("1")
        l2 = Literal(l, datatype=rdflib.XSD.integer)
        self.assertTrue(isinstance(l2.value, int))

    def testDatatypeGetsAutoURIRefConversion(self):
        # drewp disapproves of this behavior, but it should be
        # represented in the tests
        x = Literal("foo", datatype="http://example.com/")
        self.assertTrue(isinstance(x.datatype, URIRef))

        x = Literal("foo", datatype=Literal("pennies"))
        self.assertEqual(x.datatype, URIRef("pennies"))

class TestRepr(unittest.TestCase):
    def testOmitsMissingDatatypeAndLang(self):
        self.assertEqual(repr(Literal("foo")), "rdflib.term.Literal('foo')")

    def testOmitsMissingDatatype(self):
        self.assertEqual(
            repr(Literal("foo", lang="en")),
            "rdflib.term.Literal('foo', lang='en')",
        )

    def testOmitsMissingLang(self):
        self.assertEqual(
            repr(Literal("foo", datatype=URIRef("http://example.com/"))),
            "rdflib.term.Literal('foo', datatype=rdflib.term.URIRef('http://example.com/'))",
        )

    def testSubclassNameAppearsInRepr(self):
        class MyLiteral(Literal):
            pass

        x = MyLiteral("foo")
        self.assertEqual(repr(x), "MyLiteral('foo')")

class TestDoubleOutput(unittest.TestCase):
    def testNoDanglingPoint(self):
        """confirms the fix for https://github.com/RDFLib/rdflib/issues/237"""
        vv = Literal("0.88", datatype=_XSD_DOUBLE)
        out = vv._literal_n3(use_plain=True)
        self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
    """confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""

    def testTrueBoolean(self):
        test_value = Literal("tRue", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)
        test_value = Literal("1", datatype=_XSD_BOOLEAN)
        self.assertTrue(test_value.value)

    def testFalseBoolean(self):
        test_value = Literal("falsE", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)
        test_value = Literal("0", datatype=_XSD_BOOLEAN)
        self.assertFalse(test_value.value)

    def testNonFalseBoolean(self):
        test_value = Literal("abcd", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)
        test_value = Literal("10", datatype=_XSD_BOOLEAN)
        self.assertRaises(UserWarning)
        self.assertFalse(test_value.value)

class TestBindings(unittest.TestCase):
    def testBinding(self):
        class a:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "<<<%s>>>" % self.v

        dtA = rdflib.URIRef("urn:dt:a")
        bind(dtA, a)

        va = a("<<<2>>>")
        la = Literal(va, normalize=True)
        self.assertEqual(la.value, va)
        self.assertEqual(la.datatype, dtA)

        la2 = Literal("<<<2>>>", datatype=dtA)
        self.assertTrue(isinstance(la2.value, a))
        self.assertEqual(la2.value.v, va.v)

        class b:
            def __init__(self, v):
                self.v = v[3:-3]

            def __str__(self):
                return "B%s" % self.v

        dtB = rdflib.URIRef("urn:dt:b")
        bind(dtB, b, None, lambda x: "<<<%s>>>" % x)

        vb = b("<<<3>>>")
        lb = Literal(vb, normalize=True)
        self.assertEqual(lb.value, vb)
        self.assertEqual(lb.datatype, dtB)

    def testSpecificBinding(self):
        def lexify(s):
            return "--%s--" % s

        def unlexify(s):
            return s[2:-2]

        datatype = rdflib.URIRef("urn:dt:mystring")

        # Datatype-specific rule
        bind(datatype, str, unlexify, lexify, datatype_specific=True)

        s = "Hello"
        normal_l = Literal(s)
        self.assertEqual(str(normal_l), s)
        self.assertEqual(normal_l.toPython(), s)
        self.assertEqual(normal_l.datatype, None)

        specific_l = Literal("--%s--" % s, datatype=datatype)
        self.assertEqual(str(specific_l), lexify(s))
        self.assertEqual(specific_l.toPython(), s)
        self.assertEqual(specific_l.datatype, datatype)

class TestXsdLiterals(unittest.TestCase):
    def test_make_literals(self):
        """
        Tests literal construction.
        """
        inputs = [
            # these literals do not get converted to Python types
            ("ABCD", XSD.integer, None),
            ("ABCD", XSD.gYear, None),
            ("-10000", XSD.gYear, None),
            ("-1921-00", XSD.gYearMonth, None),
            ("1921-00", XSD.gMonthDay, None),
            ("1921-13", XSD.gMonthDay, None),
            ("-1921-00", XSD.gMonthDay, None),
            ("10", XSD.gDay, None),
            ("-1", XSD.gDay, None),
            ("0000", XSD.gYear, None),
            ("0000-00-00", XSD.date, None),
            ("NOT A VALID HEX STRING", XSD.hexBinary, None),
            ("NOT A VALID BASE64 STRING", XSD.base64Binary, None),
            # these literals get converted to python types
            ("1921-05-01", XSD.date, datetime.date),
            ("1921-05-01T00:00:00", XSD.dateTime, datetime.datetime),
            ("1921-05", XSD.gYearMonth, datetime.date),
            ("0001-01", XSD.gYearMonth, datetime.date),
            ("0001-12", XSD.gYearMonth, datetime.date),
            ("2002-01", XSD.gYearMonth, datetime.date),
            ("9999-01", XSD.gYearMonth, datetime.date),
            ("9999-12", XSD.gYearMonth, datetime.date),
            ("1921", XSD.gYear, datetime.date),
            ("2000", XSD.gYear, datetime.date),
            ("0001", XSD.gYear, datetime.date),
            ("9999", XSD.gYear, datetime.date),
            ("1982", XSD.gYear, datetime.date),
            ("2002", XSD.gYear, datetime.date),
            ("1921-05-01T00:00:00+00:30", XSD.dateTime, datetime.datetime),
            ("1921-05-01T00:00:00-00:30", XSD.dateTime, datetime.datetime),
            ("abcdef0123", XSD.hexBinary, bytes),
            ("", XSD.hexBinary, bytes),
            ("UkRGTGli", XSD.base64Binary, bytes),
            ("", XSD.base64Binary, bytes),
            ("0.0000000000000000000000000000001", XSD.decimal, Decimal),
            ("0.1", XSD.decimal, Decimal),
            ("1", XSD.integer, int),
        ]
        self.check_make_literals(inputs)

    @unittest.expectedFailure
    def test_make_literals_ki(self):
        """
        Known issues with literal construction.
        """
        inputs = [
            ("1921-01Z", XSD.gYearMonth, datetime.date),
            ("1921Z", XSD.gYear, datetime.date),
            ("1921-00", XSD.gYearMonth, datetime.date),
            ("1921-05-01Z", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:30", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01+00:00", XSD.date, datetime.date),
            ("1921-05-01T00:00:00Z", XSD.dateTime, datetime.datetime),
            ("1e-31", XSD.decimal, None),  # This is not a valid decimal value
        ]
        self.check_make_literals(inputs)

    def check_make_literals(self, inputs):
        for literal_pair in inputs:
            (lexical, type, value_cls) = literal_pair
            with self.subTest(f"testing {literal_pair}"):
                literal = Literal(lexical, datatype=type)
                if value_cls is not None:
                    self.assertIsInstance(literal.value, value_cls)
                else:
                    self.assertIsNone(literal.value)
                self.assertEqual(lexical, f"{literal}")

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>from typing import Generator

import pytest

from pypeerassets import (
    Cryptoid,
    Deck,
    Explorer,
    RpcNode,
    find_deck,
)
from pypeerassets.exceptions import *
from pypeerassets.paproto_pb2 import DeckSpawn
from pypeerassets.pautils import *
from pypeerassets.protocol import IssueMode, CardTransfer
from pypeerassets.pa_constants import param_query

@pytest.mark.xfail
def test_load_p2th_privkeys_into_local_node():

    provider = RpcNode(testnet=True)
    load_p2th_privkeys_into_local_node(provider=provider)

@pytest.mark.parametrize("prov", ["explorer", "cryptoid"])
def test_find_tx_sender(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin")
        rawtx = provider.getrawtransaction("397bda2f5e6608c872a663b2e5482d95db8ecfad00757823f0f12caa45a213a6", 1)
        assert find_tx_sender(provider, rawtx) == 'PNHGzKupyvo2YZVb1CTdRxtCGBB5ykgiug'

    if prov == "cryptoid":
        provider = Cryptoid(network="peercoin")
        rawtx = provider.getrawtransaction("397bda2f5e6608c872a663b2e5482d95db8ecfad00757823f0f12caa45a213a6", 1)
        assert find_tx_sender(provider, rawtx) == 'PNHGzKupyvo2YZVb1CTdRxtCGBB5ykgiug'

@pytest.mark.parametrize("prov", ["explorer", "cryptoid"])
def test_find_deck_spawns(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin")

    if prov == "cryptoid":
        provider = Cryptoid(network="peercoin")

    assert isinstance(find_deck_spawns(provider), Generator)

@pytest.mark.parametrize("prov", ["rpc", "explorer"])
def test_tx_serialization_order(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin-testnet")
        assert tx_serialization_order(provider,
                                      txid="f968702bcedc107959aae2c2b1a1becdccbfe7e5a32b460b2c13c1adaa33d541", blockhash="e234d2ef69f7cd1e7ee489546b39314cc838763b4e32438106cba657d9749f2f") == 1

    try:
        if prov == "rpc":
            provider = RpcNode(testnet=True)
            assert tx_serialization_order(provider,
                                          txid="f968702bcedc107959aae2c2b1a1becdccbfe7e5a32b460b2c13c1adaa33d541", blockhash="e234d2ef69f7cd1e7ee489546b39314cc838763b4e32438106cba657d9749f2f") == 1

    except:
        print("No RpcNode avaliable.")

def test_read_tx_opreturn():

    rawtx = {'blockhash': 'aef46dc82bcc9b40ff0c05e2498c4f85db7e273fc6f7e656acbcd1c8e0c93356',
             'blocktime': 1489426035,
             'confirmations': 23696,
             'hex': '010000004ad4c6580107c7390516448a1b03e7fc2c55e2e816aa0e0745e4e5089393ee1d0fbf0ad0fb020000006c49304602210099d36b8c36f29e2d7c423ebb68ef804744c3681e81ea2db7f02ea930c4aaa984022100f2d7e044e1b4034a5edb65f38fd4451ed11fe43a240eaacb58cb951de842bc190121029becbd50edbb8fee8fc1227112e6c8a6b6ead69c3660b560e5eab22c2fe8f976ffffffff0310270000000000001976a9141e667ee94ea8e62c63fe59a0269bb3c091c86ca388ac0000000000000000196a170801120f736978746f5f726f6472696775657a18052004601f0d00000000001976a91456a0a01afba6687e98e5e61b434d45a337f2cd2588ac00000000',
             'locktime': 0,
             'time': 1489425482,
             'txid': '643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3',
             'version': 1,
             'vin': [{'scriptSig': {'asm': '304602210099d36b8c36f29e2d7c423ebb68ef804744c3681e81ea2db7f02ea930c4aaa984022100f2d7e044e1b4034a5edb65f38fd4451ed11fe43a240eaacb58cb951de842bc1901 029becbd50edbb8fee8fc1227112e6c8a6b6ead69c3660b560e5eab22c2fe8f976',
                      'hex': '49304602210099d36b8c36f29e2d7c423ebb68ef804744c3681e81ea2db7f02ea930c4aaa984022100f2d7e044e1b4034a5edb65f38fd4451ed11fe43a240eaacb58cb951de842bc190121029becbd50edbb8fee8fc1227112e6c8a6b6ead69c3660b560e5eab22c2fe8f976'},
             'sequence': 4294967295,
             'txid': 'fbd00abf0f1dee939308e5e445070eaa16e8e2552cfce7031b8a44160539c707',
             'vout': 2}],
             'vout': [{'n': 0,
             'scriptPubKey': {'addresses': ['miHhMLaMWubq4Wx6SdTEqZcUHEGp8RKMZt'],
                'asm': 'OP_DUP OP_HASH160 1e667ee94ea8e62c63fe59a0269bb3c091c86ca3 OP_EQUALVERIFY OP_CHECKSIG',
                'hex': '76a9141e667ee94ea8e62c63fe59a0269bb3c091c86ca388ac',
                'reqSigs': 1,
                'type': 'pubkeyhash'},
             'value': 0.01},
             {'n': 1,
             'scriptPubKey': {'asm': 'OP_RETURN 0801120f736978746f5f726f6472696775657a18052004',
                'hex': '6a170801120f736978746f5f726f6472696775657a18052004',
                'type': 'nulldata'},
             'value': 0},
             {'n': 2,
             'scriptPubKey': {'addresses': ['moQzpzzcCYZMnAz224EY4att5A9psxN8X2'],
                'asm': 'OP_DUP OP_HASH160 56a0a01afba6687e98e5e61b434d45a337f2cd25 OP_EQUALVERIFY OP_CHECKSIG',
                'hex': '76a91456a0a01afba6687e98e5e61b434d45a337f2cd2588ac',
                'reqSigs': 1,
                'type': 'pubkeyhash'},
             'value': 0.86}]}

    assert isinstance(read_tx_opreturn(rawtx), bytes)
    assert read_tx_opreturn(rawtx) == b'\x08\x01\x12\x0fsixto_rodriguez\x18\x05 \x04'

def generate_dummy_deck():

    return Deck(
        name="decky",
        number_of_decimals=2,
        issue_mode=IssueMode.SINGLET.value,
        network="ppc",
        production=True,
        version=1,
        asset_specific_data="just testing.",
    )

def test_deck_issue_mode():
    '''test enum to issue_mode conversion'''

    deck_meta = DeckSpawn()
    deck_meta.issue_mode = 3

    assert isinstance(deck_issue_mode(deck_meta), Generator)
    assert list(deck_issue_mode(deck_meta)) == ['CUSTOM', 'ONCE']

    # Check that we handle NONE mode correctly.
    deck_meta.issue_mode = 0
    assert list(deck_issue_mode(deck_meta)) == ['NONE']

def test_issue_mode_to_enum():
    '''test issue mode to enum conversion'''

    deck = generate_dummy_deck().metainfo_to_protobuf
    deck_meta = DeckSpawn()
    deck_meta.ParseFromString(deck)

    assert isinstance(issue_mode_to_enum(deck_meta,
                      ["CUSTOM", "SINGLET"]), int)

def test_parse_deckspawn_metainfo():
    '''tests if loading of deck parameteres from protobuf works as it should.'''

    string = b'\x08\x01\x12\x0cmy_test_deck\x18\x03 \x02'
    assert parse_deckspawn_metainfo(string, 1) == {'issue_mode': IssueMode.ONCE.value,
                                                   'name': 'my_test_deck',
                                                   'number_of_decimals': 3,
                                                   'version': 1,
                                                   'asset_specific_data': b''
                                                  }

    string = b'\x08\x01\x18\x05 \x04' # without deck name
    with pytest.raises(InvalidDeckMetainfo):
        parse_deckspawn_metainfo(string, 1)

def test_validate_deckspawn_p2th():
    '''test deckspawn p2th validation'''

    provider = Explorer(network="peercoin-testnet")
    p2th = param_query('peercoin-testnet').P2TH_addr
    raw_tx = provider.getrawtransaction('643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3', 1,)

    assert validate_deckspawn_p2th(provider, raw_tx, p2th)

@pytest.mark.xfail
def test_load_deck_p2th_into_local_node():

    provider = RpcNode(testnet=True)
    deck = generate_dummy_deck()
    load_deck_p2th_into_local_node(provider, deck)

def test_validate_card_transfer_p2th():

    provider = Cryptoid(network="peercoin-testnet")
    deck = find_deck(provider, "643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3", 1)
    raw_tx = provider.getrawtransaction("809c506bc3add9e46a4d3a65348426688545213da5fb5b524acd380f2cdaf3cc", 1)

    validate_card_transfer_p2th(deck, raw_tx)

def test_parse_card_transfer_metainfo():

    card = b'\x08\x01\x12\n\xd0\xd2=\x80\x89z\xee\x83\xb8\x01\x18\x05'
    res = parse_card_transfer_metainfo(card, 1)

    assert isinstance(res, dict)

def test_postprocess_card():

    provider = Explorer(network="peercoin-testnet")
    deck = find_deck(provider, "643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3", 1)
    raw_tx = provider.getrawtransaction('809c506bc3add9e46a4d3a65348426688545213da5fb5b524acd380f2cdaf3cc', 1)
    vout = raw_tx["vout"]
    blockseq = tx_serialization_order(provider, raw_tx["blockhash"], raw_tx["txid"])
    blocknum = provider.getblock(raw_tx["blockhash"])["height"]
    sender = 'moQzpzzcCYZMnAz224EY4att5A9psxN8X2'
    card_transfer = CardTransfer(
                    deck=deck,
                    receiver=["n4KuTR5CzyQTbrpwbAKEdTfJERKmtHWWgr"],
                    amount=[1],
                    version=1,
                    )

    card = postprocess_card(card_metainfo=card_transfer.metainfo_to_dict,
                            raw_tx=raw_tx,
                            sender=sender,
                            vout=vout,
                            blockseq=blockseq,
                            blocknum=blocknum,
                            tx_confirmations=raw_tx['confirmations'],
                            deck=deck)

    assert isinstance(card, list)

def test_amount_to_exponent():

    assert isinstance(amount_to_exponent(88.99, 3), int)
    assert amount_to_exponent(88.99, 3) == 88990

def test_exponent_to_amount():

    assert isinstance(exponent_to_amount(10, 6), float)
    assert exponent_to_amount(10, 3) == 0.01
<commit_msg>fix test_read_tx_opreturn<commit_after>from typing import Generator

import pytest

from pypeerassets import (
    Cryptoid,
    Deck,
    Explorer,
    RpcNode,
    find_deck,
)
from pypeerassets.exceptions import *
from pypeerassets.paproto_pb2 import DeckSpawn
from pypeerassets.pautils import *
from pypeerassets.protocol import IssueMode, CardTransfer
from pypeerassets.pa_constants import param_query

@pytest.mark.xfail
def test_load_p2th_privkeys_into_local_node():

    provider = RpcNode(testnet=True)
    load_p2th_privkeys_into_local_node(provider=provider)

@pytest.mark.parametrize("prov", ["explorer", "cryptoid"])
def test_find_tx_sender(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin")
        rawtx = provider.getrawtransaction("397bda2f5e6608c872a663b2e5482d95db8ecfad00757823f0f12caa45a213a6", 1)
        assert find_tx_sender(provider, rawtx) == 'PNHGzKupyvo2YZVb1CTdRxtCGBB5ykgiug'

    if prov == "cryptoid":
        provider = Cryptoid(network="peercoin")
        rawtx = provider.getrawtransaction("397bda2f5e6608c872a663b2e5482d95db8ecfad00757823f0f12caa45a213a6", 1)
        assert find_tx_sender(provider, rawtx) == 'PNHGzKupyvo2YZVb1CTdRxtCGBB5ykgiug'

@pytest.mark.parametrize("prov", ["explorer", "cryptoid"])
def test_find_deck_spawns(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin")

    if prov == "cryptoid":
        provider = Cryptoid(network="peercoin")

    assert isinstance(find_deck_spawns(provider), Generator)

@pytest.mark.parametrize("prov", ["rpc", "explorer"])
def test_tx_serialization_order(prov):

    if prov == "explorer":
        provider = Explorer(network="peercoin-testnet")
        assert tx_serialization_order(provider,
                                      txid="f968702bcedc107959aae2c2b1a1becdccbfe7e5a32b460b2c13c1adaa33d541", blockhash="e234d2ef69f7cd1e7ee489546b39314cc838763b4e32438106cba657d9749f2f") == 1

    try:
        if prov == "rpc":
            provider = RpcNode(testnet=True)
            assert tx_serialization_order(provider,
                                          txid="f968702bcedc107959aae2c2b1a1becdccbfe7e5a32b460b2c13c1adaa33d541", blockhash="e234d2ef69f7cd1e7ee489546b39314cc838763b4e32438106cba657d9749f2f") == 1

    except:
        print("No RpcNode avaliable.")

def test_read_tx_opreturn():

    vout = [{'n': 0,
             'scriptPubKey': {'addresses': ['miHhMLaMWubq4Wx6SdTEqZcUHEGp8RKMZt'],
                'asm': 'OP_DUP OP_HASH160 1e667ee94ea8e62c63fe59a0269bb3c091c86ca3 OP_EQUALVERIFY OP_CHECKSIG',
                'hex': '76a9141e667ee94ea8e62c63fe59a0269bb3c091c86ca388ac',
                'reqSigs': 1,
                'type': 'pubkeyhash'},
             'value': 0.01},
             {'n': 1,
             'scriptPubKey': {'asm': 'OP_RETURN 0801120f736978746f5f726f6472696775657a18052004',
                'hex': '6a170801120f736978746f5f726f6472696775657a18052004',
                'type': 'nulldata'},
              'value': 0}
             ]

    assert isinstance(read_tx_opreturn(vout[1]), bytes)
    assert read_tx_opreturn(vout[1]) == b'\x08\x01\x12\x0fsixto_rodriguez\x18\x05 \x04'

def generate_dummy_deck():

    return Deck(
        name="decky",
        number_of_decimals=2,
        issue_mode=IssueMode.SINGLET.value,
        network="ppc",
        production=True,
        version=1,
        asset_specific_data="just testing.",
    )

def test_deck_issue_mode():
    '''test enum to issue_mode conversion'''

    deck_meta = DeckSpawn()
    deck_meta.issue_mode = 3

    assert isinstance(deck_issue_mode(deck_meta), Generator)
    assert list(deck_issue_mode(deck_meta)) == ['CUSTOM', 'ONCE']

    # Check that we handle NONE mode correctly.
    deck_meta.issue_mode = 0
    assert list(deck_issue_mode(deck_meta)) == ['NONE']

def test_issue_mode_to_enum():
    '''test issue mode to enum conversion'''

    deck = generate_dummy_deck().metainfo_to_protobuf
    deck_meta = DeckSpawn()
    deck_meta.ParseFromString(deck)

    assert isinstance(issue_mode_to_enum(deck_meta,
                      ["CUSTOM", "SINGLET"]), int)

def test_parse_deckspawn_metainfo():
    '''tests if loading of deck parameteres from protobuf works as it should.'''

    string = b'\x08\x01\x12\x0cmy_test_deck\x18\x03 \x02'
    assert parse_deckspawn_metainfo(string, 1) == {'issue_mode': IssueMode.ONCE.value,
                                                   'name': 'my_test_deck',
                                                   'number_of_decimals': 3,
                                                   'version': 1,
                                                   'asset_specific_data': b''
                                                  }

    string = b'\x08\x01\x18\x05 \x04' # without deck name
    with pytest.raises(InvalidDeckMetainfo):
        parse_deckspawn_metainfo(string, 1)

def test_validate_deckspawn_p2th():
    '''test deckspawn p2th validation'''

    provider = Explorer(network="peercoin-testnet")
    p2th = param_query('peercoin-testnet').P2TH_addr
    raw_tx = provider.getrawtransaction('643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3', 1,)

    assert validate_deckspawn_p2th(provider, raw_tx, p2th)

@pytest.mark.xfail
def test_load_deck_p2th_into_local_node():

    provider = RpcNode(testnet=True)
    deck = generate_dummy_deck()
    load_deck_p2th_into_local_node(provider, deck)

def test_validate_card_transfer_p2th():

    provider = Cryptoid(network="peercoin-testnet")
    deck = find_deck(provider, "643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3", 1)
    raw_tx = provider.getrawtransaction("809c506bc3add9e46a4d3a65348426688545213da5fb5b524acd380f2cdaf3cc", 1)

    validate_card_transfer_p2th(deck, raw_tx)

def test_parse_card_transfer_metainfo():

    card = b'\x08\x01\x12\n\xd0\xd2=\x80\x89z\xee\x83\xb8\x01\x18\x05'
    res = parse_card_transfer_metainfo(card, 1)

    assert isinstance(res, dict)

def test_postprocess_card():

    provider = Explorer(network="peercoin-testnet")
    deck = find_deck(provider, "643dccd585211766fc03f71e92fbf299cfc2bdbf3f2cae0ad85adec3141069f3", 1)
    raw_tx = provider.getrawtransaction('809c506bc3add9e46a4d3a65348426688545213da5fb5b524acd380f2cdaf3cc', 1)
    vout = raw_tx["vout"]
    blockseq = tx_serialization_order(provider, raw_tx["blockhash"], raw_tx["txid"])
    blocknum = provider.getblock(raw_tx["blockhash"])["height"]
    sender = 'moQzpzzcCYZMnAz224EY4att5A9psxN8X2'
    card_transfer = CardTransfer(
                    deck=deck,
                    receiver=["n4KuTR5CzyQTbrpwbAKEdTfJERKmtHWWgr"],
                    amount=[1],
                    version=1,
                    )

    card = postprocess_card(card_metainfo=card_transfer.metainfo_to_dict,
                            raw_tx=raw_tx,
                            sender=sender,
                            vout=vout,
                            blockseq=blockseq,
                            blocknum=blocknum,
                            tx_confirmations=raw_tx['confirmations'],
                            deck=deck)

    assert isinstance(card, list)

def test_amount_to_exponent():

    assert isinstance(amount_to_exponent(88.99, 3), int)
    assert amount_to_exponent(88.99, 3) == 88990

def test_exponent_to_amount():

    assert isinstance(exponent_to_amount(10, 6), float)
    assert exponent_to_amount(10, 3) == 0.01
<|endoftext|>


<commit_before>import io
import json
import struct
import unittest

try:
    from mock import patch, Mock
except ImportError:
    from unittest.mock import patch, Mock
from poyonga import Groonga, GroongaResult
from poyonga.client import get_send_data_for_gqtp, convert_gqtp_result_data, GQTP_HEADER_SIZE
from poyonga.const import GRN_STATUS_UNSUPPORTED_COMMAND_VERSION

try:
    import pyarrow as pa
except ImportError:
    pa = None

class PoyongaHTTPTestCase(unittest.TestCase):
    def setUp(self):
        self.g = Groonga()

    @patch("poyonga.client.urlopen")
    def test_json_result_with_http(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], {}]"]
        mock_urlopen.return_value = m
        ret = self.g.call("status")
        self.assertEqual(type(ret), GroongaResult)
        self.assertEqual(ret.status, 0)

    @patch("poyonga.client.urlopen")
    def test_load_list(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        ret = self.g.call("load",
                          table="Site",
                          values=[{"_key": "groonga.org"}])
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @patch("poyonga.client.urlopen")
    def test_load_json(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        ret = self.g.call("load",
                          table="Site",
                          values=json.dumps([{"_key": "groonga.org"}]))
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @patch("poyonga.client.urlopen")
    def test_load_io(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        json_values = json.dumps([{"_key": "groonga.org"}])
        ret = self.g.call("load",
                          table="Site",
                          values=io.BytesIO(json_values.encode()))
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data.read()))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @unittest.skipUnless(pa, "require pyarrow")
    @patch("poyonga.client.urlopen")
    def test_select_apache_arrow(self, mock_urlopen):
        m = Mock()
        metadata_fields = [
            pa.field("return_code", pa.int32()),
            pa.field("start_time", pa.timestamp("ns")),
            pa.field("elapsed_time", pa.float64()),
        ]
        metadata_metadata = {
            "GROONGA:data_type": "metadata",
        }
        metadata_schema = pa.schema(metadata_fields, metadata_metadata)
        sec_to_ns = 1_000_000_000
        metadata = [
            [0],
            [int(1337566253.89858 * sec_to_ns)],
            [0.000354],
        ]
        metadata_record_batch = pa.record_batch(metadata,
                                                schema=metadata_schema)
        result_set_fields = [
            pa.field("name", pa.string()),
        ]
        result_set_metadata = {
            "GROONGA:n_hits": "29"
        }
        result_set_schema = pa.schema(result_set_fields, result_set_metadata)
        result_set = [
            ["Groonga", "poyonga"],
        ]
        result_set_record_batch = pa.record_batch(result_set,
                                                  schema=result_set_schema)
        output = pa.BufferOutputStream()
        with pa.RecordBatchStreamWriter(output, metadata_schema) as writer:
            writer.write(metadata_record_batch)
        with pa.RecordBatchStreamWriter(output, result_set_schema) as writer:
            writer.write(result_set_record_batch)
        m.read.side_effect = [output.getvalue().to_pybytes()]
        m.headers = {
            "content-type": "application/x-apache-arrow-streaming",
        }
        mock_urlopen.return_value = m
        ret = self.g.call("select",
                          command_version="3",
                          table="Site",
                          output_type="apache-arrow")
        self.assertEqual(ret.status, 0)
        self.assertEqual(ret.start_time, 1337566253.89858)
        self.assertEqual(ret.elapsed, 0.000354)
        self.assertEqual(ret.hit_num, 29)
        self.assertEqual(ret.items,
                         pa.Table.from_batches([result_set_record_batch]))

class PoyongaGQTPTestCase(unittest.TestCase):
    def setUp(self):
        self.g = Groonga(protocol="gqtp")

    @patch("poyonga.client.socket.socket")
    def test_json_result_with_gqtp(self, mock_socket):
        m = Mock()
        _data = b"{}"
        _proto, _qtype, _keylen, _level, _flags, _status, _size, _opaque, _cas = (
            0xC7,
            0x02,
            0,
            0,
            0,
            0,
            2,
            0,
            0,
        )
        packdata = struct.pack(
            "!BBHBBHIIQ2s",
            _proto,
            _qtype,
            _keylen,
            _level,
            _flags,
            _status,
            _size,
            _opaque,
            _cas,
            _data,
        )
        m.recv.return_value = packdata
        mock_socket.return_value = m
        ret = self.g.call("status")
        self.assertEqual(type(ret), GroongaResult)
        self.assertEqual(ret.status, 0)

class PoyongaFunctions(unittest.TestCase):
    def test_get_send_data(self):
        d = get_send_data_for_gqtp("status")
        self.assertEqual(30, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(6, size)

    def test_get_send_data_with_args(self):
        kwargs = {"table": "Site"}
        d = get_send_data_for_gqtp("select", **kwargs)
        self.assertEqual(45, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        # check body length
        self.assertEqual(21, d[11])

        body = d[GQTP_HEADER_SIZE:]
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(len(body), size)

    def test_get_send_data_with_args_and_two_bytes(self):
        kwargs = {"table": "Sitは"}
        d = get_send_data_for_gqtp("select", **kwargs)
        self.assertEqual(47, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        # check body length
        self.assertEqual(23, d[11])

        body = d[GQTP_HEADER_SIZE:]
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(len(body), size)

    def test_convert_gqtp_result(self):
        grn = Groonga()
        s = grn._clock_gettime()
        e = grn._clock_gettime()
        senddata = get_send_data_for_gqtp("status")
        rawdata = senddata[:GQTP_HEADER_SIZE] + b'{"test": 0}'
        d = convert_gqtp_result_data(s, e, 0, rawdata)
        d = json.loads(d)
        self.assertEqual(d[0][0], 0)

    def test_convert_gqtp_result_fail(self):
        grn = Groonga()
        s = grn._clock_gettime()
        e = grn._clock_gettime()
        senddata = get_send_data_for_gqtp("status")
        rawdata = senddata[:GQTP_HEADER_SIZE] + b'{"test": 0}'
        d = convert_gqtp_result_data(s, e, GRN_STATUS_UNSUPPORTED_COMMAND_VERSION, rawdata)
        d = json.loads(d)
        self.assertEqual(d[0][0], -71)

class PoyongaResult(unittest.TestCase):
    def test_str(self):
        status = 0
        start_time = 0.0
        elapsed = 1.0
        raw_result = [[status, start_time, elapsed], False]
        result = GroongaResult(json.dumps(raw_result))
        self.assertEqual('<GroongaResult ' +
                         f'status={status} ' +
                         f'start_time={start_time} ' +
                         f'elapsed={elapsed}>',
                         str(result))

if __name__ == "__main__":
    unittest.main()
<commit_msg>add unit test for load with input_type='apache-arrow'<commit_after>import io
import json
import struct
import unittest

try:
    from mock import patch, Mock
except ImportError:
    from unittest.mock import patch, Mock
from poyonga import Groonga, GroongaResult
from poyonga.client import get_send_data_for_gqtp, convert_gqtp_result_data, GQTP_HEADER_SIZE
from poyonga.const import GRN_STATUS_UNSUPPORTED_COMMAND_VERSION

try:
    import pyarrow as pa
except ImportError:
    pa = None

class PoyongaHTTPTestCase(unittest.TestCase):
    def setUp(self):
        self.g = Groonga()

    @patch("poyonga.client.urlopen")
    def test_json_result_with_http(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], {}]"]
        mock_urlopen.return_value = m
        ret = self.g.call("status")
        self.assertEqual(type(ret), GroongaResult)
        self.assertEqual(ret.status, 0)

    @patch("poyonga.client.urlopen")
    def test_load_list(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        ret = self.g.call("load",
                          table="Site",
                          values=[{"_key": "groonga.org"}])
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @patch("poyonga.client.urlopen")
    def test_load_json(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        ret = self.g.call("load",
                          table="Site",
                          values=json.dumps([{"_key": "groonga.org"}]))
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @unittest.skipUnless(pa, "require pyarrow")
    @patch("poyonga.client.urlopen")
    def test_load_apache_arrow(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        values = [pa.array(["groonga.org"])]
        batch = pa.record_batch(values, names=["_key"])
        sink = pa.BufferOutputStream()
        with pa.ipc.new_stream(sink, batch.schema) as writer:
            writer.write_batch(batch)
        values = sink.getvalue().to_pybytes()

        ret = self.g.call("load", table="Site", values=values, input_type="apache-arrow")
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        print(request.data)

        reader = pa.ipc.open_stream(request.data)
        batches = [b for b in reader]
        print(reader.schema, batches[0].to_pydict())
        self.assertEqual({"_key": ["groonga.org"]}, batches[0].to_pydict())
        self.assertEqual({"Content-type": "application/x-apache-arrow-streaming"},
                         request.headers)

    @patch("poyonga.client.urlopen")
    def test_load_io(self, mock_urlopen):
        m = Mock()
        m.read.side_effect = ["[[0, 1337566253.89858, 0.000354], 1]"]
        mock_urlopen.return_value = m
        json_values = json.dumps([{"_key": "groonga.org"}])
        ret = self.g.call("load",
                          table="Site",
                          values=io.BytesIO(json_values.encode()))
        self.assertEqual(ret.body, 1)
        request = mock_urlopen.call_args[0][0]
        self.assertEqual([{"_key": "groonga.org"}],
                         json.loads(request.data.read()))
        self.assertEqual({"Content-type": "application/json"},
                         request.headers)

    @unittest.skipUnless(pa, "require pyarrow")
    @patch("poyonga.client.urlopen")
    def test_select_apache_arrow(self, mock_urlopen):
        m = Mock()
        metadata_fields = [
            pa.field("return_code", pa.int32()),
            pa.field("start_time", pa.timestamp("ns")),
            pa.field("elapsed_time", pa.float64()),
        ]
        metadata_metadata = {
            "GROONGA:data_type": "metadata",
        }
        metadata_schema = pa.schema(metadata_fields, metadata_metadata)
        sec_to_ns = 1_000_000_000
        metadata = [
            [0],
            [int(1337566253.89858 * sec_to_ns)],
            [0.000354],
        ]
        metadata_record_batch = pa.record_batch(metadata,
                                                schema=metadata_schema)
        result_set_fields = [
            pa.field("name", pa.string()),
        ]
        result_set_metadata = {
            "GROONGA:n_hits": "29"
        }
        result_set_schema = pa.schema(result_set_fields, result_set_metadata)
        result_set = [
            ["Groonga", "poyonga"],
        ]
        result_set_record_batch = pa.record_batch(result_set,
                                                  schema=result_set_schema)
        output = pa.BufferOutputStream()
        with pa.RecordBatchStreamWriter(output, metadata_schema) as writer:
            writer.write(metadata_record_batch)
        with pa.RecordBatchStreamWriter(output, result_set_schema) as writer:
            writer.write(result_set_record_batch)
        m.read.side_effect = [output.getvalue().to_pybytes()]
        m.headers = {
            "content-type": "application/x-apache-arrow-streaming",
        }
        mock_urlopen.return_value = m
        ret = self.g.call("select",
                          command_version="3",
                          table="Site",
                          output_type="apache-arrow")
        self.assertEqual(ret.status, 0)
        self.assertEqual(ret.start_time, 1337566253.89858)
        self.assertEqual(ret.elapsed, 0.000354)
        self.assertEqual(ret.hit_num, 29)
        self.assertEqual(ret.items,
                         pa.Table.from_batches([result_set_record_batch]))

class PoyongaGQTPTestCase(unittest.TestCase):
    def setUp(self):
        self.g = Groonga(protocol="gqtp")

    @patch("poyonga.client.socket.socket")
    def test_json_result_with_gqtp(self, mock_socket):
        m = Mock()
        _data = b"{}"
        _proto, _qtype, _keylen, _level, _flags, _status, _size, _opaque, _cas = (
            0xC7,
            0x02,
            0,
            0,
            0,
            0,
            2,
            0,
            0,
        )
        packdata = struct.pack(
            "!BBHBBHIIQ2s",
            _proto,
            _qtype,
            _keylen,
            _level,
            _flags,
            _status,
            _size,
            _opaque,
            _cas,
            _data,
        )
        m.recv.return_value = packdata
        mock_socket.return_value = m
        ret = self.g.call("status")
        self.assertEqual(type(ret), GroongaResult)
        self.assertEqual(ret.status, 0)

class PoyongaFunctions(unittest.TestCase):
    def test_get_send_data(self):
        d = get_send_data_for_gqtp("status")
        self.assertEqual(30, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(6, size)

    def test_get_send_data_with_args(self):
        kwargs = {"table": "Site"}
        d = get_send_data_for_gqtp("select", **kwargs)
        self.assertEqual(45, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        # check body length
        self.assertEqual(21, d[11])

        body = d[GQTP_HEADER_SIZE:]
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(len(body), size)

    def test_get_send_data_with_args_and_two_bytes(self):
        kwargs = {"table": "Sitは"}
        d = get_send_data_for_gqtp("select", **kwargs)
        self.assertEqual(47, len(d))
        self.assertEqual(ord("\xc7"), d[0])
        # check body length
        self.assertEqual(23, d[11])

        body = d[GQTP_HEADER_SIZE:]
        (size,) = struct.unpack("!I", d[8:12])
        self.assertEqual(len(body), size)

    def test_convert_gqtp_result(self):
        grn = Groonga()
        s = grn._clock_gettime()
        e = grn._clock_gettime()
        senddata = get_send_data_for_gqtp("status")
        rawdata = senddata[:GQTP_HEADER_SIZE] + b'{"test": 0}'
        d = convert_gqtp_result_data(s, e, 0, rawdata)
        d = json.loads(d)
        self.assertEqual(d[0][0], 0)

    def test_convert_gqtp_result_fail(self):
        grn = Groonga()
        s = grn._clock_gettime()
        e = grn._clock_gettime()
        senddata = get_send_data_for_gqtp("status")
        rawdata = senddata[:GQTP_HEADER_SIZE] + b'{"test": 0}'
        d = convert_gqtp_result_data(s, e, GRN_STATUS_UNSUPPORTED_COMMAND_VERSION, rawdata)
        d = json.loads(d)
        self.assertEqual(d[0][0], -71)

class PoyongaResult(unittest.TestCase):
    def test_str(self):
        status = 0
        start_time = 0.0
        elapsed = 1.0
        raw_result = [[status, start_time, elapsed], False]
        result = GroongaResult(json.dumps(raw_result))
        self.assertEqual('<GroongaResult ' +
                         f'status={status} ' +
                         f'start_time={start_time} ' +
                         f'elapsed={elapsed}>',
                         str(result))

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>import pyplate
import unittest
import os
from struct import unpack, calcsize

class TestPyplate(unittest.TestCase):
	def setUp(self):
		self.template_name = "template"
		self.data_name     = "test"
		self.data_key      = "test[0]"
		numeric          = "1"
		alphanumeric     = "a"
		self.test_byte   = pyplate.String(numeric)
		self.test_char   = pyplate.String(alphanumeric)
		self.test_bool   = pyplate.String(numeric)
		self.test_short  = pyplate.String(numeric * calcsize("h"))
		self.test_int    = pyplate.String(numeric * calcsize("i"))
		self.test_long   = pyplate.String(numeric * calcsize("l"))
		self.test_llong  = pyplate.String(numeric * calcsize("q"))
		self.test_float  = pyplate.String(numeric * calcsize("f"))
		self.test_double = pyplate.String(numeric * calcsize("d"))
		self.test_file   = pyplate.File("test/data/data_file.txt", 'rb')
	def tearDown(self):
		self.test_file.close()
	def test_BYTE_extract(self):
		t_byte = (pyplate.BYTE)(name=self.data_name)
		extracted_values = t_byte.extract(self.test_byte)[1]
		self.assertTrue(
			unpack("c", str(self.test_byte)) == extracted_values[0]["value"]
		)
	def test_CHAR_extract(self):
		t_char = (pyplate.CHAR)(name=self.data_name)
		extracted_values = t_char.extract(self.test_char)[1]
		self.assertTrue(
			unpack("c", str(self.test_char)) == extracted_values[0]["value"]
		)
	def test_BOOL_extract(self):
		t_bool = (pyplate.BOOL)(name=self.data_name)
		extracted_values = t_bool.extract(self.test_bool)[1]
		self.assertTrue(
			unpack("?", str(self.test_bool)) == extracted_values[0]["value"]
		)
	def test_SHORT_extract(self):
		t_short = (pyplate.SHORT)(name=self.data_name)
		extracted_values = t_short.extract(self.test_short)[1]
		self.assertTrue(
			unpack("h", str(self.test_short)) == extracted_values[0]["value"]
		)
	def test_INT_extract(self):
		t_int = (pyplate.INT)(name=self.data_name)
		extracted_values = t_int.extract(self.test_int)[1]
		self.assertTrue(
			unpack("i", str(self.test_int)) == extracted_values[0]["value"]
		)
	def test_LONG_extract(self):
		t_long = (pyplate.LONG)(name=self.data_name)
		extracted_values = t_long.extract(self.test_long)[1]
		self.assertTrue(
			unpack("l", str(self.test_long)) == extracted_values[0]["value"]
		)
	def test_LLONG_extract(self):
		t_llong = (pyplate.LLONG)(name=self.data_name)
		extracted_values = t_llong.extract(self.test_llong)[1]
		self.assertTrue(
			unpack("q", str(self.test_llong)) == extracted_values[0]["value"]
		)
	def test_FLOAT_extract(self):
		t_float = (pyplate.FLOAT)(name=self.data_name)
		extracted_values = t_float.extract(self.test_float)[1]
		self.assertTrue(
			unpack("f", str(self.test_float)) == extracted_values[0]["value"]
		)
	def test_DOUBLE_extract(self):
		t_double = (pyplate.DOUBLE)(name=self.data_name)
		extracted_values = t_double.extract(self.test_double)[1]
		self.assertTrue(
			unpack("d", str(self.test_double)) == extracted_values[0]["value"]
		)
	def test_template_extract(self):
		t_template = pyplate.template(name=self.template_name)(
			(pyplate.CHAR)(name=self.data_name),
			pyplate.template(name="test2")(
				(pyplate.INT)(name="datatest2")
			)
		)
		compiled_strings = pyplate.String(str(self.test_char) + str(self.test_int))
		t_template.extract(compiled_strings)
		self.assertTrue(
			len(t_template["templates"]["test2"]["variables"]["datatest2"][0]["value"]) == 1,
			len(t_template["variables"][self.data_name][0]["value"]) == 1
		)

if __name__ == '__main__':
	unittest.main()<commit_msg>add test for file extraction<commit_after>import pyplate
import unittest
import os
from struct import unpack, calcsize

class TestPyplate(unittest.TestCase):
	def setUp(self):
		self.template_name = "template"
		self.data_name     = "test"
		self.data_key      = "test[0]"
		numeric          = "1"
		alphanumeric     = "a"
		self.test_byte   = pyplate.String(numeric)
		self.test_char   = pyplate.String(alphanumeric)
		self.test_bool   = pyplate.String(numeric)
		self.test_short  = pyplate.String(numeric * calcsize("h"))
		self.test_int    = pyplate.String(numeric * calcsize("i"))
		self.test_long   = pyplate.String(numeric * calcsize("l"))
		self.test_llong  = pyplate.String(numeric * calcsize("q"))
		self.test_float  = pyplate.String(numeric * calcsize("f"))
		self.test_double = pyplate.String(numeric * calcsize("d"))
		self.test_file_path = "test/data/data_file.txt"
	def test_BYTE_extract(self):
		t_byte = (pyplate.BYTE)(name=self.data_name)
		extracted_values = t_byte.extract(self.test_byte)[1]
		self.assertTrue(
			unpack("c", str(self.test_byte)) == extracted_values[0]["value"]
		)
	def test_CHAR_extract(self):
		t_char = (pyplate.CHAR)(name=self.data_name)
		extracted_values = t_char.extract(self.test_char)[1]
		self.assertTrue(
			unpack("c", str(self.test_char)) == extracted_values[0]["value"]
		)
	def test_BOOL_extract(self):
		t_bool = (pyplate.BOOL)(name=self.data_name)
		extracted_values = t_bool.extract(self.test_bool)[1]
		self.assertTrue(
			unpack("?", str(self.test_bool)) == extracted_values[0]["value"]
		)
	def test_SHORT_extract(self):
		t_short = (pyplate.SHORT)(name=self.data_name)
		extracted_values = t_short.extract(self.test_short)[1]
		self.assertTrue(
			unpack("h", str(self.test_short)) == extracted_values[0]["value"]
		)
	def test_INT_extract(self):
		t_int = (pyplate.INT)(name=self.data_name)
		extracted_values = t_int.extract(self.test_int)[1]
		self.assertTrue(
			unpack("i", str(self.test_int)) == extracted_values[0]["value"]
		)
	def test_LONG_extract(self):
		t_long = (pyplate.LONG)(name=self.data_name)
		extracted_values = t_long.extract(self.test_long)[1]
		self.assertTrue(
			unpack("l", str(self.test_long)) == extracted_values[0]["value"]
		)
	def test_LLONG_extract(self):
		t_llong = (pyplate.LLONG)(name=self.data_name)
		extracted_values = t_llong.extract(self.test_llong)[1]
		self.assertTrue(
			unpack("q", str(self.test_llong)) == extracted_values[0]["value"]
		)
	def test_FLOAT_extract(self):
		t_float = (pyplate.FLOAT)(name=self.data_name)
		extracted_values = t_float.extract(self.test_float)[1]
		self.assertTrue(
			unpack("f", str(self.test_float)) == extracted_values[0]["value"]
		)
	def test_DOUBLE_extract(self):
		t_double = (pyplate.DOUBLE)(name=self.data_name)
		extracted_values = t_double.extract(self.test_double)[1]
		self.assertTrue(
			unpack("d", str(self.test_double)) == extracted_values[0]["value"]
		)
	def test_template_extract(self):
		t_template = pyplate.template(name=self.template_name)(
			(pyplate.CHAR)(name=self.data_name),
			pyplate.template(name="test2")(
				(pyplate.INT)(name="datatest2")
			)
		)
		compiled_strings = pyplate.String(str(self.test_char) + str(self.test_int))
		t_template.extract(compiled_strings)
		self.assertTrue(
			len(t_template["templates"]["test2"]["variables"]["datatest2"][0]["value"]) == 1,
			len(t_template["variables"][self.data_name][0]["value"]) == 1
		)
	def test_file_extract(self):
		with pyplate.File(self.test_file_path) as t_file:
			t_template = pyplate.template(name=self.template_name)(
				(pyplate.CHAR)(name=self.data_name, repeat=4),
				(pyplate.SHORT)(name=self.data_name+"short")
			)
			t_template.extract(t_file)
			self.assertTrue(
				len(t_template["variables"][self.data_name]) == 4,
				len(t_template["variables"][self.data_name+"short"]) == 1
			)

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


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

"""
Tests API of the TEC class.

I will eventually need to fix this note, but here it is. I am intentionally not
testing the cases where the sub-dicts have incorrect data. The Electrode tests
deal with these cases comprehensively and there is no additional logic at the
TEC level. In the event that I do need to test these cases, I can always reach
back in the VCS history and grab the previous version of this file.
"""

__author__ = "Joshua Ryan Smith (joshua.r.smith@gmail.com)"
__version__ = ""
__date__ = ""
__copyright__ = "Copyright (c) 2012 Joshua Ryan Smith"
__license__ = ""

from tec import TEC
import unittest

class InstantiationInputNonDict(unittest.TestCase):
  """
  Tests instantiation when non-dict data is used.
  """

  def test_TEC_no_input_arg(self):
    """Attempt to instantiate TEC with no input argument."""
    self.assertRaises(TypeError,TEC,None)

  def test_TEC_non_dict_input_arg(self):
    """Attempt to instantiate Electrode with a non-dict input argument."""
    self.assertRaises(TypeError,TEC,"this string is not a dict.")

class InstantiationInputIncomplete(unittest.TestCase):
  """
  Tests instantiating when input dict is missing required data.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate a TEC object.
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.input_params = input_params

  def test_TEC_input_arg_sans_Emitter(self):
    """Instantiating argument missing Emitter."""
    del(self.input_params["Emitter"])
    self.assertRaises(KeyError,TEC,self.input_params)

  def test_TEC_input_arg_sans_Collector(self):
    """Instantiating argument missing Emitter."""
    del(self.input_params["Collector"])
    self.assertRaises(KeyError,TEC,self.input_params)

class InstantiationInputFieldsWrongType(unittest.TestCase):
  """
  Tests instantiating when input dict has non-numeric data items.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate a TEC object.
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.input_params = input_params

  def test_TEC_input_Emitter_non_numeric(self):
    """Instantiating argument Emitter is non-numeric."""
    self.input_params["Emitter"] = "this string is non-numeric."
    self.assertRaises(TypeError,TEC,self.input_params)

  def test_TEC_input_Collector_non_numeric(self):
    """Instantiating argument Collector is non-numeric."""
    self.input_params["Collector"] = "this string is non-numeric."
    self.assertRaises(TypeError,TEC,self.input_params)

class SetInputWrongType(unittest.TestCase):
  """
  Tests setting attributes when input data is non-numeric.
  """

  def setUp(self):
    """
    Set up a TEC object for the tests
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.TEC_obj = TEC(input_params)

  def test_TEC_set_Emitter_non_numeric(self):
    """Set argument Emitter non-numeric."""
    non_num = "this string is non-numeric."
    self.assertRaises(TypeError,self.TEC_obj["Emitter"],non_num)

  def test_TEC_set_Collector_non_numeric(self):
    """Set argument Collector non-numeric."""
    non_num = "this string is non-numeric."
    self.assertRaises(TypeError,self.TEC_obj["Collector"],non_num)

class CalculatorsReturnType(unittest.TestCase):
  """
  Tests output types of the TEC calculator methods.
  """

  def setUp(self):
    """
    Set up a TEC object for the tests
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.TEC_obj = TEC(input_params)

if __name__ == '__main__':
  unittest.main()
<commit_msg>Tests to type check output of methods.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Tests API of the TEC class.

I will eventually need to fix this note, but here it is. I am intentionally not
testing the cases where the sub-dicts have incorrect data. The Electrode tests
deal with these cases comprehensively and there is no additional logic at the
TEC level. In the event that I do need to test these cases, I can always reach
back in the VCS history and grab the previous version of this file.
"""

__author__ = "Joshua Ryan Smith (joshua.r.smith@gmail.com)"
__version__ = ""
__date__ = ""
__copyright__ = "Copyright (c) 2012 Joshua Ryan Smith"
__license__ = ""

from tec import TEC
import unittest

class InstantiationInputNonDict(unittest.TestCase):
  """
  Tests instantiation when non-dict data is used.
  """

  def test_TEC_no_input_arg(self):
    """Attempt to instantiate TEC with no input argument."""
    self.assertRaises(TypeError,TEC,None)

  def test_TEC_non_dict_input_arg(self):
    """Attempt to instantiate Electrode with a non-dict input argument."""
    self.assertRaises(TypeError,TEC,"this string is not a dict.")

class InstantiationInputIncomplete(unittest.TestCase):
  """
  Tests instantiating when input dict is missing required data.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate a TEC object.
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.input_params = input_params

  def test_TEC_input_arg_sans_Emitter(self):
    """Instantiating argument missing Emitter."""
    del(self.input_params["Emitter"])
    self.assertRaises(KeyError,TEC,self.input_params)

  def test_TEC_input_arg_sans_Collector(self):
    """Instantiating argument missing Emitter."""
    del(self.input_params["Collector"])
    self.assertRaises(KeyError,TEC,self.input_params)

class InstantiationInputFieldsWrongType(unittest.TestCase):
  """
  Tests instantiating when input dict has non-numeric data items.
  """

  def setUp(self):
    """
    Set up a dictionary that can properly instantiate a TEC object.
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.input_params = input_params

  def test_TEC_input_Emitter_non_numeric(self):
    """Instantiating argument Emitter is non-numeric."""
    self.input_params["Emitter"] = "this string is non-numeric."
    self.assertRaises(TypeError,TEC,self.input_params)

  def test_TEC_input_Collector_non_numeric(self):
    """Instantiating argument Collector is non-numeric."""
    self.input_params["Collector"] = "this string is non-numeric."
    self.assertRaises(TypeError,TEC,self.input_params)

class SetInputWrongType(unittest.TestCase):
  """
  Tests setting attributes when input data is non-numeric.
  """

  def setUp(self):
    """
    Set up a TEC object for the tests
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.TEC_obj = TEC(input_params)

  def test_TEC_set_Emitter_non_numeric(self):
    """Set argument Emitter non-numeric."""
    non_num = "this string is non-numeric."
    self.assertRaises(TypeError,self.TEC_obj["Emitter"],non_num)

  def test_TEC_set_Collector_non_numeric(self):
    """Set argument Collector non-numeric."""
    non_num = "this string is non-numeric."
    self.assertRaises(TypeError,self.TEC_obj["Collector"],non_num)

class CalculatorsReturnType(unittest.TestCase):
  """
  Tests output types of the TEC calculator methods.
  """

  def setUp(self):
    """
    Set up a TEC object for the tests
    """

    em_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    co_params = {"temp":1,\
                 "barrier_ht":1,\
                 "voltage":1,\
                 "position":0,\
                 "richardson":10,\
                 "emissivity":0.5}

    input_params = {"Emitter":em_params, "Collector":co_params}

    self.TEC_obj = TEC(input_params)

  def test_get_motive_numeric(self):
    """
    get_motive returns numeric type.
    """
    self.assertTrue(isinstance(self.TEC_obj.get_motive(),float))

  def test_get_motive_numpy_array(self):
    """
    get_motive returns numpy array type.
    """
    pass

  def test_get_max_motive(self):
    """
    get_max_motive returns numeric type.
    """
    self.assertTrue(isinstance(self.TEC_obj.get_max_motive(),float))

  def test_get_max_motive_with_position(self):
    """
    get_max_motive returns tuple.
    """
    self.assertTrue(isinstance(self.TEC_obj.get_max_motive(with_position=1),tuple))

  def test_get_max_motive_with_position_tuple_items(self):
    """
    get_max_motive returns tuple with numeric items.
    """
    tup = self.TEC_obj.get_max_motive(with_position=1)
    self.assertTrue(all( type(indx) is float for indx in tup ))

# ========================================
  def test_calc_interelectrode_spacing(self):
    """
    calc_interelectrode_spacing returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_interelectrode_spacing(),float))

  def test_calc_output_voltage(self):
    """
    calc_output_voltage returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_output_voltage(),float))

  def test_calc_contact_potential(self):
    """
    calc_contact_potential returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_contact_potential(),float))

  def test_calc_forward_current_density(self):
    """
    calc_forward_current_density returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_forward_current_density(),float))

  def test_calc_back_current_density(self):
    """
    calc_back_current_density returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_back_current_density(),float))

  def test_calc_output_current_density(self):
    """
    calc_output_current_density returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_output_current_density(),float))

  def test_calc_output_power_density(self):
    """
    calc_output_power_density returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_output_power_density(),float))

  def test_calc_load_resistance(self):
    """
    calc_load_resistance returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_load_resistance(),float))

  def test_calc_carnot_efficiency(self):
    """
    calc_carnot_efficiency returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_carnot_efficiency(),float))

  def test_calc_radiation_efficiency(self):
    """
    calc_radiation_efficiency returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_radiation_efficiency(),float))

  def test_calc_electronic_efficiency(self):
    """
    calc_electronic_efficiency returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_electronic_efficiency(),float))

  def test_calc_total_efficiency(self):
    """
    calc_total_efficiency returns numeric.
    """
    self.assertTrue(isinstance(self.TEC_obj.calc_total_efficiency(),float))

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


<commit_before>import math

import numpy as np

import meshplex

def test_mesh_line():
    pts = [0.0, 1.0, 3.0, 4.0]
    cells = [[0, 1], [1, 2], [2, 3]]
    mesh = meshplex.MeshLine(pts, cells)
    print(mesh.cell_volumes)
    ref = [1.0, 2.0, 1.0]
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_tri():
    # two triangles in 5D
    points = [
        [0.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0, 0.0],
    ]
    cells = [[0, 1, 2], [0, 3, 2]]
    mesh = meshplex.Mesh(points, cells)

    ref = [0.5, 0.5]
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_tetra():
    points = [
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ]
    cells = [[0, 1, 2, 3]]
    mesh = meshplex.Mesh(points, cells)

    ref = [1 / 6]
    print(mesh.cell_volumes)
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_simplex5():
    points = [
        [0.0, 0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 1.0],
    ]
    cells = [[0, 1, 2, 3, 4]]
    mesh = meshplex.Mesh(points, cells)

    ref = [1 / 24]
    print(mesh.cell_volumes)
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

if __name__ == "__main__":
    # test_vol_tri()
    test_vol_tetra()
<commit_msg>lint fix<commit_after>import numpy as np

import meshplex

def test_mesh_line():
    pts = [0.0, 1.0, 3.0, 4.0]
    cells = [[0, 1], [1, 2], [2, 3]]
    mesh = meshplex.MeshLine(pts, cells)
    print(mesh.cell_volumes)
    ref = [1.0, 2.0, 1.0]
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_tri():
    # two triangles in 5D
    points = [
        [0.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0, 0.0],
    ]
    cells = [[0, 1, 2], [0, 3, 2]]
    mesh = meshplex.Mesh(points, cells)

    ref = [0.5, 0.5]
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_tetra():
    points = [
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ]
    cells = [[0, 1, 2, 3]]
    mesh = meshplex.Mesh(points, cells)

    ref = [1 / 6]
    print(mesh.cell_volumes)
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

def test_vol_simplex5():
    points = [
        [0.0, 0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 1.0],
    ]
    cells = [[0, 1, 2, 3, 4]]
    mesh = meshplex.Mesh(points, cells)

    ref = [1 / 24]
    print(mesh.cell_volumes)
    assert np.all(np.abs(mesh.cell_volumes - ref) < np.abs(ref) * 1.0e-13)

if __name__ == "__main__":
    # test_vol_tri()
    test_vol_tetra()
<|endoftext|>


<commit_before>#http://docs.travis-ci.com/user/database-setup/#MySQL<commit_msg>Delete accidental addition of travis-mysql.py<commit_after><|endoftext|>