Example #1
0
 def test_multiple_inputs(self):
     self.engine.add(Cog(
         name="1",
         output=lambda: "test1",
     ))
     self.engine.add(Cog(
         name="2",
         output=lambda: "test2",
     ))
     self.engine.add(Cog(
         name="3",
         output=lambda: "test3",
     ))
     testcog = TestCog(inputs=dict(
         input1="1",
         input2="2",
         input3="3",
     ),
                       should_be_called_with=dict(
                           input1="test1",
                           input2="test2",
                           input3="test3",
                       ))
     self.engine.add(testcog)
     self.engine.start()
     testcog.test()
Example #2
0
    def test_no_cache(self, mock_get_json):
        mock_get_json.return_value = EXAMPLE_API_DATA
        self.wheel.add(Cog(
            name="cached_sched",
            output=lambda: None
        ))
        self.wheel.add(Cog(
            name="season_start",
            output=lambda: "2018-10-10"
        ))
        self.wheel.add(Cog(
            name="today",
            output=lambda: datetime.strptime("2018-10-12", "%Y-%m-%d")
        ))
        self.wheel.add(ScheduleUpdateCog(
            name="sched",
            inputs=dict(
                cached_sched="cached_sched",
                season_start="season_start",
                now="today",
            )
        ))

        testcog = TestCog(
            inputs=dict(sched="sched"),
            should_be_called_with=dict(
                sched=EXAMPLE_SCHED_OUTPUT,
            )
        )
        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #3
0
    def test_basic(self):
        self.wheel.add(Cog(
            name="sched",
            output=lambda: EXAMPLE_SCHED_OUTPUT,
        ))
        self.wheel.add(
            Cog(
                name="ratings",
                output=lambda: EXAMPLE_RATINGS_OUTPUT,
            ))
        self.wheel.add(Cog(
            name="teams",
            output=lambda: EXAMPLE_TEAMS_OUTPUT,
        ))
        self.wheel.add(Cog(
            name="diffs",
            output=lambda: EXAMPLE_DIFFS_OUTPUT,
        ))
        self.wheel.add(
            RenderDayMatchesCog(name="pages",
                                inputs=dict(
                                    sched="sched",
                                    ratings="ratings",
                                    teams="teams",
                                    diffs="diffs",
                                )))
        testcog = TestCog(inputs=dict(pages="pages"))

        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #4
0
 def test_missing_deps(self):
     """
     Test that an appropriate assertion is raised when a cog
     declares an input that is not provided.
     """
     self.engine.add(Cog(output=lambda: "out"))
     self.engine.add(Cog(inputs=dict(input1="ineedthisinput")))
     with self.assertRaises(CogMissingError):
         self.engine.start()
Example #5
0
    def test_missing_args(self):
        self.engine.add(Cog(name="input1", output=lambda: "out"))
        self.engine.add(Cog(name="input2", output=lambda: "out"))

        class MyCog(Cog):
            def __call__(self, input1=None):
                pass

        self.engine.add(MyCog(inputs=dict(
            input1="input1",
            input2="input2",
        )))
        with self.assertRaises(CogArgError):
            self.engine.start()
Example #6
0
    def test_render_page(self):
        self.wheel.add(
            Cog(name="teams",
                output=lambda: {
                    1: {
                        "abbreviation": "TOR"
                    },
                    2: {
                        "abbreviation": "ANA"
                    },
                }))
        self.wheel.add(
            Cog(name="ratings",
                output=lambda: {
                    "2018-10-13": {
                        1: {
                            "rating": 1507.5,
                            "gp": 1,
                        },
                        2: {
                            "rating": 1492.5,
                            "gp": 1,
                        },
                    },
                    "2018-10-14": {
                        1: {
                            "rating": 1514.6764000042328,
                            "gp": 2,
                        },
                        2: {
                            "rating": 1485.3235999957672,
                            "gp": 2,
                        },
                    }
                }))
        self.wheel.add(
            RenderDayRatingsCog(
                name="render",
                inputs=dict(
                    ratings_by_day="ratings",
                    teams="teams",
                ),
            ))

        testcog = TestCog(inputs=dict(render="render", ), )
        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #7
0
 def map(self, incogname, mapper, outcogname):
     mappercog = Cog(
         name=outcogname,
         inputs=dict(l=incogname),
         output=lambda l: [mapper(**i) for i in l],
     )
     self.add(mappercog)
Example #8
0
 def test_cog(self):
     cog = Cog(
         name="test",
         inputs=dict(
             arg1="1",
             arg2="2",
             arg3="3",
         ),
     )
