|
| 1 | +"""Tests for BuildConfig model and Build.readthedocs_yaml_data field.""" |
| 2 | + |
| 3 | +import django_dynamic_fixture as fixture |
| 4 | +import pytest |
| 5 | + |
| 6 | +from readthedocs.builds.models import Build |
| 7 | +from readthedocs.builds.models import BuildConfig |
| 8 | +from readthedocs.projects.models import Project |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.django_db |
| 12 | +class TestBuildConfig: |
| 13 | + """Test BuildConfig model functionality.""" |
| 14 | + |
| 15 | + def test_buildconfig_creation(self): |
| 16 | + """Test that BuildConfig can be created with data.""" |
| 17 | + config_data = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 18 | + build_config = BuildConfig.objects.create(data=config_data) |
| 19 | + |
| 20 | + assert build_config.pk is not None |
| 21 | + assert build_config.data == config_data |
| 22 | + |
| 23 | + def test_buildconfig_unique_constraint(self): |
| 24 | + """Test that BuildConfig enforces unique constraint on data.""" |
| 25 | + config_data = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 26 | + |
| 27 | + # Create first BuildConfig |
| 28 | + BuildConfig.objects.create(data=config_data) |
| 29 | + |
| 30 | + # Try to create another with the same data - should raise IntegrityError |
| 31 | + from django.db import IntegrityError |
| 32 | + with pytest.raises(IntegrityError): |
| 33 | + BuildConfig.objects.create(data=config_data) |
| 34 | + |
| 35 | + def test_buildconfig_get_or_create(self): |
| 36 | + """Test that get_or_create works correctly for deduplication.""" |
| 37 | + config_data = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 38 | + |
| 39 | + # First call creates |
| 40 | + build_config1, created1 = BuildConfig.objects.get_or_create(data=config_data) |
| 41 | + assert created1 is True |
| 42 | + |
| 43 | + # Second call gets existing |
| 44 | + build_config2, created2 = BuildConfig.objects.get_or_create(data=config_data) |
| 45 | + assert created2 is False |
| 46 | + assert build_config1.pk == build_config2.pk |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.django_db |
| 50 | +class TestBuildReadthedocsYamlData: |
| 51 | + """Test Build.readthedocs_yaml_data field and integration.""" |
| 52 | + |
| 53 | + def test_build_saves_with_config_creates_buildconfig(self): |
| 54 | + """Test that saving a Build with config creates BuildConfig.""" |
| 55 | + project = fixture.get(Project) |
| 56 | + config_data = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 57 | + |
| 58 | + build = fixture.get(Build, project=project) |
| 59 | + build.config = config_data |
| 60 | + build.save() |
| 61 | + |
| 62 | + # Check that both old and new fields are populated |
| 63 | + assert build._config == config_data |
| 64 | + assert build.readthedocs_yaml_data is not None |
| 65 | + assert build.readthedocs_yaml_data.data == config_data |
| 66 | + |
| 67 | + def test_build_with_same_config_reuses_buildconfig(self): |
| 68 | + """Test that builds with same config reuse the same BuildConfig.""" |
| 69 | + project = fixture.get(Project) |
| 70 | + config_data = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 71 | + |
| 72 | + # Create first build |
| 73 | + build1 = fixture.get(Build, project=project) |
| 74 | + build1.config = config_data |
| 75 | + build1.save() |
| 76 | + |
| 77 | + # Create second build with same config |
| 78 | + build2 = fixture.get(Build, project=project) |
| 79 | + build2.config = config_data |
| 80 | + build2.save() |
| 81 | + |
| 82 | + # Both should reference the same BuildConfig |
| 83 | + assert build1.readthedocs_yaml_data.pk == build2.readthedocs_yaml_data.pk |
| 84 | + assert BuildConfig.objects.count() == 1 |
| 85 | + |
| 86 | + def test_build_with_different_config_creates_new_buildconfig(self): |
| 87 | + """Test that builds with different configs create separate BuildConfigs.""" |
| 88 | + project = fixture.get(Project) |
| 89 | + config_data1 = {"build": {"os": "ubuntu-22.04"}, "python": {"version": "3.11"}} |
| 90 | + config_data2 = {"build": {"os": "ubuntu-20.04"}, "python": {"version": "3.10"}} |
| 91 | + |
| 92 | + # Create first build |
| 93 | + build1 = fixture.get(Build, project=project) |
| 94 | + build1.config = config_data1 |
| 95 | + build1.save() |
| 96 | + |
| 97 | + # Create second build with different config |
| 98 | + build2 = fixture.get(Build, project=project) |
| 99 | + build2.config = config_data2 |
| 100 | + build2.save() |
| 101 | + |
| 102 | + # Should have different BuildConfigs |
| 103 | + assert build1.readthedocs_yaml_data.pk != build2.readthedocs_yaml_data.pk |
| 104 | + assert BuildConfig.objects.count() == 2 |
| 105 | + |
| 106 | + def test_build_without_config_does_not_create_buildconfig(self): |
| 107 | + """Test that a Build without config doesn't create a BuildConfig.""" |
| 108 | + project = fixture.get(Project) |
| 109 | + build = fixture.get(Build, project=project) |
| 110 | + |
| 111 | + # Build has no config set |
| 112 | + build.save() |
| 113 | + |
| 114 | + assert build._config is None |
| 115 | + assert build.readthedocs_yaml_data is None |
| 116 | + assert BuildConfig.objects.count() == 0 |
| 117 | + |
| 118 | + def test_build_with_config_reference_uses_same_buildconfig(self): |
| 119 | + """Test that a Build with config reference (old style) doesn't create a new BuildConfig.""" |
| 120 | + from readthedocs.builds.models import Version |
| 121 | + |
| 122 | + project = fixture.get(Project) |
| 123 | + version = fixture.get(Version, project=project) |
| 124 | + config_data = {"build": {"os": "ubuntu-22.04"}} |
| 125 | + |
| 126 | + # Create a build with actual config |
| 127 | + build1 = fixture.get(Build, project=project, version=version) |
| 128 | + build1.config = config_data |
| 129 | + build1.save() |
| 130 | + |
| 131 | + # Create a build with same config on the same version |
| 132 | + # (which will use the reference style in _config) |
| 133 | + build2 = fixture.get(Build, project=project, version=version) |
| 134 | + build2.config = config_data |
| 135 | + build2.save() |
| 136 | + |
| 137 | + # build2 should have a reference in _config, not actual data |
| 138 | + assert Build.CONFIG_KEY in build2._config |
| 139 | + # build1 should have created a BuildConfig |
| 140 | + assert build1.readthedocs_yaml_data is not None |
| 141 | + # build2 should not create a new BuildConfig since it uses reference style |
| 142 | + assert build2.readthedocs_yaml_data is None |
| 143 | + # There should only be one BuildConfig created |
| 144 | + assert BuildConfig.objects.count() == 1 |
| 145 | + |
| 146 | + def test_buildconfig_related_builds(self): |
| 147 | + """Test that BuildConfig.builds related manager works.""" |
| 148 | + project = fixture.get(Project) |
| 149 | + config_data = {"build": {"os": "ubuntu-22.04"}} |
| 150 | + |
| 151 | + # Create BuildConfig |
| 152 | + build_config = BuildConfig.objects.create(data=config_data) |
| 153 | + |
| 154 | + # Create builds that reference it |
| 155 | + build1 = fixture.get(Build, project=project, readthedocs_yaml_data=build_config) |
| 156 | + build2 = fixture.get(Build, project=project, readthedocs_yaml_data=build_config) |
| 157 | + |
| 158 | + # Check related manager |
| 159 | + assert build_config.builds.count() == 2 |
| 160 | + assert build1 in build_config.builds.all() |
| 161 | + assert build2 in build_config.builds.all() |
0 commit comments