Fakultas Ilmu Komputer UI

tests.py 43.4 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
        self.material_numbers = [5, 10, 25, 100]
        self.invalid_material_numbers = [-100, -10, -1, 0, 1, 2, 3, 4]
        self.stdout = StringIO()

    def test_command_output_with_given_num_of_materi(self):
        for num_of_materi in self.material_numbers:
            call_command("generatedummy", num_of_materi, stdout=self.stdout)
            self.assertIn(
                f"Successfully created {num_of_materi} materi\n",
                self.stdout.getvalue()
            )

    def test_command_should_generate_materi_objects(self):
        for num_of_materi in self.material_numbers:
            call_command("generatedummy", num_of_materi, stdout=self.stdout)
            self.assertEqual(Materi.objects.count(), num_of_materi)
            Materi.objects.all().delete()

    def test_command_should_raise_exception_if_invalid_values_are_given(self):
        with self.assertRaises(IndexError):
            for num_of_materi in self.invalid_material_numbers:
                call_command("generatedummy", num_of_materi)


class RemoveDummyCommandTest(TestCase):

    def test_calling_remove_dummy_command_should_remove_generated_dummy_objects(self):
        stdout = StringIO()
        call_command("generatedummy", 50)

        call_command("removedummy", stdout=stdout)

        self.assertEqual("Successfully remove all dummy object\n", stdout.getvalue())
        self.assertEqual(User.objects.count(), 0)
        self.assertEqual(Category.objects.count(), 0)
        self.assertEqual(Materi.objects.count(), 0)
        self.assertEqual(VerificationSetting.objects.count(), 0)
        self.assertEqual(VerificationReport.objects.count(), 0)


1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
class RatingMateriTest(TestCase):
    def setUp(self):
        self.url = '/administration/'
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        self.user_one_credential = {
            "email": "user_one@user.id",
            "password": id_generator()
        }
        self.user_two_credential = {
            "email": "user_two@user.id",
            "password": id_generator()
        }
        self.contributor = get_user_model().objects.create_user(
            **self.contributor_credential, name="Kontributor", is_contributor=True
        )
        self.user_one = get_user_model().objects.create_user(**self.user_one_credential, name="User One")
        self.user_two = get_user_model().objects.create_user(**self.user_two_credential, name="User Two")
        self.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
               status="PENDING", cover=self.cover, content=self.content).save()
        Materi(title="Materi Dua", author="Author", uploader=self.contributor,
               publisher="Publisher", descriptions="Deskripsi Materi Dua",
               status="APPROVE", cover=self.cover, content=self.content).save()
        self.materi1 = Materi.objects.all()[0]
        self.materi2 = Materi.objects.all()[1]

    def test_rating_model_can_be_created_with_proper_parameter(self):
        Rating(materi=self.materi1, user=self.user_one, score=5).save()
        rating = Rating.objects.first()
        self.assertEqual(rating.materi, self.materi1)
        self.assertEqual(rating.user, self.user_one)
        self.assertTrue(0 < rating.score < 6)
        self.assertEqual(rating.__str__(), "Material:Materi 1 | User:User One | Rating:5")

    def test_rating_model_should_not_be_created_with_rating_more_than_5(self):
        with self.assertRaises(ValidationError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=6).save()
        self.assertTrue('Rating score must be integer between 1-5' in str(context.exception))

    def test_rating_model_should_not_be_created_with_rating_less_than_1(self):
        with self.assertRaises(ValidationError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=0).save()
        self.assertTrue('Rating score must be integer between 1-5' in str(context.exception))

    def test_one_materi_should_be_able_to_be_related_to_multiple_rating(self):
        Rating(materi=self.materi1, user=self.user_one, score=1).save()
        Rating(materi=self.materi1, user=self.user_two, score=2).save()
        rating_one = Rating.objects.get(user=self.user_one)
        rating_two = Rating.objects.get(user=self.user_two)
        self.assertEqual(rating_one.materi, self.materi1)
        self.assertEqual(rating_two.materi, self.materi1)
        self.assertEqual(rating_one.user, self.user_one)
        self.assertEqual(rating_two.user, self.user_two)

    def test_one_user_should_be_able_to_be_related_to_two_rating(self):
        Rating(materi=self.materi1, user=self.user_one, score=3).save()
        Rating(materi=self.materi2, user=self.user_one, score=3).save()
        rating_one = Rating.objects.filter(materi=self.materi1).first()
        rating_two = Rating.objects.filter(materi=self.materi2).first()
        self.assertEqual(rating_one.materi, self.materi1)
        self.assertEqual(rating_two.materi, self.materi2)
        self.assertEqual(rating_one.user, self.user_one)
        self.assertEqual(rating_two.user, self.user_one)

    def test_two_rating_should_not_have_same_user_and_materi(self):
        with self.assertRaises(IntegrityError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=1).save()
            Rating(materi=self.materi1, user=self.user_one, score=2).save()
        self.assertTrue('already exists' in str(context.exception))

    def test_materi_in_rating_should_not_be_null(self):
        with self.assertRaises(IntegrityError):
            Rating(user=self.user_one, score=1).save()

    def test_user_in_rating_should_not_be_null(self):
        with self.assertRaises(IntegrityError):
            Rating(materi=self.materi1, score=1).save()

    def test_score_in_rating_should_not_be_null(self):
        with self.assertRaises(TypeError):
            Rating(materi=self.materi1, user=self.user_one).save()
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160


class fileManagementUtilTest(TestCase):
    def setUp(self):
        self.filename = "image_with_exif_data.gif"
        self.file_content = open(settings.BASE_DIR + "/app/test_files/" +
                self.filename, "rb").read()

    def test_get_random_filename_isCorrect(self):
        generated_name = get_random_filename(self.filename)

        self.assertTrue(generated_name != self.filename)
        # 40 from 36 expected name length + 4 from extension
        self.assertEqual(len(generated_name), 40)
        self.assertTrue(generated_name[-4:] == ".gif")

    def test_remove_image_exifdata_isCorrect(self):
        with tempfile.TemporaryDirectory() as d:
            image_with_exif_data_path = os.path.join(d, self.filename)
            img = open(image_with_exif_data_path, "wb")
            img.write(self.file_content)
            img.close()

            remove_image_exifdata(image_with_exif_data_path)
            sanitized_img = open(image_with_exif_data_path, "rb").read()

            self.assertTrue(len(sanitized_img) < len(self.file_content))
            self.assertTrue(b'<exif:' not in sanitized_img)
For faster browsing, not all history is shown. View entire blame