How to use rule_label method in hypothesis

Best Python code snippet using hypothesis

test_db_access.py

Source:test_db_access.py Github

copy

Full Screen

1import pytest2from controller import db_access3from unittest import mock4from controller.offline_db_access import get_db as mock_get_db5"""6All tests use the mock feature to replace get_db with the offline version.7This is because tests are run outside the Flask application. See db_access.py for more information.8All tests use the fixtures defined in conftest.py for setup and teardown.9"""10@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)11def test_create_policy(mock):12 # Should store a new policy entry in the database13 new_policy_uri = 'https://example.com#test'14 db_access.create_policy(new_policy_uri)15 assert db_access.policy_exists(new_policy_uri)16 # Should reject duplicate policies17 with pytest.raises(ValueError):18 db_access.create_policy(new_policy_uri)19@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)20def test_delete_policy(mock):21 # Should remove an existing policy22 policy_uri = 'https://example.com#policy'23 db_access.create_policy(policy_uri)24 db_access.delete_policy(policy_uri)25 assert not db_access.policy_exists(policy_uri)26@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)27def test_policy_exists(mock):28 # Should return true if the policy exists29 uri = 'https://example.com#test'30 db_access.create_policy(uri)31 assert db_access.policy_exists(uri)32 # Should return false if the policy doesn't exist33 nonexistent_uri = 'https://example.com#nonexistent'34 assert not db_access.policy_exists(nonexistent_uri)35@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)36def test_set_policy_attribute(mock):37 # Should set a valid attribute for an existing policy38 uri = 'https://example.com#test'39 new_policy_type = 'http://creativecommons.org/ns#License'40 db_access.create_policy(uri)41 db_access.set_policy_attribute(uri, 'TYPE', new_policy_type)42 stored_policy_type = db_access.query_db('SELECT TYPE FROM POLICY WHERE URI = ?', (uri,), one=True)[0]43 assert stored_policy_type == new_policy_type44 # Should reject attribute change for a policy that does not exist45 with pytest.raises(ValueError):46 db_access.set_policy_attribute('https://example.com#nonexistent', 'TYPE', new_policy_type)47 # Should reject changing attribute that is not permitted48 with pytest.raises(ValueError):49 db_access.set_policy_attribute(uri, 'NONEXISTENT', new_policy_type)50@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)51def test_get_policy(mock):52 # Should raise an exception when the policy doesn't exist53 policy_uri = 'https://example.com#policy'54 with pytest.raises(ValueError):55 db_access.get_policy(policy_uri)56 # Should get all the attributes of the policy57 db_access.create_policy(policy_uri)58 policy_type = 'http://creativecommons.org/ns#License'59 db_access.set_policy_attribute(policy_uri, 'TYPE', policy_type)60 rule_uri = 'http://example.com#rule'61 rule_type_uri = 'http://www.w3.org/ns/odrl/2/permission'62 rule_label = 'Rule'63 db_access.create_rule(rule_uri, rule_type_uri, rule_label)64 db_access.add_rule_to_policy(rule_uri, policy_uri)65 policy = db_access.get_policy(policy_uri)66 expected_policy_attributes = ['URI', 'TYPE', 'LABEL', 'JURISDICTION', 'LEGAL_CODE', 'HAS_VERSION', 'LANGUAGE',67 'SEE_ALSO', 'SAME_AS', 'COMMENT', 'LOGO', 'CREATED', 'STATUS', 'RULES']68 assert all(attr in policy for attr in expected_policy_attributes)69 assert policy['URI'] == policy_uri70 assert policy['TYPE'] == policy_type71 assert policy['RULES'] == [rule_uri]72@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)73def test_get_all_policies(mock):74 policy1_uri = 'https://example.com#policy1'75 policy2_uri = 'https://example.com#policy2'76 db_access.create_policy(policy1_uri)77 db_access.create_policy(policy2_uri)78 # Should retrieve every policy79 assert db_access.get_all_policies() == [policy1_uri, policy2_uri]80@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)81def test_policy_has_rule(mock):82 # Should return false if the policy does not have the rule83 policy_uri = 'https://example.com#policy'84 rule_uri = 'http://example.com#rule'85 assert not db_access.policy_has_rule(policy_uri, rule_uri)86 # Should return true if the policy has the rule87 db_access.create_policy(policy_uri)88 rule_type = 'http://www.w3.org/ns/odrl/2/permission'89 rule_label = 'Rule'90 db_access.create_rule(rule_uri, rule_type, rule_label)91 db_access.add_rule_to_policy(rule_uri, policy_uri)92 assert db_access.policy_has_rule(policy_uri, rule_uri)93@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)94def test_create_rule(mock):95 # Should raise an exception when the rule type is not valid96 rule_label = 'Rule'97 with pytest.raises(ValueError):98 db_access.create_rule('rule', 'invalid_type', rule_label)99 # Should add a rule100 rule_uri = 'http://example.com#rule'101 rule_type = 'http://www.w3.org/ns/odrl/2/permission'102 db_access.create_rule(rule_uri, rule_type, rule_label)103 assert db_access.rule_exists(rule_uri)104 # Should raise an exception when that rule already exists105 with pytest.raises(ValueError):106 db_access.create_rule(rule_uri, rule_type, rule_label)107@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)108def test_delete_rule(mock):109 # Should not be able to delete if the rule is still used in any policies110 policy_uri = 'https://example.com#policy'111 db_access.create_policy(policy_uri)112 rule_uri = 'http://example.com#rule'113 rule_type = 'http://www.w3.org/ns/odrl/2/permission'114 rule_label = 'Rule'115 db_access.create_rule(rule_uri, rule_type, rule_label)116 assignor_uri = 'http://example.com#assignor'117 db_access.create_party(assignor_uri)118 db_access.add_assignor_to_rule(assignor_uri, rule_uri)119 assignee_uri = 'http://example.com#assignee'120 db_access.create_party(assignee_uri)121 db_access.add_assignee_to_rule(assignee_uri, rule_uri)122 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'123 db_access.add_action_to_rule(action_uri, rule_uri)124 db_access.add_rule_to_policy(rule_uri, policy_uri)125 with pytest.raises(ValueError):126 db_access.delete_rule(rule_uri)127 # Should delete the rule128 db_access.remove_rule_from_policy(rule_uri, policy_uri)129 db_access.delete_rule(rule_uri)130 assert not db_access.rule_exists(rule_uri)131 # Should also remove any actions from the rule132 assert not db_access.rule_has_action(rule_uri, action_uri)133 # Should also remove any assignors from the rule134 assert not db_access.rule_has_assignor(assignor_uri, rule_uri)135 # Should also remove any assignees from the rule136 assert not db_access.rule_has_assignee(assignee_uri, rule_uri)137@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)138def test_get_rule(mock):139 # Should raise an exception when the rule doesn't exist140 rule_uri = 'http://example.com#rule'141 with pytest.raises(ValueError):142 db_access.get_rule(rule_uri)143 # Should get uri, type and label of the rule144 rule_type = 'http://www.w3.org/ns/odrl/2/permission'145 rule_label = 'Rule'146 db_access.create_rule(rule_uri, rule_type, rule_label)147 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'148 db_access.add_action_to_rule(action_uri, rule_uri)149 assignor_uri = 'https://example.com#assignor'150 assignee_uri = 'https://example.com#assignee'151 db_access.create_party(assignor_uri)152 db_access.create_party(assignee_uri)153 db_access.add_assignor_to_rule(assignor_uri, rule_uri)154 db_access.add_assignee_to_rule(assignee_uri, rule_uri)155 rule = db_access.get_rule(rule_uri)156 assert rule['URI'] == rule_uri157 assert rule['TYPE_URI'] == rule_type158 assert rule['TYPE_LABEL'] == 'Permission'159 assert rule['LABEL'] == rule_label160 # Should get actions associated with the rule161 action = rule['ACTIONS'][0]162 assert action['LABEL'] == 'Distribute'163 assert action['URI'] == action_uri164 assert action['DEFINITION'] == 'To supply the Asset to third-parties.'165 # Should get assignors and assignees associated with the rule166 assert rule['ASSIGNORS'] == [assignor_uri]167 assert rule['ASSIGNEES'] == [assignee_uri]168@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)169def test_add_rule_to_policy(mock):170 # Should raise an exception if the rule does not exist171 rule_uri = 'https://example.com#nonexistant'172 policy_uri = 'https://example.com#policy'173 db_access.create_policy(policy_uri)174 with pytest.raises(ValueError):175 db_access.add_rule_to_policy(rule_uri, policy_uri)176 # Should raise an exception if the policy does not exist177 rule_uri = 'https://example.com#rule'178 rule_type = 'http://www.w3.org/ns/odrl/2/permission'179 rule_label = 'Rule'180 db_access.create_rule(rule_uri, rule_type, rule_label)181 db_access.delete_policy(policy_uri)182 with pytest.raises(ValueError):183 db_access.add_rule_to_policy(rule_uri, policy_uri)184 # Should add a rule to a policy185 db_access.create_policy(policy_uri)186 db_access.add_rule_to_policy(rule_uri, policy_uri)187 query_str = 'SELECT COUNT(1) FROM POLICY_HAS_RULE WHERE RULE_URI = ? AND POLICY_URI = ?'188 rule_assigned = db_access.query_db(query_str, (rule_uri, policy_uri), one=True)[0]189 assert rule_assigned190 # Should raise an exception if the rule is already included in the policy191 with pytest.raises(ValueError):192 db_access.add_rule_to_policy(rule_uri, policy_uri)193@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)194def test_remove_rule_from_policy(mock):195 # Should remove a rule from a policy196 policy_uri = 'https://example.com#policy'197 db_access.create_policy(policy_uri)198 rule_uri = 'https://example.com#rule'199 rule_type = 'http://www.w3.org/ns/odrl/2/permission'200 rule_label = 'Rule'201 db_access.create_rule(rule_uri, rule_type, rule_label)202 db_access.add_rule_to_policy(rule_uri, policy_uri)203 db_access.remove_rule_from_policy(rule_uri, policy_uri)204 assert not db_access.policy_has_rule(policy_uri, rule_uri)205@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)206def test_get_rules_for_policy(mock):207 # Should return a list of URIs for all Rules assigned to a Policy208 policy_uri = 'https://example.com#policy'209 db_access.create_policy(policy_uri)210 rule_type = 'http://www.w3.org/ns/odrl/2/permission'211 rule_label = 'Rule'212 rule1_uri = 'https://example.com#rule1'213 rule2_uri = 'https://example.com#rule2'214 db_access.create_rule(rule1_uri, rule_type, rule_label)215 db_access.create_rule(rule2_uri, rule_type, rule_label)216 db_access.add_rule_to_policy(rule1_uri, policy_uri)217 db_access.add_rule_to_policy(rule2_uri, policy_uri)218 assert db_access.get_rules_for_policy(policy_uri) == [rule1_uri, rule2_uri]219 # Shouldn't include any other Rules220 rule3_uri = 'https://example.com#rule3'221 db_access.create_rule(rule3_uri, rule_type, rule_label)222 assert db_access.get_rules_for_policy(policy_uri) == [rule1_uri, rule2_uri]223@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)224def test_get_all_rules(mock):225 rule1 = 'https://example.com#rule1'226 rule2 = 'https://example.com#rule2'227 rule_type = 'http://www.w3.org/ns/odrl/2/permission'228 rule1_label = 'Rule1'229 rule2_label = 'Rule2'230 db_access.create_rule(rule1, rule_type, rule1_label)231 db_access.create_rule(rule2, rule_type, rule2_label)232 rules = db_access.get_all_rules()233 assert rules == [rule1, rule2]234@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)235def test_rule_exists(mock):236 # Should return false if the policy doesn't exist237 rule_uri = 'https://example.com#rule'238 assert not db_access.rule_exists(rule_uri)239 # Should return true if the party exists240 rule_type = 'http://www.w3.org/ns/odrl/2/permission'241 rule_label = 'Rule'242 db_access.create_rule(rule_uri, rule_type, rule_label)243 assert db_access.rule_exists(rule_uri)244@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)245def test_get_permitted_rule_types(mock):246 permitted_rule_types = db_access.get_permitted_rule_types()247 assert permitted_rule_types == [248 {'URI': 'http://www.w3.org/ns/odrl/2/permission', 'LABEL': 'Permission'},249 {'URI': 'http://www.w3.org/ns/odrl/2/prohibition', 'LABEL': 'Prohibition'},250 {'URI': 'http://www.w3.org/ns/odrl/2/duty', 'LABEL': 'Duty'}251 ]252@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)253def test_get_policies_for_rule(mock):254 rule_uri = 'https://example.com#rule'255 rule_type = 'http://www.w3.org/ns/odrl/2/permission'256 rule_label = 'Rule'257 db_access.create_rule(rule_uri, rule_type, rule_label)258 policy1 = 'https://example.com#policy1'259 db_access.create_policy(policy1)260 db_access.add_rule_to_policy(rule_uri, policy1)261 policy2 = 'https://example.com#policy2'262 db_access.create_policy(policy2)263 db_access.add_rule_to_policy(rule_uri, policy2)264 policies = db_access.get_policies_for_rule(rule_uri)265 assert {'URI': policy1, 'LABEL': None} in policies266 assert {'URI': policy2, 'LABEL': None} in policies267@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)268def test_action_exists(mock):269 # Should return false if the action doesn't exist270 action_uri = 'nonexistent'271 assert not db_access.action_exists(action_uri)272 # Should return true if the action exists273 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'274 assert db_access.action_exists(action_uri)275@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)276def test_get_action(mock):277 # Should raise an exception if the action does not exist278 action_uri = 'nonexistent'279 with pytest.raises(ValueError):280 db_access.get_action(action_uri)281 # Should return uri, label, and definition282 action_uri = 'http://www.w3.org/ns/odrl/2/acceptTracking'283 action = db_access.get_action(action_uri)284 assert action['LABEL'] == 'Accept Tracking'285 assert action['URI'] == 'http://www.w3.org/ns/odrl/2/acceptTracking'286 assert action['DEFINITION'] == 'To accept that the use of the Asset may be tracked.'287@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)288def test_get_all_actions(mock):289 actions = db_access.get_all_actions()290 assert actions291 for action in actions:292 assert all(x in ['URI', 'LABEL', 'DEFINITION'] for x in action)293@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)294def test_add_action_to_rule(mock):295 # Should raise an exception if the rule doesn't exist296 rule_uri = 'https://example.com#rule'297 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'298 with pytest.raises(ValueError):299 db_access.add_action_to_rule(action_uri, rule_uri)300 # Should raise an exception if the action doesn't exist301 rule_type = 'http://www.w3.org/ns/odrl/2/permission'302 rule_label = 'Rule'303 db_access.create_rule(rule_uri, rule_type, rule_label)304 action_uri = 'nonexistent'305 with pytest.raises(ValueError):306 db_access.add_action_to_rule(action_uri, rule_uri)307 # Should add an action to a rule308 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'309 db_access.add_action_to_rule(action_uri, rule_uri)310 assert db_access.rule_has_action(rule_uri, action_uri)311 # Should raise an exception if the rule already has that action312 with pytest.raises(ValueError):313 db_access.add_action_to_rule(action_uri, rule_uri)314@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)315def test_remove_action_from_rule(mock):316 # Should remove an action from a rule317 rule_uri = 'https://example.com#rule'318 rule_type = 'http://www.w3.org/ns/odrl/2/permission'319 rule_label = 'Rule'320 db_access.create_rule(rule_uri, rule_type, rule_label)321 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'322 db_access.add_action_to_rule(action_uri, rule_uri)323 db_access.remove_action_from_rule(action_uri, rule_uri)324 assert not db_access.rule_has_action(rule_uri, action_uri)325@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)326def test_get_actions_for_rule(mock):327 rule_uri = 'https://example.com#rule'328 rule_type = 'http://www.w3.org/ns/odrl/2/permission'329 rule_label = 'Rule'330 db_access.create_rule(rule_uri, rule_type, rule_label)331 action1 = 'http://www.w3.org/ns/odrl/2/acceptTracking'332 action2 = 'http://www.w3.org/ns/odrl/2/aggregate'333 db_access.add_action_to_rule(action1, rule_uri)334 db_access.add_action_to_rule(action2, rule_uri)335 actions = db_access.get_actions_for_rule(rule_uri)336 assert all(action['URI'] in [action1, action2] for action in actions)337@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)338def test_get_rules_using_action(mock):339 action_uri = 'http://www.w3.org/ns/odrl/2/acceptTracking'340 rule1_uri = 'https://example.com#rule1'341 rule1_type = 'http://www.w3.org/ns/odrl/2/permission'342 rule1_label = 'Rule1'343 db_access.create_rule(rule1_uri, rule1_type, rule1_label)344 db_access.add_action_to_rule(action_uri, rule1_uri)345 rule2_uri = 'https://example.com#rule2'346 rule2_type = 'http://www.w3.org/ns/odrl/2/permission'347 rule2_label = 'Rule2'348 db_access.create_rule(rule2_uri, rule2_type, rule2_label)349 db_access.add_action_to_rule(action_uri, rule2_uri)350 rules = db_access.get_rules_using_action(action_uri)351 assert all(rule['URI'] in [rule1_uri, rule2_uri] for rule in rules)352@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)353def test_get_policies_using_action(mock):354 action_uri = 'http://www.w3.org/ns/odrl/2/acceptTracking'355 rule1_uri = 'https://example.com#rule1'356 rule1_type = 'http://www.w3.org/ns/odrl/2/permission'357 rule1_label = 'Rule1'358 db_access.create_rule(rule1_uri, rule1_type, rule1_label)359 db_access.add_action_to_rule(action_uri, rule1_uri)360 rule2_uri = 'https://example.com#rule2'361 rule2_type = 'http://www.w3.org/ns/odrl/2/permission'362 rule2_label = 'Rule2'363 db_access.create_rule(rule2_uri, rule2_type, rule2_label)364 db_access.add_action_to_rule(action_uri, rule2_uri)365 policy1_uri = 'http://example.com#policy1'366 policy2_uri = 'http://example.com#policy2'367 policy3_uri = 'http://example.com#policy3'368 db_access.create_policy(policy1_uri)369 db_access.create_policy(policy2_uri)370 db_access.create_policy(policy3_uri)371 db_access.add_rule_to_policy(rule1_uri, policy1_uri)372 db_access.add_rule_to_policy(rule2_uri, policy2_uri)373 db_access.add_rule_to_policy(rule2_uri, policy3_uri)374 policies = db_access.get_policies_using_action(action_uri)375 assert all(policy['URI'] in [policy1_uri, policy2_uri, policy3_uri] for policy in policies)376@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)377def test_rule_has_action(mock):378 # Should return false if the rule does not have the action379 rule_uri = 'https://example.com#rule'380 rule_type = 'http://www.w3.org/ns/odrl/2/permission'381 rule_label = 'Rule'382 db_access.create_rule(rule_uri, rule_type, rule_label)383 action_uri = 'http://www.w3.org/ns/odrl/2/distribute'384 assert not db_access.rule_has_action(rule_uri, rule_type)385 # Should return true if the rule has the action386 db_access.add_action_to_rule(action_uri, rule_uri)387 assert db_access.rule_has_action(rule_uri, action_uri)388@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)389def test_create_party(mock):390 # Should create a party with the URI, label and comment given391 party_uri = 'https://example.com#party'392 label = 'Party'393 comment = 'This is a test party.'394 db_access.create_party(party_uri, label, comment)395 assert db_access.party_exists(party_uri)396 # Should raise an exception of the party already exists397 with pytest.raises(ValueError):398 db_access.create_party(party_uri)399@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)400def test_delete_party(mock):401 # Should raise an exception if the Party is currently assigned to a Rule402 party_uri = 'https://example.com#party'403 rule_uri = 'https://example.com#rule'404 rule_type = 'http://www.w3.org/ns/odrl/2/permission'405 db_access.create_party(party_uri)406 db_access.create_rule(rule_uri, rule_type)407 db_access.add_assignor_to_rule(party_uri, rule_uri)408 with pytest.raises(ValueError):409 db_access.delete_party(party_uri)410 # Should delete the Party if not currently assigned to a Rule (deleting the rule drops the assignment)411 db_access.delete_rule(rule_uri)412 db_access.delete_party(party_uri)413 assert not db_access.party_exists(party_uri)414@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)415def test_party_exists(mock):416 # Should return false if the party does not exist417 party_uri = 'https://example.com#party'418 assert not db_access.party_exists(party_uri)419 # Should return true of the party exists420 db_access.create_party(party_uri)421 assert db_access.party_exists(party_uri)422@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)423def test_get_rules_for_party(mock):424 party_uris = ['https://example.com/party/1', 'https://example.com/party/2']425 for party_uri in party_uris:426 db_access.create_party(party_uri)427 rule_uris = ['http://example.com/rule/1', 'http://example.com/rule/2', 'http://example.com/rule/3',428 'http://example.com/rule/4']429 rule_type = 'http://www.w3.org/ns/odrl/2/permission'430 for rule_uri in rule_uris:431 db_access.create_rule(rule_uri, rule_type)432 db_access.add_assignor_to_rule(party_uris[0], rule_uris[0])433 db_access.add_assignor_to_rule(party_uris[0], rule_uris[1])434 db_access.add_assignee_to_rule(party_uris[0], rule_uris[2])435 db_access.add_assignor_to_rule(party_uris[1], rule_uris[3])436 # Should list the parties assigned to a rule and only those parties437 assert {rule_uris[0], rule_uris[1], rule_uris[2]} == set(db_access.get_rules_for_party(party_uris[0]))438 assert [rule_uris[3]] == db_access.get_rules_for_party(party_uris[1])439@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)440def test_add_assignor_to_rule(mock):441 # Should raise an exception if the rule doesn't exist442 assignor_uri = 'http://example.com#assignor'443 rule_uri = 'https://example.com#rule'444 db_access.create_party(assignor_uri)445 with pytest.raises(ValueError):446 db_access.add_assignor_to_rule(assignor_uri, rule_uri)447 # Should raise an exception if the assignor doesn't exist448 rule_type = 'http://www.w3.org/ns/odrl/2/permission'449 db_access.create_rule(rule_uri, rule_type)450 db_access.delete_party(assignor_uri)451 with pytest.raises(ValueError):452 db_access.add_assignor_to_rule(assignor_uri, rule_uri)453 # Should add an assignor to a rule454 db_access.create_party(assignor_uri)455 db_access.add_assignor_to_rule(assignor_uri, rule_uri)456 assert db_access.rule_has_assignor(rule_uri, assignor_uri)457 # Should raise an exception if the rule already has that assignor458 with pytest.raises(ValueError):459 db_access.add_assignor_to_rule(assignor_uri, rule_uri)460@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)461def test_remove_assignor_from_rule(mock):462 # Should remove an assignor from a rule463 assignor_uri = 'http://example.com#assignor'464 rule_uri = 'https://example.com#rule'465 rule_type = 'http://www.w3.org/ns/odrl/2/permission'466 rule_label = 'Rule'467 db_access.create_rule(rule_uri, rule_type, rule_label)468 db_access.create_party(assignor_uri)469 db_access.add_assignor_to_rule(assignor_uri, rule_uri)470 db_access.remove_assignor_from_rule(assignor_uri, rule_uri)471 assert not db_access.rule_has_assignor(rule_uri, assignor_uri)472@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)473def test_rule_has_assignor(mock):474 # Should return false if the rule doesn't have that assignor475 assignor_uri = 'http://example.com#assignor'476 rule_uri = 'https://example.com#rule'477 rule_type = 'http://www.w3.org/ns/odrl/2/permission'478 db_access.create_rule(rule_uri, rule_type)479 assert not db_access.rule_has_assignor(rule_uri, assignor_uri)480 # Should return true if the rule does have that assignor481 db_access.create_party(assignor_uri)482 db_access.add_assignor_to_rule(assignor_uri, rule_uri)483 assert db_access.rule_has_assignor(rule_uri, assignor_uri)484@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)485def test_get_assignors_for_rule(mock):486 rule_uri = 'https://example.com#rule'487 rule_type = 'http://www.w3.org/ns/odrl/2/permission'488 db_access.create_rule(rule_uri, rule_type)489 assignor1 = 'https://example.com#assignor1'490 assignor2 = 'https://example.com#assignor2'491 db_access.create_party(assignor1)492 db_access.create_party(assignor2)493 db_access.add_assignor_to_rule(assignor1, rule_uri)494 db_access.add_assignor_to_rule(assignor2, rule_uri)495 assignors = db_access.get_assignors_for_rule(rule_uri)496 assert assignors == [assignor1, assignor2]497@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)498def test_add_assignee_to_rule(mock):499 # Should raise an exception if the rule doesn't exist500 rule_uri = 'https://example.com#rule'501 assignee_uri = 'http://example.com#assignee'502 db_access.create_party(assignee_uri)503 with pytest.raises(ValueError):504 db_access.add_assignee_to_rule(assignee_uri, rule_uri)505 # Should raise an exception if the assignee doesn't exist506 rule_type = 'http://www.w3.org/ns/odrl/2/permission'507 db_access.delete_party(assignee_uri)508 db_access.create_rule(rule_uri, rule_type)509 with pytest.raises(ValueError):510 db_access.add_assignee_to_rule(assignee_uri, rule_uri)511 # Should add an assignee to a rule512 db_access.create_party(assignee_uri)513 db_access.add_assignee_to_rule(assignee_uri, rule_uri)514 assert db_access.rule_has_assignee(rule_uri, assignee_uri)515 # Should raise an exception if the rule already has that assignee516 with pytest.raises(ValueError):517 db_access.add_assignee_to_rule(assignee_uri, rule_uri)518@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)519def test_remove_assignee_from_rule(mock):520 # Should remove an assignee from a rule521 assignee_uri = 'http://example.com#assignee'522 rule_uri = 'https://example.com#rule'523 rule_type = 'http://www.w3.org/ns/odrl/2/permission'524 db_access.create_rule(rule_uri, rule_type)525 db_access.create_party(assignee_uri)526 db_access.add_assignee_to_rule(assignee_uri, rule_uri)527 db_access.remove_assignee_from_rule(assignee_uri, rule_uri)528 assert not db_access.rule_has_assignee(rule_uri, assignee_uri)529@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)530def test_rule_has_assignee(mock):531 # Should return false if the rule doesn't have that assignee532 assignee_uri = 'http://example.com#assignee'533 rule_uri = 'https://example.com#rule'534 rule_type = 'http://www.w3.org/ns/odrl/2/permission'535 db_access.create_rule(rule_uri, rule_type)536 assert not db_access.rule_has_assignee(rule_uri, assignee_uri)537 # Should return true if the rule does have that assignee538 db_access.create_party(assignee_uri)539 db_access.add_assignee_to_rule(assignee_uri, rule_uri)540 assert db_access.rule_has_assignee(rule_uri, assignee_uri)541@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)542def test_get_assignees_for_rule(mock):543 rule_uri = 'https://example.com#rule'544 rule_type = 'http://www.w3.org/ns/odrl/2/permission'545 rule_label = 'Rule'546 db_access.create_rule(rule_uri, rule_type, rule_label)547 assignee1 = 'https://example.com#assignee1'548 assignee2 = 'https://example.com#assignee2'549 db_access.create_party(assignee1)550 db_access.create_party(assignee2)551 db_access.add_assignee_to_rule(assignee1, rule_uri)552 db_access.add_assignee_to_rule(assignee2, rule_uri)553 assignees = db_access.get_assignees_for_rule(rule_uri)554 assert assignees == [assignee1, assignee2]555@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)556def test_get_all_parties(mock):557 uri1 = 'https://example.com/party/1'558 label1 = 'Party1'559 comment1 = 'This is the first party'560 uri2 = 'https://example.com/party/2'561 db_access.create_party(uri1, label1, comment1)562 db_access.create_party(uri2)563 assert db_access.get_all_parties() == [564 {'URI': uri1, 'LABEL': label1, 'COMMENT': comment1},565 {'URI': uri2, 'LABEL': None, 'COMMENT': None}566 ]567@mock.patch('controller.db_access.get_db', side_effect=mock_get_db)568def test_get_party(mock):569 # Should raise exception if the Party does not exist570 uri = 'https://example.com/party/1'571 with pytest.raises(ValueError):572 db_access.get_party(uri)573 # Should retrieve info about a Party if it exists574 label = 'Party'575 comment = 'This is a party.'576 db_access.create_party(uri, label, comment)...

