Best Python code snippet using autotest_python
test_dependency_grapher.py
Source:test_dependency_grapher.py
1from unittest import TestCase, mock2from servicer.dependency_grapher import DependencyGrapher3class DependencyGrapherTest(TestCase):4 def setUp(self):5 self.logger = mock.Mock()6 self.dependency_grapher = DependencyGrapher(7 config={8 'args': {'ignore_dependencies': False},9 'graph': {'implicit-step-dependencies': True},10 'services': {11 'service_1': {12 'name': 'service_1',13 'steps': {'build': {}, 'test': {}},14 },15 'service_2': {16 'name': 'service_2',17 'steps': {'build': {}, 'test': {}, 'deploy': {}},18 },19 },20 },21 active_services=[22 'service_1',23 'service_2',24 ],25 steps={26 'build': {'config': {}},27 'test': {'config': {}},28 'deploy': {'config': {}},29 },30 step_order=[31 'build',32 'test',33 'deploy',34 ],35 active_steps=[36 'build',37 'test',38 'deploy',39 ],40 logger=self.logger,41 )42 self.dependency_grapher.toposort2 = mock.Mock()43class DependencyGrapherTest(DependencyGrapherTest):44 def test_initialized(self):45 pass46class OrderServiceStepsTest(DependencyGrapherTest):47 def setUp(self):48 super().setUp()49 self.dependency_grapher.add_dependencies = mock.Mock()50 self.dependency_grapher.toposort2 = mock.Mock(return_value=[51 ['service_1:build', 'service_2:build'],52 ['service_1:test', 'service_2:test'],53 ])54 def test_returns_a_service_step_order(self):55 result = self.dependency_grapher.order_service_steps(self.dependency_grapher.config['services'].keys())56 self.assertEqual(self.dependency_grapher.add_dependencies.mock_calls, [57 mock.call({}, 'service_1:build', True),58 mock.call({}, 'service_1:test', True),59 mock.call({}, 'service_2:build', True),60 mock.call({}, 'service_2:test', True),61 mock.call({}, 'service_2:deploy', True),62 ])63 self.dependency_grapher.toposort2.assert_called_with({})64 self.assertEqual(result, [65 ['service_1:build', 'service_2:build'],66 ['service_1:test', 'service_2:test'],67 ])68 def test_returns_an_empty_service_step_order(self):69 self.dependency_grapher.toposort2 = mock.Mock(return_value=[])70 result = self.dependency_grapher.order_service_steps([])71 self.dependency_grapher.toposort2.assert_called_with({})72 self.assertEqual(result, [])73 def test_removes_orphaned_dependencies(self):74 def mock_add_dependencies(dependencies, service_step_name, follow_dependencies=True):75 dependencies['service_1:test'] = set(['service_1:build'])76 self.dependency_grapher.add_dependencies = mock.Mock(side_effect=mock_add_dependencies)77 self.dependency_grapher.config['args']['ignore_dependencies'] = True78 result = self.dependency_grapher.order_service_steps(self.dependency_grapher.config['services'].keys())79 self.assertEqual(self.dependency_grapher.add_dependencies.mock_calls, [80 mock.call({'service_1:test': set()}, 'service_1:build', False),81 mock.call({'service_1:test': set()}, 'service_1:test', False),82 mock.call({'service_1:test': set()}, 'service_2:build', False),83 mock.call({'service_1:test': set()}, 'service_2:test', False),84 mock.call({'service_1:test': set()}, 'service_2:deploy', False),85 ])86 self.dependency_grapher.toposort2.assert_called_with({'service_1:test': set()})87 self.assertEqual(result, [88 ['service_1:build', 'service_2:build'],89 ['service_1:test', 'service_2:test'],90 ])91class ServiceStepDependsOnTest(DependencyGrapherTest):92 def setUp(self):93 super().setUp()94 self.dependency_grapher.get_depends_on = mock.Mock(return_value=[])95 self.dependencies = {}96 def test_empty_depends_on(self):97 result = self.dependency_grapher.service_step_depends_on('service_1:build')98 self.assertEqual(self.dependency_grapher.get_depends_on.mock_calls, [99 mock.call({'name': 'service_1', 'steps': {'build': {}, 'test': {}}}),100 mock.call({}),101 ])102 self.assertEqual(result, [])103 def test_depends_on_from_service_config(self):104 self.dependency_grapher.get_depends_on.side_effect = [105 ['service_2:build', 'service_3:build'],106 [],107 ]108 result = self.dependency_grapher.service_step_depends_on('service_1:build')109 self.assertEqual(result, ['service_2:build', 'service_3:build'])110 def test_depends_on_from_service_step_config(self):111 self.dependency_grapher.get_depends_on.side_effect = [112 [],113 ['service_2:test', 'service_3:test'],114 ]115 result = self.dependency_grapher.service_step_depends_on('service_1:build')116 self.assertEqual(result, ['service_2:test', 'service_3:test'])117 def test_depends_on_from_implicit_dependencies(self):118 result = self.dependency_grapher.service_step_depends_on('service_1:test')119 self.assertEqual(result, ['service_1:build'])120 def test_depends_on_with_no_implicit_dependencies(self):121 self.dependency_grapher.config['graph']['implicit-step-dependencies'] = False122 result = self.dependency_grapher.service_step_depends_on('service_1:test')123 self.assertEqual(result, [])124 def test_depends_on_chooses_automatic_step(self):125 self.dependency_grapher.config['services']['service_3'] = {}126 self.dependency_grapher.get_depends_on.side_effect = [127 ['service_2'],128 ['service_3'],129 ]130 result = self.dependency_grapher.service_step_depends_on('service_1:build')131 self.assertEqual(result, ['service_2:build'])132class GetDependsOnTest(DependencyGrapherTest):133 def setUp(self):134 super().setUp()135 def test_returns_empty_list_if_no_depends_on(self):136 result = self.dependency_grapher.get_depends_on({})137 self.assertEqual(result, [])138 def test_returns_list_with_depends_on_as_list(self):139 result = self.dependency_grapher.get_depends_on({'depends_on': ['blackberries', 'blueberries']})140 self.assertEqual(result, ['blackberries', 'blueberries'])141 def test_returns_list_with_depends_on_as_value(self):142 result = self.dependency_grapher.get_depends_on({'depends_on': 'raspberries'})143 self.assertEqual(result, ['raspberries'])144class AddDependenciesTest(DependencyGrapherTest):145 def setUp(self):146 super().setUp()147 self.dependency_grapher.service_step_depends_on = mock.Mock(return_value=[])148 self.dependency_grapher.add_dependency = mock.Mock()149 self.dependency_grapher.config['services'] = {150 'service_1': {151 'name': 'service_1',152 'steps': {'build': {}, 'test': {}},153 },154 'service_2': {155 'name': 'service_2',156 'steps': {'build': {}, 'test': {}, 'deploy': {}},157 },158 'service_3': {159 'name': 'service_3',160 'steps': {'build': {}, 'test': {}},161 },162 'service_4': {163 'name': 'service_4',164 'steps': {'build': {}, 'deploy': {}},165 },166 }167 self.dependency_grapher.active_services = ['service_1', 'service_2']168 self.dependencies = {}169 def test_skips_if_an_entry_already_exists(self):170 self.dependencies = {'service_1:build': set(['strawberry', 'mango', 'banana'])}171 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')172 self.dependency_grapher.service_step_depends_on.assert_not_called()173 self.dependency_grapher.add_dependency.assert_not_called()174 self.assertEqual(self.dependencies, {175 'service_1:build': set(['strawberry', 'mango', 'banana']),176 })177 def test_adds_an_empty_set_with_no_dependencies(self):178 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')179 self.assertEqual(self.dependency_grapher.service_step_depends_on.mock_calls, [180 mock.call('service_1:build'),181 ])182 self.dependency_grapher.add_dependency.assert_not_called()183 self.assertEqual(self.dependencies, {184 'service_1:build': set(),185 })186 def test_adds_a_service_step_with_dependencies(self):187 self.dependency_grapher.service_step_depends_on.return_value = [188 'service_2:build',189 'service_3:build',190 ]191 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')192 self.assertEqual(self.dependency_grapher.add_dependency.mock_calls, [193 mock.call({'service_1:build': set()}, 'service_1:build', 'service_2', 'build', True),194 mock.call({'service_1:build': set()}, 'service_1:build', 'service_3', 'build', True),195 ])196 def test_adds_a_soft_service_wildcard_dependency(self):197 self.dependency_grapher.service_step_depends_on.return_value = [198 '*:build',199 ]200 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')201 self.assertEqual(self.dependency_grapher.add_dependency.mock_calls, [202 mock.call({'service_1:build': set()}, 'service_1:build', '*', 'build', False),203 ])204 def test_adds_a_hard_service_wildcard_dependency(self):205 self.dependency_grapher.service_step_depends_on.return_value = [206 '*:build!',207 ]208 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')209 self.assertEqual(self.dependency_grapher.add_dependency.mock_calls, [210 mock.call({'service_1:build': set()}, 'service_1:build', '*', 'build!', False),211 ])212 def test_throws_an_error_for_an_invalid_service_dependency(self):213 self.dependency_grapher.service_step_depends_on.return_value = [214 'service_9000:build',215 ]216 with self.assertRaises(ValueError) as context:217 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')218 self.dependency_grapher.add_dependency.assert_not_called()219 self.assertTrue('Invalid service dependency specified: service_9000:build, "service_9000" must be included in services: [service_1,service_2,service_3,service_4]' in str(context.exception))220 def test_throws_an_error_for_an_invalid_step_dependency(self):221 self.dependency_grapher.service_step_depends_on.return_value = [222 'service_2:party',223 ]224 with self.assertRaises(ValueError) as context:225 result = self.dependency_grapher.add_dependencies(self.dependencies, 'service_1:build')226 self.dependency_grapher.add_dependency.assert_not_called()227 self.assertTrue('Invalid step dependency specified: service_2:party, "party" must be included in steps: [build,test,deploy]' in str(context.exception))228class AddDependencyTest(DependencyGrapherTest):229 def setUp(self):230 super().setUp()231 self.dependency_grapher.add_dependencies = mock.Mock()232 self.dependency_grapher.active_services = ['service_1', 'service_2']233 self.dependencies = {'service_1:build': set()}234 def test_adds_a_dependency_without_following(self):235 result = self.dependency_grapher.add_dependency(self.dependencies, 'service_1:build', 'service_2', 'build', False)236 self.dependency_grapher.add_dependencies.assert_not_called()237 self.assertEqual(self.dependencies, {238 'service_1:build': set(['service_2:build']),239 })240 def test_adds_a_dependency_with_following(self):241 result = self.dependency_grapher.add_dependency(self.dependencies, 'service_1:build', 'service_2', 'build', True)242 self.dependency_grapher.add_dependencies.assert_called_with({'service_1:build': {'service_2:build'}}, 'service_2:build', True)243 self.assertEqual(self.dependencies, {244 'service_1:build': set(['service_2:build']),...
test_team.py
Source:test_team.py
1import pytest2from scoring_engine.models.team import Team3from scoring_engine.models.user import User4from scoring_engine.models.service import Service5from scoring_engine.models.check import Check6from tests.scoring_engine.helpers import generate_sample_model_tree, populate_sample_data7from tests.scoring_engine.unit_test import UnitTest8class TestTeam(UnitTest):9 def test_init_whiteteam(self):10 team = Team(name="White Team", color="White")11 assert team.name == "White Team"12 assert team.color == "White"13 assert team.id is None14 assert team.current_score == 015 assert team.is_red_team is False16 assert team.is_white_team is True17 assert team.is_blue_team is False18 def test_init_blueteam(self):19 team = Team(name="Blue Team", color="Blue")20 assert team.name == "Blue Team"21 assert team.color == "Blue"22 assert team.id is None23 assert team.current_score == 024 assert team.is_red_team is False25 assert team.is_white_team is False26 assert team.is_blue_team is True27 def test_init_redteam(self):28 team = Team(name="Red Team", color="Red")29 assert team.name == "Red Team"30 assert team.color == "Red"31 assert team.id is None32 assert team.current_score == 033 assert team.is_red_team is True34 assert team.is_white_team is False35 assert team.is_blue_team is False36 def test_simple_save(self):37 white_team = Team(name="White Team", color="White")38 self.session.add(white_team)39 self.session.commit()40 assert white_team.id is not None41 assert len(self.session.query(Team).all()) == 142 def test_multiple_saves(self):43 white_team = Team(name="White Team", color="White")44 self.session.add(white_team)45 blue_team = Team(name="Blue", color="Blue")46 self.session.add(blue_team)47 self.session.commit()48 assert len(self.session.query(Team).all()) == 249 def test_services(self):50 team = generate_sample_model_tree('Team', self.session)51 service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1')52 service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", host='2.3.4.5')53 self.session.add(service_1)54 self.session.add(service_2)55 self.session.commit()56 assert team.services == [service_1, service_2]57 def test_users(self):58 team = generate_sample_model_tree('Team', self.session)59 user_1 = User(username="testuser", password="testpass", team=team)60 user_2 = User(username="abcuser", password="abcpass", team=team)61 self.session.add(user_1)62 self.session.add(user_2)63 self.session.commit()64 assert team.users == [user_2, user_1] # TODO - Figure out why this is flipped65 def test_current_score(self):66 team = generate_sample_model_tree('Team', self.session)67 service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1')68 self.session.add(service_1)69 check_1 = Check(service=service_1, result=True, output='Good output')70 self.session.add(check_1)71 service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", host='127.0.0.2')72 self.session.add(service_2)73 check_2 = Check(service=service_2, result=True, output='Good output')74 self.session.add(check_2)75 check_3 = Check(service=service_2, result=True, output='Good output')76 self.session.add(check_3)77 service_3 = Service(name="Example Service 3", team=team, check_name="SSH IPv4 Check", host='127.0.0.3')78 self.session.add(service_3)79 check_3 = Check(service=service_3, result=False, output='bad output')80 self.session.add(check_3)81 self.session.commit()82 assert team.current_score == 30083 def test_place(self):84 team_1 = Team(name="Blue Team 1", color="Blue")85 self.session.add(team_1)86 service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", host='127.0.0.1')87 self.session.add(service_1)88 check_1 = Check(service=service_1, result=True, output='Good output')89 check_2 = Check(service=service_1, result=True, output='Good output')90 self.session.add(check_1)91 self.session.add(check_2)92 self.session.commit()93 team_2 = Team(name="Blue Team 2", color="Blue")94 self.session.add(team_2)95 service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", host='127.0.0.1')96 self.session.add(service_1)97 check_1 = Check(service=service_1, result=True, output='Good output')98 check_2 = Check(service=service_1, result=True, output='Good output')99 self.session.add(check_1)100 self.session.add(check_2)101 self.session.commit()102 team_3 = Team(name="Blue Team 3", color="Blue")103 self.session.add(team_3)104 service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", host='127.0.0.1')105 self.session.add(service_1)106 check_1 = Check(service=service_1, result=True, output='Good output')107 check_2 = Check(service=service_1, result=False, output='Good output')108 self.session.add(check_1)109 self.session.add(check_2)110 self.session.commit()111 assert team_1.place == 1112 assert team_2.place == 1113 assert team_3.place == 3114 def test_get_array_of_scores(self):115 populate_sample_data(self.session)116 results = Team.get_all_rounds_results()117 assert 'rounds' in results118 assert results['rounds'] == ['Round 0', 'Round 1', 'Round 2']119 assert 'rgb_colors' in results120 assert 'Blue Team 1' in results['rgb_colors']121 assert results['rgb_colors']['Blue Team 1'].startswith('rgba')122 assert 'scores' in results123 assert results['scores'] == {'Blue Team 1': [0, 100, 100]}124 def test_get_round_scores(self):125 team = populate_sample_data(self.session)126 assert team.get_round_scores(0) == 0127 assert team.get_round_scores(1) == 100128 assert team.get_round_scores(2) == 0129 # with pytest.raises(IndexError):...
bl.py
Source:bl.py
1import da2from atlas_manager import execute_service3def get_things():4 return {5 'code': 200,6 'result': da.get_all_things()7 }8def get_all_services():9 return {10 'code': 200,11 'result': da.get_all_services()12 }13def get_services(thing_id):14 return {15 'code': 200,16 'result': da.get_services_of_thing(thing_id)17 }18def get_relationships():19 return{20 'code': 200,21 'result': da.get_all_relationships()22 }23def create_relationship(data):24 if 'name' not in data or 'desc' not in data or 'service1' not in data or 'service2' not in data:25 return {26 'code': 400,27 'msg': 'Name, Description, or Services not provided'28 }29 name = data['name']30 desc = data['desc']31 service_1 = data['service1']32 service_2 = data['service2']33 if 'icon' in data:34 icon = data['icon']35 else:36 icon = ''37 result = da.create_relationship(38 name=name, icon=icon, desc=desc, service_1=service_1, service_2=service_2)39 if result:40 return {41 'code': 201,42 'msg': 'Relationship created'43 }44 else:45 return {46 'code': 500,47 'msg': 'Server error. It is not you, it is us.'48 }49def create_thing(data):50 if 'id' not in data or 'name' not in data or 'desc' not in data or 'space' not in data or 'ip' not in data:51 return {52 'code': 400,53 'msg': 'ID, Name, Description, IP address or Space not provided'54 }55 id = data['id']56 name = data['name']57 desc = data['desc']58 space = data['space']59 ip = data['ip']60 if 'icon' in data:61 icon = data['icon']62 else:63 icon = ''64 result = da.create_thing(65 id=id, name=name, icon=icon, desc=desc, space=space, ip=ip)66 if result:67 return {68 'code': 201,69 'msg': 'Thing created'70 }71 else:72 return {73 'code': 500,74 'msg': 'Server error. It is not you, it is us.'75 }76def create_service(data):77 if 'thing' not in data or 'name' not in data or 'entity' not in data or 'space' not in data:78 return {79 'code': 400,80 'msg': 'Thing, Name, Entity or Space not provided'81 }82 thing = data['thing']83 name = data['name']84 entity = data['entity']85 space = data['space']86 if 'icon' in data:87 icon = data['icon']88 else:89 icon = ''90 result = da.create_service(91 thing=thing, name=name, icon=icon, entity=entity, space=space)92 if result:93 return {94 'code': 201,95 'msg': 'Service {} created'.format(name)96 }97 else:98 return {99 'code': 500,100 'msg': 'Server error. It is not you, it is us.'101 }102def create_recipe(data):103 if 'name' not in data or 'relationships' not in data or 'relationships' not in data:104 return {105 'code': 400,106 'msg': 'Name or relationships not provided'107 }108 name = data['name']109 id = name110 relationships = data['relationships']111 if 'icon' in data:112 icon = data['icon']113 else:114 icon = ''115 result = da.create_recipe(116 id=id, name=name, icon=icon, relationships=relationships)117 if result:118 return {119 'code': 201,120 'msg': 'Recipe {} created'.format(name)121 }122 else:123 return {124 'code': 500,125 'msg': 'Server error. It is not you, it is us.'126 }127def get_recipes():128 return{129 'code': 200,130 'result': da.get_recipes()131 }132def run_recipe(recipe_id):133 recipe = da.get_recipe(recipe_id)134 recipe_results = {135 'recipe': recipe,136 'result': []137 }138 for relationship in recipe['relationships']:139 relationship_results = {140 'relationship': relationship,141 'result': []142 }143 result_1 = execute_service(relationship['service1'], None)144 result_2 = execute_service(relationship['service2'], result_1)145 relationship_results['result'].append({146 'service': relationship['service1'],147 'result': result_1148 })149 relationship_results['result'].append({150 'service': relationship['service2'],151 'result': result_2152 })153 recipe_results['result'].append(relationship_results)154 return {155 'code': 200,156 'msg': 'Actions performed',157 'result': recipe_results158 }159def enable_disable_recipe(recipe_id):160 result = da.enable_disable_recipe(recipe_id)161 if result:162 return {163 'code': 200,164 'msg': 'Recipe enabled/disabled',165 }166 else:167 return {168 'code': 500,169 'msg': 'Server error. It is not you, it is us.'170 }171def delete_recipe(recipe_id):172 result = da.delete_recipe(recipe_id)173 if result:174 return {175 'code': 200,176 'msg': 'Recipe deleted'177 }178 else:179 return {180 'code': 500,181 'msg': 'Server error. It is not you, it is us.'182 }183def import_recipe(data):184 try:185 relationships = data['relationships']186 for relationship in relationships:187 service_1 = relationship['service1']188 service_2 = relationship['service2']189 da.create_relationship(name=relationship['name'], desc=relationship['desc'], service_1=service_1['name'], service_2=service_2['name'])190 da.create_service(thing=service_1['thing']['id'], name=service_1['name'], entity=service_1['entity'], space=service_1['space'], icon=service_1['icon'])191 da.create_service(thing=service_2['thing']['id'], name=service_2['name'], entity=service_2['entity'], space=service_2['space'], icon=service_2['icon'])192 193 da.create_recipe(id=data['id'], name=data['name'], relationships=[relationship['name'] for relationship in relationships], icon=data['icon'], enabled=False)194 return {195 'code': 200,196 'msg': 'Recipe imported'197 }198 except:199 return {200 'code': 500,201 'msg': 'Error importing'...
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!!