<s> from __future__ import absolute_import <EOL> import pytest <EOL> from mock import patch <EOL> from changes . constants import Status , Result <EOL> from changes . expanders . tests import TestsExpander <EOL> from changes . testutils import TestCase <EOL> class TestsExpanderTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestsExpanderTest , self ) . setUp ( ) <EOL> self . project = self . create_project ( ) <EOL> def get_expander ( self , data ) : <EOL> return TestsExpander ( self . project , data ) <EOL> def test_validate ( self ) : <EOL> with pytest . raises ( AssertionError ) : <EOL> self . get_expander ( { } ) . validate ( ) <EOL> with pytest . raises ( AssertionError ) : <EOL> self . get_expander ( { '<STR_LIT>' : [ ] } ) . validate ( ) <EOL> with pytest . raises ( AssertionError ) : <EOL> self . get_expander ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } ) . validate ( ) <EOL> self . get_expander ( { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) . validate ( ) <EOL> def test_get_test_stats ( self ) : <EOL> build = self . create_build ( <EOL> project = self . project , <EOL> status = Status . finished , <EOL> result = Result . passed , <EOL> ) <EOL> job = self . create_job ( build ) <EOL> self . create_test ( job , name = '<STR_LIT>' , duration = <NUM_LIT:50> ) <EOL> self . create_test ( job , name = '<STR_LIT>' , duration = <NUM_LIT> ) <EOL> expander = self . get_expander ( { } ) <EOL> results , avg_time = expander . get_test_stats ( self . project . slug ) <EOL> assert avg_time == <NUM_LIT> <EOL> assert results [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) ] == <NUM_LIT> <EOL> assert results [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ] == <NUM_LIT:50> <EOL> assert results [ ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ] == <NUM_LIT> <EOL> def test_sharding ( self ) : <EOL> tests = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> test_weights = { <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) : <NUM_LIT:50> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT>' ) : <NUM_LIT:15> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) : <NUM_LIT:10> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) : <NUM_LIT:200> , <EOL> } <EOL> avg_test_time = sum ( test_weights . values ( ) ) / len ( test_weights ) <EOL> groups = TestsExpander . shard_tests ( tests , <NUM_LIT:2> , test_weights , avg_test_time ) <EOL> assert len ( groups ) == <NUM_LIT:2> <EOL> groups . sort ( ) <EOL> assert groups [ <NUM_LIT:0> ] == ( <NUM_LIT> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert groups [ <NUM_LIT:1> ] == ( <NUM_LIT> , [ '<STR_LIT>' ] ) <EOL> groups = TestsExpander . shard_tests ( tests , <NUM_LIT:3> , test_weights , avg_test_time ) <EOL> assert len ( groups ) == <NUM_LIT:3> <EOL> groups . sort ( ) <EOL> assert groups [ <NUM_LIT:0> ] == ( <NUM_LIT> , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert groups [ <NUM_LIT:1> ] == ( <NUM_LIT> , [ '<STR_LIT>' ] ) <EOL> assert groups [ <NUM_LIT:2> ] == ( <NUM_LIT> , [ '<STR_LIT>' ] ) <EOL> groups = TestsExpander . shard_tests ( tests , len ( tests ) * <NUM_LIT:2> , test_weights , avg_test_time ) <EOL> assert len ( groups ) == len ( tests ) <EOL> @ patch . object ( TestsExpander , '<STR_LIT>' ) <EOL> def test_expand ( self , mock_get_test_stats ) : <EOL> mock_get_test_stats . return_value = { <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) : <NUM_LIT:50> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT>' ) : <NUM_LIT:15> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) : <NUM_LIT:10> , <EOL> ( '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ) : <NUM_LIT:200> , <EOL> } , <NUM_LIT> <EOL> results = list ( self . get_expander ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> } ) . expand ( max_executors = <NUM_LIT:2> ) ) <EOL> results . sort ( key = lambda x : x . data [ '<STR_LIT>' ] , reverse = True ) <EOL> assert len ( results ) == <NUM_LIT:2> <EOL> assert results [ <NUM_LIT:0> ] . label == '<STR_LIT>' <EOL> assert results [ <NUM_LIT:0> ] . data [ '<STR_LIT>' ] == <NUM_LIT> <EOL> assert set ( results [ <NUM_LIT:0> ] . data [ '<STR_LIT>' ] ) == set ( [ '<STR_LIT>' ] ) <EOL> assert results [ <NUM_LIT:0> ] . data [ '<STR_LIT>' ] == <NUM_LIT:2> <EOL> assert results [ <NUM_LIT:0> ] . commands [ <NUM_LIT:0> ] . label == results [ <NUM_LIT:0> ] . label <EOL> assert results [ <NUM_LIT:0> ] . commands [ <NUM_LIT:0> ] . script == results [ <NUM_LIT:0> ] . label <EOL> assert results [ <NUM_LIT:1> ] . label == '<STR_LIT>' <EOL> assert results [ <NUM_LIT:1> ] . data [ '<STR_LIT>' ] == <NUM_LIT> <EOL> assert set ( results [ <NUM_LIT:1> ] . data [ '<STR_LIT>' ] ) == set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert results [ <NUM_LIT:1> ] . data [ '<STR_LIT>' ] == <NUM_LIT:2> <EOL> assert results [ <NUM_LIT:1> ] . commands [ <NUM_LIT:0> ] . label == results [ <NUM_LIT:1> ] . label <EOL> assert results [ <NUM_LIT:1> ] . commands [ <NUM_LIT:0> ] . script == results [ <NUM_LIT:1> ] . label </s>
