예제 #1
0
    def test_will_delete_one_w_document_duplicates(self):
        """
        Test: Delete one document with document (duplicates exist).
        """

        api.insert_documents(doc3.copy())
        api.insert_documents(doc3.copy())

        original = next(api.read_document(doc3.copy()))
        result = api.delete_document(doc3.copy())

        self.assertDictEqual(original, result)

        # Ensure that there's still at least one document left:
        other = next(api.read_document(doc3.copy()))

        self.assertNotEqual(result["_id"], other["_id"])

        # Remove the ids, it should be the only key different between the two.
        result.pop("_id")
        other.pop("_id")

        # Then ensure that both documents are equal to each other. This means
        # that two was found, but only one remains even though both documents
        # are identical.
        self.assertDictEqual(result, other)
예제 #2
0
    def test_will_find_many_w_key_value(self):
        """
        Test: Find MANY documents using k, v.
        """
        api.insert_documents(doc1.copy())

        docs = api.read_document("Ticker", doc1["Ticker"])
        self.assertGreater(docs.explain()["n"],
                           1)  # There should be two results
예제 #3
0
    def test_will_delete_one_w_key_value(self):
        """
        Test: Delete one document with k, v.
        """
        api.insert_documents(doc3.copy())

        doc = {"Ticker": doc3["Ticker"], "status": "test"}

        original = next(api.read_document(doc3.copy()))
        result = api.delete_document(doc)

        self.assertDictEqual(original, result)
예제 #4
0
 def test_insert_documents(self):
     """
     Test that will insert into MongoDB.
     """
     result = api.insert_documents(documents)
     # ids should change all the time, so we need to check length instead.
     self.assertTrue("ids" in result, "ids does not exist in result")
     self.assertTrue(
         len(result["ids"]) > 0, "there are no inserted documents in ids")
예제 #5
0
    def test_will_delete_one_w_document(self):
        """
        Test: Delete one document with document.
        """

        # This test may not run last, not sure. Regardless, lets make sure
        # there's something to delete.
        # Update: Using a test runner, so it should run last. Regardless, I'm
        #         going to keep this insertion code in here.
        api.insert_documents(doc3.copy())

        original = next(api.read_document(doc3.copy()))
        result = api.delete_document(doc3.copy())

        self.assertDictEqual(original, result)

        error = next(api.read_document(doc3.copy()))

        self.assertEqual(error["Error"], "No documents found!")
예제 #6
0
    def test_insert_exact_duplicate(self):
        """
        Test: Insert exact same document.
        """
        # Get the first instance of doc1 in db:
        doc = next(api.read_document(doc1.copy()))
        # Insert the document we just got.
        result = api.insert_documents(doc)

        # Not sure what the error is going to be, but I know for sure that
        # there's going to be one relating to duplicate ids.
        self.assertTrue("Error" in result)
예제 #7
0
    def test_insert_duplicate_document(self):
        """
        Test: Insert a duplicate document.
        """

        self.assertTrue(api.insert_documents(doc1.copy()))

        docs = api.read_document({"Ticker": "AA", "status": "test"})

        first = next(docs)
        if (docs.alive):
            other = next(docs)
        else:
            self.assertTrue(False, "Duplicate document doesn't exist.")

        self.assertNotEqual(first["_id"], other["_id"])

        first.pop("_id")
        other.pop("_id")

        # Ensure that both dictionaries are the same without ids:
        self.assertDictEqual(first, other)
예제 #8
0
def create(symbol = ""):
    """
    Adds a document to the collection.

    @params: Symbol from route, JSON payload.

    @return: JSON of created document or error.

    @h_resp: 201 Created, 400 Bad Request makes sense to me as well.
    """
    status = 200
    try:
        data = { "Ticker": symbol }
        # data = request.json

        # This needs to be done. I need to convert the date from UNIX Epoch
        # to ISODate.
        # req = transform_document(request.json)
        data = request.json
        transform_document(req)

        # Neato, I found this .update() method that'll join my data:
        data.update(req)
        
        result = api.insert_documents(res)

        if "ids" in result:
            status = 201

        else:
            status = 400
            data = { "Error 400": "Document wasn't inserted!" }

        res = bottle.HTTPResponse(status = status, body = get_json(data))
        return set_headers(res)

    except Exception as e:
        print_error(e)