|
8 | 8 |
|
9 | 9 | import boto3 |
10 | 10 | import botocore |
11 | | -import moto |
| 11 | +from moto import mock_s3 |
12 | 12 | from sqlalchemy.orm import aliased, Session |
13 | 13 |
|
14 | 14 | from process_tracker.models.extract import ( |
@@ -568,42 +568,102 @@ def test_register_extracts_by_location_local(self): |
568 | 568 |
|
569 | 569 | self.assertCountEqual(expected_result, given_result) |
570 | 570 |
|
571 | | - # def test_register_extracts_by_location_s3(self): |
572 | | - # """ |
573 | | - # Testing that when the location is s3, all the extracts are registered and set to 'ready' status. |
574 | | - # The process/extract relationship should also be set to 'ready' since that is the last status the process set |
575 | | - # the extracts to. |
576 | | - # :return: |
577 | | - # """ |
578 | | - # process_status = aliased(ExtractStatus) |
579 | | - # extract_status = aliased(ExtractStatus) |
580 | | - # |
581 | | - # expected_keys = 'test_local_dir_1.csv', 'test_local_dir_2.csv' |
582 | | - # |
583 | | - # client |
584 | | - # |
585 | | - # with moto.mock_s3(): |
586 | | - # conn = boto3.resource('s3', region_name='us-east-1') |
587 | | - # conn.create_bucket(Bucket='test_bucket') |
588 | | - # |
589 | | - # for file in expected_keys: |
590 | | - # conn.Object('test_bucket', file) |
591 | | - # |
592 | | - # self.process_tracker.register_extracts_by_location(location_path='s3://test_bucket') |
593 | | - # |
594 | | - # extracts = self.session.query(Extract.extract_filename, extract_status.extract_status_name, process_status.extract_status_name)\ |
595 | | - # .join(ExtractProcess, Extract.extract_id == ExtractProcess.extract_tracking_id) \ |
596 | | - # .join(extract_status, Extract.extract_status_id == extract_status.extract_status_id) \ |
597 | | - # .join(process_status, ExtractProcess.extract_process_status_id == process_status.extract_status_id) \ |
598 | | - # .filter(ExtractProcess.process_tracking_id == self.process_tracker.process_tracking_run.process_tracking_id) |
599 | | - # |
600 | | - # given_result = [[extracts[0].extract_filename, extracts[0].extract_status_name, extracts[0].extract_status_name] |
601 | | - # ,[extracts[1].extract_filename, extracts[1].extract_status_name, extracts[1].extract_status_name]] |
602 | | - # |
603 | | - # expected_result = [['test_local_dir_1.csv', 'ready', 'ready'] |
604 | | - # , ['test_local_dir_2.csv', 'ready', 'ready']] |
605 | | - # |
606 | | - # self.assertEqual(expected_result, given_result) |
| 571 | + @mock_s3 |
| 572 | + def test_register_extracts_by_location_s3(self): |
| 573 | + """ |
| 574 | + Testing that when the location is s3, all the extracts are registered and set to 'ready' status. |
| 575 | + The process/extract relationship should also be set to 'ready' since that is the last status the process set |
| 576 | + the extracts to. |
| 577 | + :return: |
| 578 | + """ |
| 579 | + process_status = aliased(ExtractStatus) |
| 580 | + extract_status = aliased(ExtractStatus) |
| 581 | + test_bucket = "test_bucket" |
| 582 | + |
| 583 | + expected_keys = ["test_local_dir_1.csv", "test_local_dir_2.csv"] |
| 584 | + |
| 585 | + client = boto3.client( |
| 586 | + "s3", |
| 587 | + region_name="us-east-1", |
| 588 | + aws_access_key_id="fake_access_key", |
| 589 | + aws_secret_access_key="fake_secret_key", |
| 590 | + ) |
| 591 | + try: |
| 592 | + s3 = boto3.resource( |
| 593 | + "s3", |
| 594 | + region_name="us-east-1", |
| 595 | + aws_access_key_id="fake_access_key", |
| 596 | + aws_secret_access_key="fake_secret_key", |
| 597 | + ) |
| 598 | + |
| 599 | + s3.meta.client.head_bucket(Bucket=test_bucket) |
| 600 | + except botocore.exceptions.ClientError: |
| 601 | + pass |
| 602 | + else: |
| 603 | + err = "%s should not exist" % test_bucket |
| 604 | + raise EnvironmentError(err) |
| 605 | + |
| 606 | + client.create_bucket(Bucket=test_bucket) |
| 607 | + |
| 608 | + current_dir = os.path.dirname(__file__) |
| 609 | + fixtures_dir = os.path.join(current_dir, "fixtures") |
| 610 | + |
| 611 | + for file in expected_keys: |
| 612 | + |
| 613 | + key = os.path.join(test_bucket, file) |
| 614 | + |
| 615 | + print(file) |
| 616 | + print(key) |
| 617 | + print(fixtures_dir) |
| 618 | + |
| 619 | + file = os.path.join(fixtures_dir, file) |
| 620 | + client.upload_file(Filename=file, Bucket=test_bucket, Key=key) |
| 621 | + |
| 622 | + self.process_tracker.register_extracts_by_location( |
| 623 | + location_path="s3://test_bucket" |
| 624 | + ) |
| 625 | + |
| 626 | + extracts = ( |
| 627 | + self.session.query( |
| 628 | + Extract.extract_filename, |
| 629 | + extract_status.extract_status_name, |
| 630 | + process_status.extract_status_name, |
| 631 | + ) |
| 632 | + .join( |
| 633 | + ExtractProcess, Extract.extract_id == ExtractProcess.extract_tracking_id |
| 634 | + ) |
| 635 | + .join( |
| 636 | + extract_status, |
| 637 | + Extract.extract_status_id == extract_status.extract_status_id, |
| 638 | + ) |
| 639 | + .join( |
| 640 | + process_status, |
| 641 | + ExtractProcess.extract_process_status_id |
| 642 | + == process_status.extract_status_id, |
| 643 | + ) |
| 644 | + .filter( |
| 645 | + ExtractProcess.process_tracking_id |
| 646 | + == self.process_tracker.process_tracking_run.process_tracking_id |
| 647 | + ) |
| 648 | + ) |
| 649 | + |
| 650 | + given_result = list() |
| 651 | + |
| 652 | + for extract in extracts: |
| 653 | + given_result.append( |
| 654 | + [ |
| 655 | + extract.extract_filename, |
| 656 | + extract.extract_status_name, |
| 657 | + extract.extract_status_name, |
| 658 | + ] |
| 659 | + ) |
| 660 | + |
| 661 | + expected_result = [ |
| 662 | + ["test_bucket/test_local_dir_1.csv", "ready", "ready"], |
| 663 | + ["test_bucket/test_local_dir_2.csv", "ready", "ready"], |
| 664 | + ] |
| 665 | + |
| 666 | + self.assertEqual(expected_result, given_result) |
607 | 667 |
|
608 | 668 | def test_register_new_process_run(self): |
609 | 669 | """ |
|
0 commit comments