Пример #1
0
 def test_update_pass(self):
     data = {
         "name": "John",
         "number": "1234567890",
         "address": "Maple Road, Orange, AB",
         "email": "*****@*****.**"
     }
     es.index(index="addressbook", doc_type="contact", id=1, body=data)
     time.sleep(1)
     new_data = {
         "name": "Johnny",
         "number": "0987654321",
         "address": "Wine Road, Apple, CD",
         "email": "*****@*****.**"
     }
     elasticsearch_handler.update_contact("John", new_data)
     time.sleep(1)
     results = es.search(
         index="addressbook",
         doc_type="contact",
         body={"query": {
             "match": {
                 "name": new_data["name"]
             }
         }})
     updated_data = results["hits"]["hits"][0]["_source"]
     self.assertEqual(new_data["name"], updated_data["name"])
     self.assertEqual(new_data["number"], updated_data["number"])
     self.assertEqual(new_data["address"], updated_data["address"])
     self.assertEqual(new_data["email"], updated_data["email"])
Пример #2
0
def contact_name(request, name):
    if request.method == "GET":
        response = elasticsearch_handler.get_contact(name)
        return HttpResponse(json.dumps(response), content_type="application/json")

    elif request.method == "PUT":
        parameters = QueryDict(request.body)
        data = {}

        # Check if all the required fields are specified
        if parameters["name"] and parameters["number"] and parameters["address"] and parameters["email"]:
            data["name"] = parameters["name"]
            data["number"] = parameters["number"]
            data["address"] = parameters["address"]
            data["email"] = parameters["email"]
            response = elasticsearch_handler.update_contact(name, data)
            return HttpResponse(json.dumps(response), content_type="application/json")

        # Show error message if all the fields are not specified
        return HttpResponse(json.dumps({"acknowledged": False, "message": "Required fields not specified."}),
                            content_type="application/json")

    elif request.method == "DELETE":
        response = elasticsearch_handler.delete_contact(name)
        return HttpResponse(json.dumps(response), content_type="application/json")

    # Show error message if request is not GET, PUT or DELETE
    return HttpResponse(json.dumps({"acknowledged": False, "message": "Invalid request."}),
                        content_type="application/json")
Пример #3
0
 def test_update_invalid_email(self):
     data = {
         "name": "John",
         "number": "1234567890",
         "address": "Maple Road, Orange, AB",
         "email": "*****@*****.**"
     }
     es.index(index="addressbook", doc_type="contact", id=1, body=data)
     time.sleep(1)
     new_data = {
         "name": "John",
         "number": "1234567890",
         "address": "Maple Road, Orange, AB",
         "email": "johndoe#gmail,com"
     }
     response = elasticsearch_handler.update_contact("John", new_data)
     self.assertFalse(response["acknowledged"])
Пример #4
0
 def test_update_contact_exists(self):
     data1 = {
         "name": "John",
         "number": "1234567890",
         "address": "Maple Road, Orange, AB",
         "email": "*****@*****.**"
     }
     data2 = {
         "name": "Mark",
         "number": "7264527893",
         "address": "Scope Road, Peach, EF",
         "email": "*****@*****.**"
     }
     es.index(index="addressbook", doc_type="contact", id=1, body=data1)
     es.index(index="addressbook", doc_type="contact", id=2, body=data2)
     time.sleep(1)
     new_data = {
         "name": "Mark",
         "number": "0987654321",
         "address": "Wine Road, Apple, CD",
         "email": "*****@*****.**"
     }
     response = elasticsearch_handler.update_contact("John", new_data)
     self.assertFalse(response["acknowledged"])