11import unittest
22
3+ from process_tracker .models .extract import Location
4+
35from process_tracker .utilities .data_store import DataStore
46from process_tracker .location_tracker import LocationTracker
57
@@ -8,16 +10,55 @@ class TestLocationTracker(unittest.TestCase):
810 @classmethod
911 def setUpClass (cls ):
1012 cls .data_store = DataStore ()
13+ cls .session = cls .data_store .session
14+
15+ @classmethod
16+ def tearDownClass (cls ):
17+ cls .session .close ()
18+
19+ def tearDown (self ):
20+ self .session .query (Location ).delete ()
21+ self .session .commit ()
22+
23+ def test_derive_location_name_no_trailing_slash_local (self ):
24+ """
25+ Testing that if no location name is provided, and it's not a location already, the last directory is set as the
26+ location name even if a trailing slash is not provided.
27+ :return:
28+ """
29+ test_path = "/tmp/testing/test_dir"
30+
31+ expected_result = "local - test_dir"
32+ given_result = LocationTracker (
33+ location_path = test_path , data_store = self .data_store
34+ ).location_name
35+
36+ self .assertEqual (expected_result , given_result )
37+
38+ def test_derive_location_name_no_trailing_slash_s3 (self ):
39+ """
40+ Testing that if no location name is provided, and it's not a location already, the last directory is set as the
41+ location name even if a trailing slash is not provided.
42+ :return:
43+ """
44+ test_path = "s3://tmp/testing/test_dir"
45+
46+ expected_result = "s3 tmp - test_dir"
47+ given_result = LocationTracker (
48+ location_path = test_path , data_store = self .data_store
49+ ).location_name
50+
51+ self .assertEqual (expected_result , given_result )
1152
1253 def test_derive_location_name_none (self ):
1354 """
1455 Testing that if no location name is provided, and it's not a location path, the last directory is set as the
1556 location name.
1657 :return:
1758 """
18- test_path = "/tmp/testing/test_dir"
59+ test_path = "/tmp/testing/test_dir/ "
1960
20- expected_result = "test_dir"
61+ expected_result = "local - test_dir"
2162 given_result = LocationTracker (
2263 location_path = test_path , data_store = self .data_store
2364 ).location_name
@@ -29,9 +70,9 @@ def test_derive_location_name_s3(self):
2970 Testing that if no location name is provided, and it's an s3 location path, the s3 prefix is added.
3071 :return:
3172 """
32- test_path = "s3://tmp/testing/test_dir"
73+ test_path = "s3://tmp/testing/test_dir/ "
3374
34- expected_result = "s3 - test_dir"
75+ expected_result = "s3 tmp - test_dir"
3576 given_result = LocationTracker (
3677 location_path = test_path , data_store = self .data_store
3778 ).location_name
@@ -121,3 +162,78 @@ def test_determine_location_bucket_name_local(self):
121162 given_result = location .location .location_bucket_name
122163
123164 self .assertEqual (expected_result , given_result )
165+
166+ def test_determine_location_name_duplicate_name_s3 (self ):
167+ """
168+ Testing that if two different s3 locations produce the same location name
169+ that the second location will append a number to ensure uniqueness.
170+ :return:
171+ """
172+ expected_result = "s3 duplicate-test - dir - 1"
173+
174+ location = LocationTracker (
175+ location_path = "https://duplicate-test.s3.amazonaws.com/this/is/a/test/dir/file.txt" ,
176+ data_store = self .data_store ,
177+ )
178+
179+ dupe_location = LocationTracker (
180+ location_path = "https://duplicate-test.s3.amazonaws.com/this/is/another/test/dir/file.txt" ,
181+ data_store = self .data_store ,
182+ )
183+
184+ given_result = dupe_location .location .location_name
185+
186+ self .assertEqual (expected_result , given_result )
187+
188+ def test_determine_location_name_duplicate_name_local (self ):
189+ """
190+ Testing that if two different s3 locations produce the same location name
191+ that the second location will append a number to ensure uniqueness.
192+ :return:
193+ """
194+ expected_result = "local - test_dir - 1"
195+
196+ location = LocationTracker (
197+ location_path = "/tmp/duplicate_testing/test_dir/file.txt" ,
198+ data_store = self .data_store ,
199+ )
200+
201+ dupe_location = LocationTracker (
202+ location_path = "/tmp/duplicate_testing_another/test_dir/file.txt" ,
203+ data_store = self .data_store ,
204+ )
205+
206+ given_result = dupe_location .location .location_name
207+
208+ self .assertEqual (expected_result , given_result )
209+
210+ def test_determine_location_name_file_not_part_s3 (self ):
211+ """
212+ Testing that when a s3 path is provided with a filename at the end, the file is ignored.
213+ :return:
214+ """
215+ expected_result = "s3 test-bucket - dir"
216+
217+ location = LocationTracker (
218+ location_path = "https://test-bucket.s3.amazonaws.com/this/is/a/test/dir/file.txt" ,
219+ data_store = self .data_store ,
220+ )
221+
222+ given_result = location .location .location_name
223+
224+ self .assertEqual (expected_result , given_result )
225+
226+ def test_determine_location_name_file_not_part_local (self ):
227+ """
228+ Testing that when a local path is provided with a filename at the end, the file is ignored.
229+ :return:
230+ """
231+ expected_result = "local - path"
232+
233+ location = LocationTracker (
234+ location_path = "/local/dir/path/text.txt" , data_store = self .data_store
235+ )
236+
237+ given_result = location .location .location_name
238+
239+ self .assertEqual (expected_result , given_result )
0 commit comments