Best Python code snippet using playwright-python
test_helpers.py
Source:test_helpers.py
1from unittest import TestCase, skip2from unittest.mock import patch, Mock3from tools.helpers import (4 are_overlapping_edges,5 are_overlapping_boxes,6 is_readable,7 MissingSchema,8 ParserError,9 create_latest_url,10 create_filename,11 get_iso_time,12 load_gtfs,13 extract_gtfs_bounding_box,14 STOP_LAT,15 STOP_LON,16 to_json,17 from_json,18 normalize,19)20import pandas as pd21from freezegun import freeze_time22class TestVerificationFunctions(TestCase):23 def setUp(self):24 self.test_url = "some_url"25 def test_are_overlapping_crossing_edges(self):26 test_source_minimum = 45.00000027 test_source_maximum = 46.00000028 test_filter_minimum = 45.50000029 test_filter_maximum = 46.50000030 under_test = are_overlapping_edges(31 source_minimum=test_source_minimum,32 source_maximum=test_source_maximum,33 filter_minimum=test_filter_minimum,34 filter_maximum=test_filter_maximum,35 )36 self.assertTrue(under_test)37 def test_are_overlapping_included_edges(self):38 test_source_minimum = 45.00000039 test_source_maximum = 46.00000040 test_filter_minimum = 45.25000041 test_filter_maximum = 45.75000042 under_test = are_overlapping_edges(43 source_minimum=test_source_minimum,44 source_maximum=test_source_maximum,45 filter_minimum=test_filter_minimum,46 filter_maximum=test_filter_maximum,47 )48 self.assertTrue(under_test)49 test_source_minimum = 45.25000050 test_source_maximum = 45.75000051 test_filter_minimum = 45.00000052 test_filter_maximum = 46.00000053 under_test = are_overlapping_edges(54 source_minimum=test_source_minimum,55 source_maximum=test_source_maximum,56 filter_minimum=test_filter_minimum,57 filter_maximum=test_filter_maximum,58 )59 self.assertTrue(under_test)60 def test_are_not_overlapping_touching_edges(self):61 test_source_minimum = 45.00000062 test_source_maximum = 45.50000063 test_filter_minimum = 45.50000064 test_filter_maximum = 46.00000065 under_test = are_overlapping_edges(66 source_minimum=test_source_minimum,67 source_maximum=test_source_maximum,68 filter_minimum=test_filter_minimum,69 filter_maximum=test_filter_maximum,70 )71 self.assertFalse(under_test)72 test_source_minimum = 45.50000073 test_source_maximum = 46.00000074 test_filter_minimum = 45.00000075 test_filter_maximum = 45.50000076 under_test = are_overlapping_edges(77 source_minimum=test_source_minimum,78 source_maximum=test_source_maximum,79 filter_minimum=test_filter_minimum,80 filter_maximum=test_filter_maximum,81 )82 self.assertFalse(under_test)83 def test_are_not_overlapping_edges(self):84 test_source_minimum = 44.00000085 test_source_maximum = 45.00000086 test_filter_minimum = 46.00000087 test_filter_maximum = 47.50000088 under_test = are_overlapping_edges(89 source_minimum=test_source_minimum,90 source_maximum=test_source_maximum,91 filter_minimum=test_filter_minimum,92 filter_maximum=test_filter_maximum,93 )94 self.assertFalse(under_test)95 def test_are_not_overlapping_none_edges(self):96 test_source_minimum = None97 test_source_maximum = None98 test_filter_minimum = None99 test_filter_maximum = None100 under_test = are_overlapping_edges(101 source_minimum=test_source_minimum,102 source_maximum=test_source_maximum,103 filter_minimum=test_filter_minimum,104 filter_maximum=test_filter_maximum,105 )106 self.assertFalse(under_test)107 @patch("tools.helpers.are_overlapping_edges")108 def test_are_overlapping_boxes(self, mock_edges):109 test_source_minimum_latitude = Mock()110 test_source_maximum_latitude = Mock()111 test_source_minimum_longitude = Mock()112 test_source_maximum_longitude = Mock()113 test_filter_minimum_latitude = Mock()114 test_filter_maximum_latitude = Mock()115 test_filter_minimum_longitude = Mock()116 test_filter_maximum_longitude = Mock()117 mock_edges.side_effect = [True, True]118 under_test = are_overlapping_boxes(119 source_minimum_latitude=test_source_minimum_latitude,120 source_maximum_latitude=test_source_maximum_latitude,121 source_minimum_longitude=test_source_minimum_longitude,122 source_maximum_longitude=test_source_maximum_longitude,123 filter_minimum_latitude=test_filter_minimum_latitude,124 filter_maximum_latitude=test_filter_maximum_latitude,125 filter_minimum_longitude=test_filter_minimum_longitude,126 filter_maximum_longitude=test_filter_maximum_longitude,127 )128 self.assertTrue(under_test)129 mock_edges.side_effect = [True, False]130 under_test = are_overlapping_boxes(131 source_minimum_latitude=test_source_minimum_latitude,132 source_maximum_latitude=test_source_maximum_latitude,133 source_minimum_longitude=test_source_minimum_longitude,134 source_maximum_longitude=test_source_maximum_longitude,135 filter_minimum_latitude=test_filter_minimum_latitude,136 filter_maximum_latitude=test_filter_maximum_latitude,137 filter_minimum_longitude=test_filter_minimum_longitude,138 filter_maximum_longitude=test_filter_maximum_longitude,139 )140 self.assertFalse(under_test)141 mock_edges.side_effect = [False, True]142 under_test = are_overlapping_boxes(143 source_minimum_latitude=test_source_minimum_latitude,144 source_maximum_latitude=test_source_maximum_latitude,145 source_minimum_longitude=test_source_minimum_longitude,146 source_maximum_longitude=test_source_maximum_longitude,147 filter_minimum_latitude=test_filter_minimum_latitude,148 filter_maximum_latitude=test_filter_maximum_latitude,149 filter_minimum_longitude=test_filter_minimum_longitude,150 filter_maximum_longitude=test_filter_maximum_longitude,151 )152 self.assertFalse(under_test)153 mock_edges.side_effect = [False, False]154 under_test = are_overlapping_boxes(155 source_minimum_latitude=test_source_minimum_latitude,156 source_maximum_latitude=test_source_maximum_latitude,157 source_minimum_longitude=test_source_minimum_longitude,158 source_maximum_longitude=test_source_maximum_longitude,159 filter_minimum_latitude=test_filter_minimum_latitude,160 filter_maximum_latitude=test_filter_maximum_latitude,161 filter_minimum_longitude=test_filter_minimum_longitude,162 filter_maximum_longitude=test_filter_maximum_longitude,163 )164 self.assertFalse(under_test)165 @patch("tools.helpers.load_gtfs")166 def test_is_not_readable(self, mock_load_func):167 mock_load_func.side_effect = Mock(side_effect=TypeError())168 self.assertRaises(169 Exception, is_readable, url=self.test_url, load_func=mock_load_func170 )171 mock_load_func.side_effect = Mock(side_effect=MissingSchema())172 self.assertRaises(173 Exception, is_readable, url=self.test_url, load_func=mock_load_func174 )175 mock_load_func.side_effect = Mock(side_effect=ParserError())176 self.assertRaises(177 Exception, is_readable, url=self.test_url, load_func=mock_load_func178 )179 @patch("tools.helpers.load_gtfs")180 def test_is_readable(self, mock_load_func):181 mock_load_func.side_effect = "some_dataset"182 under_test = is_readable(url=self.test_url, load_func=mock_load_func)183 self.assertTrue(under_test)184class TestCreationFunctions(TestCase):185 def setUp(self):186 self.test_path = "some_path"187 @patch("tools.helpers.create_filename")188 def test_create_latest_url(self, mock_filename):189 mock_filename.return_value = "ca-some-subdivision-name-some-provider-gtfs-1.zip"190 test_country_code = "CA"191 test_subdivision_name = "Some Subdivision Name"192 test_provider = "Some Provider"193 test_data_type = "gtfs"194 test_mdb_source_id = "1"195 test_latest_url = "https://storage.googleapis.com/storage/v1/b/mdb-latest/o/ca-some-subdivision-name-some-provider-gtfs-1.zip?alt=media"196 under_test = create_latest_url(197 country_code=test_country_code,198 subdivision_name=test_subdivision_name,199 provider=test_provider,200 data_type=test_data_type,201 mdb_source_id=test_mdb_source_id,202 )203 self.assertEqual(under_test, test_latest_url)204 self.assertEqual(mock_filename.call_count, 1)205 def test_create_filename(self):206 test_country_code = "CA"207 test_subdivision_name = "Some Subdivision Name"208 test_provider = "Some Provider"209 test_data_type = "gtfs"210 test_mdb_source_id = "1"211 test_extension = "zip"212 test_filename = "ca-some-subdivision-name-some-provider-gtfs-1.zip"213 under_test = create_filename(214 country_code=test_country_code,215 subdivision_name=test_subdivision_name,216 provider=test_provider,217 data_type=test_data_type,218 mdb_source_id=test_mdb_source_id,219 extension=test_extension,220 )221 self.assertEqual(under_test, test_filename)222 def test_normalize(self):223 test_string = "test"224 under_test = normalize(test_string)225 self.assertEqual(under_test, "test")226 test_string = "Some Test"227 under_test = normalize(test_string)228 self.assertEqual(under_test, "some-test")229 test_string = "Some ~Test &!"230 under_test = normalize(test_string)231 self.assertEqual(under_test, "some-test")232 test_string = "1000 +=+=== Some ******* ~Test &!"233 under_test = normalize(test_string)234 self.assertEqual(under_test, "1000-some-test")235 test_string = "SOURCE's test..."236 under_test = normalize(test_string)237 self.assertEqual(under_test, "sources-test")238 test_string = "SOURCé-çø-Äõå-ÄÅ-cåã."239 under_test = normalize(test_string)240 self.assertEqual(under_test, "source-co-coa-co-caa")241 test_string = "Source provider,, another, , pro"242 under_test = normalize(test_string)243 self.assertEqual(under_test, "source-provider")244 @freeze_time("2022-01-01")245 def test_get_iso_time(self):246 test_time = "2022-01-01T00:00:00+00:00"247 under_test = get_iso_time()248 self.assertEqual(under_test, test_time)249class TestGtfsSpecificFunctions(TestCase):250 def setUp(self):251 self.test_url = "some_url"252 @patch("tools.helpers.gtfs_kit.read_feed")253 def test_not_loading_gtfs(self, mock_gtfs_kit):254 mock_gtfs_kit.side_effect = Mock(side_effect=TypeError())255 self.assertRaises(256 TypeError,257 load_gtfs,258 url=self.test_url,259 )260 mock_gtfs_kit.side_effect = Mock(side_effect=MissingSchema())261 self.assertRaises(262 MissingSchema,263 load_gtfs,264 url=self.test_url,265 )266 mock_gtfs_kit.side_effect = Mock(side_effect=ParserError())267 self.assertRaises(268 ParserError,269 load_gtfs,270 url=self.test_url,271 )272 @patch("tools.helpers.gtfs_kit.read_feed")273 def test_loading_gtfs(self, mock_gtfs_kit):274 test_dataset = "some_gtfs_dataset"275 mock_gtfs_kit.return_value = test_dataset276 under_test = load_gtfs(url=self.test_url)277 self.assertEqual(under_test, test_dataset)278 @patch("tools.helpers.load_gtfs")279 def test_extract_gtfs_bounding_box_none_stops(self, mock_load_gtfs):280 test_bounding_box = (None, None, None, None)281 test_stops = None282 type(mock_load_gtfs.return_value).stops = test_stops283 under_test = extract_gtfs_bounding_box(url=self.test_url)284 self.assertEqual(under_test, test_bounding_box)285 @patch("tools.helpers.load_gtfs")286 def test_extract_gtfs_bounding_box_empty_dataframe(self, mock_load_gtfs):287 test_bounding_box = (None, None, None, None)288 test_stops = pd.DataFrame()289 type(mock_load_gtfs.return_value).stops = test_stops290 under_test = extract_gtfs_bounding_box(url=self.test_url)291 self.assertEqual(under_test, test_bounding_box)292 @patch("tools.helpers.load_gtfs")293 def test_extract_gtfs_bounding_box_missing_columns(self, mock_load_gtfs):294 test_bounding_box = (None, None, None, None)295 test_stops = pd.DataFrame({"some_column": []})296 type(mock_load_gtfs.return_value).stops = test_stops297 under_test = extract_gtfs_bounding_box(url=self.test_url)298 self.assertEqual(under_test, test_bounding_box)299 @patch("tools.helpers.load_gtfs")300 def test_extract_gtfs_bounding_box_empty_columns(self, mock_load_gtfs):301 test_bounding_box = (None, None, None, None)302 test_stops = pd.DataFrame({STOP_LAT: [], STOP_LON: []})303 type(mock_load_gtfs.return_value).stops = test_stops304 under_test = extract_gtfs_bounding_box(url=self.test_url)305 self.assertEqual(under_test, test_bounding_box)306 @patch("tools.helpers.load_gtfs")307 def test_extract_gtfs_bounding_box_nan_values(self, mock_load_gtfs):308 test_bounding_box = (None, None, None, None)309 test_stops = pd.DataFrame({STOP_LAT: [pd.NA], STOP_LON: [pd.NA]})310 type(mock_load_gtfs.return_value).stops = test_stops311 under_test = extract_gtfs_bounding_box(url=self.test_url)312 self.assertEqual(under_test, test_bounding_box)313 @patch("tools.helpers.load_gtfs")314 def test_extract_gtfs_bounding_box_stops_present(self, mock_load_gtfs):315 test_bounding_box = (44.00000, 45.000000, -110.000000, -109.000000)316 test_stops = pd.DataFrame(317 {318 STOP_LAT: [44.000000, 45.000000, pd.NA],319 STOP_LON: [-110.000000, -109.000000, pd.NA],320 }321 )322 type(mock_load_gtfs.return_value).stops = test_stops323 under_test = extract_gtfs_bounding_box(url=self.test_url)324 self.assertEqual(under_test, test_bounding_box)325class TestInOutFunctions(TestCase):326 def setUp(self):327 self.test_path = "some_path"328 self.test_obj = {"some_key": "some_value"}329 @patch("tools.helpers.open")330 @patch("tools.helpers.json.dump")331 def test_to_json(self, mock_json, mock_open):332 under_test = to_json(path=self.test_path, obj=self.test_obj)333 self.assertIsNone(under_test)334 mock_open.assert_called_once()335 mock_json.assert_called_once()336 @patch("tools.helpers.open")337 @patch("tools.helpers.json.load")338 def test_from_json(self, mock_json, mock_open):339 mock_json.return_value = self.test_obj340 under_test = from_json(path=self.test_path)341 self.assertEqual(under_test, self.test_obj)342 mock_open.assert_called_once()343 mock_json.assert_called_once()344 @skip345 def test_to_csv(self):...
test_Trajectory.py
Source:test_Trajectory.py
...37 translated_traj = Trajectory.translate(discrete_traj, 3, 3.5)38 for (x, y) , (test_x, test_y) in zip(translated_traj, discrete_test_traj):39 self.assertAlmostEqual(x, test_x, delta=0.01)40 self.assertAlmostEqual(y, test_y, delta=0.01)41 def test_bounding_box(self):42 discrete_traj = [(0,0), (2,2), (7,1)]43 test_bounding_box = [(0,0), (7,2)]44 bounding_box = Trajectory.bounding_box(discrete_traj)45 self.assertEqual(bounding_box, test_bounding_box)46 def test_get_path_not_starting_at_0(self):47 test_path = Path(Line(start=(0+0j), end=(2+2j)),48 Line(start=(2+2j), end=(7+1j)),49 Line(start=(7+1j), end=(0+0j)))50 expected_result = Trajectory.discretize(Path(Line(start=(2+2j), end=(7+1j)),51 Line(start=(7+1j), end=(0+0j)), Line(start=(0+0j), end=(2+2j))), 1)52 traj = Trajectory()53 traj.paths.append(test_path)54 result = traj.get_path(0, 1, 1)55 self.assertEqual(result, expected_result)...
test_shape.py
Source:test_shape.py
...5def test_segments():6 n = 100007 segment_tests_2d(n)8 segment_tests_3d(n)9def test_bounding_box():10 def check(X,min,max):11 box = bounding_box(X)12 try:13 assert all(box.min==min)14 assert all(box.max==max)15 except:16 print 'check failed: X %s, box %s, expected %s %s'%(X,box,min,max)17 raise18 check([[4,-2],[-1,5]],[-1,-2],[4,5])19 check(asarray([[4.,-2],[-1,5]]),[-1,-2],[4,5])20 check([[[1,2,3],[2,4,2]],[7,-1,-2]],[1,-1,-2],[7,4,3])21def test_consistency():22 random.seed(98183)23 shapes = [24 Sphere((1,2),2),25 Sphere((1,2,3),2),26 Box((-1,-2),(1,2)),27 Box((-1,-2,-3),(1,2,3)),28 Capsule((-.5,-.5),(1,2),1),29 Capsule((-.5,-.5,-.5),(1,2,3),1),30 Cylinder(0,(0,0,1),1),31 Cylinder((-1,-2,-3),(4,2,1),1.5),32 ThickShell(TriangleSoup([(0,1,2)]),[(0,0,0),(1,0,0),(0,1,0)],(.1,.2,.3)),33 ThickShell(TriangleSoup([(0,1,2)]),random.randn(3,3),.2*abs(random.randn(3)))]34 n = 100035 def sv(x):36 return '[%s]'%','.join(map(str,x))37 for shape in shapes:38 print shape39 box = shape.bounding_box()40 scale = max(box.sizes())41 sobol = Sobol(box.thickened(scale/pi))42 inner_box = empty_box(len(box.min))43 small = 1e-4*scale44 for _ in range(n):45 X = sobol.vector()46 if shape.lazy_inside(X):47 inner_box.enlarge(X)48 phi = shape.phi(X)49 normal = shape.normal(X)50 project = X-phi*normal51 phi2 = shape.phi(project)52 #print 'shape %r, X %s, phi %g, normal %s'%(shape,sv(X),phi,sv(normal))53 if abs(phi2)>=small:54 print 'project = %s, X = %s, phi = %g, normal = %s, phi2 = %g, small = %g'%(sv(project),sv(X),phi,sv(normal),phi2,small)55 assert abs(phi2)<small,'%s = %s - %g * %s, abs(%g) !< %g'%(sv(project),sv(X),phi,sv(normal),phi2,small)56 assert shape.lazy_inside(X)==(phi<=0)57 surface = shape.surface(X)58 inner_box.enlarge(surface)59 assert abs(shape.phi(surface))<small, 'X %s, phi %g, surface %s, surface phi %g'%(sv(X),phi,sv(surface),shape.phi(surface))60 surface_phi = magnitude(surface-X)61 assert abs(surface_phi-abs(phi))<small,'%s != %s'%(surface_phi,phi)62 # TODO: test boundary and principal_curvatures63 assert box.lazy_inside(inner_box.min)64 assert box.lazy_inside(inner_box.max)65 box_error = max(box.sizes()-inner_box.sizes())/scale66 if box_error>5e-3:67 print 'box error %g'%box_error68 print 'box %s, sizes %s, volume %g\ninner box %s, sizes %s, volume %g'%(box,box.sizes(),box.volume(),inner_box,inner_box.sizes(),inner_box.volume())69 assert False70"""71def test_generate_triangles():72 tolerance=1e-573 for shape in shapes:74 print shape75 surface=shape.generate_triangles()76 if not isinstance(shape,PLANE_f):77 assert not surface.mesh.non_manifold_nodes()78 assert surface.mesh.orientations_consistent()79 particles=surface.particles80 for X in particles.X:81 assert abs(shape.phi(X))<tolerance82 for t in range(1,len(surface.mesh.elements)+1):83 i,j,k=surface.mesh.elements[t]84 X=(particles.X[i]+particles.X[j]+particles.X[k])/385 shape_normal=shape.normal(X)86 surface_normal=surface.normal(X,t)87 dot=dot(shape_normal,surface_normal)88 assert dot>.7,'(%s) . (%s) = %s'%(shape_normal,surface_normal,dot)89"""90if __name__=='__main__':91 test_segments()92 test_bounding_box()...
TestBoundingBox.py
Source:TestBoundingBox.py
1# Copyright (C) 2014-2021 CEA/DEN, EDF R&D2#3# This library is free software; you can redistribute it and/or4# modify it under the terms of the GNU Lesser General Public5# License as published by the Free Software Foundation; either6# version 2.1 of the License, or (at your option) any later version.7#8# This library is distributed in the hope that it will be useful,9# but WITHOUT ANY WARRANTY; without even the implied warranty of10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11# Lesser General Public License for more details.12#13# You should have received a copy of the GNU Lesser General Public14# License along with this library; if not, write to the Free Software15# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA16#17# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com18#19"""20 Unit test of ...21"""22#=========================================================================23# Initialization of the test24#=========================================================================25import os26import math27from ModelAPI import *28from salome.shaper import model29__updated__ = "2020-11-12"30#=========================================================================31# test Bounding Box32#=========================================================================33def test_Bounding_Box():34 model.begin()35 partSet = model.moduleDocument()36 Part_1 = model.addPart(partSet)37 Part_1_doc = Part_1.document()38 ### Create Cone39 Cone_1 = model.addCone(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 10, 5, 10)40 model.do()41 ### Create BoundingBox42 BoundingBox_1 = model.getBoundingBox(Part_1_doc, model.selection("SOLID", "Cone_1_1"))43 model.end()44 myDelta = 1e-645 Props = model.getGeometryCalculation(Part_1_doc,model.selection("SOLID", "BoundingBox_1_1"))46 print(" Basic Properties:")47 print(" Wires length: ", Props[0])48 print(" Surface area: ", Props[1])49 print(" Volume : ", Props[2])50 aReflength = 20051 aReslength = Props[0]52 assert (math.fabs(aReslength - aReflength) < myDelta), "The surface is wrong: expected = {0}, real = {1}".format(aReflength, aReslength)53 aRefSurface = 160054 aResSurface = Props[1]55 assert (math.fabs(aResSurface - aRefSurface) < myDelta), "The surface is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface)56 aRefVolume = 400057 aResVolume = Props[2]58 assert (math.fabs(aResVolume - aRefVolume) < myDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefVolume, aResVolume)59if __name__ == '__main__':60 test_Bounding_Box()61 #=========================================================================62 # End of test...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!