Example #9
0
    def test_long_cog_chain(self):
        self.engine.add(Cog(name="1", output=lambda: "1"))
        self.engine.add(
            Cog(
                name="2",
                inputs=dict(d="1", ),
                output=lambda d: d + "2",
            ))
        self.engine.add(
            Cog(
                name="3",
                inputs=dict(d="2"),
                output=lambda d: d + "3",
            ))
        testcog = TestCog(inputs=dict(d="3"),
                          should_be_called_with=dict(d="123", ))
        self.engine.add(testcog)
        self.engine.start()

        testcog.test()
Example #10
0
 def test_lambda_cog(self):
     self.engine.add(Cog(
         name="1",
         output=lambda: "testvalue",
     ))
     testcog = TestCog(
         name="2",
         inputs=dict(test="1", ),
         should_be_called_with=dict(test="testvalue"),
     )
     self.engine.add(testcog)
     self.engine.start()
     testcog.test()
Example #11
0
    def test_cog_map(self):
        datacog = Cog(name="list",
                      output=lambda: [{
                          'i': i,
                          'j': i + 1
                      } for i in range(10)])

        def map(i, j):
            return i + j

        testcog = TestCog(inputs=dict(d="outcog", ),
                          should_be_called_with=dict(
                              d=[1, 3, 5, 7, 9, 11, 13, 15, 17, 19], ))
        self.engine.add(datacog)
        self.engine.add(testcog)
        self.engine.map("list", map, "outcog")
        self.engine.start()
        testcog.test()
Example #12
0
    def test_sanity(self):
        self.wheel.add(
            Cog(name="ratings",
                output=lambda: {
                    "2018-10-13": {
                        1: {
                            "rating": 1500.0,
                            "gp": 1,
                        },
                        2: {
                            "rating": 1500.0,
                            "gp": 1,
                        },
                    },
                    "2018-10-14": {
                        1: {
                            "rating": 1505.0,
                            "gp": 2,
                        },
                        2: {
                            "rating": 1495.0,
                            "gp": 2,
                        },
                    }
                }))
        self.wheel.add(
            TeamDiffsByDayCog(name="diffs", inputs=dict(ratings="ratings")))

        testcog = TestCog(inputs=dict(diffs="diffs"),
                          should_be_called_with=dict(
                              diffs={
                                  "2018-10-13": {
                                      1: 0.0,
                                      2: 0.0,
                                  },
                                  "2018-10-14": {
                                      1: 2.5,
                                      2: -2.5,
                                  }
                              }))

        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #13
0
    def test_teams_cog_cached(self):
        self.wheel.add(
            Cog(name="raw_teams",
                output=lambda: {
                    1: {
                        "id": 1,
                        "name": "New Jersey Devils",
                        "abbreviation": "NJD",
                    },
                    2: {
                        "id": 2,
                        "name": "New York Islanders",
                        "abbreviation": "NYI",
                    }
                }))

        self.wheel.add(
            TeamsCog(
                name="teams",
                inputs=dict(cached_teams="raw_teams", ),
            ))

        testcog = TestCog(
            inputs=dict(teams="teams", ),
            should_be_called_with=dict(
                teams={
                    1: {
                        "id": 1,
                        "name": "New Jersey Devils",
                        "abbreviation": "NJD",
                    },
                    2: {
                        "id": 2,
                        "name": "New York Islanders",
                        "abbreviation": "NYI",
                    }
                }),
        )
        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #14
0
    def test_cog_map_map(self):
        datacog = Cog(name="list",
                      output=lambda: [{
                          'i': i,
                          'j': i + 1
                      } for i in range(3)])

        def map1(i, j):
            return {'r': i + j}

        def map2(r):
            return 2 * r

        testcog = TestCog(inputs=dict(d="out2", ),
                          should_be_called_with=dict(d=[2, 6, 10], ))
        self.engine.add(datacog)
        self.engine.add(testcog)
        self.engine.map("list", map1, "out1")
        self.engine.map("out1", map2, "out2")
        self.engine.start()
        testcog.test()
Example #15
0
    def test_sanity(self):
        self.wheel.add(Cog(
            name="teams",
            output=lambda: {
                "1": {
                    "abbreviation": "NJD",
                }
            }
        ))
        self.wheel.add(TeamRenderCog(
            name="pages",
            inputs=dict(
                teams="teams",
            )
        ))
        testcog = TestCog(
            inputs=dict(pages="pages")
        )

        self.wheel.add(testcog)
        self.wheel.start()
