Beispiel #1
0
    def test_uniqueness(self):
        """
        Tests if the uniqueness of the model is handled properly
        :return: None
        """
        media_list, user = self.generate_sample_media_list()
        standard_kwargs = media_list.__json__(False)
        standard_kwargs.pop("id")
        standard_kwargs["service"] = media_list.service
        standard_kwargs["media_type"] = media_list.media_type

        try:
            duplicate = MediaList(**standard_kwargs)
            db.session.add(duplicate)
            db.session.commit()
            self.fail()
        except IntegrityError:
            db.session.rollback()

        for key, value in [("name", "ABABABABA"),
                           ("user_id", self.generate_sample_user(True)[0].id),
                           ("service", ListService.ANIMEPLANET),
                           ("media_type", MediaType.ANIME)]:
            kwargs = dict(standard_kwargs)
            kwargs[key] = value

            generated = MediaList(**kwargs)
            db.session.add(generated)
            db.session.commit()
Beispiel #2
0
 def generate_sample_media_list(self) -> Tuple[MediaList, User]:
     """
     Generates a sample media item
     :return: The media list and the associated user
     """
     user = self.generate_sample_user(True)[0]
     media_list = MediaList(name="ABC",
                            user=user,
                            service=ListService.ANILIST,
                            media_type=MediaType.MANGA)
     db.session.add(media_list)
     db.session.commit()
     return media_list, user
Beispiel #3
0
 def test_hashing(self):
     """
     Tests using the model objects as keys in a dictionary
     :return: None
     """
     media_list, user = self.generate_sample_media_list()
     media_list_2 = MediaList(name="XYZ",
                              user=user,
                              service=ListService.ANILIST,
                              media_type=MediaType.MANGA)
     db.session.add(media_list_2)
     db.session.commit()
     mapping = {media_list: 100, media_list_2: 200}
     self.assertEqual(mapping[media_list], 100)
     self.assertEqual(mapping[media_list_2], 200)
Beispiel #4
0
 def test_equality(self):
     """
     Tests checking equality for model objects
     :return: None
     """
     media_list, user = self.generate_sample_media_list()
     media_list_2 = MediaList(name="XYZ",
                              user=user,
                              service=ListService.ANILIST,
                              media_type=MediaType.MANGA)
     db.session.add(media_list_2)
     db.session.commit()
     self.assertEqual(media_list, media_list)
     self.assertNotEqual(media_list, media_list_2)
     self.assertNotEqual(media_list, 100)
    def test_uniqueness(self):
        """
        Tests if the uniqueness of the model is handled properly
        :return: None
        """
        media_list_item, media_list, media_user_state, \
            user, media_item, media_id = \
            self.generate_sample_media_list_item()

        standard_kwargs = media_list_item.__json__(False)
        standard_kwargs.pop("id")

        media_list_kwargs = media_list.__json__(False)
        media_list_kwargs.pop("id")
        media_list_kwargs["service"] = ListService.KITSU
        media_list_kwargs["media_type"] = MediaType.ANIME
        new_media_list = MediaList(**media_list_kwargs)
        db.session.add(new_media_list)
        db.session.commit()

        media_user_state_kwargs = media_user_state.__json__(False)
        media_user_state_kwargs.pop("id")
        media_user_state_kwargs["user_id"] = \
            self.generate_sample_user(True)[0].id
        media_user_state_kwargs["consuming_state"] \
            = media_user_state.consuming_state
        new_media_user_state = MediaUserState(**media_user_state_kwargs)
        db.session.add(new_media_user_state)
        db.session.commit()

        try:
            duplicate = MediaListItem(**standard_kwargs)
            db.session.add(duplicate)
            db.session.commit()
            self.fail()
        except IntegrityError:
            db.session.rollback()

        for key, value in [("media_list_id", new_media_list.id),
                           ("media_user_state_id", new_media_user_state.id)]:
            kwargs = dict(standard_kwargs)
            kwargs[key] = value

            generated = MediaListItem(**kwargs)
            db.session.add(generated)
            db.session.commit()
 def generate_sample_media_list_item(
     self
 ) -> Tuple[MediaListItem, MediaList, MediaUserState, User, MediaItem,
            MediaId]:
     """
     Generates a media list item
     :return: The media list item, media list, media user state, user,
              media item and media id
     """
     user = self.generate_sample_user(True)[0]
     media_item = MediaItem(
         media_type=MediaType.MANGA,
         media_subtype=MediaSubType.MANGA,
         english_title="Fly Me to the Moon",
         romaji_title="Tonikaku Cawaii",
         cover_url="https://s4.anilist.co/file/anilistcdn/media/manga/"
         "cover/medium/nx101177-FjjD5UWB3C3t.png",
         latest_release=None,
         releasing_state=ReleasingState.RELEASING)
     media_id = MediaId(media_item=media_item,
                        service_id="101177",
                        service=ListService.ANILIST,
                        media_type=media_item.media_type,
                        media_subtype=media_item.media_subtype)
     media_user_state = MediaUserState(
         media_id=media_id,
         user=user,
         progress=10,
         score=75,
         consuming_state=ConsumingState.CURRENT)
     media_list = MediaList(name="ABC",
                            user=user,
                            service=ListService.ANILIST,
                            media_type=MediaType.MANGA)
     media_list_item = MediaListItem(media_list=media_list,
                                     media_user_state=media_user_state)
     db.session.add(media_item)
     db.session.add(media_id)
     db.session.add(media_user_state)
     db.session.add(media_list)
     db.session.add(media_list_item)
     db.session.commit()
     return media_list_item, media_list, media_user_state, \
         user, media_item, media_id
Beispiel #7
0
def do_media_item_inserts():
    """
    Preforms media item insertions
    :return: None
    """
    # Prepare existing DB content
    media_items: Dict[Tuple, MediaItem] = {
        x.identifier_tuple: x for x in
        MediaItem.query.all()
    }
    media_ids: List[MediaId] = MediaId.query\
        .options(db.joinedload(MediaId.media_item)) \
        .options(db.joinedload(MediaId.media_user_states)) \
        .all()
    media_service_items: \
        Dict[ListService, Dict[MediaType, Dict[str, MediaId]]] = \
        {x: {y: {} for y in MediaType} for x in ListService}
    for media_id in media_ids:
        service = media_id.service
        media_type = media_id.media_item.media_type
        service_id = media_id.service_id
        media_service_items[service][media_type][service_id] = media_id
    media_lists: Dict[Tuple, MediaList] = {
        x.identifier_tuple: x for x in
        MediaList.query.options(db.joinedload(MediaList.media_list_items))
        .all()
    }

    # Add media list items, ids and user states
    while len(media_item_queue) > 0:
        queued = media_item_queue.pop(0)
        params, service, service_ids, user_state_params, list_params = queued

        service_id = service_ids.get(service)
        if service_id is None:
            app.logger.error(f"Missing service ID for service {service}")

        media_type = params["media_type"]

        app.logger.debug(f"Inserting {params['romaji_title']} into database")

        if service_id in media_service_items[service]:
            media_id = media_service_items[service][media_type][service_id]
            media_item = media_id.media_item
            media_item.update(MediaItem(**params))
        else:
            generated = MediaItem(**params)
            identifier = generated.identifier_tuple
            existing = media_items.get(identifier)
            if existing is None:
                media_item = generated
                db.session.add(media_item)
                media_items[identifier] = media_item
            else:
                media_item = existing
                media_item.update(generated)

        # Insert Media IDs
        for extra_service, extra_id in service_ids.items():

            associated = media_service_items[extra_service][media_type].get(
                extra_id
            )

            if associated is None:
                media_id = MediaId(
                    media_item=media_item, service=extra_service,
                    service_id=extra_id
                )
                db.session.add(media_id)
                db.session.commit()
                media_service_items[extra_service][media_type][extra_id] \
                    = media_id
            elif associated.media_item.id != media_item.id:
                associated.media_item_id = media_item.id

        # Insert User States
        if user_state_params is not None:
            media_id = media_service_items[service][media_type][service_id]
            user_state_params["media_id_id"] = media_id.id
            generated = MediaUserState(**user_state_params)
            existing_user_states = {
                x.user_id: x for x in media_id.media_user_states
            }
            user_id = user_state_params["user_id"]
            existing_user_state = existing_user_states.get(user_id)

            if existing_user_state is None:
                db.session.add(generated)
                user_state = generated
                db.session.commit()
            else:
                existing_user_state.update(generated)
                user_state = existing_user_state

            if list_params is not None:
                generated = MediaList(**list_params)
                identifier = generated.identifier_tuple
                existing_list = media_lists.get(identifier)

                if existing_list is None:
                    db.session.add(generated)
                    db.session.commit()
                    media_list = generated
                    media_lists[identifier] = media_list
                else:
                    existing_list.update(generated)
                    media_list = existing_list

                media_list_item_ids = [
                    x.media_user_state_id for x in media_list.media_list_items
                ]
                if user_state.id not in media_list_item_ids:
                    list_item = MediaListItem(
                        media_list_id=media_list.id,
                        media_user_state_id=user_state.id
                    )
                    db.session.add(list_item)