Ejemplo n.º 1
0
class TestGet(AioTestCase):
  def setUp(self):
    self.table = AsyncIOMotorClient(MONGO_URI).tests.tests

  async def testNoTable(self):
    model = models.MinimalMongo()
    data = {"name": "Test get no table"}
    model.load(data)
    with self.assertRaises(InvalidOperation):
      await model.get(name = data["name"])

  async def testNoData(self):
    model = models.MinimalMongo(self.table)
    with self.assertRaises(NotFound):
      await model.get(name = "A name that doesn't exists")

  async def test(self):
    data = {"name": "Test get"}
    result = await self.table.insert_one(data)
    data["_id"] = result.inserted_id

    model = models.MinimalMongo(self.table)
    await model.get(name = data["name"])

    modeldata = model.get_data()
    self.assertEqual(sorted(data.items()), sorted(modeldata.items()))

    self.table.delete_one({"_id": modeldata["_id"]})
Ejemplo n.º 2
0
class TestCreate(AioTestCase):
  def setUp(self):
    self.table = AsyncIOMotorClient(MONGO_URI).tests.tests

  async def testNoTable(self):
    model = models.MinimalMongo()
    data = {"name": "Test create no table"}
    model.load(data)
    with self.assertRaises(InvalidOperation):
      await model.create()

  async def testNoData(self):
    model = models.MinimalMongo(self.table)
    with self.assertRaises(InvalidOperation):
      await model.create()

  async def test(self):
    model = models.MinimalMongo(self.table)
    data = {"name": "Test create"}
    model.load(data)
    await model.create()
    saved = await self.table.find_one(data)
    model_data = model.get_data()

    self.assertDictEqual(model_data, saved)
    self.assertIn("_id", model_data.keys())

    self.table.delete_one({"_id": model_data["_id"]})