コード例 #1
0
def main():
    try:
        with db_session() as session:
            data = session.query(Cats)
            colors = [cat.color for cat in data]
            tails = [cat.tail_length for cat in data]
            whiskers = [cat.whiskers_length for cat in data]

            count_colors = dict(Counter(colors))

            for color, count in count_colors.items():
                entry = session.query(CatColorsInfo).get(color)
                if entry:
                    entry.count = count
                else:
                    entry = CatColorsInfo(color, count)
                    session.add(entry)

            if tails and whiskers:

                DATA_FOR_CATS_STAT = {
                    "tail_length_mean": round(mean(tails), 2),
                    "tail_length_median": round(median(tails), 2),
                    "tail_length_mode": tuple(moda(tails)),
                    "whiskers_length_mean": round(mean(whiskers), 2),
                    "whiskers_length_median": round(median(whiskers), 2),
                    "whiskers_length_mode": tuple(moda(whiskers)),
                }

                stats = CatsStat(*DATA_FOR_CATS_STAT.values())
                session.add(stats)
    except SQLAlchemyError as err:
        sys.stderr.write("ERROR: " + str(err))
コード例 #2
0
 def do_GET(self):
     data = parse_qs(urlparse(self.path).query)
     real_path = urlparse(self.path).path.strip("/")
     try:
         if real_path in self.urls:
             with db_session() as session:
                 self.server_response(*self.urls[real_path](data, session))
         else:
             self.server_response(
                 404,
                 {
                     "status": "Nothing found",
                     "exception": ""
                 },
                 {"Content-type": "application/json"},
             )
     except SQLAlchemyError as err:
         self.server_response(
             500,
             {
                 "status": "No connection with database",
                 "exception": str(err.__cause__),
             },
             {"Content-type": "application/json"},
         )
コード例 #3
0
    def test_cats_offset(self):
        with patch("cats_sqlalhemy.DB_PATH", new=TEST_DB_PATH):
            with db_session() as session:
                status_code, data, headers = cats({"offset": ["7"]}, session)

        self.assertEqual(status_code, 200)
        self.assertEqual(len(data), 20)

        self.assertDictEqual(
            data[0],
            {
                "name": "Vika",
                "color": "black",
                "tail_length": 14,
                "whiskers_length": 10,
            },
        )
        self.assertDictEqual(
            data[-1],
            {
                "name": "Nemo",
                "color": "red & white",
                "tail_length": 17,
                "whiskers_length": 13,
            },
        )
コード例 #4
0
    def test_cats_order(self):
        with patch("cats_sqlalhemy.DB_PATH", new=TEST_DB_PATH):
            with db_session() as session:
                status_code, data, headers = cats(
                    {
                        "attribute": ["name"],
                        "order": ["desc"]
                    }, session)

        self.assertEqual(status_code, 200)
        self.assertEqual(len(data), 27)

        # check first and last cats
        self.assertDictEqual(
            data[-1],
            {
                "name": "Amur",
                "color": "black & white",
                "tail_length": 20,
                "whiskers_length": 11,
            },
        )
        self.assertDictEqual(
            data[0],
            {
                "name": "Yasha",
                "color": "red & white",
                "tail_length": 18,
                "whiskers_length": 12,
            },
        )
コード例 #5
0
    def test_cats(self):
        with patch("cats_sqlalhemy.DB_PATH", new=TEST_DB_PATH):
            with db_session() as session:
                status_code, data, headers = cats(
                    {
                        "attribute": ["tail_length"],
                        "order": ["asc"],
                        "offset": ["26"],
                        "limit": ["1"],
                    },
                    session,
                )

        self.assertEqual(status_code, 200)
        self.assertEqual(len(data), 1)

        self.assertDictEqual(
            data[0],
            {
                "name": "Kelly",
                "color": "red & white",
                "tail_length": 26,
                "whiskers_length": 11,
            },
        )
コード例 #6
0
    def test_post_cats(self):
        with patch("cats_sqlalhemy.DB_PATH", new=TEST_DB_PATH):
            with db_session() as session:
                data = '{"name": "Zzz", "color": "red & white", "tail_length": 28, "whiskers_length": 20}'
                post_cats(data, session)

                new_cat = session.query(Cats).filter(Cats.name == "Zzz").one()

                self.assertEqual(("Zzz, red & white, 28, 20"), str(new_cat))
コード例 #7
0
 def do_POST(self):
     content_length = int(self.headers["Content-Length"])
     body = self.rfile.read(content_length)
     try:
         with db_session() as session:
             self.server_response(*post_cats(body, session))
     except SQLAlchemyError as err:
         self.server_response(
             500,
             {
                 "status": "No connection with database",
                 "exception": str(err.__cause__),
             },
             {"Content-type": "text/html"},
         )
コード例 #8
0
    def test_cats_limit(self):
        with patch("cats_sqlalhemy.DB_PATH", new=TEST_DB_PATH):
            with db_session() as session:
                status_code, data, headers = cats({"limit": ["1"]}, session)

        self.assertEqual(status_code, 200)
        self.assertEqual(len(data), 1)

        self.assertDictEqual(
            data[0],
            {
                "name": "Tihon",
                "color": "red & white",
                "tail_length": 15,
                "whiskers_length": 12,
            },
        )