How to use to_string method in locust

Best Python code snippet using locust

device_spec_test.py

Source:device_spec_test.py Github

copy

Full Screen

...25class DeviceSpecTest(test_util.TensorFlowTestCase, parameterized.TestCase):26 @parameterized.named_parameters(*TEST_V1_AND_V2)27 def test_empty(self, device_spec_type):28 d = device_spec_type()29 self.assertEqual("", d.to_string())30 d.parse_from_string("")31 self.assertEqual("", d.to_string())32 @parameterized.named_parameters(*TEST_V1_AND_V2)33 def test_constructor(self, device_spec_type):34 d = device_spec_type(job="j", replica=0, task=1,35 device_type="CPU", device_index=2)36 self.assertEqual("j", d.job)37 self.assertEqual(0, d.replica)38 self.assertEqual(1, d.task)39 self.assertEqual("CPU", d.device_type)40 self.assertEqual(2, d.device_index)41 self.assertEqual("/job:j/replica:0/task:1/device:CPU:2", d.to_string())42 d = device_spec_type(device_type="GPU", device_index=0)43 self.assertEqual("/device:GPU:0", d.to_string())44 def testto_string_legacy(self):45 """DeviceSpecV1 allows direct mutation."""46 d = device_spec.DeviceSpecV1()47 d.job = "foo"48 self.assertEqual("/job:foo", d.to_string())49 d.task = 350 self.assertEqual("/job:foo/task:3", d.to_string())51 d.device_type = "CPU"52 d.device_index = 053 self.assertEqual("/job:foo/task:3/device:CPU:0", d.to_string())54 d.task = None55 d.replica = 1256 self.assertEqual("/job:foo/replica:12/device:CPU:0", d.to_string())57 d.device_type = "GPU"58 d.device_index = 259 self.assertEqual("/job:foo/replica:12/device:GPU:2", d.to_string())60 d.device_type = "CPU"61 d.device_index = 162 self.assertEqual("/job:foo/replica:12/device:CPU:1", d.to_string())63 d.device_type = None64 d.device_index = None65 self.assertEqual("/job:foo/replica:12", d.to_string())66 # Test wildcard67 d = device_spec.DeviceSpecV1(job="foo", replica=12, task=3,68 device_type="GPU")69 self.assertEqual("/job:foo/replica:12/task:3/device:GPU:*", d.to_string())70 @parameterized.named_parameters(*TEST_V1_AND_V2)71 def test_replace(self, device_spec_type):72 d = device_spec_type()73 d = d.replace(job="foo")74 self.assertEqual("/job:foo", d.to_string())75 d = d.replace(task=3)76 self.assertEqual("/job:foo/task:3", d.to_string())77 d = d.replace(device_type="CPU", device_index=0)78 self.assertEqual("/job:foo/task:3/device:CPU:0", d.to_string())79 d = d.replace(task=None, replica=12)80 self.assertEqual("/job:foo/replica:12/device:CPU:0", d.to_string())81 d = d.replace(device_type="GPU", device_index=2)82 self.assertEqual("/job:foo/replica:12/device:GPU:2", d.to_string())83 d = d.replace(device_type="CPU", device_index=1)84 self.assertEqual("/job:foo/replica:12/device:CPU:1", d.to_string())85 d = d.replace(device_type=None, device_index=None)86 self.assertEqual("/job:foo/replica:12", d.to_string())87 # Test wildcard88 d = device_spec.DeviceSpecV1(job="foo", replica=12, task=3,89 device_type="GPU")90 self.assertEqual("/job:foo/replica:12/task:3/device:GPU:*", d.to_string())91 @parameterized.named_parameters(*TEST_V1_AND_V2)92 def testto_string(self, device_spec_type):93 d = device_spec_type(job="foo")94 self.assertEqual("/job:foo", d.to_string())95 d = device_spec_type(job="foo", task=3)96 self.assertEqual("/job:foo/task:3", d.to_string())97 d = device_spec_type(job="foo", task=3, device_type="cpu", device_index=0)98 self.assertEqual("/job:foo/task:3/device:CPU:0", d.to_string())99 d = device_spec_type(job="foo", replica=12, device_type="cpu",100 device_index=0)101 self.assertEqual("/job:foo/replica:12/device:CPU:0", d.to_string())102 d = device_spec_type(job="foo", replica=12, device_type="gpu",103 device_index=2)104 self.assertEqual("/job:foo/replica:12/device:GPU:2", d.to_string())105 d = device_spec_type(job="foo", replica=12)106 self.assertEqual("/job:foo/replica:12", d.to_string())107 # Test wildcard108 d = device_spec_type(job="foo", replica=12, task=3, device_type="GPU")109 self.assertEqual("/job:foo/replica:12/task:3/device:GPU:*", d.to_string())110 def test_parse_legacy(self):111 d = device_spec.DeviceSpecV1()112 d.parse_from_string("/job:foo/replica:0")113 self.assertEqual("/job:foo/replica:0", d.to_string())114 d.parse_from_string("/replica:1/task:0/cpu:0")115 self.assertEqual("/replica:1/task:0/device:CPU:0", d.to_string())116 d.parse_from_string("/replica:1/task:0/device:CPU:0")117 self.assertEqual("/replica:1/task:0/device:CPU:0", d.to_string())118 d.parse_from_string("/job:muu/device:GPU:2")119 self.assertEqual("/job:muu/device:GPU:2", d.to_string())120 with self.assertRaisesRegexp(ValueError, "Cannot specify multiple"):121 d.parse_from_string("/job:muu/device:GPU:2/cpu:0")122 @parameterized.named_parameters(*TEST_V1_AND_V2)123 def test_to_from_string(self, device_spec_type):124 d = device_spec_type.from_string("/job:foo/replica:0")125 self.assertEqual("/job:foo/replica:0", d.to_string())126 self.assertEqual(0, d.replica)127 d = device_spec_type.from_string("/replica:1/task:0/cpu:0")128 self.assertEqual("/replica:1/task:0/device:CPU:0", d.to_string())129 self.assertAllEqual([1, 0, "CPU", 0],130 [d.replica, d.task, d.device_type, d.device_index])131 d = device_spec_type.from_string("/replica:1/task:0/device:CPU:0")132 self.assertEqual("/replica:1/task:0/device:CPU:0", d.to_string())133 self.assertAllEqual([1, 0, "CPU", 0],134 [d.replica, d.task, d.device_type, d.device_index])135 d = device_spec_type.from_string("/job:muu/device:GPU:2")136 self.assertEqual("/job:muu/device:GPU:2", d.to_string())137 self.assertAllEqual(["muu", "GPU", 2],138 [d.job, d.device_type, d.device_index])139 with self.assertRaisesRegexp(ValueError, "Cannot specify multiple"):140 d.parse_from_string("/job:muu/device:GPU:2/cpu:0")141 def test_merge_legacy(self):142 d = device_spec.DeviceSpecV1.from_string("/job:foo/replica:0")143 self.assertEqual("/job:foo/replica:0", d.to_string())144 d.merge_from(device_spec.DeviceSpecV1.from_string("/task:1/device:GPU:2"))145 self.assertEqual("/job:foo/replica:0/task:1/device:GPU:2", d.to_string())146 d = device_spec.DeviceSpecV1()147 d.merge_from(device_spec.DeviceSpecV1.from_string("/task:1/cpu:0"))148 self.assertEqual("/task:1/device:CPU:0", d.to_string())149 d.merge_from(device_spec.DeviceSpecV1.from_string("/job:boo/device:GPU:0"))150 self.assertEqual("/job:boo/task:1/device:GPU:0", d.to_string())151 d.merge_from(device_spec.DeviceSpecV1.from_string("/job:muu/cpu:2"))152 self.assertEqual("/job:muu/task:1/device:CPU:2", d.to_string())153 d.merge_from(device_spec.DeviceSpecV1.from_string(154 "/job:muu/device:MyFunnyDevice:2"))155 self.assertEqual("/job:muu/task:1/device:MyFunnyDevice:2", d.to_string())156 def test_merge_removed(self):157 with self.assertRaises(AttributeError):158 d = device_spec.DeviceSpecV2()159 d.merge_from(device_spec.DeviceSpecV2.from_string("/task:1/cpu:0"))160 @parameterized.named_parameters(*TEST_V1_AND_V2)161 def test_combine(self, device_spec_type):162 d = device_spec_type.from_string("/job:foo/replica:0")163 self.assertEqual("/job:foo/replica:0", d.to_string())164 d = d.make_merged_spec(165 device_spec_type.from_string("/task:1/device:GPU:2"))166 self.assertEqual("/job:foo/replica:0/task:1/device:GPU:2", d.to_string())167 d = device_spec_type()168 d = d.make_merged_spec(device_spec_type.from_string("/task:1/cpu:0"))169 self.assertEqual("/task:1/device:CPU:0", d.to_string())170 d = d.make_merged_spec(171 device_spec_type.from_string("/job:boo/device:GPU:0"))172 self.assertEqual("/job:boo/task:1/device:GPU:0", d.to_string())173 d = d.make_merged_spec(device_spec_type.from_string("/job:muu/cpu:2"))174 self.assertEqual("/job:muu/task:1/device:CPU:2", d.to_string())175 d = d.make_merged_spec(device_spec_type.from_string(176 "/job:muu/device:MyFunnyDevice:2"))177 self.assertEqual("/job:muu/task:1/device:MyFunnyDevice:2", d.to_string())178if __name__ == "__main__":...

