from api.test_utils import DefaultUserTestCase, create_client
from assignments.factories import AssignmentFactory, StudentSubmissionFactory


class MyAssignmentsTest(DefaultUserTestCase):
    def setUp(self):
        super(MyAssignmentsTest, self).setUp()
        self.assignment = AssignmentFactory(owner=self.teacher)

        self.submission1 = StudentSubmissionFactory(
            student=self.student1, assignment=self.assignment
        )
        self.submission2 = StudentSubmissionFactory(
            student=self.student2, assignment=self.assignment
        )

        self.client = create_client(self.student1)

    def query_my_assignments(self):
        query = """
                query MyActivityQuery {
                  myActivity {
                    edges {
                      node {
                        id
                        title
                        slug
                        metaTitle
                        mySubmissions {
                          edges {
                            node {
                              id
                              text
                              assignment {
                                id
                                title
                              }
                            }
                          }
                        }
                        myAnswers {
                          edges {
                            node {
                              id
                              survey {
                                id
                                title
                              }
                            }
                          }
                        }
                        myContentBookmarks {
                          edges {
                            node {
                              id
                              uuid
                              note {
                                id
                                text
                              }
                              contentBlock {
                                id
                                type
                                contents
                              }
                            }
                          }
                        }
                        myChapterBookmarks {
                          edges {
                            node {
                              id
                              note {
                                id
                                text
                              }
                              chapter {
                                id
                                title
                                description
                              }
                            }
                          }
                        }
                        bookmark {
                          id
                          note {
                            id
                            text
                          }
                          module {
                            id
                            teaser
                            metaTitle
                            intro
                          }
                        }
                      }
                    }
                  }
                  myInstrumentActivity {
                      edges {
                        node {
                          id
                        title
                        contents
                        type {
                          id
                          type
                          category {
                            id
                            name
                          }
                        }
                        slug
                          bookmarks {
                            id
                          uuid
                          note {
                            id
                            text
                          }
                          }
                        }
                      }
                    }
                }

                """

        result = self.client.execute(query)

        self.assertIsNone(result.get("errors"))
        return result

    @staticmethod
    def get_content(result):
        return result.get("data").get("myActivity").get("edges")

    def test_my_assignment_query(self):
        result = self.query_my_assignments()
        contents = self.get_content(result)
        self.assertEqual(len(contents), 1)
        self.assertEqual(
            contents[0]
            .get("node")
            .get("mySubmissions")
            .get("edges")[0]
            .get("node")
            .get("text"),
            self.submission1.text,
        )