Full Screen

Full Screen

metric.py

Source:metric.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3# @Time : 2019/3/13 20:534# @Author : Steve Wu5# @Site : 6# @File : metric.py7# @Software: PyCharm8# @Github : https://github.com/stevehamwu9from .ft_rule import *10import numpy as np11import pandas as pd12def metrics(true_labels, pred_labels):13 succ, real_right, pred_right = 0, 0, 014 for i in range(len(true_labels)):15 if true_labels[i] == 1:16 real_right += 117 if pred_labels[i] == 1:18 succ += 119 if pred_labels[i] == 1:20 pred_right += 121 precision = succ / pred_right if pred_right > 0 else 022 recall = succ / real_right23 f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 024 return precision, recall, f125def test_rules(data=pd.read_csv('/data/wujipeng/ec/data/han/processed_data.csv'), rule_range=range(1, 16), strict=True):26 pos0 = 6927 rules = np.zeros((len(data), 15))28 rule_labels = []29 for i, row in data.iterrows():30 clauses = [clause.strip().split(' ') for clause in row['clause'].split('\x01')]31 natures = [nature.strip().split(' ') for nature in row['nature'].split('\x01')]32 keyword = row['keyword'].replace(' ', '')33 emotion = row['emotion']34 clause_pos = [int(pos) - pos0 for pos in row['clause_pos'].split(' ')]35 rule_label = np.zeros(len(clauses), dtype=int)36 f = clause_pos.index(0)37 K = keyword38 for r in rule_range:39 if r in [6, 7, 8, 12, 13]:40 continue41 # constraints42 if r == 4:43 # 0,1: Happiness 2:Anger 3: Sadness 4: Fear 5:Disgust 6:Surprise44 if emotion not in [0, 1, 4, 6]:45 continue46 if r in [14, 15]:47 if sum(rules[i]) > 0:48 continue49 found = eval('rule{}'.format(r))(f, clauses, natures, K, strict)50 if found:51 rules[i][r - 1] = 152 if found[0] == 'F':53 rule_label[f] = 154 elif found[0] == 'B':55 rule_label[f - 1] = 156 elif found[0] == 'A':57 rule_label[f + 1] = 158 break59 rule_labels.append(rule_label)...

