def test_table_r_friendly_preview(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()
        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/",
            data={"headings_type": "es_r_friendly"},
            content_type="application/json",
        )

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/preview/")
        assert len(response.json()) == 1
        data = response.json()[0]
        assert set(data.keys()) == {
            "id",
            "name",
            "preview",
            "column_headings",
            "heading",
            "should_split",
            "parent",
            "include",
        }
 def test_list_tables(self):
     selection = create_data_selection(self.client, self.datasource,
                                       self.url_prefix)
     response = self.client.get(
         f"{self.url_prefix}{self.datasource.id}/selections/{selection['id']}/tables/"
     )
     assert len(response.json()) == 2
 def setUp(self, client, upload_obj_validated):
     selection = create_data_selection(client, upload_obj_validated,
                                       self.url_prefix)
     self.selection_id = selection["id"]
     self.flattens_url = f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/flattens/"
     self.datasource = upload_obj_validated
     self.client = client
    def test_table_split_include_preview(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/",
            data={"split": True},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK
        array_tables = response.json()["array_tables"]

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/",
            data={"headings_type": "es_r_friendly"},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{array_tables[0]['id']}/",
            data={"include": False},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/preview/")
        assert len(response.json()) == 4
        data = response.json()[0]
        assert set(data.keys()) == {
            "id",
            "name",
            "preview",
            "heading",
            "column_headings",
            "should_split",
            "parent",
            "include",
        }
    def test_table_preview(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/preview/")
        assert len(response.json()) == 1
        data = response.json()[0]
        assert set(data.keys()) == {
            "id", "name", "preview", "heading", "should_split", "parent",
            "include"
        }
    def test_table_preview_file_not_found(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()

        mocked_open = self.mocker.patch("core.views.open")
        mocked_open.return_value.__enter__.side_effect = FileNotFoundError(
            errno.ENOENT, "File not found.")

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/preview/")
        assert response.status_code == status.HTTP_404_NOT_FOUND
        assert response.json() == {"detail": "Datasource expired."}
    def test_table_split_file_not_found(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()
        mocked_open = self.mocker.patch("core.views.DataPreprocessor.restore")
        mocked_open.side_effect = FileNotFoundError(errno.ENOENT,
                                                    "File not found.")

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/",
            data={"split": True},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_404_NOT_FOUND
        assert response.json() == {"detail": "Datasource expired."}
    def test_table_split_preview_no_left_space(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        tables = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        ).json()

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/",
            data={"split": True},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK
        array_tables = response.json()["array_tables"]

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/",
            data={"headings_type": "es_r_friendly"},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{array_tables[0]['id']}/",
            data={"include": False},
            content_type="application/json",
        )
        assert response.status_code == status.HTTP_200_OK

        mocked_split = self.mocker.patch("core.views.store_preview_csv")
        mocked_split.side_effect = OSError(errno.ENOSPC, "No left space.")

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
            f"{tables[0]['id']}/preview/")
        assert response.status_code == status.HTTP_413_REQUEST_ENTITY_TOO_LARGE
        assert response.json() == {
            "detail":
            "Currently, the space limit was reached. Please try again later."
        }
    def test_delete_table(self):
        selection = create_data_selection(self.client,
                                          self.validated_datasource,
                                          self.url_prefix)
        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/"
        )
        assert len(response.json()) == 2

        table_data = response.json()[0]
        assert table_data["include"]

        response = self.client.patch(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/{table_data['id']}/",
            content_type="application/json",
            data={"include": False},
        )
        assert response.status_code == status.HTTP_200_OK
        assert not response.json()["include"]

        response = self.client.get(
            f"{self.url_prefix}{self.validated_datasource.id}/selections/{selection['id']}/tables/{table_data['id']}/"
        )
        assert not response.json()["include"]
 def test_create_selections_successful(self):
     create_data_selection(self.client, self.datasource, self.url_prefix)