Full Screen

Full Screen

test_criterion.py

Source:test_criterion.py Github

copy

Full Screen

...28Tests for `prewikka.dataprovider.Criterion()`.29"""30import copy31from prewikka.dataprovider import Criterion, CriterionOperator32def test_criterion_to_string():33 """34 Test `prewikka.dataprovider.Criterion.to_string()` method.35 """36 # empty criterion37 criterion = Criterion()38 assert criterion.to_string() == ''39 # simple criterion40 criterion = Criterion('alert.messageid', '=', 'fakemessageid')41 assert criterion.to_string() == "alert.messageid = 'fakemessageid'"42 criterion = Criterion('alert.messageid', '==', 'fakemessageid')43 assert criterion.to_string() == "alert.messageid = 'fakemessageid'"44 criterion = Criterion('alert.messageid', '!=', 'fakemessageid')45 assert criterion.to_string() == "alert.messageid != 'fakemessageid'"46 criterion = Criterion('alert.messageid', '>', 'fakemessageid')47 assert criterion.to_string() == "alert.messageid > 'fakemessageid'"48 criterion = Criterion('alert.messageid', '>=', 'fakemessageid')49 assert criterion.to_string() == "alert.messageid >= 'fakemessageid'"50 criterion = Criterion('alert.messageid', '<', 'fakemessageid')51 assert criterion.to_string() == "alert.messageid < 'fakemessageid'"52 criterion = Criterion('alert.messageid', '<=', 'fakemessageid')53 assert criterion.to_string() == "alert.messageid <= 'fakemessageid'"54 criterion = Criterion('alert.messageid', '<>*', 'fakemessageid')55 assert criterion.to_string() == "alert.messageid <>* 'fakemessageid'"56 # combined criterion57 criterion_1 = Criterion('alert.messageid', '=', 'fakemessageid1')58 criterion_2 = Criterion('alert.messageid', '=', 'fakemessageid2')59 criterion = Criterion(criterion_1, '||', criterion_2)60 assert criterion.to_string() == "alert.messageid = 'fakemessageid1' || alert.messageid = 'fakemessageid2'"61 criterion = Criterion(criterion_1, '&&', criterion_2)62 assert criterion.to_string() == "alert.messageid = 'fakemessageid1' && alert.messageid = 'fakemessageid2'"63def test_criterion_get_path():64 """65 Test `prewikka.dataprovider.Criterion.get_path()` method.66 """67 # empty criterion68 criterion = Criterion()69 results = set()70 assert criterion.get_paths() == results71 # simple criterion72 criterion = Criterion('alert.messageid', '=', 'fakemessageid')73 results = set()74 results.add('alert.messageid')75 assert criterion.get_paths() == results76 # combined criterion (with || operator)77 criterion_1 = Criterion('alert.messageid', '=', 'fakemessageid1')78 criterion_2 = Criterion('heartbeat.messageid', '=', 'fakemessageid2')79 criterion = Criterion(criterion_1, '||', criterion_2)80 results = set()81 results.add('alert.messageid')82 results.add('heartbeat.messageid')83 assert criterion.get_paths() == results84 # combined criterion (with && operator)85 criterion_1 = Criterion('alert.messageid', '=', 'fakemessageid1')86 criterion_2 = Criterion('heartbeat.messageid', '=', 'fakemessageid2')87 criterion = Criterion(criterion_1, '&&', criterion_2)88 results = set()89 results.add('alert.messageid')90 results.add('heartbeat.messageid')91 assert criterion.get_paths() == results92def test_criterion_flatten():93 """94 Test `prewikka.dataprovider.Criterion.flatten()` method.95 """96 criterion_1 = Criterion('alert.messageid', '=', 'fakemessageid1')97 criterion_2 = Criterion('alert.messageid', '=', 'fakemessageid2')98 criterion_3 = Criterion('alert.messageid', '=', 'fakemessageid3')99 criterion_4 = Criterion('alert.messageid', '=', 'fakemessageid4')100 criterion = ((criterion_1 & criterion_2) & criterion_3) | criterion_4101 flattened = criterion.flatten()102 assert flattened.operator == CriterionOperator.OR103 assert len(flattened.operands) == 2104 assert flattened.operands[0].operator == CriterionOperator.AND105 assert flattened.operands[0].operands == [criterion_1, criterion_2, criterion_3]106 assert flattened.operands[1] == criterion_4107 criterion = Criterion(None, '!', Criterion(None, '!', Criterion(None, '!', criterion_1)))108 flattened = criterion.flatten()109 assert flattened.operator == CriterionOperator.NOT110 assert len(flattened.operands) == 1111 assert flattened.operands[0] == criterion_1112def test_criterion_operations():113 """114 Test `prewikka.dataprovider.Criterion` operations.115 """116 criterion_0 = Criterion()117 criterion_1 = Criterion('alert.messageid', '=', 'fakemessageid1')118 criterion_2 = Criterion('heartbeat.messageid', '=', 'fakemessageid2')119 # __str__()120 assert str(criterion_1) == criterion_1.to_string()121 # __bool__() / __nonzero__()122 assert not criterion_0123 assert criterion_1124 # __copy__()125 criterion_copy = copy.copy(criterion_1)126 assert criterion_copy != criterion_1127 assert criterion_copy.to_string() == criterion_1.to_string()128 # __json__()129 assert criterion_1.__json__() == {'left': 'alert.messageid', 'operator': CriterionOperator.EQUAL, 'right': 'fakemessageid1'}130 # __iadd__()131 criterion_iadd = copy.copy(criterion_1)132 criterion_iadd += criterion_2133 assert criterion_iadd.to_string() == Criterion(criterion_1, '&&', criterion_2).to_string()134 # __ior__()135 criterion_ior = copy.copy(criterion_1)136 criterion_ior |= criterion_2137 assert criterion_ior.to_string() == Criterion(criterion_1, '||', criterion_2).to_string()138 # __iand__()139 criterion_iand = copy.copy(criterion_1)140 criterion_iand &= criterion_2141 assert criterion_iand.to_string() == Criterion(criterion_1, '&&', criterion_2).to_string()142 # __add__()143 criterion_add = criterion_1 + criterion_2144 assert criterion_add.to_string() == Criterion(criterion_1, '&&', criterion_2).to_string()145 # __or__()146 criterion_or = criterion_1 | criterion_2147 assert criterion_or.to_string() == Criterion(criterion_1, '||', criterion_2).to_string()148 # __and__()149 criterion_and = criterion_1 & criterion_2...

Full Screen

Full Screen

test_formatting.py

Source:test_formatting.py Github

copy

Full Screen

...13 # There are already some tests in test_api.py, but this is a regression14 # test for the bug in issue #1319 which caused incorrect formatting of the15 # seconds for precision=016 angle = Angle(-1.23456789, unit=u.degree)17 assert angle.to_string(precision=3) == '-1d14m04.444s'18 assert angle.to_string(precision=1) == '-1d14m04.4s'19 assert angle.to_string(precision=0) == '-1d14m04s'20 angle2 = Angle(-1.23456789, unit=u.hourangle)21 assert angle2.to_string(precision=3, unit=u.hour) == '-1h14m04.444s'22 assert angle2.to_string(precision=1, unit=u.hour) == '-1h14m04.4s'23 assert angle2.to_string(precision=0, unit=u.hour) == '-1h14m04s'24def test_to_string_decimal():25 # There are already some tests in test_api.py, but this is a regression26 # test for the bug in issue #1323 which caused decimal formatting to not27 # work28 angle1 = Angle(2., unit=u.degree)29 assert angle1.to_string(decimal=True, precision=3) == '2.000'30 assert angle1.to_string(decimal=True, precision=1) == '2.0'31 assert angle1.to_string(decimal=True, precision=0) == '2'32 angle2 = Angle(3., unit=u.hourangle)33 assert angle2.to_string(decimal=True, precision=3) == '3.000'34 assert angle2.to_string(decimal=True, precision=1) == '3.0'35 assert angle2.to_string(decimal=True, precision=0) == '3'36 angle3 = Angle(4., unit=u.radian)37 assert angle3.to_string(decimal=True, precision=3) == '4.000'38 assert angle3.to_string(decimal=True, precision=1) == '4.0'39 assert angle3.to_string(decimal=True, precision=0) == '4'40def test_to_string_formats():41 a = Angle(1.113355, unit=u.deg)42 assert a.to_string(format='latex') == r'$1^\circ06{}^\prime48.078{}^{\prime\prime}$'43 assert a.to_string(format='unicode') == '1°06′48.078″'44 a = Angle(1.113355, unit=u.hour)45 assert a.to_string(format='latex') == r'$1^\mathrm{h}06^\mathrm{m}48.078^\mathrm{s}$'46 assert a.to_string(format='unicode') == '1ʰ06ᵐ48.078ˢ'47 a = Angle(1.113355, unit=u.radian)48 assert a.to_string(format='latex') == r'$1.11336\mathrm{rad}$'49 assert a.to_string(format='unicode') == '1.11336rad'50def test_to_string_fields():51 a = Angle(1.113355, unit=u.deg)52 assert a.to_string(fields=1) == r'1d'53 assert a.to_string(fields=2) == r'1d07m'54 assert a.to_string(fields=3) == r'1d06m48.078s'55def test_to_string_padding():56 a = Angle(0.5653, unit=u.deg)57 assert a.to_string(unit='deg', sep=':', pad=True) == r'00:33:55.08'58 # Test to make sure negative angles are padded correctly59 a = Angle(-0.5653, unit=u.deg)60 assert a.to_string(unit='deg', sep=':', pad=True) == r'-00:33:55.08'61def test_sexagesimal_rounding_up():62 a = Angle(359.9999999999, unit=u.deg)63 assert a.to_string(precision=None) == '360d00m00s'64 assert a.to_string(precision=4) == '360d00m00.0000s'65 assert a.to_string(precision=5) == '360d00m00.00000s'66 assert a.to_string(precision=6) == '360d00m00.000000s'67 assert a.to_string(precision=7) == '359d59m59.9999996s'68 a = Angle(3.999999, unit=u.deg)69 assert a.to_string(fields=2, precision=None) == '4d00m'70 assert a.to_string(fields=2, precision=1) == '4d00m'71 assert a.to_string(fields=2, precision=5) == '4d00m'72 assert a.to_string(fields=1, precision=1) == '4d'73 assert a.to_string(fields=1, precision=5) == '4d'74def test_to_string_scalar():75 a = Angle(1.113355, unit=u.deg)76 assert isinstance(a.to_string(), six.text_type)77def test_to_string_radian_with_precision():78 """79 Regression test for a bug that caused ``to_string`` to crash for angles in80 radians when specifying the precision.81 """82 # Check that specifying the precision works83 a = Angle(3., unit=u.rad)84 assert a.to_string(precision=3, sep='fromunit') == '3.000rad'85def test_sexagesimal_round_down():86 a1 = Angle(1, u.deg).to(u.hourangle)87 a2 = Angle(2, u.deg)88 assert a1.to_string() == '0h04m00s'89 assert a2.to_string() == '2d00m00s'90def test_to_string_fields_colon():91 a = Angle(1.113355, unit=u.deg)92 assert a.to_string(fields=2, sep=':') == '1:07'93 assert a.to_string(fields=3, sep=':') == '1:06:48.078'...

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