Ejemplo n.º 1
0
 def test_remove(self):
     Registry.set("foo", "bar", "dummy")
     FooManager.remove("bar")
     with self.assertRaises(NotFound) as cm:
         FooManager.remove("bar")
     self.assertEqual("No foo matched your argument: bar!",
                      str(cm.exception))
Ejemplo n.º 2
0
    def update_quota(cls, cost: int):
        """
        Update current date youtube quota usage  according to this guide
        https://developers.google.com/youtube/v3/determine_quota_cost.

        :param int cost:
        """
        date = cls.quota_date()
        quota = Registry.get(cls.quota_key, date, default=0) + cost
        Registry.set(cls.quota_key, {date: quota})
Ejemplo n.º 3
0
    def test_get(self):
        with self.assertRaises(NotFound) as cm:
            FooManager.get("bar")
        self.assertEqual("No foo matched your argument: bar!",
                         str(cm.exception))

        Registry.set("foo", "bar", self.data)
        obj = FooManager.get("bar")
        self.assertIsInstance(obj, FooManager.model)
        self.assertDictEqual(self.data, obj.asdict())
Ejemplo n.º 4
0
    def set(cls, data: Dict):
        obj = cls.model(**data)
        key = getattr(obj, cls.key)

        with contextlib.suppress(KeyError):
            data = Registry.get(cls.namespace, key)
            for field in attr.fields(cls.model):
                if field.metadata.get("keep") and not getattr(obj, field.name):
                    setattr(obj, field.name, data.get(field.name))

        Registry.set(cls.namespace, key, obj.asdict())
        return obj
Ejemplo n.º 5
0
    def test_persist(self):
        try:
            Registry.set(1, 2, 3, 4)
            tmp = tempfile.mkdtemp()
            file_path = os.path.join(tmp, "foo.json")
            Registry.persist(file_path)

            Registry.set(1, 2, 3, 5)
            Registry._obj = {}

            Registry.from_file(file_path)

            self.assertEqual({"1": {"2": {"3": 4}}}, Registry())
        finally:
            shutil.rmtree(tmp)
Ejemplo n.º 6
0
    def test_from_file(self):
        try:
            tmp = tempfile.mkdtemp()
            file_path = os.path.join(tmp, "foo.json")
            with open(file_path, "w") as fp:
                json.dump(dict(a=True), fp)

            Registry.from_file(file_path)

            self.assertEqual(dict(a=True), Registry())

            Registry.set("a", False)

            self.assertFalse(Registry.get("a"))

            Registry.from_file(file_path)
            self.assertFalse(Registry.get("a"))

        finally:
            shutil.rmtree(tmp)
Ejemplo n.º 7
0
 def set(cls, *args, **kwargs):
     for key, value in kwargs.items():
         Registry.set(cls.namespace, key, value)
Ejemplo n.º 8
0
 def update(cls, obj, data: Dict):
     new = attr.evolve(obj, **data)
     key = getattr(new, cls.key)
     Registry.set(cls.namespace, key, new.asdict())
     return new
Ejemplo n.º 9
0
 def test_find_youtube_id(self):
     Registry.set("track", "a", "youtube_id", 1)
     self.assertEqual(1, TrackManager.find_youtube_id("a"))
     self.assertIsNone(TrackManager.find_youtube_id("b"))
Ejemplo n.º 10
0
    def test_clear(self):
        Registry.set(1, 2, 3, 4, 5)
        self.assertEqual({4: 5}, Registry.get(1, 2, 3))

        Registry.clear()
        self.assertEqual({}, Registry())
Ejemplo n.º 11
0
    def test_get(self):
        Registry.set(1, 2, 3, 4, 5)
        self.assertEqual({4: 5}, Registry.get(1, 2, 3))

        with self.assertRaises(KeyError):
            Registry.get(2)
Ejemplo n.º 12
0
    def test_set(self):
        Registry.set(1, 2, 3, 4, 5)
        self.assertEqual({1: {2: {3: {4: 5}}}}, Registry())

        Registry.set(1, 3, 5)
        self.assertEqual({1: {2: {3: {4: 5}}, 3: 5}}, Registry())