Full Screen

Full Screen

install_lint.bzl

Source:install_lint.bzl Github

copy

Full Screen

1# -*- python -*-2load("@drake//tools/skylark:drake_py.bzl", "py_test_isolated")3def install_lint(4 existing_rules = None):5 """Within the current package, checks that any install rules are6 depended-on by Drake's master //:install rule.7 """8 if existing_rules == None:9 existing_rules = native.existing_rules().values()10 package_name = "//" + native.package_name() # e.g., "//systems/framework"11 # For each rule tagged as "install", find a dependency chain from12 # //:install that reaches it. When there is no such chain, it is likely13 # that the developer forgot to list their package in the install.14 query_results = []15 for one_rule in existing_rules:16 tags = one_rule.get("tags")17 if "install" not in tags:18 continue19 if "nolint" in tags:20 continue21 rule_name = one_rule["name"]22 rule_label = "{}:{}".format(package_name, rule_name)23 if rule_label == "//:install":24 # Don't lint a self-loop.25 continue26 genquery_name = "install_lint_genquery_{}".format(rule_name)27 native.genquery(28 name = genquery_name,29 expression = "somepath(//:install, {})".format(rule_label),30 scope = [31 "//:install",32 rule_label,33 ],34 testonly = 1,35 tags = ["lint"],36 visibility = ["//visibility:private"],37 )38 query_results.append(genquery_name)39 # Report all of the install_lint results.40 if query_results:41 args = []42 data = []43 for label in query_results:44 args.append("--genquery_output=$(location {})".format(label))45 data.append(label)46 py_test_isolated(47 name = "install_lint",48 size = "small",49 srcs = ["@drake//tools/lint:install_lint_reporter.py"],50 main = "@drake//tools/lint:install_lint_reporter.py",51 args = args,52 data = data,53 tags = ["lint", "install_lint"],...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful