def test_get_financial_comparisons(recorder):
    result_tuple = polygon_model.get_similar_companies(
        ticker="TSLA",
        us_only=True,
    )

    recorder.capture(result_tuple)
    def call_getpoly(self, other_args: List[str]):
        """Process get command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="getpoly",
            description=
            """Get similar companies from polygon to compare with.""",
        )

        try:
            ns_parser = parse_known_args_and_warn(parser, other_args)
            if not ns_parser:
                return
            self.similar, self.user = polygon_model.get_similar_companies(
                self.ticker)

            if self.ticker.upper() in self.similar:
                self.similar.remove(self.ticker.upper())

            if len(self.similar) > 10:
                random.shuffle(self.similar)
                self.similar = sorted(self.similar[:10])
                print(
                    "The limit of stocks to compare with are 10. Hence, 10 random similar stocks will be displayed.\n",
                )

            if self.similar:
                print(
                    f"[{self.user}] Similar Companies: {', '.join(self.similar)}",
                    "\n")

        except Exception as e:
            print(e, "\n")
Esempio n. 3
0
    def call_getpoly(self, other_args: List[str]):
        """Process get command"""
        parser = argparse.ArgumentParser(
            add_help=False,
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="getpoly",
            description="""Get similar companies from polygon to compare with.""",
        )
        parser.add_argument(
            "-u",
            "--us_only",
            action="store_true",
            default=False,
            dest="us_only",
            help="Show only stocks from the US stock exchanges",
        )
        parser.add_argument(
            "-l",
            "--limit",
            default=10,
            dest="limit",
            type=check_positive,
            help="Limit of stocks to retrieve.",
        )
        if other_args and "-" not in other_args[0][0]:
            other_args.insert(0, "-l")
        ns_parser = parse_known_args_and_warn(parser, other_args)
        if ns_parser:
            if self.ticker:
                self.similar, self.user = polygon_model.get_similar_companies(
                    self.ticker, ns_parser.us_only
                )

                if self.ticker.upper() in self.similar:
                    self.similar.remove(self.ticker.upper())

                if len(self.similar) > ns_parser.limit:
                    random.shuffle(self.similar)
                    self.similar = sorted(self.similar[: ns_parser.limit])
                    console.print(
                        f"The limit of stocks to compare are {ns_parser.limit}. The subsample will occur randomly.\n",
                    )

                self.similar = [self.ticker] + self.similar

                if self.similar:
                    console.print(
                        f"[{self.user}] Similar Companies: {', '.join(self.similar)}",
                        "\n",
                    )

            else:
                console.print(
                    "You need to 'set' a ticker to get similar companies from first!"
                )
def test_get_similar_companies_invalid_status(mocker, recorder):
    mock_response = requests.Response()
    mock_response.status_code = 400
    # pylint: disable=protected-access
    mock_response._content = b"""{"error":"MOCK_ERROR"}"""
    mocker.patch(target="requests.get",
                 new=mocker.Mock(return_value=mock_response))
    result_tuple = polygon_model.get_similar_companies(
        ticker="TSLA",
        us_only=True,
    )

    recorder.capture(result_tuple)