File size: 7,586 Bytes
6387ca7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | diff --git a/test/data/dataset/test_dataframe_upload.py b/test/data/dataset/test_dataframe_upload.py
new file mode 100644
index 00000000..f0ce5690
--- /dev/null
+++ b/test/data/dataset/test_dataframe_upload.py
@@ -0,0 +1,190 @@
+# -*- coding: utf-8 -*-
+
+
+
+
+
+
+
+import unittest
+import pandas as pd
+
+from cartoframes.data import Dataset, StrategiesRegistry
+from cartoframes.data.dataset.registry.base_dataset import BaseDataset
+from cartoframes.lib import context
+
+
+class CapturingContext():
+
+
+ def __init__(self):
+ self.copy_query = None
+ self.copy_data = None
+ self.long_running_query = None
+
+ def upload(self, query, data):
+ self.copy_query = query
+ self.copy_data = list(data)
+
+ def execute_query(self, query, **kwargs):
+ return {'rows': [{'current_schema': 'public'}]}
+
+ def execute_long_running_query(self, query):
+ self.long_running_query = query
+
+ def get_schema(self):
+ return 'public'
+
+
+class TestDataFrameUpload(unittest.TestCase):
+ def setUp(self):
+ self._capture = CapturingContext()
+ self._original_create_context = context.create_context
+ context.create_context = lambda credentials: self._capture
+
+ self._original_exists = BaseDataset.exists
+ BaseDataset.exists = lambda instance: False
+
+ def tearDown(self):
+ context.create_context = self._original_create_context
+ BaseDataset.exists = self._original_exists
+ StrategiesRegistry.instance = None
+
+ def _upload(self, df, **kwargs):
+ kwargs.setdefault('table_name', 'fake_table')
+ kwargs.setdefault('credentials', 'fake')
+ Dataset(df).upload(**kwargs)
+ return self._capture
+
+
+
+ def test_cartodb_id_and_the_geom_are_both_kept(self):
+ df = pd.DataFrame.from_dict({'cartodb_id': [1], 'the_geom': ['POINT (1 1)']})
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(cartodb_id,the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1|SRID=4326;POINT (1 1)\n'])
+
+ def test_create_table_has_no_dangling_comma(self):
+ df = pd.DataFrame.from_dict({'cartodb_id': [1], 'the_geom': ['POINT (1 1)']})
+ cap = self._upload(df)
+
+ self.assertIn(
+ 'CREATE TABLE fake_table (cartodb_id bigint, the_geom geometry(Point, 4326))',
+ cap.long_running_query)
+ self.assertNotIn('(,', cap.long_running_query)
+
+ def test_single_geometry_column_is_renamed_to_the_geom(self):
+ df = pd.DataFrame([['POINT (1 1)']], columns=['geom'])
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'SRID=4326;POINT (1 1)\n'])
+
+ def test_several_geometry_columns_prioritize_the_geom(self):
+ df = pd.DataFrame([['POINT (0 0)', 'POINT (1 1)', 'POINT (2 2)']],
+ columns=['geom', 'the_geom', 'geometry'])
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(geom,the_geom,geometry) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'POINT (0 0)|SRID=4326;POINT (1 1)|POINT (2 2)\n'])
+
+ def test_the_geom_webmercator_is_dropped(self):
+ df = pd.DataFrame([[1, 'POINT (1 1)', 'fake']],
+ columns=['cartodb_id', 'the_geom', 'the_geom_webmercator'])
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(cartodb_id,the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1|SRID=4326;POINT (1 1)\n'])
+
+ def test_ordinary_columns_without_geometry(self):
+ df = pd.DataFrame([[1, True, 'text']], columns=['col1', 'col2', 'col3'])
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(col1,col2,col3) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1|True|text\n'])
+
+ def test_column_names_are_normalized(self):
+ df = pd.DataFrame([[1]], columns=['My Column'])
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(my_column) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1\n'])
+
+ def test_null_values_become_empty_fields(self):
+ df = pd.DataFrame.from_dict({'test': [None, [None, None]]})
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(test) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'\n', b'\n'])
+
+ def test_null_geometry_becomes_empty_field(self):
+ df = pd.DataFrame.from_dict({'test': [None], 'the_geom': [None]})
+ cap = self._upload(df)
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(test,the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'|\n'])
+
+
+
+ def test_with_lnglat_appends_synthesized_geometry(self):
+ df = pd.DataFrame([[1, 1]], columns=['lng', 'lat'])
+ cap = self._upload(df, with_lnglat=('lng', 'lat'))
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(lng,lat,the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1|1|SRID=4326;POINT (1 1)\n'])
+
+ def test_with_lnglat_drops_existing_geometry(self):
+ df = pd.DataFrame([[1, 1, 'POINT (2 2)']], columns=['lng', 'lat', 'the_geom'])
+ cap = self._upload(df, with_lnglat=('lng', 'lat'))
+
+ self.assertEqual(
+ cap.copy_query,
+ "COPY fake_table(lng,lat,the_geom) FROM stdin WITH (FORMAT csv, DELIMITER '|');")
+ self.assertEqual(cap.copy_data, [b'1|1|SRID=4326;POINT (1 1)\n'])
+
+ def test_with_lnglat_synthesized_geometry_in_create_table(self):
+ df = pd.DataFrame([[1, 1]], columns=['lng', 'lat'])
+ cap = self._upload(df, with_lnglat=('lng', 'lat'))
+
+ self.assertIn(
+ 'CREATE TABLE fake_table (lng bigint, lat bigint, the_geom geometry(Point, 4326))',
+ cap.long_running_query)
+
+
+
+ def test_create_table_type_mapping(self):
+ df = pd.DataFrame.from_dict({
+ 'a_int': pd.Series([1], dtype='int64'),
+ 'a_float': pd.Series([1.0], dtype='float64'),
+ 'a_text': pd.Series(['x'], dtype='object'),
+ 'a_bool': pd.Series([True], dtype='bool'),
+ })
+ cap = self._upload(df)
+
+ self.assertIn('a_int bigint', cap.long_running_query)
+ self.assertIn('a_float numeric', cap.long_running_query)
+ self.assertIn('a_text text', cap.long_running_query)
+ self.assertIn('a_bool boolean', cap.long_running_query)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+
+
+
+
+set -euo pipefail
+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+cd /testbed
+
+
+
+
+python -m pytest test/utils/test_columns.py -q
+
+
+python -m pytest test/data/dataset/test_dataframe_upload.py -q
|