1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
import pytest
from telegram._dice import Dice
from telegram._reaction import ReactionTypeEmoji
from telegram._storyarea import (
LocationAddress,
StoryArea,
StoryAreaPosition,
StoryAreaType,
StoryAreaTypeLink,
StoryAreaTypeLocation,
StoryAreaTypeSuggestedReaction,
StoryAreaTypeUniqueGift,
StoryAreaTypeWeather,
)
from telegram.constants import StoryAreaTypeType
from tests.auxil.slots import mro_slots
@pytest.fixture
def story_area_position():
return StoryAreaPosition(
x_percentage=StoryAreaPositionTestBase.x_percentage,
y_percentage=StoryAreaPositionTestBase.y_percentage,
width_percentage=StoryAreaPositionTestBase.width_percentage,
height_percentage=StoryAreaPositionTestBase.height_percentage,
rotation_angle=StoryAreaPositionTestBase.rotation_angle,
corner_radius_percentage=StoryAreaPositionTestBase.corner_radius_percentage,
)
class StoryAreaPositionTestBase:
x_percentage = 50.0
y_percentage = 10.0
width_percentage = 15
height_percentage = 15
rotation_angle = 0.0
corner_radius_percentage = 8.0
class TestStoryAreaPositionWithoutRequest(StoryAreaPositionTestBase):
def test_slot_behaviour(self, story_area_position):
inst = story_area_position
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_position):
assert story_area_position.x_percentage == self.x_percentage
assert story_area_position.y_percentage == self.y_percentage
assert story_area_position.width_percentage == self.width_percentage
assert story_area_position.height_percentage == self.height_percentage
assert story_area_position.rotation_angle == self.rotation_angle
assert story_area_position.corner_radius_percentage == self.corner_radius_percentage
def test_to_dict(self, story_area_position):
json_dict = story_area_position.to_dict()
assert json_dict["x_percentage"] == self.x_percentage
assert json_dict["y_percentage"] == self.y_percentage
assert json_dict["width_percentage"] == self.width_percentage
assert json_dict["height_percentage"] == self.height_percentage
assert json_dict["rotation_angle"] == self.rotation_angle
assert json_dict["corner_radius_percentage"] == self.corner_radius_percentage
def test_equality(self, story_area_position):
a = story_area_position
b = StoryAreaPosition(
self.x_percentage,
self.y_percentage,
self.width_percentage,
self.height_percentage,
self.rotation_angle,
self.corner_radius_percentage,
)
c = StoryAreaPosition(
self.x_percentage + 10.0,
self.y_percentage,
self.width_percentage,
self.height_percentage,
self.rotation_angle,
self.corner_radius_percentage,
)
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def location_address():
return LocationAddress(
country_code=LocationAddressTestBase.country_code,
state=LocationAddressTestBase.state,
city=LocationAddressTestBase.city,
street=LocationAddressTestBase.street,
)
class LocationAddressTestBase:
country_code = "CC"
state = "State"
city = "City"
street = "12 downtown"
class TestLocationAddressWithoutRequest(LocationAddressTestBase):
def test_slot_behaviour(self, location_address):
inst = location_address
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, location_address):
assert location_address.country_code == self.country_code
assert location_address.state == self.state
assert location_address.city == self.city
assert location_address.street == self.street
def test_to_dict(self, location_address):
json_dict = location_address.to_dict()
assert json_dict["country_code"] == self.country_code
assert json_dict["state"] == self.state
assert json_dict["city"] == self.city
assert json_dict["street"] == self.street
def test_equality(self, location_address):
a = location_address
b = LocationAddress(self.country_code, self.state, self.city, self.street)
c = LocationAddress("some_other_code", self.state, self.city, self.street)
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area():
return StoryArea(
position=StoryAreaTestBase.position,
type=StoryAreaTestBase.type,
)
class StoryAreaTestBase:
position = StoryAreaPosition(
x_percentage=50.0,
y_percentage=10.0,
width_percentage=15,
height_percentage=15,
rotation_angle=0.0,
corner_radius_percentage=8.0,
)
type = StoryAreaTypeLink(url="some_url")
class TestStoryAreaWithoutRequest(StoryAreaTestBase):
def test_slot_behaviour(self, story_area):
inst = story_area
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area):
assert story_area.position == self.position
assert story_area.type == self.type
def test_to_dict(self, story_area):
json_dict = story_area.to_dict()
assert json_dict["position"] == self.position.to_dict()
assert json_dict["type"] == self.type.to_dict()
def test_equality(self, story_area):
a = story_area
b = StoryArea(self.position, self.type)
c = StoryArea(self.position, StoryAreaTypeLink(url="some_other_url"))
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type():
return StoryAreaType(type=StoryAreaTypeTestBase.type)
class StoryAreaTypeTestBase:
type = StoryAreaTypeType.LOCATION
latitude = 100.5
longitude = 200.5
address = LocationAddress(
country_code="cc",
state="State",
city="City",
street="12 downtown",
)
reaction_type = ReactionTypeEmoji(emoji="emoji")
is_dark = False
is_flipped = False
url = "http_url"
temperature = 35.0
emoji = "emoji"
background_color = 0xFF66CCFF
name = "unique_gift_name"
class TestStoryAreaTypeWithoutRequest(StoryAreaTypeTestBase):
def test_slot_behaviour(self, story_area_type):
inst = story_area_type
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type):
assert story_area_type.type == self.type
def test_type_enum_conversion(self, story_area_type):
assert type(StoryAreaType("location").type) is StoryAreaTypeType
assert StoryAreaType("unknown").type == "unknown"
def test_to_dict(self, story_area_type):
assert story_area_type.to_dict() == {"type": self.type}
def test_equality(self, story_area_type):
a = story_area_type
b = StoryAreaType(self.type)
c = StoryAreaType("unknown")
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type_location():
return StoryAreaTypeLocation(
latitude=TestStoryAreaTypeLocationWithoutRequest.latitude,
longitude=TestStoryAreaTypeLocationWithoutRequest.longitude,
address=TestStoryAreaTypeLocationWithoutRequest.address,
)
class TestStoryAreaTypeLocationWithoutRequest(StoryAreaTypeTestBase):
type = StoryAreaTypeType.LOCATION
def test_slot_behaviour(self, story_area_type_location):
inst = story_area_type_location
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type_location):
assert story_area_type_location.type == self.type
assert story_area_type_location.latitude == self.latitude
assert story_area_type_location.longitude == self.longitude
assert story_area_type_location.address == self.address
def test_to_dict(self, story_area_type_location):
json_dict = story_area_type_location.to_dict()
assert isinstance(json_dict, dict)
assert json_dict["type"] == self.type
assert json_dict["latitude"] == self.latitude
assert json_dict["longitude"] == self.longitude
assert json_dict["address"] == self.address.to_dict()
def test_equality(self, story_area_type_location):
a = story_area_type_location
b = StoryAreaTypeLocation(self.latitude, self.longitude, self.address)
c = StoryAreaTypeLocation(self.latitude + 0.5, self.longitude, self.address)
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type_suggested_reaction():
return StoryAreaTypeSuggestedReaction(
reaction_type=TestStoryAreaTypeSuggestedReactionWithoutRequest.reaction_type,
is_dark=TestStoryAreaTypeSuggestedReactionWithoutRequest.is_dark,
is_flipped=TestStoryAreaTypeSuggestedReactionWithoutRequest.is_flipped,
)
class TestStoryAreaTypeSuggestedReactionWithoutRequest(StoryAreaTypeTestBase):
type = StoryAreaTypeType.SUGGESTED_REACTION
def test_slot_behaviour(self, story_area_type_suggested_reaction):
inst = story_area_type_suggested_reaction
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type_suggested_reaction):
assert story_area_type_suggested_reaction.type == self.type
assert story_area_type_suggested_reaction.reaction_type == self.reaction_type
assert story_area_type_suggested_reaction.is_dark is self.is_dark
assert story_area_type_suggested_reaction.is_flipped is self.is_flipped
def test_to_dict(self, story_area_type_suggested_reaction):
json_dict = story_area_type_suggested_reaction.to_dict()
assert isinstance(json_dict, dict)
assert json_dict["type"] == self.type
assert json_dict["reaction_type"] == self.reaction_type.to_dict()
assert json_dict["is_dark"] is self.is_dark
assert json_dict["is_flipped"] is self.is_flipped
def test_equality(self, story_area_type_suggested_reaction):
a = story_area_type_suggested_reaction
b = StoryAreaTypeSuggestedReaction(self.reaction_type, self.is_dark, self.is_flipped)
c = StoryAreaTypeSuggestedReaction(self.reaction_type, not self.is_dark, self.is_flipped)
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type_link():
return StoryAreaTypeLink(
url=TestStoryAreaTypeLinkWithoutRequest.url,
)
class TestStoryAreaTypeLinkWithoutRequest(StoryAreaTypeTestBase):
type = StoryAreaTypeType.LINK
def test_slot_behaviour(self, story_area_type_link):
inst = story_area_type_link
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type_link):
assert story_area_type_link.type == self.type
assert story_area_type_link.url == self.url
def test_to_dict(self, story_area_type_link):
json_dict = story_area_type_link.to_dict()
assert isinstance(json_dict, dict)
assert json_dict["type"] == self.type
assert json_dict["url"] == self.url
def test_equality(self, story_area_type_link):
a = story_area_type_link
b = StoryAreaTypeLink(self.url)
c = StoryAreaTypeLink("other_http_url")
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type_weather():
return StoryAreaTypeWeather(
temperature=TestStoryAreaTypeWeatherWithoutRequest.temperature,
emoji=TestStoryAreaTypeWeatherWithoutRequest.emoji,
background_color=TestStoryAreaTypeWeatherWithoutRequest.background_color,
)
class TestStoryAreaTypeWeatherWithoutRequest(StoryAreaTypeTestBase):
type = StoryAreaTypeType.WEATHER
def test_slot_behaviour(self, story_area_type_weather):
inst = story_area_type_weather
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type_weather):
assert story_area_type_weather.type == self.type
assert story_area_type_weather.temperature == self.temperature
assert story_area_type_weather.emoji == self.emoji
assert story_area_type_weather.background_color == self.background_color
def test_to_dict(self, story_area_type_weather):
json_dict = story_area_type_weather.to_dict()
assert isinstance(json_dict, dict)
assert json_dict["type"] == self.type
assert json_dict["temperature"] == self.temperature
assert json_dict["emoji"] == self.emoji
assert json_dict["background_color"] == self.background_color
def test_equality(self, story_area_type_weather):
a = story_area_type_weather
b = StoryAreaTypeWeather(self.temperature, self.emoji, self.background_color)
c = StoryAreaTypeWeather(self.temperature - 5.0, self.emoji, self.background_color)
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def story_area_type_unique_gift():
return StoryAreaTypeUniqueGift(
name=TestStoryAreaTypeUniqueGiftWithoutRequest.name,
)
class TestStoryAreaTypeUniqueGiftWithoutRequest(StoryAreaTypeTestBase):
type = StoryAreaTypeType.UNIQUE_GIFT
def test_slot_behaviour(self, story_area_type_unique_gift):
inst = story_area_type_unique_gift
for attr in inst.__slots__:
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
def test_expected_values(self, story_area_type_unique_gift):
assert story_area_type_unique_gift.type == self.type
assert story_area_type_unique_gift.name == self.name
def test_to_dict(self, story_area_type_unique_gift):
json_dict = story_area_type_unique_gift.to_dict()
assert isinstance(json_dict, dict)
assert json_dict["type"] == self.type
assert json_dict["name"] == self.name
def test_equality(self, story_area_type_unique_gift):
a = story_area_type_unique_gift
b = StoryAreaTypeUniqueGift(self.name)
c = StoryAreaTypeUniqueGift("other_name")
d = Dice(5, "test")
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
|