class CardSerializer(serializers.ModelSerializer): board = BoardSerializer() owner = AccountSerializer() company = CompanySerializer() class Meta: model = Card fields = '__all__'
def test_get_boards(self): url = reverse('boards') query = Board.objects.all() serializer = BoardSerializer(query, many=True) response = self.client.get(url) nt.assert_equal(response.data, serializer.data) nt.assert_equal(response.status_code, status.HTTP_200_OK)
def test_get_by_id_board(self): url = reverse('board', args=[self.board1.id]) query = Board.objects.get(id=self.board1.id) serializer = BoardSerializer(query) response = self.client.get(url) nt.assert_equal(response.data, serializer.data) nt.assert_equal(response.status_code, status.HTTP_200_OK)
def create_game(self, request): context = { "request": self.request, } player = self.request.user new_game = Game.objects.create(player=player) serializer = BoardSerializer(instance=new_game, context=context) creation_message = "Creating board (this turn doesnt count as a movement.)" Movement.objects.create(game=new_game, player=player, result=creation_message) return Response(serializer.data)
def list(request): datalist = Board.objects.all() print(datalist) if request.method == 'GET': # get방식으로 연결이 들어오면 # json 형식으로 전송할 수 있도록 직렬화작업 print('test========') serializer = BoardSerializer( datalist, many=True) # 직렬화, many=True => 여러개의 데이터 들어간다는 의미 print(serializer) return JsonResponse(serializer.data, safe=False, json_dumps_params={ 'ensure_ascii': False }) # serializer안에 들어있는 데이터를 json 형식으로 바꿔서 리턴
class GameSerializersTestCase(TestCase): def setUp(self): self.player = Player.objects.create() self.game = Game.objects.create(player=self.player) self.data = {"code": str(self.game.code), "player": self.player} self.serializer = BoardSerializer(data=self.data) def test_board_serializer(self): serializer = BoardSerializer(instance=self.game) self.assertIsNotNone(serializer.instance) def test_board_serializer_is_valid(self): self.assertTrue(self.serializer.is_valid()) def test_board_serializer_creation(self): serializer = BoardSerializer(data=self.data) serializer.is_valid() self.assertIsNotNone(serializer.data) self.assertEqual(serializer.data.get("code"), str(self.game.code)) def test_board_has_movements(self): self.assertTrue("movements" in self.serializer.fields.keys())
class BoardAPIView(APIView): lookup_field = 'id' permission_classes = [AllowAny] queryset = Board.objects.none() serializer_class = BoardSerializer serializer = BoardSerializer(queryset) def get(self,request,*args,**kwargs): try: self.queryset = Board.objects.get(id=kwargs.get("id")) serializer=self.serializer_class(self.queryset) return Response(serializer.data, status=status.HTTP_200_OK) except (Board.DoesNotExist, TypeError): return Response(status=status.HTTP_404_NOT_FOUND) def post(self, request, *args, **kwargs): return Response(data="METHOD_NOT_ALLOWED", status=status.HTTP_405_METHOD_NOT_ALLOWED) def put(self,request,*args,**kwargs): try: self.queryset = Board.objects.get(id=kwargs.get('id')) serializer = self.serializer_class(data=request.data) except Board.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if serializer.is_valid(): serializer.update(instance=self.queryset,validated_data=request.data) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self,request,*args,**kwargs): try: obj=Board.objects.get(id=kwargs.get('id')) obj.delete() return Response(status=status.HTTP_200_OK) except Board.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND)
def test_board_serializer_creation(self): serializer = BoardSerializer(data=self.data) serializer.is_valid() self.assertIsNotNone(serializer.data) self.assertEqual(serializer.data.get("code"), str(self.game.code))
def test_board_serializer(self): serializer = BoardSerializer(instance=self.game) self.assertIsNotNone(serializer.instance)
def setUp(self): self.player = Player.objects.create() self.game = Game.objects.create(player=self.player) self.data = {"code": str(self.game.code), "player": self.player} self.serializer = BoardSerializer(data=self.data)