Best Python code snippet using hypothesis
tests_engine.py
Source:tests_engine.py  
1from django.test import SimpleTestCase, tag2from game.engine import *3import json, os4# Constants5INITIAL_BOARDSTATE = {6    # Formatted as Position Object for Chessboard.js7    # Black side8    "a8": "bR", "b8": "bN", "c8": "bB", "d8": "bQ", "e8": "bK", "f8": "bB", "g8": "bN", "h8": "bR",9    "a7": "bP", "b7": "bP", "c7": "bP", "d7": "bP", "e7": "bP", "f7": "bP", "g7": "bP", "h7": "bP",10    11    # White side12    "a2": "wP", "b2": "wP", "c2": "wP", "d2": "wP", "e2": "wP", "f2": "wP", "g2": "wP", "h2": "wP",13    "a1": "wR", "b1": "wN", "c1": "wB", "d1": "wQ", "e1": "wK", "f1": "wB", "g1": "wN", "h1": "wR",14}15@tag('core', 'piece')16class PieceTestCase(SimpleTestCase):17    def testPiece(self):18        test_piece = Piece('w', 'K', 'a1', 'kingCorp')19        20        self.assertEqual(test_piece.color, 'w')21        self.assertEqual(test_piece.rank, 'K')22        self.assertEqual(test_piece.pos, 'a1')23        self.assertEqual(test_piece.corp, 'kingCorp')24        self.assertEqual(test_piece.getTraits(), "color:w,rank:K,corp:kingCorp,pos:a1")25@tag('core', 'action')26class ActionTestCase(SimpleTestCase):27    def testMovementActionInit(self):28        # TODO: This whole data path situation is a bit jank, holding until persisting actions to history in DB to use fixtures29        module_dir = os.path.dirname(__file__)30        data_path = os.path.join(module_dir, 'mockups/sample_requests.json')31        with open(data_path, 'r') as f:32            payload = json.load(f)33            test_action = Action(json.dumps(payload["test_movement"]))34            35            self.assertEqual(test_action.actionType, ActionType.MOVEMENT)36            self.assertEqual(test_action.activePiece, Piece(Color.WHITE, Rank.ROOK, 'a8'))37            self.assertEqual(test_action.targetPiece, Piece(Color.WHITE, Rank.ROOK, 'a7'))38    39    def testAttackAttemptActionInit(self):40        # TODO: This whole data path situation is a bit jank, holding until persisting actions to history in DB to use fixtures41        module_dir = os.path.dirname(__file__)42        data_path = os.path.join(module_dir, 'mockups/sample_requests.json')43        with open(data_path, 'r') as f:44            payload = json.load(f)45            test_action = Action(json.dumps(payload["test_attack"]))46            47            self.assertEqual(test_action.actionType, ActionType.ATTACK_ATTEMPT)48            self.assertEqual(test_action.activePiece, Piece(Color.WHITE, Rank.PAWN, 'a4'))49            self.assertEqual(test_action.targetPiece, Piece(Color.BLACK, Rank.PAWN, 'a5'))50    51    def testHightlightActionInit(self):52        # TODO: This whole data path situation is a bit jank, holding until persisting actions to history in DB to use fixtures53        module_dir = os.path.dirname(__file__)54        data_path = os.path.join(module_dir, 'mockups/sample_requests.json')55        with open(data_path, 'r') as f:56            payload = json.load(f)57            test_action = Action(json.dumps(payload["test_highlight"]))58            59            self.assertEqual(test_action.actionType, ActionType.HIGHLIGHT)60            self.assertEqual(test_action.activePiece, Piece(Color.BLACK, Rank.ROOK, 'a8'))61            62@tag('core', 'helpers')63class HelperTestCase(SimpleTestCase):64    @tag('getDirection')65    def testGetDirectionFunc(self):66        module_dir = os.path.dirname(__file__)67        data_path = os.path.join(module_dir, 'mockups/sample_boardstates.json')68        with open(data_path, 'r') as f:69            test_cases = json.load(f)["getDirectionTests"]70            for test in test_cases:71                match test["label"]:72                    case "N":73                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), 1)74                    case "S":75                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), -1)76                    case "E":77                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), 10)78                    case "W":79                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), -10)80                    case "NE":81                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), 11)82                    case "NW":83                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), -9)84                    case "SE":85                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), 9)86                    case "SW":87                        self.assertEqual(getDirection(test["startPos"], test["endPos"]), -11)88@tag('core', 'boardstate')89class BoardstateTestCase(SimpleTestCase):90    91    @tag('moveset', 'pawn')92    def testGetValidPawnMoveset(self):93        # Filedata import94        module_dir = os.path.dirname(__file__)95        boardstate_data_path = os.path.join(module_dir, 'mockups/sample_boardstates.json')96        corp_data_path = os.path.join(module_dir, 'mockups/example_corp_shape.json')97        boardstate_file = open(boardstate_data_path, 'r')98        corp_file = open(corp_data_path, 'r')99        100        boardstate_payload = json.load(boardstate_file)101        corp_payload = json.load(corp_file)102        test_cases = boardstate_payload["pawnTests"]103        for test in test_cases:104            match test["label"]:105                case "getPawnValidMoveset-WhiteFullBoard":106                    current = Boardstate(INITIAL_BOARDSTATE, True, corp_payload)107                    test_pawn = Piece(test["piece"]["color"], test["piece"]["rank"], test["piece"]["pos"], test["piece"]["corp"])108                    (test_in_range, test_setup, test_movement) = current.getValidMoveset(test_pawn)109                    self.assertEqual(test_in_range, [])110                    self.assertEqual(test_setup, [])111                    self.assertCountEqual(test_movement, ["c3", "d3", "e3"])112                113                # case "getPawnValidMoveset-BlackFullBoard":114                #     current = Boardstate(INITIAL_BOARDSTATE, False, corp_payload)115                #     test_pawn = Piece(test["piece"]["color"], test["piece"]["rank"], test["piece"]["pos"], test["piece"]["corp"])116                #     (test_in_range, test_setup, test_movement) = current.getValidMoveset(test_pawn)117                #     self.assertEqual(test_in_range, [])118                #     self.assertEqual(test_setup, [])119                #     self.assertCountEqual(test_movement, ["c6", "d6", "e6"])120        121        boardstate_file.close()122        corp_file.close()        123        124    @tag('moveset', 'bishop')125    def testGetValidBishopMoveset(self):126        # Filedata import127        module_dir = os.path.dirname(__file__)128        boardstate_data_path = os.path.join(module_dir, 'mockups/sample_boardstates.json')129        corp_data_path = os.path.join(module_dir, 'mockups/example_corp_shape.json')130        boardstate_file = open(boardstate_data_path, 'r')131        corp_file = open(corp_data_path, 'r')132        133        boardstate_payload = json.load(boardstate_file)134        corp_payload = json.load(corp_file)135        test_cases = boardstate_payload["bishopTests"]136        for test in test_cases:137            match test["label"]:138                # case "getBishopValidMoveset-WhiteFullBoard":139                #     current = Boardstate(INITIAL_BOARDSTATE, True, corp_payload)140                #     test_bishop = Piece(test["piece"]["color"], test["piece"]["rank"], test["piece"]["pos"], test["piece"]["corp"])141                #     (test_in_range, test_setup, test_movement) = current.getValidMoveset(test_bishop)142                #     self.assertEqual(test_in_range, [])143                #     self.assertEqual(test_setup, [])144                #     self.assertCountEqual(test_movement, [])145                146                case "getBishopValidMoveset-BlackFullBoard":147                    current = Boardstate(INITIAL_BOARDSTATE, False, corp_payload)148                    test_bishop = Piece(test["piece"]["color"], test["piece"]["rank"], test["piece"]["pos"], test["piece"]["corp"])149                    (test_in_range, test_setup, test_movement) = current.getValidMoveset(test_bishop)150                    self.assertEqual(test_in_range, [])151                    self.assertEqual(test_setup, [])152                    self.assertCountEqual(test_movement, [])153        154        boardstate_file.close()...test_validators.py
Source:test_validators.py  
...61class TestLengthRange(unittest.TestCase):62    def test_none(self):63        actual = validators.LengthRange(4, 5)(None, [])64        self.assertIsNone(actual)65    def test_in_range(self):66        value = 'abcd'67        actual = validators.LengthRange(4, 5)(value, [])68        self.assertEqual(value, actual)69    def test_too_long(self):70        value = '123456'71        try:72            validators.LengthRange(4, 5)(value, ['node'])73            self.assertTrue(False)74        except ValueError as ex:75            self.assertEqual('node: String is too long', ex.args[0])76    def test_too_short(self):77        value = 'abc'78        try:79            validators.LengthRange(4, 5)(value, ['node'])80            self.assertTrue(False)81        except ValueError as ex:82            self.assertEqual('node: String is too short', ex.args[0])83class TestOnly(unittest.TestCase):84    def test_none(self):85        actual = validators.Only('user', 'node')(None, [])86        self.assertIsNone(actual)87    def test_in_set(self):88        value = 'user'89        actual = validators.Only('user', 'node')(value, [])90        self.assertEqual(value, actual)91    def test_not_in_set(self):92        value = 'abcd'93        try:94            validators.Only('user', 'node')(value, ['root'])95            self.assertTrue(False)96        except ValueError as ex:97            self.assertEqual('root: Invalid value', ex.args[0])98class TestMinimum(unittest.TestCase):99    def test_none(self):100        actual = validators.Minimum(4)(None, [])101        self.assertIsNone(actual)102    def test_in_range(self):103        value = 4.5104        actual = validators.Minimum(4)(value, [])105        self.assertEqual(value, actual)106    def test_too_small(self):107        value = 3.99108        try:109            validators.Minimum(4)(value, ['node'])110            self.assertTrue(False)111        except ValueError as ex:112            self.assertEqual('node: Value is too small', ex.args[0])113class TestExclusiveMinimum(unittest.TestCase):114    def test_none(self):115        actual = validators.ExclusiveMinimum(4)(None, [])116        self.assertIsNone(actual)117    def test_in_range(self):118        value = 4.5119        actual = validators.ExclusiveMinimum(4)(value, [])120        self.assertEqual(value, actual)121    def test_too_small(self):122        value = 4123        try:124            validators.ExclusiveMinimum(4)(value, ['node'])125            self.assertTrue(False)126        except ValueError as ex:127            self.assertEqual('node: Value is too small', ex.args[0])128class TestMaximum(unittest.TestCase):129    def test_none(self):130        actual = validators.Maximum(5.3)(None, [])131        self.assertIsNone(actual)132    def test_in_range(self):133        value = 4.5134        actual = validators.Maximum(5.3)(value, [])135        self.assertEqual(value, actual)136    def test_too_big(self):137        value = 5.301138        try:139            validators.Maximum(5.3)(value, ['node'])140            self.assertTrue(False)141        except ValueError as ex:142            self.assertEqual('node: Value is too large', ex.args[0])143class TestExclusiveMaximum(unittest.TestCase):144    def test_none(self):145        actual = validators.ExclusiveMaximum(5.3)(None, [])146        self.assertIsNone(actual)147    def test_in_range(self):148        value = 4.5149        actual = validators.ExclusiveMaximum(5.3)(value, [])150        self.assertEqual(value, actual)151    def test_too_big(self):152        value = 5.3153        try:154            validators.ExclusiveMaximum(5.3)(value, ['node'])155            self.assertTrue(False)156        except ValueError as ex:157            self.assertEqual('node: Value is too large', ex.args[0])158class TestRange(unittest.TestCase):159    def test_none(self):160        actual = validators.Range(4, 5.3)(None, [])161        self.assertIsNone(actual)162    def test_in_range(self):163        value = 4.5164        actual = validators.Range(4, 5.3)(value, [])165        self.assertEqual(value, actual)166    def test_too_big(self):167        value = 6.1168        try:169            validators.Range(4, 5.3)(value, ['node'])170            self.assertTrue(False)171        except ValueError as ex:172            self.assertEqual('node: Value is too large', ex.args[0])173    def test_too_small(self):174        value = 3175        try:176            validators.Range(4, 5.3)(value, ['node'])...test002.py
Source:test002.py  
...46    """)47@step()48def test():49    results=controller.recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,2001)50    test_in_range(results[0], -5,40)51    results=controller.recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,2002)52    test_in_range(results[0], -5,40)53@step()54def powercycle():55    """test result on poweron"""56    if not node[0].powercycle():57        return58    59    results=controller.recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,2001)60    test_in_range(results[0], -5,40)61    results=controller.recv_domoticz_mqtt(SENSOR_TYPE_SINGLE,2002)62    test_in_range(results[0], -5,40)63if __name__=='__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
