How to use is_root method in localstack

Best Python code snippet using localstack_python

test_constraints.py

Source:test_constraints.py Github

copy

Full Screen

1# Copyright (c) Facebook, Inc. and its affiliates.2#3# This source code is licensed under the MIT license found in the4# LICENSE file in the root directory of this source tree.5import sys6import unittest7import torch8from fairseq.token_generation_constraints import *9def tensorize(constraints: List[List[int]]) -> torch.Tensor:10 return [torch.tensor(x) for x in constraints]11class TestHelperRoutines(unittest.TestCase):12 def setUp(self):13 self.examples = [14 ([[]], torch.tensor([[0]])),15 ([[], []], torch.tensor([[0], [0]])),16 ([[torch.tensor([1, 2])], []], torch.tensor([[1, 1, 2, 0], [0, 0, 0, 0]])),17 (18 [19 [20 torch.tensor([3, 1, 2]),21 torch.tensor([3]),22 torch.tensor([4, 5, 6, 7]),23 ],24 [],25 [torch.tensor([1, 8, 9, 10, 1, 4, 11, 12])],26 ],27 torch.tensor(28 [29 [3, 3, 1, 2, 0, 3, 0, 4, 5, 6, 7, 0],30 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],31 [1, 1, 8, 9, 10, 1, 4, 11, 12, 0, 0, 0],32 ]33 ),34 ),35 ]36 def test_packing(self):37 """Ensures the list of lists of tensors gets packed correctly."""38 for batch_constraints, expected_tensor in self.examples:39 packed = pack_constraints(batch_constraints)40 assert torch.equal(packed, expected_tensor)41class TestUnorderedConstraintState(unittest.TestCase):42 def setUp(self):43 # Tuples of (contraint set, expected printed graph, token counts per node)44 self.examples = [45 (46 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),47 "([None].False#6 ([1].True#4 ([2].False#1 [3].True#1) [3].True#1 [4].True#1) ([4].False#2 ([5].True#2 ([6].False#1 [7].True#1))))",48 {1: 4, 2: 1, 3: 2, 4: 3, 5: 2, 6: 1, 7: 1},49 ),50 ([], "[None].False#0", {}),51 (tensorize([[0]]), "([None].False#1 [0].True#1)", {0: 1}),52 (53 tensorize([[100000, 1, 2, 3, 4, 5]]),54 "([None].False#1 ([100000].False#1 ([1].False#1 ([2].False#1 ([3].False#1 ([4].False#1 [5].True#1))))))",55 {100000: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1},56 ),57 (58 tensorize([[1, 2], [1, 2]]),59 "([None].False#2 ([1].False#2 [2].True#2))",60 {1: 2, 2: 2},61 ),62 (63 tensorize([[1, 2], [3, 4]]),64 "([None].False#2 ([1].False#1 [2].True#1) ([3].False#1 [4].True#1))",65 {1: 1, 2: 1, 3: 1, 4: 1},66 ),67 ]68 self.sequences = [69 (70 self.examples[0][0],71 [],72 {"bank": 0, "num_completed": 0, "finished": False, "is_root": True},73 ),74 (75 self.examples[0][0],76 [1, 2],77 {"bank": 2, "num_completed": 0, "finished": False, "is_root": False},78 ),79 (80 self.examples[0][0],81 [1, 2, 94],82 {"bank": 1, "num_completed": 1, "finished": False, "is_root": True},83 ),84 (85 self.examples[0][0],86 [1, 3, 999, 1, 4],87 {"bank": 4, "num_completed": 2, "finished": False, "is_root": False},88 ),89 (90 self.examples[0][0],91 [1, 3, 999, 1, 4, 999],92 {"bank": 4, "num_completed": 2, "finished": False, "is_root": True},93 ),94 (95 self.examples[0][0],96 [4, 5, 6, 8],97 {"bank": 2, "num_completed": 1, "finished": False, "is_root": True},98 ),99 (100 self.examples[0][0],101 # Tricky, because in last three, goes down [1->4] branch, could miss [1] and [4->5]102 # [[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]],103 [1, 2, 3, 1, 3, 1, 4, 4, 5, 6, 7, 1, 4, 5],104 {"bank": 14, "num_completed": 6, "finished": True, "is_root": False},105 ),106 (107 self.examples[0][0],108 [1, 2, 3, 999, 1, 3, 1, 4, 4, 5, 6, 7, 1, 4, 5, 117],109 {"bank": 14, "num_completed": 6, "finished": True, "is_root": True},110 ),111 (112 tensorize([[1], [2, 3]]),113 # Should not be able to get credit for entering 1 a second time114 [1, 1],115 {"bank": 1, "num_completed": 1, "finished": False, "is_root": True},116 ),117 (118 self.examples[4][0],119 [1, 2, 1, 2],120 {"bank": 4, "num_completed": 2, "finished": True, "is_root": False},121 ),122 (123 self.examples[4][0],124 [1, 2, 1, 2, 1],125 {"bank": 4, "num_completed": 2, "finished": True, "is_root": True},126 ),127 (128 self.examples[5][0],129 [1, 2, 3, 4, 5],130 {"bank": 4, "num_completed": 2, "finished": True, "is_root": True},131 ),132 ]133 def test_graphs(self):134 """135 Test whether unordered graph systems are created correctly.136 """137 for example in self.examples:138 constraints, expected, gold_counts = example139 c = ConstraintNode.create(constraints)140 assert (141 ConstraintNode.print_graph(c) == expected142 ), f"got {ConstraintNode.print_graph(c)}, expected {expected}"143 assert (144 c.token_counts() == gold_counts145 ), f"{c} got {c.token_counts()} wanted {gold_counts}"146 def test_next_tokens(self):147 """148 Tests that the set of next tokens is correct.149 """150 for example in self.examples:151 constraints, expected, gold_counts = example152 root = ConstraintNode.create(constraints)153 root_tokens = set(root.children.keys())154 for sequence in constraints:155 state = UnorderedConstraintState(root)156 for token in sequence:157 all_tokens = root_tokens.union(state.node.children.keys())158 assert (159 all_tokens == state.next_tokens()160 ), f"ALL {all_tokens} NEXT {state.next_tokens()}"161 state = state.advance(token)162 def test_sequences(self):163 for constraints, tokens, expected in self.sequences:164 state = UnorderedConstraintState.create(pack_constraints([constraints])[0])165 for token in tokens:166 state = state.advance(token)167 result = {}168 for attr in expected.keys():169 result[attr] = getattr(state, attr)170 assert (171 result == expected172 ), f"TEST({tokens}) GOT: {result} WANTED: {expected}"173class TestOrderedConstraintState(unittest.TestCase):174 def setUp(self):175 self.sequences = [176 (177 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),178 [],179 {"bank": 0, "num_completed": 0, "finished": False, "is_root": True},180 ),181 (182 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),183 [1, 2],184 {"bank": 2, "num_completed": 0, "finished": False, "is_root": False},185 ),186 (187 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),188 [1, 2, 94],189 {"bank": 0, "num_completed": 0, "finished": False, "is_root": True},190 ),191 (192 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),193 [1, 3, 999, 1, 4],194 {"bank": 0, "num_completed": 0, "finished": False, "is_root": True},195 ),196 (197 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),198 [1, 2, 3, 999, 999],199 {"bank": 3, "num_completed": 1, "finished": False, "is_root": False},200 ),201 (202 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),203 [1, 2, 3, 77, 1, 3, 1],204 {"bank": 6, "num_completed": 2, "finished": False, "is_root": False},205 ),206 (207 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),208 [1, 2, 3, 1, 3, 1, 4, 4, 5, 6, 7, 1, 4, 5],209 {"bank": 14, "num_completed": 6, "finished": True, "is_root": False},210 ),211 (212 tensorize([[1, 2, 3], [1, 3], [1, 4], [4, 5, 6, 7], [1], [4, 5]]),213 [1, 2, 999, 1, 2, 3, 999, 1, 3, 1, 4, 4, 5, 6, 7, 1, 4, 5, 117],214 {"bank": 14, "num_completed": 6, "finished": True, "is_root": False},215 ),216 (217 tensorize([[1], [2, 3]]),218 [1, 1],219 {"bank": 1, "num_completed": 1, "finished": False, "is_root": False},220 ),221 (222 tensorize([[1, 2], [1, 2]]),223 [1, 2, 1, 2],224 {"bank": 4, "num_completed": 2, "finished": True, "is_root": False},225 ),226 (227 tensorize([[1, 2], [1, 2]]),228 [1, 2, 1, 2, 1],229 {"bank": 4, "num_completed": 2, "finished": True, "is_root": False},230 ),231 (232 tensorize([[1, 2], [3, 4]]),233 [1, 2, 3, 4, 5],234 {"bank": 4, "num_completed": 2, "finished": True, "is_root": False},235 ),236 ]237 def test_sequences(self):238 for i, (constraints, tokens, expected) in enumerate(self.sequences):239 state = OrderedConstraintState.create(pack_constraints([constraints])[0])240 for token in tokens:241 state = state.advance(token)242 result = {}243 for attr in expected.keys():244 result[attr] = getattr(state, attr)245 assert (246 result == expected247 ), f"TEST({tokens}) GOT: {result} WANTED: {expected}"248if __name__ == "__main__":...

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 localstack 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