#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2017 EMBL - European Bioinformatics Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import sys

# import fixtures
from test_utils.emg_fixtures import *  # noqa


@pytest.mark.skipif(sys.version_info < (3, 6),
                    reason="requires Python 3.6")
@pytest.mark.django_db
def test_list_samples(live_server, runs):
    """
    List samples and its metadata for given study
    """

    from jsonapi_client import Session

    with Session('%s/v0.2/' % live_server.url) as s:
        study = s.get('studies', 'SRP0025').resource
        assert study.accession == 'SRP0025'
        # list samples
        sample_list = study.samples
        assert len(sample_list) == 1
        for sample in sample_list:
            # list runs
            run_list = sample.runs
            assert len(run_list) == 1
            for run in run_list:
                print(
                    study.accession,
                    study.project_id,
                    sample.accession,
                    # sample.biome.biome_name,
                    sample.runs_count,
                    run.accession,
                    run.experiment_type,
                )

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2017 EMBL - European Bioinformatics Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import os
import sys

from jsonapi_client import Session

from django.core.management import call_command

# import fixtures
from test_utils.emg_fixtures import *  # noqa


@pytest.mark.skipif(sys.version_info < (3, 6),
                    reason="requires Python 3.6")
@pytest.mark.usefixtures('mongodb')
@pytest.mark.django_db
class TestExamples(object):

    def test_list(self, live_server, runs):
        """
        List samples and its metadata for given study
        """

        with Session('%s/v0.2/' % live_server.url) as s:
            study = s.get('studies', 'SRP0025').resource
            assert study.accession == 'SRP0025'
            # list samples
            sample_list = study.samples
            assert len(sample_list) == 1
            for sample in sample_list:
                # list runs
                run_list = sample.runs
                assert len(run_list) == 1
                for run in run_list:
                    print(
                        study.accession,
                        study.project_id,
                        sample.accession,
                        sample.biome.biome_name,
                        sample.runs_count,
                        run.accession,
                        run.experiment_type.experiment_type,
                    )

    def test_annotations(self, live_server, run):
        call_command('import_summary', 'ABC01234',
                     os.path.join(
                         os.path.dirname(os.path.abspath(__file__)),
                         'metadata'
                     ), suffix='.go_slim')

        with Session('%s/v0.2/' % live_server.url) as s:
            run = s.get('runs', 'ABC01234').resource
            for analysis in run.analysis:
                go_terms = []
                for go in analysis.go_slim:
                    go_terms.append(go.accession)
                expected = ['GO:0006810', 'GO:0008152', 'GO:0055114']
                assert go_terms == expected

add example test for annotations