예제 #1
0
    def test_collection_die(self):
        db = Database()
        time.sleep(3)
        db.initDiceSets("dice/resources")
        db.remove_all_dice_in_set("basic")
        db.remove_all_dice_in_set("halloween")
        self.assertEqual(db.total_dice_in_set("basic"), 0)

        # Insert files in collection Die
        db.initDiceCollection("basic")
        self.assertEqual(db.total_dice_in_set("basic"), 6)
        self.assertEqual(db.total_dice_in_set("basic"), 6)

        db.initDiceCollection("halloween")
        halloween_set = db.get_all_dice_in_set("halloween")
        halloween_len = halloween_set.count()
        self.assertEqual(halloween_len, 5)
예제 #2
0
 def test_collection_dice(self):
     db = Database()
     time.sleep(3)
     db.initDiceSets("dice/resources")
     db.remove_all_dice_in_set("basic")
     db.remove_all_dice_in_set("halloween")
     db.initDiceCollection("basic")
     db.initDiceCollection("halloween")
     diceset = db.getAllCollection("DiceSet")
     diceset_len = diceset.count()
     self.assertEqual(diceset_len, 2)
예제 #3
0
import random as rnd
import re
from dice.classes.database import Database

db = Database()


class Die:

    def __init__(self, diejson):
        self.faces = []
        self.pip = None

        self.faces.append(diejson["face0"])
        self.faces.append(diejson["face1"])
        self.faces.append(diejson["face2"])
        self.faces.append(diejson["face3"])
        self.faces.append(diejson["face4"])
        self.faces.append(diejson["face5"])
        self.throw_die()

    def throw_die(self):
        if self.faces:  # pythonic for list is not empty
            self.pip = rnd.choice(self.faces)
            return self.pip
        else:
            raise IndexError("throw_die(): empty die error.")


class DiceSet:
예제 #4
0
    def test_init(self):
        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app

        with tested_app.test_client() as client:
            db = Database()
            db.initDiceSets("dice/resources")
            db.remove_all_dice_in_set("basic")
            db.remove_all_dice_in_set("halloween")
            db.initDiceCollection("basic")
            db.initDiceCollection("halloween")
            reply = client.get("/")
            body = json.loads(str(reply.data, 'utf8'))
            expected = {"message": "App started"}
            self.assertEqual(body, expected)
예제 #5
0
    def test_roll(self):

        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app

        with tested_app.test_client() as client:
            db = Database()
            db.initDiceSets("dice/resources")
            db.remove_all_dice_in_set("basic")
            db.remove_all_dice_in_set("halloween")
            db.initDiceCollection("basic")
            db.initDiceCollection("halloween")

            # non-integer dice number
            reply = client.get('/rolldice/pippo/basic')
            body = json.loads(str(reply.data, 'utf8'))
            self.assertEqual(reply.status_code, 200)
            self.assertEqual(
                body, {
                    "message": "Dice number needs to be an integer!",
                    "dice_set": "",
                    "roll": [],
                })

            # wrong dice number (<0)
            reply = client.get('/rolldice/0/basic')
            body = json.loads(str(reply.data, 'utf8'))
            self.assertEqual(reply.status_code, 200)
            self.assertEqual(body, {
                "message": "Wrong dice number!",
                "dice_set": "",
                "roll": [],
            })

            # wrong dice number (> len diceset)
            reply = client.get('/rolldice/12/basic')
            body = json.loads(str(reply.data, 'utf8'))
            self.assertEqual(reply.status_code, 200)
            self.assertEqual(body, {
                "message": "Wrong dice number!",
                "dice_set": "",
                "roll": [],
            })

            # non-existing dice set
            reply = client.get('/rolldice/6/pippo')
            body = json.loads(str(reply.data, 'utf8'))
            self.assertEqual(reply.status_code, 200)
            self.assertEqual(
                body, {
                    "message": "Dice set pippo doesn't exist!",
                    "dice_set": "",
                    "roll": [],
                })

            # correct roll
            reply = client.get('/rolldice/5/basic')
            body = json.loads(str(reply.data, 'utf8'))
            self.assertEqual(reply.status_code, 200)
            dice_set = body["dice_set"]
            roll = body["roll"]
            self.assertEqual(dice_set, "basic")
            self.assertEqual(len(roll), 5)
예제 #6
0
    def test_available_sets(self):
        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app

        with tested_app.test_client() as client:
            db = Database()
            db.initDiceSets("dice/resources")
            db.remove_all_dice_in_set("basic")
            db.remove_all_dice_in_set("halloween")
            db.initDiceCollection("basic")
            db.initDiceCollection("halloween")
            # get available dice sets
            reply = client.get("/dicesets")
            body = json.loads(str(reply.data, 'utf8'))
            available_dicesets = body["available_dicesets"]
            self.assertEqual(len(available_dicesets), 2)