Best Python code snippet using avocado_python
test_rule_reduction.py
Source:test_rule_reduction.py  
1import numpy as np2from scipy.sparse import csr_matrix3from knodle.transformation.rule_reduction import _get_merged_matrix, reduce_rule_matches, _get_rule_by_label_iterator4def test_reduction():5    # test rule iterator6    mapping_rule_class_t = np.array([7        [1, 0],8        [0, 1],9        [1, 0],10        [1, 0]])11    rule_iterator = list(_get_rule_by_label_iterator(mapping_rule_class_t))12    expected_iterator = [13        np.array([0, 2, 3]),14        np.array([1])15    ]16    assert len(rule_iterator) == len(expected_iterator)17    assert np.array_equal(rule_iterator[0], expected_iterator[0])18    assert np.array_equal(rule_iterator[1], expected_iterator[1])19    # test _get_merged_matrix20    rule_matches_z = np.array([21        [0, 1, 0, 1],22        [1, 0, 1, 1],23        [0, 0, 1, 0],24        [1, 0, 1, 0],25        [0, 0, 1, 0]])26    to_keep_mask = np.array([1, 2])27    merged_rule_matches_z = _get_merged_matrix(28        full_matches=rule_matches_z, to_keep_mask=to_keep_mask, label_rule_masks=expected_iterator)29    expected_merged = np.array([30        [1],31        [1],32        [0],33        [1],34        [0]])35    assert np.array_equal(merged_rule_matches_z, expected_merged)36    # test end-to-end merge37    test_rule_matches_z = np.array([38        [0, 1, 0, 0],39        [0, 0, 1, 0],40        [1, 0, 0, 0]])41    out = reduce_rule_matches(42        rule_matches_z=rule_matches_z, mapping_rules_labels_t=mapping_rule_class_t,43        rule_matches_rest={"test_matches": test_rule_matches_z},44        drop_rules=False, max_rules=2, min_coverage=1.0)45    expected = {46        "train_rule_matches_z": np.array([47            [1, 1],48            [1, 0],49            [1, 0],50            [1, 0],51            [1, 0]52        ]),53        "test_matches": np.array([54            [0, 1],55            [1, 0],56            [1, 0]57        ]),58        "mapping_rules_labels_t": np.array([59            [1, 0],60            [0, 1]61        ])62    }63    assert np.array_equal(out.get("train_rule_matches_z"), expected["train_rule_matches_z"])64    assert np.array_equal(out.get("test_matches"), expected["test_matches"])65    assert np.array_equal(out.get("mapping_rules_labels_t"), expected["mapping_rules_labels_t"])66    # test end-to-end by drop67    out = reduce_rule_matches(68        rule_matches_z=rule_matches_z, mapping_rules_labels_t=mapping_rule_class_t,69        rule_matches_rest={"test_matches": test_rule_matches_z},70        drop_rules=True, max_rules=2, min_coverage=0.0)71    expected = {72        "train_rule_matches_z": np.array([73            [0, 0],74            [1, 1],75            [0, 1],76            [1, 1],77            [0, 1]78        ]),79        "test_matches": np.array([80            [0, 0],81            [0, 1],82            [1, 0]83        ]),84        "mapping_rules_labels_t": np.array([85            [1, 0],86            [1, 0]87        ])88    }89    assert np.array_equal(out.get("train_rule_matches_z"), expected["train_rule_matches_z"])90    assert np.array_equal(out.get("test_matches"), expected["test_matches"])91    assert np.array_equal(out.get("mapping_rules_labels_t"), expected["mapping_rules_labels_t"])92def test_reduction_for_sparse():93    # test rule iterator94    mapping_rule_class_t = csr_matrix([95        [1, 0],96        [0, 1],97        [1, 0],98        [1, 0]])99    rule_iterator = list(_get_rule_by_label_iterator(mapping_rule_class_t))100    expected_iterator = [101        np.array([0, 2, 3]),102        np.array([1])103    ]104    assert len(rule_iterator) == len(expected_iterator)105    assert np.array_equal(rule_iterator[0], expected_iterator[0])106    assert np.array_equal(rule_iterator[1], expected_iterator[1])107    # test _get_merged_matrix108    rule_matches_z = csr_matrix([109        [0, 1, 0, 1],110        [1, 0, 1, 1],111        [0, 0, 1, 0],112        [1, 0, 1, 0],113        [0, 0, 1, 0]])114    to_keep_mask = np.array([1, 2])115    merged_rule_matches_z = _get_merged_matrix(116        full_matches=rule_matches_z, to_keep_mask=to_keep_mask, label_rule_masks=expected_iterator)117    expected_merged = csr_matrix([118        [1],119        [1],120        [0],121        [1],122        [0]])123    assert (merged_rule_matches_z != expected_merged).nnz == 0124    # test end-to-end merge125    test_rule_matches_z = csr_matrix([126        [0, 1, 0, 0],127        [0, 0, 1, 0],128        [1, 0, 0, 0]])129    out = reduce_rule_matches(130        rule_matches_z=rule_matches_z, mapping_rules_labels_t=mapping_rule_class_t,131        rule_matches_rest={"test_matches": test_rule_matches_z},132        drop_rules=False, max_rules=2, min_coverage=1.0)133    expected = {134        "train_rule_matches_z": csr_matrix([135            [1, 1],136            [1, 0],137            [1, 0],138            [1, 0],139            [1, 0]140        ]),141        "test_matches": csr_matrix([142            [0, 1],143            [1, 0],144            [1, 0]145        ]),146        "mapping_rules_labels_t": csr_matrix([147            [1, 0],148            [0, 1]149        ])150    }151    assert (out.get("train_rule_matches_z") != expected["train_rule_matches_z"]).nnz == 0152    assert (out.get("test_matches") != expected["test_matches"]).nnz == 0153    assert (out.get("mapping_rules_labels_t") != expected["mapping_rules_labels_t"]).nnz == 0154    assert isinstance(out.get("train_rule_matches_z"), csr_matrix)155    assert isinstance(out.get("mapping_rules_labels_t"), csr_matrix)156    # test end-to-end by drop with sparse matches and dense mapping T157    mapping_rule_class_t = np.array([158        [1, 0],159        [0, 1],160        [1, 0],161        [1, 0]])162    out = reduce_rule_matches(163        rule_matches_z=rule_matches_z, mapping_rules_labels_t=mapping_rule_class_t,164        rule_matches_rest={"test_matches": test_rule_matches_z},165        drop_rules=True, max_rules=2, min_coverage=0.0)166    expected = {167        "train_rule_matches_z": csr_matrix([168            [0, 0],169            [1, 1],170            [0, 1],171            [1, 1],172            [0, 1]173        ]),174        "test_matches": csr_matrix([175            [0, 0],176            [0, 1],177            [1, 0]178        ]),179        "mapping_rules_labels_t": np.array([180            [1, 0],181            [1, 0]182        ])183    }184    assert (out.get("train_rule_matches_z") != expected["train_rule_matches_z"]).nnz == 0185    assert (out.get("test_matches") != expected["test_matches"]).nnz == 0186    assert np.array_equal(out.get("mapping_rules_labels_t"), expected["mapping_rules_labels_t"])187    assert isinstance(out.get("train_rule_matches_z"), csr_matrix)...test_scoreboard.py
Source:test_scoreboard.py  
1from datetime import datetime, date2from hockeynor import scoreboard3TEST_DATA = """{"Matches": [ 4{ "MatchId": 7267686,5  "HomeTeamShortName": "  Stavanger Ishockeyklubb  ",6  "AwayTeamShortName": "Sparta Elite   ",7  "HomeTeamScore": 5,8  "AwayTeamScore": 4,9  "FetchScoreFromRaven": false,10  "StartDate": "/Date(1603382400000)/",11  "FormattedDate": "22.10.2020",12  "FormattedShortDate": "22.10",13  "FormattedStartTime": "18:00"},14{   "MatchId": 7267685,15    "HomeTeamShortName": "Narvik",16    "AwayTeamShortName": "Manglerud Star Elite",17    "HomeTeamScore": 1,18    "AwayTeamScore": 3,19    "FetchScoreFromRaven": false,20    "StartDate": "/Date(1603384200000)/",21    "FormattedDate": "22.10.2020",22    "FormattedShortDate": "22.10",23    "FormattedStartTime": "18:30" },24{25            "MatchId": 7267710,26            "HomeTeamShortName": "Frisk Asker Elite   ",27            "AwayTeamShortName": "Manglerud Star Elite",28            "HomeTeamScore": null,29            "AwayTeamScore": null,30            "FetchScoreFromRaven": false,31            "StartDate": "/Date(1605456000000)/",32            "FormattedDate": "15.11.2020",33            "FormattedShortDate": "15.11",34            "FormattedStartTime": "17:00"35        }36]}37"""38TEST_MATCHES = [39    {'away': 'SIL', 'away_score': 3, 'home': 'NH', 'home_score': 0, 'match_id': 7267711,40     'start_date': datetime(2020, 11, 14, 16, 0)},41    {'away': 'Grüner', 'away_score': 1, 'home': 'Stjernen Elite', 'home_score': 7, 'match_id': 7267712,42     'start_date': datetime(2020, 11, 14, 16, 0)},43    {'away': 'Oilers', 'away_score': 6, 'home': 'L.I.K', 'home_score': 1, 'match_id': 7267709,44     'start_date': datetime(2020, 11, 14, 18, 0)},45    {'away': 'M/S', 'away_score': 4, 'home': 'F/A', 'home_score': 5, 'match_id': 7267710,46     'start_date': datetime(2020, 11, 15, 17, 0)},47    {'away': 'L.I.K', 'away_score': 4, 'home': 'NH', 'home_score': 1, 'match_id': 7267855,48     'start_date': datetime(2020, 11, 17, 18, 30)},49    {'away': 'L.I.K', 'away_score': 4, 'home': 'NH', 'home_score': None, 'match_id': 7267856,50     'start_date': datetime(2020, 11, 18, 18, 30)},51    {'away': 'Oilers', 'away_score': None, 'home': 'SIL', 'home_score': None, 'match_id': 7267747,52     'start_date': datetime(2020, 12, 8, 19, 0)}]53def test_filter_date():54    result = scoreboard.filter_datetime(TEST_MATCHES, start=datetime(1977, 4, 29))55    assert isinstance(result, list)56    assert len(result) == 757def test_filter_datetime_for_future():58    future = scoreboard.filter_datetime(TEST_MATCHES, start=datetime(2020, 12, 8))59    assert len(future) == 160    assert future[0] == TEST_MATCHES[-1]61def test_filter_datetime_for_yesterday():62    yesterday = scoreboard.filter_datetime(TEST_MATCHES, start=datetime(2020, 11, 18, 0, 0), end=datetime(2020, 11, 18, 23, 59))63    assert len(yesterday) == 164    assert yesterday[0] == TEST_MATCHES[-2]65def test_filter_datetime_for_yesterday_with_no_time_set():66    yesterday = scoreboard.filter_datetime(TEST_MATCHES,67                                           start=datetime(2020, 11, 18),68                                           end=datetime(2020, 11, 18))69    assert len(yesterday) == 170    assert yesterday[0] == TEST_MATCHES[-2]71def test_filter_datetime_day_in_past():72    october14 = scoreboard.filter_datetime(TEST_MATCHES, day=datetime(2020, 11, 14))73    assert len(october14) == 374    assert TEST_MATCHES[0] in october1475    assert TEST_MATCHES[1] in october1476    assert TEST_MATCHES[2] in october1477def test_filter_today():78    matches = [79        {'away': 'SIL', 'away_score': 3, 'home': 'NH', 'home_score': 0, 'match_id': 7267711,80         'start_date': datetime(2020, 11, 14, 16, 0)},81        {'away': 'Grüner', 'away_score': 1, 'home': 'Stjernen Elite', 'home_score': 7, 'match_id': 42,82         'start_date': datetime.now()}]83    result = scoreboard.today(matches)84    assert isinstance(result, list)85    assert len(result) == 186    assert result[0]['match_id'] == 4287def test_filter_past():88    matches = [89        {'away': 'SIL', 'away_score': 3, 'home': 'NH', 'home_score': 0, 'match_id': 1,90         'start_date': datetime(2020, 11, 14, 16, 0)},91        {'away': 'Grüner', 'away_score': 1, 'home': 'Stjernen Elite', 'home_score': 7, 'match_id': 42,92         'start_date': datetime.now()}]93    result = scoreboard.past(matches)94    assert isinstance(result, list)95    assert len(result) == 196    assert result[0]['match_id'] == 197def test_filter_future():98    matches = [99        {'away': 'SIL', 'away_score': 3, 'home': 'NH', 'home_score': 0, 'match_id': 1,100         'start_date': datetime(2099, 11, 14, 16, 0)},101        {'away': 'Grüner', 'away_score': 1, 'home': 'Stjernen Elite', 'home_score': 7, 'match_id': 42,102         'start_date': datetime.now()}]103    result = scoreboard.future(matches)104    assert isinstance(result, list)105    assert len(result) == 1106    assert result[0]['match_id'] == 1107def test_transform():108    result = scoreboard.transform(TEST_DATA)109    assert isinstance(result, list)110    assert len(result) == 3111    assert result[0]['home'] == 'Stavanger Ishockeyklubb'112    assert result[0]['away'] == 'Sparta Elite'113    assert result[0]['home_score'] == 5114    assert result[0]['away_score'] == 4115    assert isinstance(result[0]['start_date'], datetime)116    assert result[1]['start_date'] == datetime(year=2020, month=10, day=22, hour=18, minute=30)117    assert result[2]['match_id'] == 7267710118def test_build_url():...test_league.py
Source:test_league.py  
1import unittest2import json3from src.app.process_data.league import LEC4from src.app.process_data.match import Match5from src.tests.test_data import matches_one_day, matches_two_days, head_to_head, wins_in_second_half6class TestLeague(unittest.TestCase):7    def test_create_standings_one_day(self):8        test_matches = []9        for match in matches_one_day:10            test_matches.append(Match(match['teams'], match['week'], match['result']))11        lec = LEC.from_matches(test_matches)12        lec.create_standings()13        self.assertIn('XL', lec.standings[1])14        self.assertIn('VIT', lec.standings[1])15        self.assertIn('S04', lec.standings[1])16        self.assertIn('RGE', lec.standings[1])17        self.assertIn('MAD', lec.standings[1])18        self.assertIn('SK', lec.standings[6])19        self.assertIn('OG', lec.standings[6])20        self.assertIn('G2', lec.standings[6])21        self.assertIn('MSF', lec.standings[6])22        self.assertIn('FNC', lec.standings[6])23    def test_create_standings_two_days(self):24        test_matches = []25        for match in matches_two_days:26            test_matches.append(Match(match['teams'], match['week'], match['result']))27        lec = LEC.from_matches(test_matches)28        lec.create_standings()29        self.assertIn('XL', lec.standings[3])30        self.assertIn('VIT', lec.standings[5])31        self.assertIn('S04', lec.standings[7])32        self.assertIn('RGE', lec.standings[1])33        self.assertIn('MAD', lec.standings[1])34        self.assertIn('SK', lec.standings[5])35        self.assertIn('OG', lec.standings[7])36        self.assertIn('G2', lec.standings[9])37        self.assertIn('MSF', lec.standings[9])38        self.assertIn('FNC', lec.standings[3])39    def test_create_standings_season(self):40        matches = Match.from_json('src/tests/lec_test.json')41        lec = LEC.from_matches(matches)42        lec.create_standings()43        self.assertIn('XL', lec.standings[6])44        self.assertIn('VIT', lec.standings[8])45        self.assertIn('S04', lec.standings[10])46        self.assertIn('RGE', lec.standings[2])47        self.assertIn('MAD', lec.standings[1])48        self.assertIn('SK', lec.standings[3])49        self.assertIn('OG', lec.standings[9])50        self.assertIn('G2', lec.standings[4])51        self.assertIn('MSF', lec.standings[7])52        self.assertIn('FNC', lec.standings[5])53    def test_create_table_one_day(self):54        test_matches = []55        for match in matches_one_day:56            test_matches.append(Match(match['teams'], match['week'], match['result']))57        lec = LEC.from_matches(test_matches)58        lec.create_table()59        self.assertEqual(lec.table['XL'], 1)60        self.assertEqual(lec.table['VIT'], 1)61        self.assertEqual(lec.table['S04'], 1)62        self.assertEqual(lec.table['RGE'], 1)63        self.assertEqual(lec.table['MAD'], 1)64    def test_create_table_two_days(self):65        test_matches = []66        for match in matches_two_days:67            test_matches.append(Match(match['teams'], match['week'], match['result']))68        lec = LEC.from_matches(test_matches)69        lec.create_table()70        self.assertEqual(lec.table['RGE'], 2)71        self.assertEqual(lec.table['MAD'], 2)72        self.assertEqual(lec.table['XL'], 1)73        self.assertEqual(lec.table['VIT'], 1)74        self.assertEqual(lec.table['S04'], 1)75        self.assertEqual(lec.table['OG'], 1)76        self.assertEqual(lec.table['FNC'], 1)77        self.assertEqual(lec.table['SK'], 1)78    def test_create_table_season(self):79        matches = Match.from_json('src/tests/lec_test.json')80        lec = LEC.from_matches(matches)81        lec.create_table()82        self.assertEqual(lec.table['RGE'], 11)83        self.assertEqual(lec.table['MAD'], 11)84        self.assertEqual(lec.table['XL'], 7)85        self.assertEqual(lec.table['VIT'], 6)86        self.assertEqual(lec.table['S04'], 5)87        self.assertEqual(lec.table['OG'], 6)88        self.assertEqual(lec.table['FNC'], 7)89        self.assertEqual(lec.table['SK'], 8)90        self.assertEqual(lec.table['G2'], 8)91        self.assertEqual(lec.table['MSF'], 6)92    def test_head_to_head(self):93        test_matches = []94        for match in head_to_head:95            test_matches.append(Match(match['teams'], match['week'], match['result']))96        lec = LEC.from_matches(test_matches)97        lec.create_standings()98        self.assertIn('OG', lec.standings[1])99        self.assertIn('G2', lec.standings[1])100        self.assertIn('FNC', lec.standings[1])101    def test_wins_in_second_half(self):102        test_matches = []103        for match in wins_in_second_half:104            test_matches.append(Match(match['teams'], match['week'], match['result']))105        lec = LEC.from_matches(test_matches)106        lec.create_standings()107        self.assertIn('FNC', lec.standings[1])108        self.assertIn('OG', lec.standings[2])...testspindex.py
Source:testspindex.py  
...12def test_build(spindex):13    for i,(x,y) in boxes:14        spindex.insert(i, [x,y,x+1,y+1])15    print('built')16def test_matches(spindex, matchbox):17    matches = list(spindex.intersects(matchbox))18    print('matches', len(matches))19################20d = pg.VectorData('data/ne_10m_admin_0_countries.shp')21matchbox = [1,1,20,20]22d.create_spatial_index()23print('default',d.spindex)24pg.vector.data.DEFAULT_SPATIAL_INDEX = 'rtree'25d.create_spatial_index()26print('default rtree',d.spindex)27print(len(list(d.quick_overlap(matchbox))))28pg.vector.data.DEFAULT_SPATIAL_INDEX = 'quadtree'29d.create_spatial_index()30print('default quadtree',d.spindex)31print(len(list(d.quick_overlap(matchbox))))32d.create_spatial_index('rtree')33print('specify rtree',d.spindex)34print(len(list(d.quick_overlap(matchbox))))35d.create_spatial_index('quadtree')36print('specify quadtree',d.spindex)37print(len(list(d.quick_overlap(matchbox))))38################39n = 1000040boxes = [(i, (uniform(-180,180),uniform(-90,90)) ) for i in range(n)]41matchbox = [1,1,20,20]42print('rtree')43spindex = test_rtree_init(None)44test_build(spindex)45test_matches(spindex, matchbox)46print('rtree, rtree backend')47spindex = test_rtree_init('rtree')48test_build(spindex)49test_matches(spindex, matchbox)50print('rtree, pyrtree backend')51spindex = test_rtree_init('pyrtree')52test_build(spindex)53test_matches(spindex, matchbox)54print('quadtree')55spindex = test_quadtree_init(None, bbox=[-180,-90,180,90])56test_build(spindex)57test_matches(spindex, matchbox)58print('quadtree, pyqtree backend')59spindex = test_quadtree_init('pyqtree', bbox=[-180,-90,180,90])60test_build(spindex)...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!!
