Skip to content

Commit

Permalink
Fix upreferred validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jun 25, 2023
1 parent 12308de commit 6b93f97
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,31 @@ jobs:
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-uploadcoveralls@v1
env:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
- name: "Run tests"
run: |
julia --color=yes --project=. -e 'import Pkg; Pkg.add("Coverage")'
julia --color=yes --threads=auto --check-bounds=yes --depwarn=yes --code-coverage=user --project=. -e 'import Pkg; Pkg.test(coverage=true)'
DQ_TEST_UPREFERRED=true julia --color=yes --threads=auto --check-bounds=yes --depwarn=yes --code-coverage=user --project=. -e 'import Pkg; Pkg.test(coverage=true)'
julia --color=yes --project=. coverage.jl
- name: "Coveralls"
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: lcov.info
parallel: true
flag-name: julia-${{ matrix.version }}-${{ matrix.os }}-${{ matrix.arch }}

coveralls:
name: Indicate completion to coveralls
runs-on: ubuntu-latest
needs: test
steps:
- name: Finish
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true

docs:
name: Documentation
runs-on: ubuntu-latest
Expand Down
18 changes: 14 additions & 4 deletions ext/DynamicQuantitiesUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,28 @@ else
import ..Unitful: @u_str
end

# This lets the user override the preferred units:
function unitful_equivalences()
si_units = (length=u"m", mass=u"kg", time=u"s", current=u"A", temperature=u"K", luminosity=u"cd", amount=u"mol")
function get_si_units()
return (length=u"m", mass=u"kg", time=u"s", current=u"A", temperature=u"K", luminosity=u"cd", amount=u"mol")
end

function validate_upreferred()
si_units = get_si_units()
for k in keys(si_units)
if Unitful.upreferred(si_units[k]) !== si_units[k]
error("Found custom `Unitful.preferunits`. This is not supported when interfacing Unitful and DynamicQuantities: you must leave the default `Unitful.upreferred`, which is the SI base units.")
end
end
return true
end

function unitful_equivalences()
si_units = get_si_units()
return NamedTuple((k => si_units[k] for k in keys(si_units)))
end

Base.convert(::Type{Unitful.Quantity}, x::DynamicQuantities.Quantity) =
let
validate_upreferred()
cumulator = DynamicQuantities.ustrip(x)
dims = DynamicQuantities.dimension(x)
equiv = unitful_equivalences()
Expand All @@ -42,9 +51,10 @@ Base.convert(::Type{DynamicQuantities.Quantity{T,D}}, x::Unitful.Quantity) where
return DynamicQuantities.Quantity(convert(T, value), dimension)
end

Base.convert(::Type{DynamicQuantities.Dimensions}, d::Unitful.Dimensions) = convert(DynamicQuantities.Dimensions{DynamicQuantities.DEFAULT_DIM_TYPE}, d)
Base.convert(::Type{DynamicQuantities.Dimensions}, d::Unitful.Dimensions) = convert(DynamicQuantities.DEFAULT_DIM_TYPE, d)
Base.convert(::Type{DynamicQuantities.Dimensions{R}}, d::Unitful.Dimensions{D}) where {R,D} =
let
validate_upreferred()
cumulator = DynamicQuantities.Dimensions{R}()
for dim in D
dim_symbol = _map_dim_name_to_dynamic_units(typeof(dim))
Expand Down
19 changes: 12 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import Ratios: SimpleRatio
@eval Base.round(T, x::SimpleRatio) = round(T, x.num // x.den)
end

@safetestset "Unitful.jl integration tests" begin
include("test_unitful.jl")
end

@safetestset "Unit tests" begin
include("unittests.jl")
end
if parse(Bool, get(ENV, "DQ_TEST_UPREFERRED", "false"))
@safetestset "Test upreferred disallowed" begin
include("test_ban_upreferred.jl")
end
else
@safetestset "Unitful.jl integration tests" begin
include("test_unitful.jl")
end
@safetestset "Unit tests" begin
include("unittests.jl")
end
end
13 changes: 13 additions & 0 deletions test/test_ban_upreferred.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### These are tests we need to run with a fresh Julia runtime

import Unitful
import Unitful: @u_str
Unitful.preferunits(u"km")
using Test
import DynamicQuantities

x_unitful = 1.5u"km"
x_dq = DynamicQuantities.Quantity(1500.0, length=1)

@test_throws ErrorException convert(DynamicQuantities.Quantity, x_unitful)
@test_throws ErrorException convert(Unitful.Quantity, x_dq)
2 changes: 2 additions & 0 deletions test/test_unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ for T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE

@test isapprox(convert(DynamicQuantities.Quantity{T,D}, x_unitful), x; atol=1e-6)
@test risapprox(convert(Unitful.Quantity, convert(DynamicQuantities.Quantity{T,D}, x_unitful)), Unitful.upreferred(x_unitful); atol=1e-6)

@test typeof(convert(DynamicQuantities.Dimensions, Unitful.dimension(x_unitful))) == DynamicQuantities.Dimensions{DEFAULT_DIM_BASE_TYPE}
end

0 comments on commit 6b93f97

Please sign in to comment.