class TestEnum(unittest.TestCase): def setUp(self): self.enum = Enum( "A", "B", EnumItem("C", help_text="help", foo="foo", bar="bar"), ) def test_duplicate_items(self): self.assertRaises(ValueError, Enum, "A", "A") self.assertRaises(ValueError, Enum, EnumItem("A"), "A") def test_in_enum(self): self.assertTrue("A" in self.enum) self.assertTrue("B" in self.enum) self.assertTrue("C" in self.enum) self.assertTrue("X" not in self.enum) def test_iter(self): self.assertEqual(list(self.enum), ["A", "B", "C"]) def test_getitem(self): self.assertEqual(self.enum["A"], 0) self.assertEqual(self.enum["B"], 1) self.assertEqual(self.enum["C"], 2) self.assertEqual(self.enum[0], "A") self.assertEqual(self.enum[1], "B") self.assertEqual(self.enum[2], "C") def test_get(self): self.assertEqual(self.enum.get("B"), 1) self.assertEqual(self.enum.get(1), "B") def test_get_mapping(self): self.assertEqual(self.enum.get_mapping(), [(0, "A"), (1, "B"), (2, "C")]) self.assertEqual(type(self.enum.get_mapping()[0][1]), str) def test_get_item(self): self.assertEqual(self.enum.get_item("A"), "A") self.assertRaises(KeyError, self.enum.get_item, "X") def test_get_help_text(self): self.assertEqual(self.enum.get_item_help_text("A"), "") self.assertEqual(self.enum.get_item_help_text("C"), "help") def test_get_option(self): self.assertEqual(self.enum.get_item_option("A", "foo"), None) self.assertEqual(self.enum.get_item_option("C", "foo"), "foo") self.assertEqual(self.enum.get_item_option("C", "bar"), "bar") self.assertEqual(self.enum.get_item_option("C", "bar", "default"), "bar") self.assertEqual(self.enum.get_item_option("C", "baz"), None) self.assertEqual(self.enum.get_item_option("C", "baz", "default"), "default") self.assertEqual(self.enum.get_item("C")["foo"], "foo") self.assertRaises(KeyError, self.enum.get_item("C").__getitem__, "baz")
def setUp(self): self.enum = Enum( "A", "B", EnumItem("C", help_text="help", foo="foo", bar="bar"), )
# -*- coding: utf-8 -*- import os from django.conf import settings from django.db import models from django.contrib.auth.models import User from kobo.shortcuts import random_string from kobo.types import Enum UPLOAD_STATES = Enum( "NEW", "STARTED", "FINISHED", "FAILED", ) class FileUpload(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL) name = models.CharField(max_length=255) checksum = models.CharField(max_length=255) size = models.PositiveIntegerField() target_dir = models.CharField(max_length=255) upload_key = models.CharField(max_length=255) state = models.PositiveIntegerField(default=0, choices=UPLOAD_STATES.get_mapping()) dt_created = models.DateTimeField(auto_now_add=True) dt_finished = models.DateTimeField(null=True, blank=True)
# -*- coding: utf-8 -*- from kobo.types import Enum __all__ = ( "TASK_STATES", "FINISHED_STATES", "FAILED_STATES", ) TASK_STATES = Enum( "FREE", # default state for new tasks "ASSIGNED", # assigned to a worker "OPEN", # opened by a worker and being processed "CLOSED", # successfully finished "CANCELED", # canceled by user request "FAILED", # failed "INTERRUPTED", # interrupted by an external event (power outage, process killed, etc.) "TIMEOUT", # reached timeout and killed by task manager "CREATED", # task is created, but still not ready to be processed ) FINISHED_STATES = ( TASK_STATES["CLOSED"], TASK_STATES["CANCELED"], TASK_STATES["FAILED"], TASK_STATES["INTERRUPTED"], TASK_STATES["TIMEOUT"], ) FAILED_STATES = (