LEFT | RIGHT |
(no file at all) | |
| 1 # This file is part of trytond_gis. The COPYRIGHT file at the top level of |
| 2 # this repository contains the full copyright notices and license terms. |
| 3 import unittest |
| 4 from sql import Null |
| 5 |
| 6 from trytond.pool import Pool |
| 7 from trytond.tests.test_tryton import activate_module, with_transaction |
| 8 |
| 9 |
| 10 class TestGeographicFields(unittest.TestCase): |
| 11 |
| 12 @classmethod |
| 13 def setUpClass(cls): |
| 14 activate_module('tests') |
| 15 |
| 16 @with_transaction() |
| 17 def test_create_save(self): |
| 18 "Testing create/write with GIS types" |
| 19 pool = Pool() |
| 20 |
| 21 GISPoint = pool.get('test.gis.point') |
| 22 point_a = { |
| 23 'meta': {'srid': '4326'}, |
| 24 'type': 'Point', |
| 25 'coordinates': [1.0, 2.0], |
| 26 } |
| 27 point, = GISPoint.create([{ |
| 28 'point': point_a, |
| 29 }]) |
| 30 self.assertEqual(point.point, point_a) |
| 31 |
| 32 point_b = { |
| 33 'meta': {'srid': '4326'}, |
| 34 'type': 'Point', |
| 35 'coordinates': [2.0, 1.0], |
| 36 } |
| 37 point.point = point_b |
| 38 point.save() |
| 39 |
| 40 reload_point = GISPoint(point.id) |
| 41 self.assertEqual(reload_point.point, point_b) |
| 42 |
| 43 point.point = Null |
| 44 point.save() |
| 45 |
| 46 reload_point = GISPoint(point.id) |
| 47 self.assertIsNone(reload_point.point) |
| 48 |
| 49 @with_transaction() |
| 50 def test_search(self): |
| 51 "Testing search with GIS types" |
| 52 pool = Pool() |
| 53 |
| 54 GISPoint = pool.get('test.gis.point') |
| 55 point_a = { |
| 56 'meta': {'srid': '4326'}, |
| 57 'type': 'Point', |
| 58 'coordinates': [1.0, 2.0], |
| 59 } |
| 60 point_b = { |
| 61 'meta': {'srid': '4326'}, |
| 62 'type': 'Point', |
| 63 'coordinates': [2.0, 1.0], |
| 64 } |
| 65 GISPoint.create([{ |
| 66 'point': point_a, |
| 67 }, { |
| 68 'point': point_a, |
| 69 }, { |
| 70 'point': point_b, |
| 71 }, { |
| 72 }]) |
| 73 |
| 74 points = GISPoint.search([ |
| 75 ('point', '=', point_a), |
| 76 ]) |
| 77 self.assertEqual(len(points), 2) |
| 78 for point in points: |
| 79 self.assertEqual(point.point, point_a) |
| 80 |
| 81 points = GISPoint.search([ |
| 82 ('point', '!=', point_a), |
| 83 ]) |
| 84 # For some reasons NULL are not taken into account |
| 85 self.assertEqual(len(points), 1) |
| 86 self.assertNotEqual(points[0].point, point_a) |
| 87 |
| 88 points = GISPoint.search([ |
| 89 ('point', '=', Null), |
| 90 ]) |
| 91 self.assertEqual(len(points), 1) |
| 92 self.assertIsNone(points[0].point) |
| 93 |
| 94 points = GISPoint.search([ |
| 95 ('point', '!=', Null), |
| 96 ]) |
| 97 self.assertEqual(len(points), 3) |
| 98 for point in points: |
| 99 self.assertIsNotNone(point.point) |
| 100 |
| 101 |
| 102 def suite(): |
| 103 suite_ = unittest.TestSuite() |
| 104 suite_.addTests(unittest.TestLoader().loadTestsFromTestCase( |
| 105 TestGeographicFields)) |
| 106 return suite_ |
LEFT | RIGHT |