Example #16
0
    def test_teams_cog_new(self):
        self.wheel.add(Cog(name="raw_teams", output=lambda: None))

        teamscog = TeamsCog(
            name="teams",
            inputs=dict(cached_teams="raw_teams", ),
        )
        teamscog.get_raw_teams = mock.MagicMock(
            return_value={
                "teams": [{
                    "id": 1,
                    "name": "New Jersey Devils",
                }, {
                    "id": 2,
                    "name": "New York Islanders",
                }]
            })

        self.wheel.add(teamscog)

        testcog = TestCog(
            inputs=dict(teams="teams", ),
            should_be_called_with=dict(
                teams={
                    1: {
                        "id": 1,
                        "name": "New Jersey Devils",
                    },
                    2: {
                        "id": 2,
                        "name": "New York Islanders",
                    }
                }),
        )
        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #17
0
    # TeamsImgCog,
)
from celly.cogs.schedule import ScheduleUpdateCog
from celly.cogwheel import CogWheel
from celly.date import DATE_F

logging.basicConfig(level=logging.INFO)

CWD = os.getcwd()
DATA_DIR = os.path.join(CWD, "data/")
BUILD_DIR = os.path.join(CWD, "build/")

wheel = CogWheel()

wheel.add(Cog(
    name="date_f",
    output=lambda: DATE_F,
))

wheel.add(Cog(
    name="current_season",
    output=lambda: "20182019",
))

wheel.add(Cog(
    name="data_directory",
    output=lambda: DATA_DIR,
))

wheel.add(Cog(
    name="build_directory",
    output=lambda: BUILD_DIR,
Example #18
0
 def test_lambda_cog(self):
     cog = Cog("test", output=lambda: "test_data")
     assert cog() == "test_data"
Example #19
0
    def test_ratings_basic(self):
        self.wheel.add(Cog(name="teams", output=lambda: {
            1: {},
            2: {},
        }))
        self.wheel.add(
            Cog(name="games",
                output=lambda: {
                    "2018-10-12": [],
                    "2018-10-13": [{
                        "gameType": "R",
                        "gameDate": "2018-10-13T17:00:00Z",
                        "season": "20182019",
                        "teams": {
                            "home": {
                                "team": {
                                    "id": 1,
                                },
                                "score": 3,
                            },
                            "away": {
                                "team": {
                                    "id": 2,
                                },
                                "score": 2,
                            },
                        },
                        "linescore": {
                            "currentPeriod": 3
                        },
                    }],
                    "2018-10-14": [],
                    "2018-10-15": [{
                        "gameType": "R",
                        "gameDate": "2018-10-15T17:00:00Z",
                        "season": "20182019",
                        "teams": {
                            "home": {
                                "team": {
                                    "id": 1,
                                },
                                "score": 3,
                            },
                            "away": {
                                "team": {
                                    "id": 2,
                                },
                                "score": 2,
                            },
                        },
                        "linescore": {
                            "currentPeriod": 3
                        },
                    }],
                }))

        self.wheel.add(
            TeamRatingsByDayCog(
                name="ratings",
                inputs=dict(
                    days="games",
                    teams="teams",
                ),
            ))

        testcog = TestCog(
            inputs=dict(ratings="ratings", ),
            should_be_called_with=dict(
                ratings={
                    "2018-10-12": {
                        1: {
                            "rating": 1500.0,
                            "gp": 0,
                        },
                        2: {
                            "rating": 1500.0,
                            "gp": 0,
                        },
                    },
                    "2018-10-13": {
                        1: {
                            "rating": 1507.5,
                            "gp": 1,
                        },
                        2: {
                            "rating": 1492.5,
                            "gp": 1,
                        },
                    },
                    "2018-10-14": {
                        1: {
                            "rating": 1507.5,
                            "gp": 1,
                        },
                        2: {
                            "rating": 1492.5,
                            "gp": 1,
                        },
                    },
                    "2018-10-15": {
                        1: {
                            "rating": 1514.6764000042328,
                            "gp": 2,
                        },
                        2: {
                            "rating": 1485.3235999957672,
                            "gp": 2,
                        },
                    }
                }),
        )

        self.wheel.add(testcog)
        self.wheel.start()
        testcog.test()
Example #20
0
 def test_missing_initial_deps(self):
     self.engine.add(Cog(inputs=dict(input1="ineedthisinput")))
     with self.assertRaises(CogMissingError):
         self.engine.start()